]>
git.defcon.no Git - hermes/blob - guc-clients/add_apikey.pl
a543c23023414ed0c6be6c081db88481e4c7ac15
18 my $config = AppConfig
->new( { CREATE
=> 1 } );
20 $config->define("api_url=s");
21 $config->define("api_keyfile=s");
25 "host=s" => \
$host_ip,
26 "access=s" => \
$level,
27 "configfile=s" => \
$configfile,
32 (( $configfile ) && ( not -f
$configfile ))
39 $config->file( $configfile );
40 $api_base = $config->api_url;
41 $keyfile = $config->api_keyfile;
44 ( ( $level ne "limited_read" ) &&
45 ( $level ne "full_read" ) &&
46 ( $level ne "read_write" ) ) ||
48 ( not $api_base =~ m/^http:\/\
/\w+/ ) ||
64 --access=limited_read|full_read|read_write
65 --configfile=/path/to/config/file (--config, -c)
67 (c) 2012, Gjøvik University College, jonl. Licence: MIT.
73 open KEY, "<" . $keyfile;
74 chomp( $api_key = <KEY> );
77 my ($g_ua, $session, $auth_key, $data);
79 $g_ua = LWP::UserAgent->new;
80 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
84 $data = exec_apinode('auth/new_apikey', { 'host_ip' => $host_ip, 'access' => $level });
89 ################################################################################################
95 my ( $response, $data );
97 $session = "" if not defined $session;
98 $auth_key = "" if not defined $auth_key;
99 my $url = $api_base . "/" . $node;
101 $param->{'session'} = $session;
102 $param->{'auth_key'} = $auth_key;
104 $response = $g_ua->post( $url, $param );
105 if ( $response->is_success )
107 if ( $response->content =~ m/\s*{/ )
109 $data = decode_json( $response->content);
113 $data = $response->content;
122 my $response = $g_ua->post( $api_base . "/auth/login",
123 [ "api_key" => $api_key ] );
125 my $data = decode_json( $response->content) if $response->is_success;
126 die("HTTP error") unless $response->is_success;
128 if ( $data->{'response'} eq "ok" )
130 $session = $data->{'session'};
131 $auth_key = $data->{'auth_key'};
137 undef $data; undef $response;
142 my $response = $g_ua->post( $api_base . "/auth/logout",
143 [ "session" => $session ] );
144 die("HTTP error") unless $response->is_success;
145 undef $session; undef $auth_key;