Authentik.pm

filter: Statementstmt 100% 71 / 71 Branchbran 87.5% 14 / 16 Conditioncond 42.8% 6 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 22 / 22 Podpod 83.3% 5 / 6 totaltotal 86.9% 120 / 138 CC 14 SCAR 8.8
CC
14
Cov
90%
default_ua
5.5
_build_oidc
4.0
application_url
3.5
CRAP 2.4
« Authentik/API.pm ↑ Summary Authentik/Error/API.pm »
1 package WWW::Authentik;
2  
3 # ABSTRACT: Perl client for the authentik identity provider (OIDC + REST API v3)
4  
5 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Types::Standard qw( InstanceOf Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use URI::Escape qw( uri_escape_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use WWW::Authentik::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use WWW::Authentik::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use WWW::Authentik::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use WWW::Authentik::Error::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use WWW::Authentik::Error::Network;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use WWW::Authentik::Error::Validation;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use WWW::Authentik::OIDC;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 7 our $VERSION = '0.001';
19  
20  
21 7 has base_url => (
22 is => 'ro',
23 isa => Str,
24 required => 1
25 );
26  
27  
28 7 has application => (
29 is => 'ro',
30 isa => Str,
31 predicate => 'has_application'
32 );
33  
34  
35 7 has token => (
36 is => 'ro',
37 isa => Str,
38 predicate => 'has_token'
39 );
40  
41  
42 7 has client_id => (
43 is => 'ro',
44 isa => Str,
45 predicate => 'has_client_id'
46 );
47  
48  
49 7 has ua => (
50 is => 'lazy',
51 isa => InstanceOf['LWP::UserAgent']
52 );
53  
54 4 ▶sub _build_ua { $_[0]->default_ua }
Subroutine _build_ua: called CC=1 SCAR=0.0
55  
56 5 ▶sub default_ua {
Subroutine default_ua: called CC=4 SCAR=17.1
Pod documented
57 5 my ( $self ) = @_;
58 5 my $ua = LWP::UserAgent->new(
59 timeout => 30,
60 agent => 'WWW-Authentik/'.$VERSION,
61 # no redirects: authentik answers 302 wherever it wants a browser, and
62 # none of those may carry the API token anywhere
63 max_redirect => 0,
64 # authentik 2026.8.3 hangs on every second request that announces the TE
65 # connection token, which LWP does by default; see below
66 send_te => 0,
67 ssl_opts => { verify_hostname => 1 }
68 );
69 # An LWP from before 6.33 does not know send_te: it only carps, and only
70 # under -w, and the caller would meet the hang in production instead. Say
71 # it here, once, loudly.
72 WWW::Authentik::Error::Validation->throw( message => 'this LWP::UserAgent ('
73 .( $LWP::UserAgent::VERSION // 'unknown version' ).') does not take send_te, so it would announce the TE '
74 .'connection token and authentik would leave every second request unanswered. libwww-perl 6.33 or newer '
75 .'is needed.' )
76 5 ▶ unless defined $ua->{send_te} && !$ua->{send_te};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ua->{"send_te"} and not $ua->{"send_te"} 0 5 5
Condition20%1/5
defined $ua->{"send_te"} and not $ua->{"send_te"}
!ll&&!rl&&r
005
$LWP::UserAgent::VERSION // "unknown version"
l!l
00
MC/DC0%0/2
defined $ua->{"send_te"} and not $ua->{"send_te"}
defined $ua->{"send_te"} not $ua->{"send_te"}
MC/DC0%0/1
$LWP::UserAgent::VERSION // "unknown version"
$LWP::UserAgent::VERSION
Truth table33%1/3
A and not(B)
A= defined $ua->{"send_te"}B= not $ua->{"send_te"}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $LWP::UserAgent::VERSION
Aresult
11
00
77 5 return $ua;
78 }
79  
80  
81 7 has oidc => (
82 is => 'lazy',
83 init_arg => undef
84 );
85  
86 10 ▶sub _build_oidc {
Subroutine _build_oidc: called CC=4 SCAR=13.9
87 10 my ( $self ) = @_;
88 10 ▶ WWW::Authentik::Error::Validation->throw( message => __PACKAGE__.'->oidc needs an application slug' )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_application and length $self->application 1 9 10
Condition66%2/3
$self->has_application and length $self->application
!ll&&!rl&&r
109
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
ABresult
0X0
100
111
89 unless $self->has_application && length $self->application;
90 9 ▶ return WWW::Authentik::OIDC->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->has_client_id ? : 1 8 9
91 application_url => $self->application_url,
92 ua => $self->ua,
93 $self->has_client_id ? ( client_id => $self->client_id ) : ()
94 );
95 }
96  
97  
98 7 has api => (
99 is => 'lazy',
100 init_arg => undef
101 );
102  
103 9 ▶sub _build_api {
Subroutine _build_api: called CC=3 SCAR=11.0
104 9 my ( $self ) = @_;
105 9 ▶ WWW::Authentik::Error::Validation->throw( message => __PACKAGE__.'->api needs an API token' )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_token and length $self->token 1 8 9
Condition66%2/3
$self->has_token and length $self->token
!ll&&!rl&&r
108
MC/DC50%1/2
$self->has_token and length $self->token
$self->has_token length $self->token
Truth table66%2/3
A and B
A= $self->has_tokenB= length $self->token
ABresult
0X0
100
111
106 unless $self->has_token && length $self->token;
107 8 return WWW::Authentik::API->new( base_url => $self->base_url, token => $self->token, ua => $self->ua );
108 }
109  
110  
111 24 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
112 24 my ( $orig, $class, @args ) = @_;
113 24 my $args = $class->$orig(@args);
114 24 ▶ $args->{base_url} =~ s{/+\z}{} if defined $args->{base_url};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"base_url"} 23 1 24
115 24 return $args;
116 7 };
117  
118 23 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
119 23 my ( $self ) = @_;
120 23 ▶ WWW::Authentik::Error::Validation->throw( message => __PACKAGE__.' needs a base_url' ) unless length $self->base_url;
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->base_url 1 22 23
121 22 return;
122 }
123  
124 1 ▶sub api_url { $_[0]->base_url.'/api/v3' }
Subroutine api_url: called CC=1 SCAR=0.0
Pod documented
125  
126  
127 15 ▶sub application_url {
Subroutine application_url: called CC=3 SCAR=12.5
Pod documented
128 15 my ( $self ) = @_;
129 15 ▶ WWW::Authentik::Error::Validation->throw( message => __PACKAGE__.'->application_url needs an application slug' )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_application and length $self->application 0 15 15
Condition33%1/3
$self->has_application and length $self->application
!ll&&!rl&&r
0015
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
ABresult
0X0
100
111
130 unless $self->has_application && length $self->application;
131 15 return $self->base_url.'/application/o/'.uri_escape_utf8( $self->application );
132 }
133  
134  
135 1 ▶sub issuer { $_[0]->application_url.'/' }
Subroutine issuer: called CC=1 SCAR=0.0
Pod documented
136  
137  
138 2 ▶sub for_application {
Subroutine for_application: called CC=2 SCAR=6.9
Pod documented
139 2 my ( $self, $slug ) = @_;
140 2 ▶ return ref($self)->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->has_token ? : 1 1 2
141 base_url => $self->base_url,
142 application => $slug,
143 ua => $self->ua,
144 $self->has_token ? ( token => $self->token ) : ()
145 # deliberately not the client_id: it belongs to this application's provider
146 );
147 }
148  
149  
150 7 1;
151  
152 __END__
« Authentik/API.pm ↑ Summary Authentik/Error/API.pm »