]>
git.defcon.no Git - hermes/blob - guc-clients/add_apikey.pl
16 my $configfile = undef;
18 my $config = AppConfig
->new( { CREATE
=> 1 } );
20 $config->define("api_url=s");
21 $config->define("api_keyfile=s");
24 "/usr/local/etc/hermes/hermes_config",
25 "/usr/local/etc/hermes/config",
27 $ENV{"HOME"} . "/.hermes/config",
28 $ENV{"HOME"} . "/.hermes_config",
29 ) { $configfile = $_ if ( -f
$_ ); }
33 "host=s" => \
$host_ip,
34 "access=s" => \
$level,
35 "configfile=s" => \
$configfile,
40 (( $configfile ) && ( not -f
$configfile ))
47 $config->file( $configfile );
48 $api_base = $config->api_url;
49 $keyfile = $config->api_keyfile;
52 ( ( $level ne "limited_read" ) &&
53 ( $level ne "full_read" ) &&
54 ( $level ne "read_write" ) ) ||
56 ( not $api_base =~ m/^http:\/\
/\w+/ ) ||
72 --access=limited_read|full_read|read_write
73 --configfile=/path/to/config/file (--config, -c)
75 (c) 2012, Gjøvik University College, jonl. Licence: MIT.
81 open KEY, "<" . $keyfile;
82 chomp( $api_key = <KEY> );
85 my ($g_ua, $session, $auth_key, $data);
87 $g_ua = LWP::UserAgent->new;
88 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
92 $data = exec_apinode('auth/new_apikey', { 'host_ip' => $host_ip, 'access' => $level });
97 ################################################################################################
103 my ( $response, $data );
105 $session = "" if not defined $session;
106 $auth_key = "" if not defined $auth_key;
107 my $url = $api_base . "/" . $node;
109 $param->{'session'} = $session;
110 $param->{'auth_key'} = $auth_key;
112 $response = $g_ua->post( $url, $param );
113 if ( $response->is_success )
115 if ( $response->content =~ m/\s*{/ )
117 $data = decode_json( $response->content);
121 $data = $response->content;
130 my $response = $g_ua->post( $api_base . "/auth/login",
131 [ "api_key" => $api_key ] );
133 my $data = decode_json( $response->content) if $response->is_success;
134 die("HTTP error") unless $response->is_success;
136 if ( $data->{'response'} eq "ok" )
138 $session = $data->{'session'};
139 $auth_key = $data->{'auth_key'};
145 undef $data; undef $response;
150 my $response = $g_ua->post( $api_base . "/auth/logout",
151 [ "session" => $session ] );
152 die("HTTP error") unless $response->is_success;
153 undef $session; undef $auth_key;