| 1 | package WWW::Keycloak; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Perl client for Keycloak identity management (OIDC + Admin REST API) | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Types::Standard qw( InstanceOf Str ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use URI::Escape qw( uri_escape_utf8 ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use WWW::Keycloak::Admin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use WWW::Keycloak::Auth; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use WWW::Keycloak::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use WWW::Keycloak::Error::API; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use WWW::Keycloak::Error::Network; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use WWW::Keycloak::Error::Validation; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 6 | ▶ | use WWW::Keycloak::OIDC; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 6 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 6 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 6 | has base_url => ( | |||||||||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||||||||||
| 23 | isa => Str, | ||||||||||||||||||||||||||||
| 24 | required => 1 | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 6 | has realm => ( | |||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||
| 30 | isa => Str, | ||||||||||||||||||||||||||||
| 31 | required => 1 | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 6 | has username => ( is => 'ro', isa => Str, predicate => 'has_username' ); | |||||||||||||||||||||||||||
| 36 | 6 | has password => ( is => 'ro', isa => Str ); | |||||||||||||||||||||||||||
| 37 | 6 | has client_id => ( is => 'ro', isa => Str, predicate => 'has_client_id' ); | |||||||||||||||||||||||||||
| 38 | 6 | has client_secret => ( is => 'ro', isa => Str ); | |||||||||||||||||||||||||||
| 39 | 6 | has token => ( is => 'ro', isa => Str, predicate => 'has_token' ); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 6 | has auth_realm => ( | |||||||||||||||||||||||||||
| 43 | is => 'lazy', | ||||||||||||||||||||||||||||
| 44 | isa => Str | ||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 8 | ▶ | sub _build_auth_realm { $_[0]->has_username ? 'master' : $_[0]->realm } | ||||||||||||||||||||||||||
|
Subroutine
_build_auth_realm: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 6 | has ua => ( | |||||||||||||||||||||||||||
| 51 | is => 'lazy', | ||||||||||||||||||||||||||||
| 52 | isa => InstanceOf['LWP::UserAgent'] | ||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 4 | ▶ | sub _build_ua { | ||||||||||||||||||||||||||
|
Subroutine
_build_ua: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | # no redirects: nothing here needs one, and none may carry the admin token elsewhere | ||||||||||||||||||||||||||||
| 57 | 4 | return LWP::UserAgent->new( timeout => 30, agent => 'WWW-Keycloak/'.$VERSION, max_redirect => 0, ssl_opts => { verify_hostname => 1 } ); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 6 | has auth => ( | |||||||||||||||||||||||||||
| 62 | is => 'lazy', | ||||||||||||||||||||||||||||
| 63 | isa => InstanceOf['WWW::Keycloak::Auth'] | Types::Standard::Undef | ||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 11 | ▶ | sub _build_auth { | ||||||||||||||||||||||||||
|
Subroutine
_build_auth: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 67 | 11 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 68 | 11 | ▶ | return WWW::Keycloak::Auth->new( ua => $self->ua, token => $self->token ) if $self->has_token; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 9 | ▶ | 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
| |||||||||||||||||||||||||||||
| 70 | return WWW::Keycloak::Auth->new( | ||||||||||||||||||||||||||||
| 71 | ua => $self->ua, | ||||||||||||||||||||||||||||
| 72 | token_endpoint => $self->base_url.'/realms/'.uri_escape_utf8( $self->auth_realm ).'/protocol/openid-connect/token', | ||||||||||||||||||||||||||||
| 73 | 8 | map { $_ => $self->$_ } grep { defined $self->$_ } qw( username password client_id client_secret ) | |||||||||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 6 | has oidc => ( | |||||||||||||||||||||||||||
| 79 | is => 'lazy', | ||||||||||||||||||||||||||||
| 80 | init_arg => undef | ||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 4 | ▶ | sub _build_oidc { | ||||||||||||||||||||||||||
|
Subroutine
_build_oidc: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | 4 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 85 | 4 | return WWW::Keycloak::OIDC->new( issuer => $self->issuer, ua => $self->ua ); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 6 | has admin => ( | |||||||||||||||||||||||||||
| 90 | is => 'lazy', | ||||||||||||||||||||||||||||
| 91 | init_arg => undef | ||||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 9 | ▶ | sub _build_admin { | ||||||||||||||||||||||||||
|
Subroutine
_build_admin: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 95 | 9 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 96 | 9 | ▶ | return WWW::Keycloak::Admin->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | base_url => $self->base_url, | ||||||||||||||||||||||||||||
| 98 | realm => $self->realm, | ||||||||||||||||||||||||||||
| 99 | ua => $self->ua, | ||||||||||||||||||||||||||||
| 100 | $self->auth ? ( auth => $self->auth ) : () | ||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 19 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 106 | 19 | my ( $orig, $class, @args ) = @_; | |||||||||||||||||||||||||||
| 107 | 19 | my $args = $class->$orig(@args); | |||||||||||||||||||||||||||
| 108 | 19 | ▶ | $args->{base_url} =~ s{/+\z}{} if defined $args->{base_url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 19 | return $args; | |||||||||||||||||||||||||||
| 110 | 6 | }; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 17 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | 17 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 114 | 17 | for (qw( base_url realm )) { | |||||||||||||||||||||||||||
| 115 | 33 | ▶ | WWW::Keycloak::Error::Validation->throw( message => __PACKAGE__.' needs a '.$_ ) unless length $self->$_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 15 | return; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 7 | ▶ | sub issuer { $_[0]->base_url.'/realms/'.uri_escape_utf8( $_[0]->realm ) } | ||||||||||||||||||||||||||
|
Subroutine
issuer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | ▶ | sub for_realm { | ||||||||||||||||||||||||||
|
Subroutine
for_realm: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 1 | my ( $self, $realm ) = @_; | |||||||||||||||||||||||||||
| 125 | 1 | ▶ | return ref($self)->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | base_url => $self->base_url, | ||||||||||||||||||||||||||||
| 127 | realm => $realm, | ||||||||||||||||||||||||||||
| 128 | ua => $self->ua, | ||||||||||||||||||||||||||||
| 129 | auth_realm => $self->auth_realm, | ||||||||||||||||||||||||||||
| 130 | $self->auth ? ( auth => $self->auth ) : () | ||||||||||||||||||||||||||||
| 131 | ); | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 6 | 1; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||||||||||