| 1 | package WWW::Keycloak::Auth; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Get and keep a valid admin token for the Keycloak Admin API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | with 'WWW::Keycloak::Role::HTTP'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Types::Standard qw( CodeRef InstanceOf Int Str ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use WWW::Keycloak::Error::Validation; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 7 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 7 | has ua => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | isa => InstanceOf['LWP::UserAgent'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | required => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 7 | has token_endpoint => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | isa => Str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 7 | has username => ( is => 'ro', isa => Str, predicate => 'has_username' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 7 | has password => ( is => 'ro', isa => Str ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 7 | has client_id => ( is => 'ro', isa => Str, predicate => 'has_client_id' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 7 | has client_secret => ( is => 'ro', isa => Str ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 7 | has fixed_token => ( is => 'ro', isa => Str, init_arg => 'token', predicate => 'has_fixed_token' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 7 | has margin => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | isa => Int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | default => 30 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | has now => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | isa => CodeRef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 14 | ▶ | default => sub { sub { time } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 7 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 7 | has _access => ( is => 'rw' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 7 | has _access_expires => ( is => 'rw' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 7 | has _refresh => ( is => 'rw' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 7 | has _refresh_expires => ( is => 'rw' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 20 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 20 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 20 | ▶ | return if $self->has_fixed_token; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 17 | ▶ | WWW::Keycloak::Error::Validation->throw( message => __PACKAGE__.' needs username and password, client_id and client_secret, or token' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
$self->has_username and defined $self->password or $self->has_client_id and defined $self->client_secret
$self->has_username and defined $self->password
$self->has_client_id and defined $self->client_secret
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | unless ( $self->has_username && defined $self->password ) || ( $self->has_client_id && defined $self->client_secret ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 15 | ▶ | WWW::Keycloak::Error::Validation->throw( message => __PACKAGE__.' needs a token_endpoint' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->token_endpoint and length $self->token_endpoint
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | unless defined $self->token_endpoint && length $self->token_endpoint; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 14 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 4 | ▶ | sub renewable { $_[0]->has_fixed_token ? 0 : 1 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
renewable: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 133 | ▶ | sub token { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
token: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 133 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 133 | ▶ | return $self->fixed_token if $self->has_fixed_token; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 130 | my $now = $self->now->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 130 | ▶ | return $self->_access if defined $self->_access && $now < $self->_access_expires - $self->margin; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->_access and $now < $self->_access_expires - $self->margin
MC/DC100%2/2
defined $self->_access and $now < $self->_access_expires - $self->margin
defined $self->_access
$now < $self->_access_expires - $self->margin
Truth table100%3/3
A and B
A= defined $self->_accessB= $now < $self->_access_expires - $self->margin
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 17 | ▶ | if ( defined $self->_refresh && $now < $self->_refresh_expires - $self->margin ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->_refresh and $now < $self->_refresh_expires - $self->margin
MC/DC50%1/2
defined $self->_refresh and $now < $self->_refresh_expires - $self->margin
defined $self->_refresh
$now < $self->_refresh_expires - $self->margin
Truth table66%2/3
A and B
A= defined $self->_refreshB= $now < $self->_refresh_expires - $self->margin
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | my $renewed = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | $self->_grant( { grant_type => 'refresh_token', refresh_token => $self->_refresh, $self->_client } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | ▶ | return $self->_access if $renewed; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 16 | ▶ | $self->_grant( $self->has_username | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | ? { grant_type => 'password', username => $self->username, password => $self->password, $self->_client } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | : { grant_type => 'client_credentials', $self->_client } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 13 | return $self->_access; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 3 | ▶ | sub invalidate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
invalidate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 3 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | $self->_access(undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | $self->_refresh(undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 18 | ▶ | sub _client { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_client: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 18 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 18 | ▶ | client_id => $self->has_client_id ? $self->client_id : 'admin-cli', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | defined $self->client_secret ? ( client_secret => $self->client_secret ) : () | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 18 | ▶ | sub _grant { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_grant: called CC=7 SCAR=20.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 18 | my ( $self, $form ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 18 | my $now = $self->now->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 18 | my $data = eval { $self->send_request( POST => $self->token_endpoint, form => $form )->{data} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 18 | ▶ | if ( my $error = $@ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 4 | ▶ | die $error unless ref $error && $error->isa('WWW::Keycloak::Error::API'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $error and $error->isa("WWW::Keycloak::Error::API")
MC/DC0%0/2
ref $error and $error->isa("WWW::Keycloak::Error::API")
ref $error
$error->isa("WWW::Keycloak::Error::API")
Truth table33%1/3
A and B
A= ref $errorB= $error->isa("WWW::Keycloak::Error::API")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 4 | ▶ | $self->api_error_class->throw( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | message => 'admin login failed: '.$error->http_status.( defined $error->api_message ? ' - '.$error->api_message : '' ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | http_status => $error->http_status, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | api_message => $error->api_message, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | oauth_error => $error->oauth_error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 14 | $self->_access( $data->{access_token} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 14 | ▶ | $self->_access_expires( $now + ( $data->{expires_in} || 60 ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$data->{'expires_in'} || 60
MC/DC0%0/1
$data->{'expires_in'} || 60
$data->{'expires_in'}
Truth table50%1/2
A
A= $data->{'expires_in'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 14 | $self->_refresh( $data->{refresh_token} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 14 | ▶ | $self->_refresh_expires( $now + ( $data->{refresh_expires_in} || 0 ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$data->{'refresh_expires_in'} || 0
MC/DC0%0/1
$data->{'refresh_expires_in'} || 0
$data->{'refresh_expires_in'}
Truth table50%1/2
A
A= $data->{'refresh_expires_in'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 14 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||