| 1 | package Plack::Middleware::Auth::Basic; | ||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use parent qw(Plack::Middleware); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Plack::Util::Accessor qw( realm authenticator ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | sub prepare_app { | ||||||||||||||||||||||||||||||
|
Subroutine
prepare_app: called CC=5 SCAR=21.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 9 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | my $auth = $self->authenticator or die 'authenticator is not set'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | if (Scalar::Util::blessed($auth) && $auth->can('authenticate')) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
builtin::blessed($auth) and $auth->can("authenticate")
MC/DC0%0/2
builtin::blessed($auth) and $auth->can("authenticate")
builtin::blessed($auth)
$auth->can("authenticate")
Truth table33%1/3
A and B
A= builtin::blessed($auth)B= $auth->can("authenticate")
| |||||||||||||||||||||||||||||||||
| 13 | 0 | ▶ | $self->authenticator(sub { $auth->authenticate(@_[0,1]) }); # because Authen::Simple barfs on 3 params | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 14 | } elsif (ref $auth ne 'CODE') { | ||||||||||||||||||||||||||||||||
| 15 | 0 | die 'authenticator should be a code reference or an object that responds to authenticate()'; | |||||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 10 | ▶ | sub call { | ||||||||||||||||||||||||||||||
|
Subroutine
call: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 20 | 10 | my($self, $env) = @_; | |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | my $auth = $env->{HTTP_AUTHORIZATION} | ||||||||||||||||||||||||||||||||
| 23 | 10 | ▶ | or return $self->unauthorized; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | # note the 'i' on the regex, as, according to RFC2617 this is a | ||||||||||||||||||||||||||||||||
| 26 | # "case-insensitive token to identify the authentication scheme" | ||||||||||||||||||||||||||||||||
| 27 | 8 | ▶ | if ($auth =~ /^Basic (.*)$/i) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 28 | 8 | ▶ | my($user, $pass) = split /:/, (MIME::Base64::decode($1) || ":"), 2; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
&MIME::Base64::decode($1) || ':'
MC/DC0%0/1
&MIME::Base64::decode($1) || ':'
&MIME::Base64::decode($1)
Truth table50%1/2
A
A= &MIME::Base64::decode($1)
| |||||||||||||||||||||||||||||||||
| 29 | 8 | ▶ | $pass = '' unless defined $pass; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 30 | 8 | ▶ | if ($self->authenticator->($user, $pass, $env)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | 6 | $env->{REMOTE_USER} = $user; | |||||||||||||||||||||||||||||||
| 32 | 6 | return $self->app->($env); | |||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 2 | return $self->unauthorized; | |||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 4 | ▶ | sub unauthorized { | ||||||||||||||||||||||||||||||
|
Subroutine
unauthorized: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 40 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||
| 41 | 4 | my $body = 'Authorization required'; | |||||||||||||||||||||||||||||||
| 42 | return [ | ||||||||||||||||||||||||||||||||
| 43 | 4 | ▶ | 401, | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->realm || 'restricted area'
MC/DC0%0/1
$self->realm || 'restricted area'
$self->realm
Truth table50%1/2
A
A= $self->realm
| |||||||||||||||||||||||||||||||||
| 44 | [ 'Content-Type' => 'text/plain', | ||||||||||||||||||||||||||||||||
| 45 | 'Content-Length' => length $body, | ||||||||||||||||||||||||||||||||
| 46 | 'WWW-Authenticate' => 'Basic realm="' . ($self->realm || "restricted area") . '"' ], | ||||||||||||||||||||||||||||||||
| 47 | [ $body ], | ||||||||||||||||||||||||||||||||
| 48 | ]; | ||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 4 | 1; | |||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | __END__ | ||||||||||||||||||||||||||||||||