| 1 | package PAGI::Middleware::Auth::Basic; | ||||||||||||||||||||||||||||
| 2 | 2 | $PAGI::Middleware::Auth::Basic::VERSION = '0.002002'; | |||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use parent 'PAGI::Middleware'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use MIME::Base64 qw(decode_base64); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | PAGI::Middleware::Auth::Basic - HTTP Basic Authentication middleware | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use PAGI::Middleware::Builder; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | my $app = builder { | ||||||||||||||||||||||||||||
| 18 | enable 'Auth::Basic', | ||||||||||||||||||||||||||||
| 19 | realm => 'Restricted Area', | ||||||||||||||||||||||||||||
| 20 | authenticator => sub { | ||||||||||||||||||||||||||||
| 21 | my ($username, $password) = @_; | ||||||||||||||||||||||||||||
| 22 | return $username eq 'admin' && $password eq 'secret'; | ||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | $my_app; | ||||||||||||||||||||||||||||
| 25 | }; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # In your app: | ||||||||||||||||||||||||||||
| 28 | async sub app { | ||||||||||||||||||||||||||||
| 29 | my ($scope, $receive, $send) = @_; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | my $auth = $scope->{'pagi.auth'}; | ||||||||||||||||||||||||||||
| 32 | my $username = $auth->{username}; | ||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | PAGI::Middleware::Auth::Basic implements HTTP Basic Authentication (RFC 7617). | ||||||||||||||||||||||||||||
| 38 | It validates credentials and returns 401 Unauthorized for failed authentication. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =over 4 | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =item * authenticator (required) | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Coderef that receives ($username, $password) and returns true for valid credentials. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =item * realm (default: 'Restricted') | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | The authentication realm shown in the WWW-Authenticate header. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =item * paths (optional) | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | Arrayref of path patterns to protect. If not specified, all paths are protected. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =back | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 3 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 61 | 3 | my ($self, $config) = @_; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | $self->{authenticator} = $config->{authenticator} | ||||||||||||||||||||||||||||
| 64 | 3 | ▶ | // die "Auth::Basic requires 'authenticator' option"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"authenticator"} // die("Auth::Basic requires 'authenticator' option")
MC/DC0%0/1
$config->{"authenticator"} // die("Auth::Basic requires 'authenticator' option")
$config->{"authenticator"}
Truth table50%1/2
A
A= $config->{"authenticator"}
| |||||||||||||||||||||||||||||
| 65 | 3 | ▶ | $self->{realm} = $config->{realm} // 'Restricted'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"realm"} // "Restricted"
MC/DC100%1/1
$config->{"realm"} // "Restricted"
$config->{"realm"}
Truth table100%2/2
A
A= $config->{"realm"}
| |||||||||||||||||||||||||||||
| 66 | 3 | $self->{paths} = $config->{paths}; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 3 | ▶ | sub wrap { | ||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.2
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 3 | my ($self, $app) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 3 | ▶ | return async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=20.9
| |||||||||||||||||||||||||||||
| 73 | 3 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||
| 74 | 3 | ▶ | if ($scope->{type} ne 'http') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 76 | 0 | return; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Check if path requires authentication | ||||||||||||||||||||||||||||
| 80 | 3 | ▶ | unless ($self->_requires_auth($scope->{path})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 82 | 0 | return; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Get Authorization header | ||||||||||||||||||||||||||||
| 86 | 3 | my $auth_header = $self->_get_header($scope, 'authorization'); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 3 | ▶ | unless ($auth_header) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 1 | await $self->_send_unauthorized($send); | |||||||||||||||||||||||||||
| 90 | 1 | return; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Parse Basic authentication | ||||||||||||||||||||||||||||
| 94 | 2 | my ($username, $password) = $self->_parse_basic_auth($auth_header); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | ▶ | unless (defined $username) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | await $self->_send_unauthorized($send); | |||||||||||||||||||||||||||
| 98 | 0 | return; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Validate credentials | ||||||||||||||||||||||||||||
| 102 | 2 | my $valid = eval { $self->{authenticator}->($username, $password) }; | |||||||||||||||||||||||||||
| 103 | 2 | ▶ | if ($@ || !$valid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$@ or not $valid
MC/DC50%1/2
$@ or not $valid
$@
not $valid
Truth table66%2/3
A or not(B)
A= $@B= not $valid
| |||||||||||||||||||||||||||||
| 104 | 1 | await $self->_send_unauthorized($send); | |||||||||||||||||||||||||||
| 105 | 1 | return; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # Add auth info to scope | ||||||||||||||||||||||||||||
| 109 | 1 | my $new_scope = { | |||||||||||||||||||||||||||
| 110 | %$scope, | ||||||||||||||||||||||||||||
| 111 | 'pagi.auth' => { | ||||||||||||||||||||||||||||
| 112 | type => 'basic', | ||||||||||||||||||||||||||||
| 113 | username => $username, | ||||||||||||||||||||||||||||
| 114 | }, | ||||||||||||||||||||||||||||
| 115 | }; | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | await $app->($new_scope, $receive, $send); | |||||||||||||||||||||||||||
| 118 | 3 | }; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 3 | ▶ | sub _requires_auth { | ||||||||||||||||||||||||||
|
Subroutine
_requires_auth: called CC=6 SCAR=32.3
| |||||||||||||||||||||||||||||
| 122 | 3 | my ($self, $path) = @_; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 3 | ▶ | return 1 unless $self->{paths}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | for my $pattern (@{$self->{paths}}) { | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | if (ref $pattern eq 'Regexp') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | return 1 if $path =~ $pattern; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | } else { | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | return 1 if index($path, $pattern) == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 0 | return 0; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2 | ▶ | sub _parse_basic_auth { | ||||||||||||||||||||||||||
|
Subroutine
_parse_basic_auth: called CC=5 SCAR=16.7
| |||||||||||||||||||||||||||||
| 137 | 2 | my ($self, $header) = @_; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 2 | ▶ | return unless $header =~ /^Basic\s+(.+)$/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 2 | my $encoded = $1; | |||||||||||||||||||||||||||
| 142 | 2 | my $decoded = eval { decode_base64($encoded) }; | |||||||||||||||||||||||||||
| 143 | 2 | ▶ | return unless $decoded; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 2 | my ($username, $password) = split /:/, $decoded, 2; | |||||||||||||||||||||||||||
| 146 | 2 | ▶ | return unless defined $username; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 2 | ▶ | return ($username, $password // ''); | ||||||||||||||||||||||||||
|
Condition50%1/2
$password // ""
MC/DC0%0/1
$password // ""
$password
Truth table50%1/2
A
A= $password
| |||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 2 | ▶ | async sub _send_unauthorized { | ||||||||||||||||||||||||||
|
Subroutine
_send_unauthorized: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 152 | 2 | my ($self, $send) = @_; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 2 | my $realm_escaped = $self->{realm}; | |||||||||||||||||||||||||||
| 155 | 2 | $realm_escaped =~ s/"/\\"/g; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 2 | my $body = 'Unauthorized'; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 2 | await $send->({ | |||||||||||||||||||||||||||
| 160 | type => 'http.response.start', | ||||||||||||||||||||||||||||
| 161 | status => 401, | ||||||||||||||||||||||||||||
| 162 | headers => [ | ||||||||||||||||||||||||||||
| 163 | ['Content-Type', 'text/plain'], | ||||||||||||||||||||||||||||
| 164 | ['Content-Length', length($body)], | ||||||||||||||||||||||||||||
| 165 | ['WWW-Authenticate', qq{Basic realm="$realm_escaped", charset="UTF-8"}], | ||||||||||||||||||||||||||||
| 166 | ], | ||||||||||||||||||||||||||||
| 167 | }); | ||||||||||||||||||||||||||||
| 168 | 2 | await $send->({ | |||||||||||||||||||||||||||
| 169 | type => 'http.response.body', | ||||||||||||||||||||||||||||
| 170 | body => $body, | ||||||||||||||||||||||||||||
| 171 | more => 0, | ||||||||||||||||||||||||||||
| 172 | }); | ||||||||||||||||||||||||||||
| 173 | 2 | } | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 3 | ▶ | sub _get_header { | ||||||||||||||||||||||||||
|
Subroutine
_get_header: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 176 | 3 | my ($self, $scope, $name) = @_; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 3 | $name = lc($name); | |||||||||||||||||||||||||||
| 179 | 3 | ▶ | for my $h (@{$scope->{headers} // []}) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"headers"} // []
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
| |||||||||||||||||||||||||||||
| 180 | 2 | ▶ | return $h->[1] if lc($h->[0]) eq $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | 1 | return; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 2 | 1; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | __END__ | ||||||||||||||||||||||||||||