| 1 | package Uniform::HTTP::Auth::Basic; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use Encode qw(encode); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use Unicode::Normalize qw(NFC); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 11 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 16 | ▶ | sub validate_challenge { | ||||||||||||||||||||||||||
|
Subroutine
validate_challenge: called CC=5 SCAR=17.0
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 16 | my ($class, $challenge) = @_; | |||||||||||||||||||||||||||
| 14 | return 'Basic challenge must use authentication parameters' | ||||||||||||||||||||||||||||
| 15 | 16 | ▶ | if defined $challenge->{token68}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | return 'Basic challenge requires realm' | ||||||||||||||||||||||||||||
| 17 | 16 | ▶ | unless exists $challenge->{params}{realm}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 15 | ▶ | if (exists $challenge->{params}{charset} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $challenge->{"params"}{"charset"} and lc $challenge->{"params"}{"charset"} ne "utf-8"
MC/DC0%0/2
exists $challenge->{"params"}{"charset"} and lc $challenge->{"params"}{"charset"} ne "utf-8"
exists $challenge->{"params"}{"charset"}
lc $challenge->{"params"}{"charset"} ne "utf-8"
Truth table66%2/3
A and B
A= exists $challenge->{"params"}{"charset"}B= lc $challenge->{"params"}{"charset"} ne "utf-8"
| |||||||||||||||||||||||||||||
| 20 | && lc($challenge->{params}{charset}) ne 'utf-8') { | ||||||||||||||||||||||||||||
| 21 | 0 | return 'Basic charset must be UTF-8'; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 15 | return; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 3 | ▶ | sub select_challenge { | ||||||||||||||||||||||||||
|
Subroutine
select_challenge: called CC=4 SCAR=14.9
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 3 | my ($class, $challenges) = @_; | |||||||||||||||||||||||||||
| 29 | 3 | for my $challenge (@$challenges) { | |||||||||||||||||||||||||||
| 30 | 3 | ▶ | next if $challenge->{malformed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 3 | ▶ | next if defined $class->validate_challenge($challenge); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 3 | return $challenge; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 0 | return; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 6 | ▶ | sub authorization { | ||||||||||||||||||||||||||
|
Subroutine
authorization: called CC=14 SCAR=27.7
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 6 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 6 | for my $field (qw(username password challenge)) { | |||||||||||||||||||||||||||
| 41 | croak "Basic authorization requires '$field'" | ||||||||||||||||||||||||||||
| 42 | 18 | ▶ | unless exists $args{$field} && defined $args{$field}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args{$field} and defined $args{$field}
MC/DC0%0/2
exists $args{$field} and defined $args{$field}
exists $args{$field}
defined $args{$field}
Truth table33%1/3
A and B
A= exists $args{$field}B= defined $args{$field}
| |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 6 | ▶ | croak "username must be a plain scalar" if ref $args{username}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 6 | ▶ | croak "password must be a plain scalar" if ref $args{password}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | croak "challenge must be a hash reference" | ||||||||||||||||||||||||||||
| 47 | 6 | ▶ | unless ref($args{challenge}) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 6 | my $error = $class->validate_challenge($args{challenge}); | |||||||||||||||||||||||||||
| 50 | 6 | ▶ | croak $error if defined $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | croak "Basic username must not contain ':'" | ||||||||||||||||||||||||||||
| 53 | 6 | ▶ | if $args{username} =~ /:/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | croak "Basic username contains an HTTP control character" | ||||||||||||||||||||||||||||
| 55 | 5 | ▶ | if $args{username} =~ /[\x00-\x1f\x7f]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | croak "Basic password contains an HTTP control character" | ||||||||||||||||||||||||||||
| 57 | 5 | ▶ | if $args{password} =~ /[\x00-\x1f\x7f]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 5 | my ($username, $password) = @args{qw(username password)}; | |||||||||||||||||||||||||||
| 60 | 5 | my $charset = $args{challenge}{params}{charset}; | |||||||||||||||||||||||||||
| 61 | 5 | my $bytes; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 5 | ▶ | if (defined $charset) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 1 | $username = NFC($username); | |||||||||||||||||||||||||||
| 65 | 1 | $password = NFC($password); | |||||||||||||||||||||||||||
| 66 | 1 | $bytes = encode('UTF-8', $username . ':' . $password, Encode::FB_CROAK()); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | else { | ||||||||||||||||||||||||||||
| 69 | 4 | ▶ | croak 'non-ASCII Basic credentials require a charset="UTF-8" challenge' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$username =~ /[^\x00-\x7f]/ or $password =~ /[^\x00-\x7f]/
MC/DC50%1/2
$username =~ /[^\x00-\x7f]/ or $password =~ /[^\x00-\x7f]/
$username =~ /[^\x00-\x7f]/
$password =~ /[^\x00-\x7f]/
Truth table66%2/3
A or B
A= $username =~ /[^\x00-\x7f]/B= $password =~ /[^\x00-\x7f]/
| |||||||||||||||||||||||||||||
| 70 | if $username =~ /[^\x00-\x7f]/ || $password =~ /[^\x00-\x7f]/; | ||||||||||||||||||||||||||||
| 71 | 3 | $bytes = $username . ':' . $password; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 4 | return 'Basic ' . encode_base64($bytes, ''); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 11 | 1; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | __END__ | ||||||||||||||||||||||||||||