| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 288 | ▶ | $request->header( Authorization => 'Basic '.encode_base64( $arg{basic}[0].':'.$arg{basic}[1], '' ) ) if $arg{basic}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 288 | ▶ | if ( exists $arg{json} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 // ""
MC/DC0%0/1
$uri->query // ""
$uri->query
Truth table50%1/2
A
A= $uri->query
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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") // ""
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 293 | ▶ | my $content = $response->decoded_content // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$response->decoded_content // ""
MC/DC0%0/1
$response->decoded_content // ""
$response->decoded_content
Truth table50%1/2
A
A= $response->decoded_content
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 293 | ▶ | my $data = length $bytes ? eval { $self->json_codec->decode($bytes) } : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 47 | ▶ | if ( ref $data eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 38 | $request_id = $data->{request_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 38 | ▶ | if ( defined $data->{detail} && !ref $data->{detail} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $data->{"detail"} and not ref $data->{"detail"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 9 | $oauth = $data->{error}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 9 | ▶ | $message = $data->{error}.( defined $data->{error_description} ? ': '.$data->{error_description} : '' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
not defined $message && length $message and my $challenge = $response->header("WWW-Authenticate")
defined $message && length $message
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | ( $oauth ) = $challenge =~ /error="([^"]+)"/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | ( $message ) = $challenge =~ /error_description="([^"]+)"/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 3 | ▶ | $message = $oauth if !defined $message && defined $oauth; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $message and defined $oauth
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
not defined $message && length $message and length $body
defined $message && length $message
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 4 | ▶ | $message = $snippet if length $snippet; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | $self->api_error_class->throw( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 47 | ▶ | message => $method.' '.$url.' failed: '.$response->status_line | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%4/6
defined $message and length $message
defined $message and length $message
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
Truth table66%2/3
A and B
A= defined $messageB= length $message
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 20 | my $value = $data->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 20 | ▶ | if ( ref $value eq 'HASH' ) { %flat = ( %flat, %{ $self->flatten_field_errors( $value, $name ) } ) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 18 | ▶ | elsif ( ref $value eq 'ARRAY' ) { $flat{$name} = [ map { ref $_ ? $self->json_codec->encode($_) : $_ } @$value ] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | else { $flat{$name} = [ defined $value ? $value : '' ] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition60%3/5
$response->code == 500 and ($response->header("Client-Warning") // "") eq "Internal response"
$response->header("Client-Warning") // ""
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||