| 1 | package Net::Async::Authentik; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Async Perl client for the authentik identity provider (IO::Async + Future) | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use Moo; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 8 | extends 'IO::Async::Notifier'; | |||||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Net::Async::Authentik::API; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Net::Async::Authentik::Error; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Net::Async::Authentik::Error::API; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use Net::Async::Authentik::Error::Network; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use Net::Async::Authentik::Error::Validation; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 8 | ▶ | use Net::Async::Authentik::OIDC; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 8 | ▶ | use Net::Async::HTTP; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 8 | ▶ | use Types::Standard qw( Object Str ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 8 | ▶ | use URI::Escape qw( uri_escape_utf8 ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 8 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | # IO::Async::Notifier->new hands every constructor key to configure(), which | ||||||||||||||||||||||||||||||||
| 21 | # croaks on keys it does not know. Keep ours away from it. | ||||||||||||||||||||||||||||||||
| 22 | 32 | ▶ | sub FOREIGNBUILDARGS { | ||||||||||||||||||||||||||||||
|
Subroutine
FOREIGNBUILDARGS: called CC=3 SCAR=12.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 23 | 32 | my ( $class, @args ) = @_; | |||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | my %arg = @args == 1 && ref $args[0] eq 'HASH' ? %{ $args[0] } : @args; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@args == 1 and ref $args[0] eq "HASH"
MC/DC0%0/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1
ref $args[0] eq "HASH"
Truth table33%1/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
| |||||||||||||||||||||||||||||||||
| 25 | 32 | delete @arg{qw( base_url application client_id token http )}; | |||||||||||||||||||||||||||||||
| 26 | 32 | return %arg; | |||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 8 | has base_url => ( is => 'ro', isa => Str, required => 1 ); | |||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 8 | has application => ( is => 'ro', isa => Str, predicate => 'has_application' ); | |||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 8 | has client_id => ( is => 'ro', isa => Str, predicate => 'has_client_id' ); | |||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 8 | has token => ( is => 'ro', isa => Str, predicate => 'has_token' ); | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 8 | has http => ( is => 'lazy', isa => Object, predicate => 'has_http' ); | |||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 5 | ▶ | sub _build_http { | ||||||||||||||||||||||||||||||
|
Subroutine
_build_http: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 44 | 5 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||
| 45 | 5 | my $http = Net::Async::HTTP->new( | |||||||||||||||||||||||||||||||
| 46 | user_agent => 'Net-Async-Authentik/'.$VERSION, | ||||||||||||||||||||||||||||||||
| 47 | # no redirects: authentik answers 302 wherever it wants a browser, and | ||||||||||||||||||||||||||||||||
| 48 | # those answers are read, not followed | ||||||||||||||||||||||||||||||||
| 49 | max_redirects => 0, | ||||||||||||||||||||||||||||||||
| 50 | # a 4xx is an answer here, so that read_response can make an error with | ||||||||||||||||||||||||||||||||
| 51 | # authentik's own message out of it | ||||||||||||||||||||||||||||||||
| 52 | fail_on_error => 0, | ||||||||||||||||||||||||||||||||
| 53 | timeout => 30 | ||||||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||||||
| 55 | 5 | $self->add_child($http); | |||||||||||||||||||||||||||||||
| 56 | 5 | return $http; | |||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 8 | has oidc => ( is => 'lazy', init_arg => undef ); | |||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 11 | ▶ | sub _build_oidc { | ||||||||||||||||||||||||||||||
|
Subroutine
_build_oidc: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 63 | 11 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||
| 64 | 11 | ▶ | return Net::Async::Authentik::OIDC->new( | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$self->has_application and length $self->application
MC/DC50%1/2
$self->has_application and length $self->application
$self->has_application
length $self->application
Truth table66%2/3
A and B
A= $self->has_applicationB= length $self->application
| |||||||||||||||||||||||||||||||||
| 65 | application_url => $self->has_application && length $self->application ? $self->application_url : undef, | ||||||||||||||||||||||||||||||||
| 66 | http => $self->http, | ||||||||||||||||||||||||||||||||
| 67 | $self->has_client_id ? ( client_id => $self->client_id ) : () | ||||||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 8 | has api => ( is => 'lazy', init_arg => undef ); | |||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 17 | ▶ | sub _build_api { | ||||||||||||||||||||||||||||||
|
Subroutine
_build_api: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 75 | 17 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||
| 76 | 17 | ▶ | return Net::Async::Authentik::API->new( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | base_url => $self->base_url, | ||||||||||||||||||||||||||||||||
| 78 | http => $self->http, | ||||||||||||||||||||||||||||||||
| 79 | $self->has_token ? ( token => $self->token ) : () | ||||||||||||||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | # The parent is not a Moo class, so there is no BUILDARGS to wrap. | ||||||||||||||||||||||||||||||||
| 85 | 33 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=4 SCAR=15.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 86 | 33 | my ( $class, @args ) = @_; | |||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my %args = @args == 1 && ref $args[0] eq 'HASH' ? %{ $args[0] } : @args; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@args == 1 and ref $args[0] eq "HASH"
MC/DC0%0/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1
ref $args[0] eq "HASH"
Truth table33%1/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
| |||||||||||||||||||||||||||||||||
| 88 | 33 | ▶ | $args{base_url} =~ s{/+\z}{} if defined $args{base_url}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 89 | 33 | return \%args; | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 31 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 93 | 31 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||
| 94 | 31 | ▶ | Net::Async::Authentik::Error::Validation->throw( message => __PACKAGE__.' needs a base_url' ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 95 | unless length $self->base_url; | ||||||||||||||||||||||||||||||||
| 96 | 30 | return; | |||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 1 | ▶ | sub api_url { $_[0]->base_url.'/api/v3' } | ||||||||||||||||||||||||||||||
|
Subroutine
api_url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | 15 | ▶ | sub application_url { | ||||||||||||||||||||||||||||||
|
Subroutine
application_url: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 103 | 15 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||
| 104 | 15 | ▶ | Net::Async::Authentik::Error::Validation->throw( message => __PACKAGE__.'->application_url needs an application slug' ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->has_application and length $self->application
MC/DC0%0/2
$self->has_application and length $self->application
$self->has_application
length $self->application
Truth table33%1/3
A and B
A= $self->has_applicationB= length $self->application
| |||||||||||||||||||||||||||||||||
| 105 | unless $self->has_application && length $self->application; | ||||||||||||||||||||||||||||||||
| 106 | 15 | return $self->base_url.'/application/o/'.uri_escape_utf8( $self->application ); | |||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 1 | ▶ | sub issuer { $_[0]->application_url.'/' } | ||||||||||||||||||||||||||||||
|
Subroutine
issuer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 2 | ▶ | sub for_application { | ||||||||||||||||||||||||||||||
|
Subroutine
for_application: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 114 | 2 | my ( $self, $slug ) = @_; | |||||||||||||||||||||||||||||||
| 115 | 2 | ▶ | return ref($self)->new( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 116 | base_url => $self->base_url, | ||||||||||||||||||||||||||||||||
| 117 | application => $slug, | ||||||||||||||||||||||||||||||||
| 118 | http => $self->http, | ||||||||||||||||||||||||||||||||
| 119 | $self->has_token ? ( token => $self->token ) : () | ||||||||||||||||||||||||||||||||
| 120 | # deliberately not the client_id: it belongs to this application's provider | ||||||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 8 | 1; | |||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||||||||||||||