Hydra/Client.pm

filter: Statementstmt 82.0% 183 / 223 Branchbran 75.0% 27 / 36 Conditioncond 72.7% 8 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 87.5% 28 / 32 Podpod 100% 19 / 19 totaltotal 81.1% 266 / 328 CC 25 SCAR 11.0
CC
25
Cov
81%
api_call
7.0
accept_login_request
6.0
exchange_token
6.0
CRAP 3.0
« Hydra/Exception/FeatureUnavailable.pm ↑ Summary Hydra/Exception.pm »
1 package WebService::Hydra::Client;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 class WebService::Hydra::Client;
9  
10 2 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Crypt::JWT qw(decode_jwt);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use JSON::MaybeUTF8;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use WebService::Hydra::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use constant OK_STATUS_CODE => 200;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use constant OK_NO_CONTENT_CODE => 204;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use constant BAD_REQUEST_STATUS_CODE => 400;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use constant HTTP_TIMEOUT_SECONDS => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 our $VERSION = '0.005';
23  
24 2 field $http;
25 2 field $jwks;
26 2 field $oidc_config;
27 1 ▶field $admin_endpoint :param :reader;
Subroutine admin_endpoint: called CC=1 SCAR=0.0
Pod documented
28 1 ▶field $public_endpoint :param :reader;
Subroutine public_endpoint: called CC=1 SCAR=0.0
Pod documented
29  
30 =head1 NAME
31  
32 WebService::Hydra::Client - Hydra Client Object
33  
34 =head2 Description
35  
36 Object::Pad based class which is used to create a Hydra Client Object which interacts with the Hydra service API.
37  
38 =head1 SYNOPSIS
39  
40 use WebService::Hydra::Client;
41 my $obj = WebService::Hydra::Client->new(admin_endpoint => 'url' , public_endpoint => 'url');
42  
43 =head1 METHODS
44  
45 =head2 new
46  
47 =over 1
48  
49 =item C<admin_endpoint>
50  
51 admin_endpoint is a string which contains admin URL for the hydra service. Eg: http://localhost:4445
52 This is a required parameter when creating Hydra Client Object using new.
53  
54 =item C<public_endpoint>
55  
56 public_endpoint is a string which contains the public URL for the hydra service. Eg: http://localhost:4444
57 This is a required parameter when creating Hydra Client Object using new.
58  
59 =back
60  
61 =head2 admin_endpoint
62  
63 Returns the base URL for the hydra service.
64  
65 =cut
66  
67 =head2 public_endpoint
68  
69 Returns the base URL for the hydra service.
70  
71 =cut
72  
73 =head2 http
74  
75 Return HTTP object.
76  
77 =cut
78  
79 5 ▶method http {
Subroutine http: called CC=2 SCAR=7.2
Pod documented
80 5 ▶ return $http //= HTTP::Tiny->new(timeout => HTTP_TIMEOUT_SECONDS);
Condition66%2/3
$http //= "HTTP::Tiny"->new("timeout", 10)
l!l&&r!l&&!r
410
MC/DC0%0/2
$http //= "HTTP::Tiny"->new("timeout", 10)
$http "HTTP::Tiny"->new("timeout", 10)
Truth table66%2/3
A //= B
A= $httpB= "HTTP::Tiny"->new("timeout", 10)
ABresult
1X1
011
000
81 2 }
82  
83 =head2 jwks
84 return jwks object
85 =cut
86  
87 2 ▶method jwks {
Subroutine jwks: called CC=2 SCAR=7.2
Pod documented
88 2 ▶ return $jwks //= $self->fetch_jwks();
Condition66%2/3
$jwks //= $self->fetch_jwks
l!l&&r!l&&!r
110
MC/DC0%0/2
$jwks //= $self->fetch_jwks
$jwks $self->fetch_jwks
Truth table66%2/3
A //= B
A= $jwksB= $self->fetch_jwks
ABresult
1X1
011
000
89 2 }
90  
91 =head2 oidc_config
92  
93 returns an object with oidc configuration
94  
95 =cut
96  
97 5 ▶method oidc_config {
Subroutine oidc_config: called CC=2 SCAR=7.2
Pod documented
98 5 ▶ return $oidc_config //= $self->fetch_openid_configuration();
Condition66%2/3
$oidc_config //= $self->fetch_openid_configuration
l!l&&r!l&&!r
230
MC/DC0%0/2
$oidc_config //= $self->fetch_openid_configuration
$oidc_config $self->fetch_openid_configuration
Truth table66%2/3
A //= B
A= $oidc_configB= $self->fetch_openid_configuration
ABresult
1X1
011
000
99 2 }
100  
101 =head2 api_call
102  
103 Takes request method, the endpoint, and the payload. It sends the request to the Hydra service, parses the response and returns:
104  
105 1. JSON object of code and data returned from the service.
106 2. Error string in case an exception is thrown.
107  
108 =cut
109  
110 4 ▶method api_call ($method, $endpoint, $payload = undef, $content_type = 'json') {
Subroutine api_call: called CC=7 SCAR=19.5
Pod documented
111  
112 try {
113  
114 4 my @args = ($method, $endpoint);
115 4 ▶ if ($payload) {
Branch: 1 branch
BranchTrueFalseTotal
if ($payload) 2 2 4
116 2 ▶ if ($content_type eq 'FORM') {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type eq 'FORM') { } 1 1 2
117 1 my $headers = {
118 'Content-Type' => 'application/x-www-form-urlencoded',
119 'Accept' => 'application/json'
120 };
121 1 push(
122 @args,
123 {
124 headers => $headers,
125 content => $self->http->www_form_urlencode($payload)});
126 } else {
127 1 my $headers = {'Content-Type' => 'application/json'};
128 1 push(
129 @args,
130 {
131 headers => $headers,
132 content => JSON::MaybeUTF8::encode_json_utf8($payload)});
133 }
134 }
135  
136 4 my $response = $self->http->request(@args);
137 3 ▶ my $data = JSON::MaybeUTF8::decode_json_utf8($response->{content} || '{}');
Condition100%2/2
$response->{'content'} || '{}'
l!l
12
MC/DC100%1/1
$response->{'content'} || '{}'
$response->{'content'}
Truth table100%2/2
A
A= $response->{'content'}
Aresult
11
00
138  
139 WebService::Hydra::Exception::HydraServiceUnreachable->new(
140 details => ["An error happened during the execution of the $endpoint request: $response->{content}"])->throw
141 3 ▶ if $response->{status} == 599;
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"status"} == 599 0 3 3
142  
143 return {
144 code => $response->{status},
145 3 data => $data
146 };
147 4 } catch ($e) {
148 1 WebService::Hydra::Exception::HydraRequestError->new(
149 details => ["Request to $endpoint failed", $e],
150 )->throw;
151 }
152 2 }
153  
154 =head2 get_login_request
155  
156 Fetches the OAuth2 login request from hydra.
157  
158 Arguments:
159  
160 =over 1
161  
162 =item C<$login_challenge>
163  
164 Authentication challenge string that is used to identify and fetch information
165 about the OAuth2 request from hydra.
166  
167 =back
168  
169 =cut
170  
171 3 ▶method get_login_request ($login_challenge) {
Subroutine get_login_request: called CC=3 SCAR=11.1
Pod documented
172 3 my $method = "GET";
173 3 my $path = "$admin_endpoint/admin/oauth2/auth/requests/login?challenge=$login_challenge";
174  
175 3 my $result = $self->api_call($method, $path);
176  
177 # "410" means that the request was already handled. This can happen on form double-submit or other errors.
178 # It's recommended to redirect the user to `request_url` to re-initiate the flow.
179 2 ▶ if ($result->{code} == 410) {
Branch: 2 branches
BranchTrueFalseTotal
if ($result->{'code'} == 410) { } 0 2 2
elsif ($result->{'code'} != 200) { } 1 1 2
180 WebService::Hydra::Exception::InvalidLoginChallenge->new(
181 message => "Login challenge has already been handled",
182 redirect_to => $result->{data}->{redirect_to},
183 0 category => 'client_redirecting_error'
184 )->throw;
185 } elsif ($result->{code} != OK_STATUS_CODE) {
186 1 WebService::Hydra::Exception::InvalidLoginChallenge->new(
187 message => "Failed to get login request",
188 category => "client",
189 details => $result
190 )->throw;
191 }
192 1 return $result->{data};
193 2 }
194  
195 =head2 accept_login_request
196  
197 Accepts the login request and returns the response from hydra.
198  
199 Arguments:
200  
201 =over 1
202  
203 =item C<$login_challenge>
204  
205 Authentication challenge string that is used to identify the login request.
206  
207 =item C<$accept_payload>
208  
209 Payload to be sent to the Hydra service to confirm the login challenge.
210  
211 =back
212  
213 =cut
214  
215 0 ▶method accept_login_request ($login_challenge, $accept_payload) {
Subroutine accept_login_request: not called CC=2 SCAR=17.9
Pod documented
216 0 my $method = "PUT";
217 0 my $path = "$admin_endpoint/admin/oauth2/auth/requests/login/accept?challenge=$login_challenge";
218  
219 0 my $result = $self->api_call($method, $path, $accept_payload);
220 0 ▶ if ($result->{code} != OK_STATUS_CODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"code"} != 200) 0 0 0
221 0 WebService::Hydra::Exception::InvalidLoginRequest->new(
222 message => "Failed to accept login request",
223 category => "client",
224 details => $result
225 )->throw;
226 }
227 0 return $result->{data};
228 2 }
229  
230 =head2 reject_login_request
231  
232 Rejects the login request and returns the response from hydra.
233  
234 Arguments:
235  
236 =over 1
237  
238 =item C<$login_challenge>
239  
240 Authentication challenge string that is used to identify the login request.
241  
242 =item C<$reject_payload>
243  
244 Payload to be sent to the Hydra service to reject the login request.
245  
246 =back
247  
248 =cut
249  
250 3 ▶method reject_login_request ($login_challenge, $reject_payload) {
Subroutine reject_login_request: called CC=2 SCAR=6.9
Pod documented
251 3 my $method = "PUT";
252 3 my $path = "$admin_endpoint/admin/oauth2/auth/requests/login/reject?challenge=$login_challenge";
253  
254 3 my $result = $self->api_call($method, $path, $reject_payload);
255 2 ▶ if ($result->{code} != OK_STATUS_CODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"code"} != 200) 1 1 2
256 1 WebService::Hydra::Exception::InvalidLoginRequest->new(
257 message => "Failed to reject login request",
258 category => "client",
259 details => $result
260 )->throw;
261 }
262 1 return $result->{data};
263 2 }
264  
265 =head2 get_logout_request
266  
267 Get the logout request and return the response from Hydra.
268  
269 =cut
270  
271 4 ▶method get_logout_request ($logout_challenge) {
Subroutine get_logout_request: called CC=3 SCAR=11.0
Pod documented
272 4 my $method = "GET";
273 4 my $path = "$admin_endpoint/admin/oauth2/auth/requests/logout?challenge=$logout_challenge";
274  
275 4 my $result = $self->api_call($method, $path);
276  
277 # "410" means that the request was already handled. This can happen on form double-submit or other errors.
278 # It's recommended to redirect the user to `request_url` to re-initiate the flow.
279 3 ▶ if ($result->{code} == 410) {
Branch: 2 branches
BranchTrueFalseTotal
if ($result->{'code'} == 410) { } 1 2 3
elsif ($result->{'code'} != 200) { } 1 1 2
280 WebService::Hydra::Exception::InvalidLogoutChallenge->new(
281 message => "Logout challenge has already been handled",
282 redirect_to => $result->{data}->{redirect_to},
283 1 category => 'client_redirecting_error'
284 )->throw;
285 } elsif ($result->{code} != OK_STATUS_CODE) {
286 1 WebService::Hydra::Exception::InvalidLogoutChallenge->new(
287 message => "Failed to get logout request",
288 category => "client",
289 details => $result
290 )->throw;
291 }
292 1 return $result->{data};
293 2 }
294  
295 =head2 accept_logout_request
296  
297 The response contains a redirect URL which the logout provider should redirect the user-agent to.
298  
299 =cut
300  
301 4 ▶method accept_logout_request ($logout_challenge) {
Subroutine accept_logout_request: called CC=2 SCAR=6.9
Pod documented
302 4 my $method = "PUT";
303 4 my $path = "$admin_endpoint/admin/oauth2/auth/requests/logout/accept?challenge=$logout_challenge";
304 4 my $result = $self->api_call($method, $path);
305 2 ▶ if ($result->{code} != OK_STATUS_CODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"code"} != 200) 1 1 2
306 1 WebService::Hydra::Exception::InvalidLogoutChallenge->new(
307 message => "Failed to accept logout request",
308 category => "client",
309 details => $result
310 )->throw;
311 }
312 1 return $result->{data};
313 2 }
314  
315 =head2 exchange_token
316  
317 Exchanges the authorization code with Hydra service for access and ID tokens.
318  
319 =cut
320  
321 0 ▶method exchange_token ($exchange_payload) {
Subroutine exchange_token: not called CC=2 SCAR=17.9
Pod documented
322 0 my $method = "POST";
323 0 my $path = "$public_endpoint/oauth2/token";
324 0 my $payload = {
325 grant_type => 'authorization_code',
326 $exchange_payload->%*
327 };
328 0 my $result = $self->api_call($method, $path, $payload, 'FORM');
329 0 ▶ if ($result->{code} != OK_STATUS_CODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"code"} != 200) 0 0 0
330 0 WebService::Hydra::Exception::TokenExchangeFailed->new(
331 message => "Failed to exchange token",
332 category => "client",
333 details => $result
334 )->throw;
335 }
336 0 return $result->{data};
337 2 }
338  
339 =head2 fetch_jwks
340  
341 Fetches the JSON Web Key Set published by Hydra which is used to validate signatures.
342  
343 =cut
344  
345 0 ▶method fetch_jwks () {
Subroutine fetch_jwks: not called CC=2 SCAR=17.9
Pod documented
346 0 my $method = "GET";
347 0 my $path = "$public_endpoint/.well-known/jwks.json";
348  
349 0 my $result = $self->api_call($method, $path);
350 0 ▶ if ($result->{code} != OK_STATUS_CODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"code"} != 200) 0 0 0
351 0 WebService::Hydra::Exception::HydraRequestError->new(
352 category => "hydra",
353 details => $result
354 )->throw;
355 }
356 0 return $result->{data};
357 2 }
358  
359 =head2 fetch_openid_configuration
360  
361 Fetches the openid-configuration from hydra
362  
363 =cut
364  
365 2 ▶method fetch_openid_configuration () {
Subroutine fetch_openid_configuration: called CC=2 SCAR=6.9
Pod documented
366 2 my $method = "GET";
367 2 my $path = "$public_endpoint/.well-known/openid-configuration";
368  
369 2 my $result = $self->api_call($method, $path);
370 2 ▶ if ($result->{code} != OK_STATUS_CODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"code"} != 200) 1 1 2
371 1 BOM::OAuth::Exceptions::Type::HydraRequestError->new(
372 category => "hydra",
373 details => $result
374 )->throw;
375 }
376 1 return $result->{data};
377 2 }
378  
379 =head2 validate_id_token
380  
381 Decodes the id_token and validates its signature against Hydra and returns the decoded payload.
382  
383 =cut
384  
385 0 ▶method validate_id_token ($id_token) {
Subroutine validate_id_token: not called CC=1 SCAR=6.9
Pod documented
386 try {
387 0 my $payload = decode_jwt(
388 token => $id_token,
389 kid_keys => $self->jwks
390 );
391 0 return $payload;
392 0 } catch ($e) {
393 0 WebService::Hydra::Exception::InvalidIdToken->new(
394 message => "Failed to validate id token",
395 category => "client",
396 details => $e
397 )->throw;
398 }
399 2 }
400  
401 =head2 validate_token
402  
403 Decodes the token and validates its signature against hydra and returns the decoded payload.
404  
405 =over 1
406  
407 =item C<$token> jwt token to be validated
408  
409 =back
410  
411 Returns the decoded payload if the token is valid, otherwise throws an exception.
412  
413 =cut
414  
415 2 ▶method validate_token ($token) {
Subroutine validate_token: called CC=1 SCAR=0.0
Pod documented
416 my $payload = decode_jwt(
417 token => $token,
418 verify_iat => 1,
419 verify_exp => 1,
420 verify_iss => $self->oidc_config->{issuer},
421 2 kid_keys => $self->jwks
422 );
423 1 return $payload;
424 2 }
425  
426 =head2 get_consent_request
427  
428 Fetches the consent request from Hydra.
429  
430 =cut
431  
432 4 ▶method get_consent_request ($consent_challenge) {
Subroutine get_consent_request: called CC=3 SCAR=11.0
Pod documented
433 4 my $method = "GET";
434 4 my $path = "$admin_endpoint/admin/oauth2/auth/requests/consent?challenge=$consent_challenge";
435  
436 4 my $result = $self->api_call($method, $path);
437  
438 3 ▶ if ($result->{code} == 410) {
Branch: 2 branches
BranchTrueFalseTotal
if ($result->{'code'} == 410) { } 1 2 3
elsif ($result->{'code'} != 200) { } 1 1 2
439 WebService::Hydra::Exception::InvalidConsentChallenge->new(
440 message => "Consent request has already been handled",
441 redirect_to => $result->{data}->{redirect_to},
442 1 category => 'client_redirecting_error'
443 )->throw;
444 } elsif ($result->{code} != OK_STATUS_CODE) {
445 1 WebService::Hydra::Exception::InvalidConsentChallenge->new(
446 message => "Failed to get consent request",
447 category => "client",
448 details => $result
449 )->throw;
450 }
451 1 return $result->{data};
452 2 }
453  
454 =head2 accept_consent_request
455  
456 Accepts the consent request and returns the response from Hydra.
457  
458 =cut
459  
460 3 ▶method accept_consent_request ($consent_challenge, $params) {
Subroutine accept_consent_request: called CC=2 SCAR=6.9
Pod documented
461 3 my $method = "PUT";
462 3 my $path = "$admin_endpoint/admin/oauth2/auth/requests/consent/accept?challenge=$consent_challenge";
463  
464 3 my $result = $self->api_call($method, $path, $params);
465 2 ▶ if ($result->{code} != OK_STATUS_CODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"code"} != 200) 1 1 2
466 1 WebService::Hydra::Exception::InvalidConsentChallenge->new(
467 message => "Failed to accept consent request",
468 category => "client",
469 details => $result
470 )->throw;
471 }
472 1 return $result->{data};
473 2 }
474  
475 =head2 revoke_login_sessions
476  
477 This endpoint invalidates authentication sessions.
478 It expects a user ID (subject) and invalidates all sessions for this user. or session ID (sid) and invalidates the session.
479  
480 =cut
481  
482 3 ▶method revoke_login_sessions (%args) {
Subroutine revoke_login_sessions: called CC=3 SCAR=11.0
Pod documented
483 3 my $method = "DELETE";
484 3 my $path = "$admin_endpoint/admin/oauth2/auth/sessions/login";
485  
486 3 my $query = join('&', map { "$_=$args{$_}" } keys %args);
487 3 ▶ $path .= "?$query" if $query;
Branch: 1 branch
BranchTrueFalseTotal
if $query 3 0 3
488  
489 3 my $result = $self->api_call($method, $path);
490 3 ▶ if ($result->{code} != OK_NO_CONTENT_CODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"code"} != 204) 1 2 3
491 1 WebService::Hydra::Exception::RevokeLoginSessionsFailed->new(
492 message => "Failed to revoke login sessions",
493 category => "client",
494 details => $result
495 )->throw;
496 }
497 2 return $result->{data};
498 2 }
499  
500 2 1;
« Hydra/Exception/FeatureUnavailable.pm ↑ Summary Hydra/Exception.pm »