Skip to content

Commit 9029a87

Browse files
author
GomoR
committed
- version: 3.03
- new: support for API Basic authorizations
1 parent 221da76 commit 9029a87

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
Revision history for Perl extension Metabrik::Client::Onyphe.
22

3+
3.03 Wed 7 Dec 07:49:50 CET 2022
4+
- new: support for API Basic authorizations
5+
36
3.02 Tue 13 Sep 11:28:31 CEST 2022
47
- new: support for calculated fields in Search API
58

bin/onyphe

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ GetOptions(
4242
"apitrackquery=i" => \$lopts{apitrackquery}, # -apitrackquery 1
4343
"apicalculated=i" => \$lopts{apicalculated}, # -apicalculated 1
4444
"apikeepalive=i" => \$lopts{apikeepalive}, # -apikeelalive 1
45+
"apiauth=s" => \$lopts{apiauth}, # -apiauth user:pass
4546
"wait=i" => \$lopts{wait}, # -wait 3
4647
# API options:
4748
"category=s" => \$lopts{category}, # -category datascan,synscan
@@ -87,6 +88,7 @@ $cli->apisize($lopts{apisize});
8788
$cli->apitrackquery($lopts{apitrackquery});
8889
$cli->apicalculated($lopts{apicalculated});
8990
$cli->apikeepalive($lopts{apikeepalive});
91+
$cli->apiauth($lopts{apiauth});
9092
$cli->wait($lopts{wait});
9193

9294
# Set API options:

lib/Metabrik/Api/Onyphe.pm

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package Metabrik::Api::Onyphe;
77
use strict;
88
use warnings;
99

10-
our $VERSION = '3.02';
10+
our $VERSION = '3.03';
1111

1212
use base qw(Metabrik::Client::Rest);
1313

@@ -21,6 +21,7 @@ sub brik_properties {
2121
url => [ qw(url) ],
2222
key => [ qw(key) ],
2323
wait => [ qw(seconds) ],
24+
auth => [ qw(user:pass) ],
2425
_maxpage => [ qw(INTERNAL) ],
2526
_sj => [ qw(INTERNAL) ],
2627
},
@@ -49,6 +50,7 @@ sub brik_properties {
4950
},
5051
require_modules => {
5152
'Metabrik::String::Json' => [ ],
53+
'Metabrik::String::Base64' => [ ],
5254
'Metabrik::File::Text' => [ ],
5355
'AnyEvent' => [ ],
5456
'AnyEvent::HTTP' => [ ],
@@ -81,9 +83,17 @@ sub api_standard {
8183
my $wait = $self->wait;
8284
my $url = $self->url;
8385
my $key = $self->key;
86+
my $auth = $self->auth;
87+
my ($username, $password); ($username, $password) = split(':', $auth)
88+
if defined($auth);
89+
90+
if (defined($username) && defined($password)) {
91+
$self->username($username);
92+
$self->password($password);
93+
}
8494

8595
$self->add_headers({
86-
'Authorization' => "apikey $key",
96+
'Authorization' => "bearer $key",
8797
'Content-Type' => 'application/json',
8898
});
8999

@@ -103,8 +113,16 @@ sub api_standard {
103113
push @args, $k.'='.$arg->{$k};
104114
}
105115
}
116+
if (defined($username) && defined($password)) {
117+
push @args, "k=$key";
118+
}
106119
$url .= '?'.join('&', @args);
107120
}
121+
else {
122+
if (defined($username) && defined($password)) {
123+
$url .= '?k='.$key;
124+
}
125+
}
108126

109127
$self->log->verbose("api_standard: using url[$url]");
110128

@@ -169,6 +187,16 @@ sub api_streaming {
169187
my $sj = $self->_sj;
170188
my $url = $self->url;
171189
my $key = $self->key;
190+
my $auth = $self->auth;
191+
my ($username, $password); ($username, $password) = split(':', $auth)
192+
if defined($auth);
193+
194+
my $basic;
195+
if (defined($username) && defined($password)) {
196+
my $sb = Metabrik::String::Base64->new_from_brik_init($self)
197+
or return;
198+
$basic = "Basic ".$sb->encode($auth);
199+
}
172200

173201
if ($req eq "GET" && defined($oql)) {
174202
$oql = URI::Escape::uri_escape_utf8($oql);
@@ -186,8 +214,16 @@ sub api_streaming {
186214
push @args, $k.'='.$arg->{$k};
187215
}
188216
}
217+
if (defined($username) && defined($password)) {
218+
push @args, "k=$key";
219+
}
189220
$url .= '?'.join('&', @args);
190221
}
222+
else {
223+
if (defined($username) && defined($password)) {
224+
$url .= '?k='.$key;
225+
}
226+
}
191227

192228
$self->log->verbose("api_streaming: using url[$url]");
193229

@@ -204,6 +240,8 @@ sub api_streaming {
204240

205241
my $cv = AnyEvent->condvar;
206242

243+
my $authorization = defined($basic) ? $basic : "bearer $key";
244+
207245
my @args = ( $url );
208246
if ($req eq "POST") {
209247
push @args, $oql;
@@ -212,7 +250,7 @@ sub api_streaming {
212250
# For each loop of processing, let callback work during X before sending a timeout.
213251
timeout => 3600,
214252
headers => {
215-
'Authorization' => "apikey $key",
253+
'Authorization' => $authorization,
216254
'Content-Type' => "application/json",
217255
},
218256
on_body => sub {

lib/Metabrik/Client/Onyphe.pm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package Metabrik::Client::Onyphe;
55
use strict;
66
use warnings;
77

8-
our $VERSION = '3.02';
8+
our $VERSION = '3.03';
99

1010
use base qw(Metabrik);
1111

@@ -26,6 +26,7 @@ sub brik_properties {
2626
apitrackquery => [ qw(0|1) ],
2727
apicalculated => [ qw(0|1) ],
2828
apikeepalive => [ qw(0|1) ],
29+
apiauth => [ qw(user:pass) ],
2930
wait => [ qw(seconds) ],
3031
# API options:
3132
category => [ qw(category|categories) ],
@@ -85,12 +86,14 @@ sub ao {
8586

8687
my $apiurl = $self->apiurl;
8788
my $apikey = $self->apikey;
89+
my $apiauth = $self->apiauth;
8890

8991
$self->brik_help_run_undef_arg('ao', $apiurl) or return;
9092
$self->brik_help_run_undef_arg('ao', $apikey) or return;
9193

9294
$self->_ao->url($apiurl);
9395
$self->_ao->key($apikey);
96+
$self->_ao->auth($apiauth);
9497

9598
return $self->_ao;
9699
}

lib/Metabrik/Client/Onyphe/Function.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package Metabrik::Client::Onyphe::Function;
55
use strict;
66
use warnings;
77

8-
our $VERSION = '3.02';
8+
our $VERSION = '3.03';
99

1010
use base qw(Metabrik::Client::Onyphe);
1111

0 commit comments

Comments
 (0)