Keycloak/Auth.pm

filter: Statementstmt 100% 84 / 84 Branchbran 94.1% 32 / 34 Conditioncond 76.6% 23 / 30 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 100% 16 / 16 Podpod 75.0% 3 / 4 totaltotal 90.7% 167 / 184 CC 29 SCAR 15.9
CC
29
Cov
94%
BUILD
8.0
_grant_f
7.6
token_f
6.0
CRAP 4.9
↑ Summary Keycloak/OIDC.pm »
1 package Net::Async::Keycloak::Auth;
2  
3 # ABSTRACT: Get and keep a valid admin token for the Keycloak Admin API, asynchronously
4  
5 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 with 'Net::Async::Keycloak::Role::HTTP';
7 8 with 'WWW::Keycloak::Role::HTTP';
8 8 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Types::Standard qw( CodeRef Int Object Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 our $VERSION = '0.001';
15  
16  
17 8 has http => (
18 is => 'ro',
19 isa => Object,
20 required => 1
21 );
22  
23  
24 8 has token_endpoint => ( is => 'ro', isa => Str );
25 8 has username => ( is => 'ro', isa => Str, predicate => 'has_username' );
26 8 has password => ( is => 'ro', isa => Str );
27 8 has client_id => ( is => 'ro', isa => Str, predicate => 'has_client_id' );
28 8 has client_secret => ( is => 'ro', isa => Str );
29 8 has fixed_token => ( is => 'ro', isa => Str, init_arg => 'token', predicate => 'has_fixed_token' );
30  
31  
32 8 has margin => ( is => 'ro', isa => Int, default => 30 );
33 20 ▶has now => ( is => 'ro', isa => CodeRef, default => sub { sub { time } } );
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
34  
35  
36 8 has _access => ( is => 'rw' );
37 8 has _access_expires => ( is => 'rw' );
38 8 has _refresh => ( is => 'rw' );
39 8 has _refresh_expires => ( is => 'rw' );
40 8 has _pending => ( is => 'rw' );
41  
42 26 ▶sub BUILD {
Subroutine BUILD: called CC=8 SCAR=20.8
Pod undocumented
43 26 my ( $self ) = @_;
44 26 ▶ return if $self->has_fixed_token;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_fixed_token 5 21 26
45 21 ▶ $self->validation_error_class->throw( message => __PACKAGE__.' needs username and password, client_id and client_secret, or token' )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_username and defined $self->password or $self->has_client_id and defined $self->client_secret 2 19 21
Condition88%8/9
$self->has_username and defined $self->password or $self->has_client_id and defined $self->client_secret
l!l&&r!l&&!r
1632
$self->has_username and defined $self->password
!ll&&!rl&&r
4116
$self->has_client_id and defined $self->client_secret
!ll&&!rl&&r
203
MC/DC75%3/4
$self->has_username and defined $self->password or $self->has_client_id and defined $self->client_secret
$self->has_username defined $self->password $self->has_client_id defined $self->client_secret
Truth table57%4/7
A and B or C and D
A= $self->has_usernameB= defined $self->passwordC= $self->has_client_idD= defined $self->client_secret
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
46 unless ( $self->has_username && defined $self->password ) || ( $self->has_client_id && defined $self->client_secret );
47 19 ▶ $self->validation_error_class->throw( message => __PACKAGE__.' needs a token_endpoint' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->token_endpoint and length $self->token_endpoint 1 18 19
Condition66%2/3
defined $self->token_endpoint and length $self->token_endpoint
!ll&&!rl&&r
1018
MC/DC50%1/2
defined $self->token_endpoint and length $self->token_endpoint
defined $self->token_endpoint length $self->token_endpoint
Truth table66%2/3
A and B
A= defined $self->token_endpointB= length $self->token_endpoint
ABresult
0X0
100
111
48 unless defined $self->token_endpoint && length $self->token_endpoint;
49 18 return;
50 }
51  
52 7 ▶sub renewable { $_[0]->has_fixed_token ? 0 : 1 }
Subroutine renewable: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_fixed_token ? : 2 5 7
53  
54 151 ▶sub token_f {
Subroutine token_f: called CC=6 SCAR=17.9
Pod documented
55 151 my ( $self ) = @_;
56 151 ▶ return Future->done( $self->fixed_token ) if $self->has_fixed_token;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_fixed_token 6 145 151
57 145 ▶ return Future->done( $self->_access )
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_access and $self->now->() < $self->_access_expires - $self->margin 117 28 145
Condition100%3/3
defined $self->_access and $self->now->() < $self->_access_expires - $self->margin
!ll&&!rl&&r
262117
MC/DC100%2/2
defined $self->_access and $self->now->() < $self->_access_expires - $self->margin
defined $self->_access $self->now->() < $self->_access_expires - $self->margin
Truth table100%3/3
A and B
A= defined $self->_accessB= $self->now->() < $self->_access_expires - $self->margin
ABresult
0X0
100
111
58 if defined $self->_access && $self->now->() < $self->_access_expires - $self->margin;
59 28 ▶ unless ( $self->_pending ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_pending) 23 5 28
60 23 ▶ my $login = $self->_renew_f->on_ready( sub { $self->_pending(undef) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
61 23 ▶ return $login if $login->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $login->is_ready 17 6 23
62 6 $self->_pending($login);
63 }
64 # every caller gets its own view: cancelling one must not cancel the login
65 # the others are waiting for
66 11 return $self->_pending->without_cancel;
67 }
68  
69  
70 6 ▶sub invalidate {
Subroutine invalidate: called CC=4 SCAR=13.9
Pod documented
71 6 my ( $self, $refused ) = @_;
72 # a refusal of a token that was already replaced says nothing about the new one
73 6 ▶ return if defined $refused && ( $self->_access // '' ) ne $refused;
Branch: 1 branch
BranchTrueFalseTotal
if defined $refused and ($self->_access // "") ne $refused 2 4 6
Condition100%5/5
defined $refused and ($self->_access // "") ne $refused
!ll&&!rl&&r
132
$self->_access // ""
l!l
32
MC/DC100%2/2
defined $refused and ($self->_access // "") ne $refused
defined $refused $self->_access
Truth table100%3/3
A and B
A= defined $refusedB= $self->_access
ABresult
0X0
100
111
74 4 $self->_access(undef);
75 4 $self->_refresh(undef);
76 4 return;
77 }
78  
79  
80 23 ▶async sub _renew_f {
Subroutine _renew_f: called CC=5 SCAR=16.1
81 23 my ( $self ) = @_;
82 23 ▶ if ( defined $self->_refresh && $self->now->() < $self->_refresh_expires - $self->margin ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_refresh and $self->now->() < $self->_refresh_expires - $self->margin) 2 21 23
Condition66%2/3
defined $self->_refresh and $self->now->() < $self->_refresh_expires - $self->margin
!ll&&!rl&&r
2102
MC/DC50%1/2
defined $self->_refresh and $self->now->() < $self->_refresh_expires - $self->margin
defined $self->_refresh $self->now->() < $self->_refresh_expires - $self->margin
Truth table66%2/3
A and B
A= defined $self->_refreshB= $self->now->() < $self->_refresh_expires - $self->margin
ABresult
0X0
100
111
83 2 my $renewed = eval {
84 2 await $self->_grant_f( { grant_type => 'refresh_token', refresh_token => $self->_refresh, $self->_client } );
85 1 1;
86 };
87 2 ▶ return $self->_access if $renewed;
Branch: 1 branch
BranchTrueFalseTotal
if $renewed 1 1 2
88 }
89 await $self->_grant_f( $self->has_username
90 ? { grant_type => 'password', username => $self->username, password => $self->password, $self->_client }
91 22 ▶ : { grant_type => 'client_credentials', $self->_client } );
Branch: 1 branch
BranchTrueFalseTotal
$self->has_username ? : 19 3 22
92 17 return $self->_access;
93 8 }
94  
95 24 ▶sub _client {
Subroutine _client: called CC=3 SCAR=11.0
96 24 my ( $self ) = @_;
97 return (
98 24 ▶ client_id => $self->has_client_id ? $self->client_id : 'admin-cli',
Branch: 2 branches
BranchTrueFalseTotal
$self->has_client_id ? : 3 21 24
defined $self->client_secret ? : 3 21 24
99 defined $self->client_secret ? ( client_secret => $self->client_secret ) : ()
100 );
101 }
102  
103 24 ▶async sub _grant_f {
Subroutine _grant_f: called CC=7 SCAR=20.3
104 24 my ( $self, $form ) = @_;
105 24 my $now = $self->now->();
106 24 my $data = eval { ( await $self->send_request_f( POST => $self->token_endpoint, form => $form ) )->{data} };
107 24 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 6 18 24
108 6 ▶ die $error unless blessed $error && $error->isa('WWW::Keycloak::Error::API');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($error) and $error->isa("WWW::Keycloak::Error::API") 0 6 6
Condition33%1/3
builtin::blessed($error) and $error->isa("WWW::Keycloak::Error::API")
!ll&&!rl&&r
006
MC/DC0%0/2
builtin::blessed($error) and $error->isa("WWW::Keycloak::Error::API")
builtin::blessed($error) $error->isa("WWW::Keycloak::Error::API")
Truth table33%1/3
A and B
A= builtin::blessed($error)B= $error->isa("WWW::Keycloak::Error::API")
ABresult
0X0
100
111
109 6 ▶ $self->api_error_class->throw(
Branch: 1 branch
BranchTrueFalseTotal
defined $error->api_message ? : 6 0 6
110 message => 'admin login failed: '.$error->http_status.( defined $error->api_message ? ' - '.$error->api_message : '' ),
111 http_status => $error->http_status,
112 api_message => $error->api_message,
113 oauth_error => $error->oauth_error
114 );
115 }
116 18 $self->_access( $data->{access_token} );
117 18 ▶ $self->_access_expires( $now + ( $data->{expires_in} || 60 ) );
Condition50%1/2
$data->{'expires_in'} || 60
l!l
180
MC/DC0%0/1
$data->{'expires_in'} || 60
$data->{'expires_in'}
Truth table50%1/2
A
A= $data->{'expires_in'}
Aresult
11
00
118 18 $self->_refresh( $data->{refresh_token} );
119 18 ▶ $self->_refresh_expires( $now + ( $data->{refresh_expires_in} || 0 ) );
Condition50%1/2
$data->{'refresh_expires_in'} || 0
l!l
180
MC/DC0%0/1
$data->{'refresh_expires_in'} || 0
$data->{'refresh_expires_in'}
Truth table50%1/2
A
A= $data->{'refresh_expires_in'}
Aresult
11
00
120 18 return;
121 8 }
122  
123 8 1;
124  
125 __END__
↑ Summary Keycloak/OIDC.pm »