Authentik/Role/HTTP.pm

filter: Statementstmt 97.8% 89 / 91 Branchbran 84.0% 42 / 50 Conditioncond 65.7% 25 / 38 MC/DCmcdc 42.8% 9 / 21 Subroutinesub 93.7% 15 / 16 Podpod 100% 8 / 8 totaltotal 83.9% 188 / 224 CC 41 SCAR 27.8
CC
41
Cov
87%
read_response
29.2
flatten_field_errors
7.6
build_request
6.0
CRAP 16.2
↑ Summary Authentik/Diff.pm »
1 package WWW::Authentik::Role::HTTP;
2  
3 # ABSTRACT: Sending requests to authentik and turning failures into exceptions
4  
5 7 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use MIME::Base64 qw( encode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use WWW::Authentik::Error::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use WWW::Authentik::Error::Network;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use WWW::Authentik::Error::Validation;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 our $VERSION = '0.001';
15  
16  
17 333 ▶sub json_codec { JSON::MaybeXS->new( utf8 => 1, canonical => 1, convert_blessed => 1 ) }
Subroutine json_codec: called CC=1 SCAR=0.0
Pod documented
18  
19  
20 47 ▶sub api_error_class { 'WWW::Authentik::Error::API' }
Subroutine api_error_class: called CC=1 SCAR=0.0
Pod documented
21 1 ▶sub network_error_class { 'WWW::Authentik::Error::Network' }
Subroutine network_error_class: called CC=1 SCAR=0.0
Pod documented
22 0 ▶sub validation_error_class { 'WWW::Authentik::Error::Validation' }
Subroutine validation_error_class: not called CC=1 SCAR=6.9
Pod documented
23  
24  
25 288 ▶sub build_request {
Subroutine build_request: called CC=6 SCAR=17.9
Pod documented
26 288 my ( $self, $method, $url, %arg ) = @_;
27 288 my $request = HTTP::Request->new( $method => $url );
28 288 $request->header( Accept => 'application/json' );
29 288 ▶ $request->header( Authorization => 'Bearer '.$arg{bearer} ) if defined $arg{bearer};
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg{"bearer"} 253 35 288
30 288 ▶ $request->header( Authorization => 'Basic '.encode_base64( $arg{basic}[0].':'.$arg{basic}[1], '' ) ) if $arg{basic};
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"basic"} 0 288 288
31 288 ▶ if ( exists $arg{json} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $arg{'json'}) { } 67 221 288
elsif ($arg{'form'}) { } 22 199 221
32 67 $request->header( 'Content-Type' => 'application/json' );
33 67 $request->content( $self->json_codec->encode( $arg{json} ) );
34 }
35 elsif ( $arg{form} ) {
36 22 my $uri = URI->new('http:');
37 22 $uri->query_form( map { $_ => $arg{form}{$_} } grep { defined $arg{form}{$_} } sort keys %{ $arg{form} } );
38 22 $request->header( 'Content-Type' => 'application/x-www-form-urlencoded' );
39 22 ▶ $request->content( $uri->query // '' );
Condition50%1/2
$uri->query // ""
l!l
220
MC/DC0%0/1
$uri->query // ""
$uri->query
Truth table50%1/2
A
A= $uri->query
Aresult
11
00
40 }
41 288 return $request;
42 }
43  
44  
45 293 ▶sub read_response {
Subroutine read_response: called CC=27 SCAR=33.7
Pod documented
46 293 my ( $self, $response, $method, $url, %arg ) = @_;
47 # decoded_content undoes a Content-Encoding; the raw content does not. With
48 # charset => 'none' it stops short of decoding the character set, which is
49 # what the JSON codec wants, because it decodes UTF-8 itself. Reading the
50 # raw content here would lose the whole body to any user agent that asked
51 # for gzip.
52 293 ▶ my $bytes = $response->decoded_content( charset => 'none' ) // '';
Condition50%1/2
$response->decoded_content("charset", "none") // ""
l!l
2930
MC/DC0%0/1
$response->decoded_content("charset", "none") // ""
$response->decoded_content("charset", "none")
Truth table50%1/2
A
A= $response->decoded_content("charset", "none")
Aresult
11
00
53 293 ▶ my $content = $response->decoded_content // '';
Condition50%1/2
$response->decoded_content // ""
l!l
2930
MC/DC0%0/1
$response->decoded_content // ""
$response->decoded_content
Truth table50%1/2
A
A= $response->decoded_content
Aresult
11
00
54 293 ▶ my $data = length $bytes ? eval { $self->json_codec->decode($bytes) } : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $bytes ? : 266 27 293
55 # authentik answers 302 at the authorize endpoint and between the stages of
56 # a flow; that is an answer, not a failure
57 293 ▶ return { status => $response->code, data => $data, location => scalar $response->header('Location'), content => $content }
Branch: 1 branch
BranchTrueFalseTotal
if $response->code < 400 246 47 293
58 if $response->code < 400;
59 47 my ( $message, $oauth, $request_id, %fields );
60 47 my $body = $content;
61 47 ▶ $body = substr( $body, 0, 500 ).'...' if length $body > 500;
Branch: 1 branch
BranchTrueFalseTotal
if length $body > 500 1 46 47
62 47 ▶ if ( ref $data eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH") 38 9 47
63 38 $request_id = $data->{request_id};
64 38 ▶ if ( defined $data->{detail} && !ref $data->{detail} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $data->{'detail'} and not ref $data->{'detail'}) { } 16 22 38
elsif (defined $data->{'error'} and not ref $data->{'error'}) { } 9 13 22
Condition66%2/3
defined $data->{"detail"} and not ref $data->{"detail"}
!ll&&!rl&&r
22016
MC/DC50%1/2
defined $data->{"detail"} and not ref $data->{"detail"}
defined $data->{"detail"} not ref $data->{"detail"}
Truth table66%2/3
A and not(B)
A= defined $data->{"detail"}B= not ref $data->{"detail"}
ABresult
0X0
100
111
65 16 $message = $data->{detail};
66 }
67 ▶ elsif ( defined $data->{error} && !ref $data->{error} ) {
Condition66%2/3
defined $data->{"error"} and not ref $data->{"error"}
!ll&&!rl&&r
1309
MC/DC50%1/2
defined $data->{"error"} and not ref $data->{"error"}
defined $data->{"error"} not ref $data->{"error"}
Truth table66%2/3
A and not(B)
A= defined $data->{"error"}B= not ref $data->{"error"}
ABresult
0X0
100
111
68 9 $oauth = $data->{error};
69 9 ▶ $message = $data->{error}.( defined $data->{error_description} ? ': '.$data->{error_description} : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $data->{'error_description'} ? : 9 0 9
70 }
71 else {
72 13 %fields = %{ $self->flatten_field_errors($data) };
73 13 $message = join '; ', map { $_.': '.join ', ', @{ $fields{$_} } } sort keys %fields;
74 }
75 }
76 47 ▶ if ( ( !defined $message || !length $message ) && ( my $challenge = $response->header('WWW-Authenticate') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $message && length $message and my $challenge = $response->header("WWW-Authenticate")) 3 44 47
Condition83%5/6
not defined $message && length $message and my $challenge = $response->header("WWW-Authenticate")
!ll&&!rl&&r
3863
defined $message && length $message
!ll&&!rl&&r
9038
MC/DC66%2/3
not defined $message && length $message and my $challenge = $response->header("WWW-Authenticate")
defined $message length $message my $challenge = $response->header("WWW-Authenticate")
Truth table60%3/5
not(A && B) and C
A= defined $messageB= length $messageC= my $challenge = $response->header("WWW-Authenticate")
ABCresult
11X0
0X00
1000
0X11
1011
77 3 ( $oauth ) = $challenge =~ /error="([^"]+)"/;
78 3 ( $message ) = $challenge =~ /error_description="([^"]+)"/;
79 3 ▶ $message = $oauth if !defined $message && defined $oauth;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $message and defined $oauth 0 3 3
Condition33%1/3
not defined $message and defined $oauth
!ll&&!rl&&r
300
MC/DC0%0/2
not defined $message and defined $oauth
not defined $message defined $oauth
Truth table33%1/3
not(A) and B
A= not defined $messageB= defined $oauth
ABresult
0X0
100
111
80 }
81 # Not every error on the way to authentik comes from authentik: a proxy, or
82 # a base_url that points somewhere else, answers HTML or plain text. Saying
83 # only "404 Not Found" would leave the reader with nothing to go on, so a
84 # squeezed snippet of whatever came back stands in.
85 47 ▶ if ( ( !defined $message || !length $message ) && length $body ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $message && length $message and length $body) 4 43 47
Condition83%5/6
not defined $message && length $message and length $body
!ll&&!rl&&r
4124
defined $message && length $message
!ll&&!rl&&r
6041
MC/DC66%2/3
not defined $message && length $message and length $body
defined $message length $message length $body
Truth table60%3/5
not(A && B) and C
A= defined $messageB= length $messageC= length $body
ABCresult
11X0
0X00
1000
0X11
1011
86 4 my $snippet = $body =~ s/\s+/ /gr =~ s/\A\s+|\s+\z//gr;
87 4 ▶ $snippet = substr( $snippet, 0, 200 ).'...' if length $snippet > 200;
Branch: 1 branch
BranchTrueFalseTotal
if length $snippet > 200 1 3 4
88 4 ▶ $message = $snippet if length $snippet;
Branch: 1 branch
BranchTrueFalseTotal
if length $snippet 4 0 4
89 }
90 $self->api_error_class->throw(
91 47 ▶ message => $method.' '.$url.' failed: '.$response->status_line
Branch: 3 branches
BranchTrueFalseTotal
defined $message && length $message ? : 45 2 47
defined $message && length $message ? : 45 2 47
length $body ? : 42 5 47
Condition66%4/6
defined $message and length $message
!ll&&!rl&&r
2045
defined $message and length $message
!ll&&!rl&&r
2045
MC/DC50%1/2
defined $message and length $message
defined $message length $message
MC/DC50%1/2
defined $message and length $message
defined $message length $message
Truth table66%2/3
A and B
A= defined $messageB= length $message
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= defined $messageB= length $message
ABresult
0X0
100
111
92 .( defined $message && length $message ? ' - '.$message : '' ),
93 http_status => $response->code,
94 api_message => ( defined $message && length $message ? $message : undef ),
95 field_errors => \%fields,
96 oauth_error => $oauth,
97 request_id => $request_id,
98 body => ( length $body ? $body : undef )
99 );
100 }
101  
102  
103 15 ▶sub flatten_field_errors {
Subroutine flatten_field_errors: called CC=7 SCAR=20.2
Pod documented
104 15 my ( $self, $data, $prefix ) = @_;
105 15 my %flat;
106 15 for my $key ( keys %$data ) {
107 20 ▶ my $name = defined $prefix ? $prefix.'.'.$key : $key;
Branch: 1 branch
BranchTrueFalseTotal
defined $prefix ? : 2 18 20
108 20 my $value = $data->{$key};
109 20 ▶ if ( ref $value eq 'HASH' ) { %flat = ( %flat, %{ $self->flatten_field_errors( $value, $name ) } ) }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'HASH') { } 2 18 20
elsif (ref $value eq 'ARRAY') { } 18 0 18
110 18 ▶ elsif ( ref $value eq 'ARRAY' ) { $flat{$name} = [ map { ref $_ ? $self->json_codec->encode($_) : $_ } @$value ] }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 18 18
111 0 ▶ else { $flat{$name} = [ defined $value ? $value : '' ] }
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
112 }
113 15 return \%flat;
114 }
115  
116  
117 288 ▶sub send_request {
Subroutine send_request: called CC=4 SCAR=14.1
Pod documented
118 288 my ( $self, $method, $url, %arg ) = @_;
119 288 my $response = $self->ua->request( $self->build_request( $method, $url, %arg ) );
120 288 ▶ $self->network_error_class->throw( message => $method.' '.$url.': '.$response->status_line )
Branch: 1 branch
BranchTrueFalseTotal
if $response->code == 500 and ($response->header("Client-Warning") // "") eq "Internal response" 1 287 288
Condition60%3/5
$response->code == 500 and ($response->header("Client-Warning") // "") eq "Internal response"
!ll&&!rl&&r
28701
$response->header("Client-Warning") // ""
l!l
10
MC/DC50%1/2
$response->code == 500 and ($response->header("Client-Warning") // "") eq "Internal response"
$response->code == 500 $response->header("Client-Warning")
Truth table66%2/3
A and B
A= $response->code == 500B= $response->header("Client-Warning")
ABresult
0X0
100
111
121 if $response->code == 500 && ( $response->header('Client-Warning') // '' ) eq 'Internal response';
122 287 return $self->read_response( $response, $method, $url, %arg );
123 }
124  
125  
126 7 1;
127  
128 __END__
↑ Summary Authentik/Diff.pm »