[Freeipa-users] First tests against the REST/JSON API

Oliver Dörr oliver at doerr-privat.de
Tue Nov 10 14:53:53 UTC 2015


Hi Alexander,

sorry for responding you privately. This was not my intention; I just 
recognized that my mail program has two reply buttons (replay and reply 
to mailing list).

I've played a bit around with your code and implemented a small Perl 
module and a test script. They both work in my environment, however I'm 
still not happy with them.

What I don't like is...
1st, I need to provide my client version
2nd, I get the message  'truncated' => bless( do{\(my $o = 0)}, 
'JSON::PP::Boolean' ) from the user_find API
3rd, I do not know what the id is used for and so I'm unsure if I could 
automatically handle it in my Perl module

However they should be a help for others who wants use Perl-script to 
access IPA-API.

Anyway thanks for your help. This was what I need to get on the track.

Oliver

Am 10.11.2015 um 09:14 schrieb Alexander Bokovoy:
> On Mon, 09 Nov 2015, Natxo Asenjo wrote:
>> hi,
>>
>> On Mon, Nov 9, 2015 at 6:58 PM, Oliver Dörr <oliver at doerr-privat.de> 
>> wrote:
>>
>>> Hi,
>>>
>>> I'm completly new to this list and the product behind it. I'm trying to
>>> use perl to get a list from my IPA installation of all users that 
>>> are on
>>> the server.
>>>
>>
>> unfortunately I cannot help you right now, but have you taken a look at
>> this blog post:
>>
>> https://vda.li/en/posts/2015/05/28/talking-to-freeipa-api-with-sessions/
>>
>> It could give you some pointers.
>>
>> Share your code if you get it to work :-)
> The code in my response (see other mail) works fine, Oliver contacted me
> privately.
>
> Oliver, please respond to the list. :)

-------------- next part --------------
package freeipa;

use strict;
use REST::Client;
use JSON;
use Data::Dumper;

our $VERSION="0.0.1";


BEGIN {
};


sub new
# Constructor of the class freeipa
{	my ($Class)=@_;
	my $self= {};

	bless($self,$Class);
	$self->{'id'}=0;
	return $self;
}	# sub news


sub set
# Setter of the class freeipa. At this moment only usuable or scalar values 
{	my $ipaObj=shift;
	my $scalar=shift;
	my $value=shift;
	
	$ipaObj->{$scalar}=$value;
}	# sub set


sub connect
# Connects to the specified IPA server
{	my $ipaObj=shift;
	
	my $headers = {
		'Accept' => 'text/plain',
		'Content-Type' => 'application/x-www-form-urlencoded',
		'Referer' => $ipaObj->{'baseUrl'}
	};	# my $headers
	
	my $client=REST::Client->new();
	my $params = $client->buildQuery({'user' => $ipaObj->{'user'}, 'password' => $ipaObj->{'password'} });
	$client->setHost($ipaObj->{'baseUrl'});
	$client->POST("/ipa/session/login_password", substr($params,1), $headers);
	$ipaObj->{'authCookie'} = $client->responseHeader('Set-Cookie');
	$ipaObj->{'restClient'}=$client;
}	# sub connect


sub ipaMethod
# Calls an API method against the IPA connection
{	my $ipaObj=shift;
	my $hashRef=shift;
	my $data = {
		'id'=>$ipaObj->{'id'},
		'params' => [
						[],
						{ 'version' => $ipaObj->{'version'} }
					]
	};	# my $data
	$ipaObj->{'id'}++;
	
	my $headers = {
		'Accept' => 'text/plain',
		'Content-Type' => 'application/json',
		'Cookie' => $ipaObj->{'authCookie'},
		'Referer' => $ipaObj->{'baseUrl'}."/ipa/session/json"
	};
	
	# Copy the specified part of the method over the default data defined in $data
	foreach my $var (keys (%{$hashRef})) { $data->{$var}=$hashRef->{$var}; }
	
	# Format the method and data to json and make a REST request against the IPA server
	my $jsonMethod = to_json($data);
	$ipaObj->{'restClient'}->POST("/ipa/session/json", $jsonMethod, $headers);
	
	# bring the JSON-formed response into perl objects...
	my $restRet=from_json($ipaObj->{'restClient'}->responseContent());
	
	# ... and return ist
	return $restRet;
}	# sub ipaMethod
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testConnection.pl
Type: application/x-perl
Size: 927 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/freeipa-users/attachments/20151110/efbde768/attachment.pl>


More information about the Freeipa-users mailing list