| 1 | package HTTP::API::Core::Auth; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 10 | our @EXPORT_OK = qw(bearer_auth basic_auth api_key_auth); | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 12 | ▶ | sub bearer_auth { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bearer_auth: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 12 | my ($token) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 12 | 12 | ▶ | die "bearer token must be a non-empty scalar\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $token or ref $token ne "" or $token eq ""
not defined $token or ref $token ne ""
MC/DC100%3/3
not defined $token or ref $token ne "" or $token eq ""
not defined $token
ref $token ne ""
$token eq ""
Truth table100%4/4
not(A) or B or C
A= not defined $tokenB= ref $token ne ""C= $token eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | if !defined($token) || ref($token) ne '' || $token eq ''; | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 9 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 9 | my ($ctx) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 9 | _set_header_if_absent($ctx->{headers}, 'Authorization', "Bearer $token"); | |||||||||||||||||||||||||||||||||||||||||
| 18 | 7 | }; | |||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 10 | ▶ | sub basic_auth { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
basic_auth: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 10 | my ($username, $password) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 10 | ▶ | die "basic auth username is required\n" if !defined($username) || ref($username) ne ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $username or ref $username ne ""
MC/DC100%2/2
not defined $username or ref $username ne ""
not defined $username
ref $username ne ""
Truth table100%3/3
not(A) or B
A= not defined $usernameB= ref $username ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 8 | ▶ | die "basic auth password is required\n" if !defined($password) || ref($password) ne ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $password or ref $password ne ""
MC/DC100%2/2
not defined $password or ref $password ne ""
not defined $password
ref $password ne ""
Truth table100%3/3
not(A) or B
A= not defined $passwordB= ref $password ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 5 | my $credentials = encode_base64("$username:$password", ''); | |||||||||||||||||||||||||||||||||||||||||
| 27 | 5 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 5 | my ($ctx) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 5 | _set_header_if_absent($ctx->{headers}, 'Authorization', "Basic $credentials"); | |||||||||||||||||||||||||||||||||||||||||
| 30 | 5 | }; | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 44 | ▶ | sub api_key_auth { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
api_key_auth: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 44 | ▶ | die "api_key auth options must be key/value pairs\n" if @_ % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | die "api_key auth option names must be scalars\n" | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 44 | ▶ | if grep { ref($_) ne '' } @_[ grep { $_ % 2 == 0 } 0 .. $#_ ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 44 | my (%args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 44 | ▶ | my $in = delete($args{in}) // 'header'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $args{"in"} // "header"
MC/DC100%1/1
delete $args{"in"} // "header"
delete $args{"in"}
Truth table100%2/2
A
A= delete $args{"in"}
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 44 | my $name = delete $args{name}; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 44 | my $value = delete $args{value}; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 44 | die "unknown api_key auth option: $_\n" for sort keys %args; | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 43 | ▶ | die "api_key in must be header or query\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
ref $in ne "" or $in ne "header" and $in ne "query"
$in ne "header" and $in ne "query"
MC/DC100%3/3
ref $in ne "" or $in ne "header" and $in ne "query"
ref $in ne ""
$in ne "header"
$in ne "query"
Truth table100%4/4
A or B and C
A= ref $in ne ""B= $in ne "header"C= $in ne "query"
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | if ref($in) ne '' || ($in ne 'header' && $in ne 'query'); | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 35 | ▶ | die "api_key name must be a non-empty scalar\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $name or ref $name ne "" or $name eq ""
not defined $name or ref $name ne ""
MC/DC100%3/3
not defined $name or ref $name ne "" or $name eq ""
not defined $name
ref $name ne ""
$name eq ""
Truth table100%4/4
not(A) or B or C
A= not defined $nameB= ref $name ne ""C= $name eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | if !defined($name) || ref($name) ne '' || $name eq ''; | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 31 | ▶ | die "api_key value must be a scalar\n" if !defined($value) || ref($value) ne ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $value or ref $value ne ""
MC/DC100%2/2
not defined $value or ref $value ne ""
not defined $value
ref $value ne ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= ref $value ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 29 | ▶ | if ($in eq 'header') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 7 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 7 | my ($ctx) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 7 | _set_header_if_absent($ctx->{headers}, $name, "$value"); | |||||||||||||||||||||||||||||||||||||||||
| 53 | 7 | }; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 23 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 23 | my ($ctx) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 23 | my $url = $ctx->{url}; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 23 | my $fragment = ''; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 23 | ▶ | if ($url =~ s/(#.*)\z//) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 11 | $fragment = $1; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 23 | my $encoded_name = _uri_escape($name); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 23 | ▶ | return if _query_has_key($url, $encoded_name); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 11 | ▶ | my $separator = index($url, '?') >= 0 ? '&' : '?'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 11 | $ctx->{url} = $url . $separator . $encoded_name . '=' . _uri_escape($value) . $fragment; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 22 | }; | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 21 | ▶ | sub _set_header_if_absent { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_header_if_absent: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 21 | my ($headers, $name, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 21 | my $wanted = lc $name; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 21 | ▶ | return if grep { lc($_) eq $wanted } keys %$headers; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 18 | $headers->{$name} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | # Compare parameter names as percent-decoded bytes. A key without '=' is | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # still present; the URL's original encoding and fragment are left untouched. | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 23 | ▶ | sub _query_has_key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_query_has_key: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 23 | my ($url, $encoded_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 23 | my $question = index($url, '?'); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 23 | ▶ | return 0 if $question < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 20 | my $wanted = $encoded_name; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 20 | $wanted =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge; | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 20 | my $query = substr($url, $question + 1); | |||||||||||||||||||||||||||||||||||||||||
| 88 | 20 | for my $pair (split /&/, $query) { | |||||||||||||||||||||||||||||||||||||||||
| 89 | 21 | my ($key) = split /=/, $pair, 2; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 21 | $key =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 21 | ▶ | return 1 if $key eq $wanted; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 8 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 34 | ▶ | sub _uri_escape { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_uri_escape: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 34 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 34 | my $bytes = "$value"; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 34 | ▶ | utf8::encode($bytes) if utf8::is_utf8($bytes); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 34 | $bytes =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord($1))/ge; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 34 | return $bytes; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | __END__ | ||||||||||||||||||||||||||||||||||||||||||