| 1 | package Dancer::Plugin::Auth::Basic; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Basic HTTP authentication for Dancer web apps | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 6 | our $VERSION = '0.030'; # VERSION | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Dancer ':syntax'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Dancer::Plugin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use Dancer::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use HTTP::Headers; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 6 | my $settings = plugin_setting; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Protected paths defined in the configuration | ||||||||||||||||||||||||||||
| 19 | 6 | my $paths = {}; | |||||||||||||||||||||||||||
| 20 | # "Global" users | ||||||||||||||||||||||||||||
| 21 | 6 | my $users = {}; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 6 | ▶ | if (exists $settings->{paths}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 5 | $paths = $settings->{paths}; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 6 | ▶ | if (exists $settings->{users}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 1 | $users = $settings->{users}; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 22 | ▶ | sub _check_password { | ||||||||||||||||||||||||||
|
Subroutine
_check_password: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 32 | 22 | my ($password, $text) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 22 | my $crypt; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 22 | ▶ | if (($crypt = $password =~ /^\$\w+\$/) || $password =~ /^\{\w+\}/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$crypt = $password =~ /^\$\w+\$/ or $password =~ /^\{\w+\}/
MC/DC100%2/2
$crypt = $password =~ /^\$\w+\$/ or $password =~ /^\{\w+\}/
$crypt = $password =~ /^\$\w+\$/
$password =~ /^\{\w+\}/
Truth table100%3/3
A or B
A= $crypt = $password =~ /^\$\w+\$/B= $password =~ /^\{\w+\}/
| |||||||||||||||||||||||||||||
| 37 | # Crypt or RFC 2307 format | ||||||||||||||||||||||||||||
| 38 | eval { | ||||||||||||||||||||||||||||
| 39 | 7 | require Authen::Passphrase; | |||||||||||||||||||||||||||
| 40 | 6 | 1; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 7 | ▶ | or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 1 | error "Can't use Authen::Passphrase: " . $@; | |||||||||||||||||||||||||||
| 44 | 1 | return 0; | |||||||||||||||||||||||||||
| 45 | }; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 6 | my $ppr; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | eval { | ||||||||||||||||||||||||||||
| 50 | 6 | ▶ | $ppr = $crypt ? Authen::Passphrase->from_crypt($password) : | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | Authen::Passphrase->from_rfc2307($password); | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 6 | ▶ | or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 2 | error "Can't construct an Authen::Passphrase recognizer object: " . | |||||||||||||||||||||||||||
| 55 | $@; | ||||||||||||||||||||||||||||
| 56 | 2 | return 0; | |||||||||||||||||||||||||||
| 57 | }; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 4 | return $ppr->match($text); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||||||||||
| 62 | # Password in cleartext | ||||||||||||||||||||||||||||
| 63 | 15 | return $password eq $text; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 32 | ▶ | sub _auth_basic { | ||||||||||||||||||||||||||
|
Subroutine
_auth_basic: called CC=15 SCAR=27.3
| |||||||||||||||||||||||||||||
| 68 | 32 | my (%options) = @_; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Get authentication data from request | ||||||||||||||||||||||||||||
| 71 | 32 | my $auth = request->header('Authorization'); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 32 | my $authorized = undef; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 32 | ▶ | if (defined $auth && $auth =~ /^Basic (.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $auth and $auth =~ /^Basic (.*)$/
MC/DC50%1/2
defined $auth and $auth =~ /^Basic (.*)$/
defined $auth
$auth =~ /^Basic (.*)$/
Truth table66%2/3
A and B
A= defined $authB= $auth =~ /^Basic (.*)$/
| |||||||||||||||||||||||||||||
| 76 | 23 | ▶ | my ($user, $password) = split(/:/, (MIME::Base64::decode($1) || ":")); | ||||||||||||||||||||||||||
|
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)
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 23 | ▶ | if (exists $options{user}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | # A single user is defined | ||||||||||||||||||||||||||||
| 80 | $authorized = $user eq $options{user} && | ||||||||||||||||||||||||||||
| 81 | 8 | ▶ | _check_password($options{password}, $password); | ||||||||||||||||||||||||||
|
Condition100%3/3
$user eq $options{'user'} && _check_password($options{'password'}, $password)
MC/DC100%2/2
$user eq $options{'user'} && _check_password($options{'password'}, $password)
$user eq $options{'user'}
_check_password($options{'password'}, $password)
Truth table100%3/3
A && B
A= $user eq $options{'user'}B= _check_password($options{'password'}, $password)
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 23 | ▶ | if (!defined($authorized) && exists($options{users})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $authorized and exists $options{"users"}
MC/DC100%2/2
not defined $authorized and exists $options{"users"}
not defined $authorized
exists $options{"users"}
Truth table100%3/3
not(A) and B
A= not defined $authorizedB= exists $options{"users"}
| |||||||||||||||||||||||||||||
| 85 | # Multiple users are defined | ||||||||||||||||||||||||||||
| 86 | $authorized = exists($options{users}->{$user}) && | ||||||||||||||||||||||||||||
| 87 | 13 | ▶ | _check_password($options{users}->{$user}, $password); | ||||||||||||||||||||||||||
|
Condition100%3/3
exists $options{'users'}{$user} && _check_password($options{'users'}{$user}, $password)
MC/DC100%2/2
exists $options{'users'}{$user} && _check_password($options{'users'}{$user}, $password)
exists $options{'users'}{$user}
_check_password($options{'users'}{$user}, $password)
Truth table100%3/3
A && B
A= exists $options{'users'}{$user}B= _check_password($options{'users'}{$user}, $password)
| |||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 23 | ▶ | if (!$authorized && defined($users)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $authorized and defined $users
MC/DC50%1/2
not $authorized and defined $users
not $authorized
defined $users
Truth table66%2/3
not(A) and B
A= not $authorizedB= defined $users
| |||||||||||||||||||||||||||||
| 91 | # Use the "global" users list | ||||||||||||||||||||||||||||
| 92 | $authorized = exists $users->{$user} && | ||||||||||||||||||||||||||||
| 93 | 9 | ▶ | _check_password($users->{$user}, $password); | ||||||||||||||||||||||||||
|
Condition66%2/3
exists $users->{$user} && _check_password($users->{$user}, $password)
MC/DC50%1/2
exists $users->{$user} && _check_password($users->{$user}, $password)
exists $users->{$user}
_check_password($users->{$user}, $password)
Truth table66%2/3
A && B
A= exists $users->{$user}B= _check_password($users->{$user}, $password)
| |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 23 | ▶ | if ($authorized) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | # Authorization successful | ||||||||||||||||||||||||||||
| 98 | 18 | request->env->{REMOTE_USER} = $user; | |||||||||||||||||||||||||||
| 99 | 18 | return 1; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 5 | ▶ | if (!defined($authorized)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | # No users defined? NONE SHALL PASS! | ||||||||||||||||||||||||||||
| 104 | 0 | warning __PACKAGE__ . ": No user/password defined"; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 14 | my $content = "Authorization required"; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | return halt(Dancer::Response->new( | ||||||||||||||||||||||||||||
| 111 | status => 401, | ||||||||||||||||||||||||||||
| 112 | content => $content, | ||||||||||||||||||||||||||||
| 113 | headers => [ | ||||||||||||||||||||||||||||
| 114 | 'Content-Type' => 'text/plain', | ||||||||||||||||||||||||||||
| 115 | 'Content-Length' => length($content), | ||||||||||||||||||||||||||||
| 116 | 'WWW-Authenticate' => 'Basic realm="' . ($options{realm} || | ||||||||||||||||||||||||||||
| 117 | 14 | ▶ | "Restricted area") . '"' | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{'realm'} || 'Restricted area'
MC/DC100%1/1
$options{'realm'} || 'Restricted area'
$options{'realm'}
Truth table100%2/2
A
A= $options{'realm'}
| |||||||||||||||||||||||||||||
| 118 | ] | ||||||||||||||||||||||||||||
| 119 | )); | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 35 | ▶ | my $check = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 123 | # Check if the request matches one of the protected paths (reverse sort the | ||||||||||||||||||||||||||||
| 124 | # paths to find the longest matching path first) | ||||||||||||||||||||||||||||
| 125 | 35 | foreach my $path (reverse sort keys %$paths) { | |||||||||||||||||||||||||||
| 126 | 47 | my $path_re = '^' . quotemeta($path); | |||||||||||||||||||||||||||
| 127 | 47 | ▶ | if (request->path_info =~ qr{$path_re}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 27 | _auth_basic %{$paths->{$path}}; | |||||||||||||||||||||||||||
| 129 | 15 | last; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 6 | }; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # Dynamic paths | ||||||||||||||||||||||||||||
| 135 | 6 | hook before => $check; | |||||||||||||||||||||||||||
| 136 | # Static paths | ||||||||||||||||||||||||||||
| 137 | 6 | hook before_file_render => $check; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 6 | register auth_basic => \&_auth_basic; | |||||||||||||||||||||||||||
| 140 | 6 | register_plugin; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 6 | 1; # End of Dancer::Plugin::Auth::Basic | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | __END__ | ||||||||||||||||||||||||||||