]>
git.defcon.no Git - hermes/blob - guc-clients/onlinetest
6 #TODO: Add support for assigning phone number
7 #TODO: Add support for overriding default domain ...
11 use Net
::LDAP
::Control
::Paged
;
12 use Net
::LDAP
::Constant
qw( LDAP_CONTROL_PAGED );
22 my $configfile = undef;
24 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
26 my $config = AppConfig
->new({ CREATE
=> 1 });
28 $config->define("api_url=s");
29 $config->define("api_keyfile=s");
32 "/usr/local/etc/hermes/hermes_config",
33 "/usr/local/etc/hermes/config",
35 $ENV{"HOME"} . "/.hermes/config",
36 $ENV{"HOME"} . "/.hermes_config",
37 ) { $configfile = $_ if ( -f
$_ ); }
41 "configfile=s" => \
$configfile,
42 "username=s" => \
$username,
49 (( $configfile ) && ( not -f
$configfile ))
55 $config->file( $configfile );
57 if ( ( not $config->api_url ) ||
58 ( not $config->api_keyfile ) ||
59 ( $config->api_keyfile && not -f
$config->api_keyfile )
67 Verify that the following options are set:
68 --username=s|--user|-u
72 Verify the contents of the configuration file.
73 Verify that the key-file exists.
77 open KEY, "<" . $config->api_keyfile;
78 chomp( $api_key = <KEY> );
81 if ( not $username =~ m/\w+/ )
82 { print "Illegal username\n"; exit; }
84 $g_ua = LWP::UserAgent->new;
85 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
89 # First: fetch a supported domain from the API...
90 $data = exec_apinode("domain/list", undef);
91 if ( $data->{'response'} eq 'ok' )
93 $domain = $data->{'list'}[0];
97 printf("Unable to get domain name. Aborting\n");
102 if ( $username =~ /@/ )
104 ( $username, $domain ) = split /@/, $username;
107 $data = exec_apinode("user/location", { 'username' => $username, 'domain' => $domain });
108 if ( not $data->{'response'} eq 'ok' )
110 printf("Unable to fetch location: %s\n", $data->{'cause'});
113 my $t = $data->{'locations'};
116 foreach my $l ( @locations )
120 printf("'%s', ", $l->{'contact'});
121 printf("'%s', ", $l->{'useragent'});
122 printf("'%s'", $l->{'expires'});
126 printf("%-70s", $l->{'contact'});
127 printf("%-35s", $l->{'useragent'});
128 printf("%s", $l->{'expires'});
136 ################################################################################################
142 my ( $response, $data );
144 $session = "" if not defined $session;
145 $auth_key = "" if not defined $auth_key;
146 my $url = $config->api_url . "/" . $node;
148 $param->{'session'} = $session;
149 $param->{'auth_key'} = $auth_key;
151 $response = $g_ua->post( $url, $param );
152 if ( $response->is_success )
154 if ( $response->content =~ m/\s*{/ )
156 $data = decode_json( $response->content);
160 $data = $response->content;
169 my $response = $g_ua->post( $config->api_url . "/auth/login",
170 [ "api_key" => $api_key ] );
172 my $data = decode_json( $response->content) if $response->is_success;
173 die("HTTP error") unless $response->is_success;
175 if ( $data->{'response'} eq "ok" )
177 $session = $data->{'session'};
178 $auth_key = $data->{'auth_key'};
182 print "Unable to log in to Hermes API\n";
185 undef $data; undef $response;
190 my $response = $g_ua->post( $config->api_url . "/auth/logout",
191 [ "session" => $session ] );
192 die("HTTP error") unless $response->is_success;
193 undef $session; undef $auth_key;