| 1 | package Net::Async::Keycloak; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Async Perl client for Keycloak identity management (IO::Async + Future) | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 7 | extends 'IO::Async::Notifier'; | |||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Net::Async::HTTP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Net::Async::Keycloak::Admin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use Net::Async::Keycloak::Auth; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use Net::Async::Keycloak::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use Net::Async::Keycloak::Error::API; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use Net::Async::Keycloak::Error::Network; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 7 | ▶ | use Net::Async::Keycloak::Error::Validation; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 7 | ▶ | use Net::Async::Keycloak::OIDC; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 7 | ▶ | use Types::Standard qw( InstanceOf Object Str ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 7 | ▶ | use URI::Escape qw( uri_escape_utf8 ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 7 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # IO::Async::Notifier->new hands every constructor key to configure(), which | ||||||||||||||||||||||||||||
| 22 | # croaks on keys it does not know. Keep ours away from it. | ||||||||||||||||||||||||||||
| 23 | 23 | ▶ | sub FOREIGNBUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
FOREIGNBUILDARGS: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 23 | my ( $class, @args ) = @_; | |||||||||||||||||||||||||||
| 25 | 23 | ▶ | my %arg = @args == 1 && ref $args[0] eq 'HASH' ? %{ $args[0] } : @args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@args == 1 and ref $args[0] eq "HASH"
MC/DC50%1/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1
ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 26 | 23 | delete @arg{qw( base_url realm username password client_id client_secret token auth_realm http auth )}; | |||||||||||||||||||||||||||
| 27 | 23 | return %arg; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 7 | has base_url => ( is => 'ro', isa => Str, required => 1 ); | |||||||||||||||||||||||||||
| 31 | 7 | has realm => ( is => 'ro', isa => Str, required => 1 ); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 7 | has username => ( is => 'ro', isa => Str, predicate => 'has_username' ); | |||||||||||||||||||||||||||
| 35 | 7 | has password => ( is => 'ro', isa => Str ); | |||||||||||||||||||||||||||
| 36 | 7 | has client_id => ( is => 'ro', isa => Str, predicate => 'has_client_id' ); | |||||||||||||||||||||||||||
| 37 | 7 | has client_secret => ( is => 'ro', isa => Str ); | |||||||||||||||||||||||||||
| 38 | 7 | has token => ( is => 'ro', isa => Str, predicate => 'has_token' ); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 7 | has auth_realm => ( is => 'lazy', isa => Str ); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 10 | ▶ | sub _build_auth_realm { $_[0]->has_username ? 'master' : $_[0]->realm } | ||||||||||||||||||||||||||
|
Subroutine
_build_auth_realm: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 7 | has http => ( is => 'lazy', isa => Object, predicate => 'has_http' ); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 6 | ▶ | sub _build_http { | ||||||||||||||||||||||||||
|
Subroutine
_build_http: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 6 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 50 | # no redirects: nothing here needs one, and none may carry the admin token elsewhere | ||||||||||||||||||||||||||||
| 51 | 6 | my $http = Net::Async::HTTP->new( user_agent => 'Net-Async-Keycloak/'.$VERSION, max_redirects => 0, timeout => 30, fail_on_error => 0 ); | |||||||||||||||||||||||||||
| 52 | 6 | $self->add_child($http); | |||||||||||||||||||||||||||
| 53 | 6 | return $http; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 7 | has auth => ( is => 'lazy', isa => InstanceOf['Net::Async::Keycloak::Auth'] | Types::Standard::Undef ); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 15 | ▶ | sub _build_auth { | ||||||||||||||||||||||||||
|
Subroutine
_build_auth: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 60 | 15 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 61 | 15 | ▶ | return Net::Async::Keycloak::Auth->new( http => $self->http, token => $self->token ) if $self->has_token; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 11 | ▶ | return unless $self->has_username || $self->has_client_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->has_username or $self->has_client_id
MC/DC100%2/2
$self->has_username or $self->has_client_id
$self->has_username
$self->has_client_id
Truth table100%3/3
A or B
A= $self->has_usernameB= $self->has_client_id
| |||||||||||||||||||||||||||||
| 63 | return Net::Async::Keycloak::Auth->new( | ||||||||||||||||||||||||||||
| 64 | http => $self->http, | ||||||||||||||||||||||||||||
| 65 | token_endpoint => $self->base_url.'/realms/'.uri_escape_utf8( $self->auth_realm ).'/protocol/openid-connect/token', | ||||||||||||||||||||||||||||
| 66 | 10 | map { $_ => $self->$_ } grep { defined $self->$_ } qw( username password client_id client_secret ) | |||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 7 | has oidc => ( is => 'lazy', init_arg => undef ); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 6 | ▶ | sub _build_oidc { Net::Async::Keycloak::OIDC->new( issuer => $_[0]->issuer, http => $_[0]->http ) } | ||||||||||||||||||||||||||
|
Subroutine
_build_oidc: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 7 | has admin => ( is => 'lazy', init_arg => undef ); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 13 | ▶ | sub _build_admin { | ||||||||||||||||||||||||||
|
Subroutine
_build_admin: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 79 | 13 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 80 | 13 | ▶ | return Net::Async::Keycloak::Admin->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | base_url => $self->base_url, | ||||||||||||||||||||||||||||
| 82 | realm => $self->realm, | ||||||||||||||||||||||||||||
| 83 | http => $self->http, | ||||||||||||||||||||||||||||
| 84 | $self->auth ? ( auth => $self->auth ) : () | ||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # The parent is not a Moo class, so there is no BUILDARGS to wrap. | ||||||||||||||||||||||||||||
| 90 | 25 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 91 | 25 | my ( $class, @args ) = @_; | |||||||||||||||||||||||||||
| 92 | 25 | ▶ | my %args = @args == 1 && ref $args[0] eq 'HASH' ? %{ $args[0] } : @args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@args == 1 and ref $args[0] eq "HASH"
MC/DC50%1/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1
ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 93 | 25 | ▶ | $args{base_url} =~ s{/+\z}{} if defined $args{base_url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 25 | return \%args; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 23 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | 23 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 99 | 23 | for (qw( base_url realm )) { | |||||||||||||||||||||||||||
| 100 | 45 | ▶ | Net::Async::Keycloak::Error::Validation->throw( message => __PACKAGE__.' needs a '.$_ ) unless length $self->$_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 21 | return; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 9 | ▶ | sub issuer { $_[0]->base_url.'/realms/'.uri_escape_utf8( $_[0]->realm ) } | ||||||||||||||||||||||||||
|
Subroutine
issuer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | ▶ | sub for_realm { | ||||||||||||||||||||||||||
|
Subroutine
for_realm: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 1 | my ( $self, $realm ) = @_; | |||||||||||||||||||||||||||
| 110 | 1 | ▶ | return ref($self)->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | base_url => $self->base_url, | ||||||||||||||||||||||||||||
| 112 | realm => $realm, | ||||||||||||||||||||||||||||
| 113 | http => $self->http, | ||||||||||||||||||||||||||||
| 114 | auth_realm => $self->auth_realm, | ||||||||||||||||||||||||||||
| 115 | $self->auth ? ( auth => $self->auth ) : () | ||||||||||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 7 | 1; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | __END__ | ||||||||||||||||||||||||||||