| 1 | package Plack::Middleware::Auth::Complex; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.014000; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = '0.003001'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use parent qw/Plack::Middleware/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use re '/s'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Authen::Passphrase; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Authen::Passphrase::BlowfishCrypt; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Data::Entropy qw/entropy_source/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Data::Entropy::Source; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Data::Entropy::RawSource::Local; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Carp qw/carp croak/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use Digest::SHA qw/hmac_sha1_base64 sha256/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use Email::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use Email::Sender::Simple qw/sendmail/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use MIME::Base64 qw/decode_base64/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use Plack::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use Tie::Hash::Expire; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | sub make_entropy_source { | ||||||||||||||||||||||||||
|
Subroutine
make_entropy_source: called CC=2 SCAR=9.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | if (-e '/dev/urandom') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 2 | Data::Entropy::Source->new( | |||||||||||||||||||||||||||
| 29 | Data::Entropy::RawSource::Local->new('/dev/urandom'), | ||||||||||||||||||||||||||||
| 30 | 'sysread' | ||||||||||||||||||||||||||||
| 31 | ) | ||||||||||||||||||||||||||||
| 32 | } else { | ||||||||||||||||||||||||||||
| 33 | 0 | carp "/dev/urandom not found, using insecure random source\n"; | |||||||||||||||||||||||||||
| 34 | 0 | entropy_source | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | sub default_opts {( | ||||||||||||||||||||||||||
|
Subroutine
default_opts: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 2 | dbi_connect => ['dbi:Pg:', '', ''], | |||||||||||||||||||||||||||
| 40 | select_user => 'SELECT passphrase, email FROM users WHERE id = ?', | ||||||||||||||||||||||||||||
| 41 | update_pass => 'UPDATE users SET passphrase = ? WHERE id = ?', | ||||||||||||||||||||||||||||
| 42 | insert_user => 'INSERT INTO users (id, passphrase, email) VALUES (?,?,?)', | ||||||||||||||||||||||||||||
| 43 | mail_subject => 'Password reset token', | ||||||||||||||||||||||||||||
| 44 | realm => 'restricted area', | ||||||||||||||||||||||||||||
| 45 | cache_fail => 0, | ||||||||||||||||||||||||||||
| 46 | cache_max_age => 5 * 60, | ||||||||||||||||||||||||||||
| 47 | token_max_age => 60 * 60, | ||||||||||||||||||||||||||||
| 48 | username_regex => qr/^\w{2,20}$/as, | ||||||||||||||||||||||||||||
| 49 | invalid_username => 'Invalid username', | ||||||||||||||||||||||||||||
| 50 | register_url => '/action/register', | ||||||||||||||||||||||||||||
| 51 | passwd_url => '/action/passwd', | ||||||||||||||||||||||||||||
| 52 | request_reset_url => '/action/request-reset', | ||||||||||||||||||||||||||||
| 53 | reset_url => '/action/reset' | ||||||||||||||||||||||||||||
| 54 | )} | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.9
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 2 | my ($class, $opts) = @_; | |||||||||||||||||||||||||||
| 58 | 2 | my %self = $class->default_opts; | |||||||||||||||||||||||||||
| 59 | 2 | %self = (%self, %$opts); | |||||||||||||||||||||||||||
| 60 | 2 | ▶ | $self{entropy_source} //= make_entropy_source; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self{"entropy_source"} //= make_entropy_source()
MC/DC0%0/2
$self{"entropy_source"} //= make_entropy_source()
$self{"entropy_source"}
make_entropy_source()
Truth table33%1/3
A //= B
A= $self{"entropy_source"}B= make_entropy_source()
| |||||||||||||||||||||||||||||
| 61 | # If the user did not set [use_scrypt], we set it to 1 if scrypt | ||||||||||||||||||||||||||||
| 62 | # is available and to 0 otherwise. | ||||||||||||||||||||||||||||
| 63 | # If the user set [use_scrypt] to 1, we try to load scrypt and | ||||||||||||||||||||||||||||
| 64 | # croak if we fail to do so. | ||||||||||||||||||||||||||||
| 65 | 2 | ▶ | unless (exists $self{use_scrypt}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | my $success = eval 'use Authen::Passphrase::Scrypt'; | |||||||||||||||||||||||||||
| 67 | 0 | $self{use_scrypt} = !!$success | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 2 | ▶ | if ($self{use_scrypt}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | eval 'use Authen::Passphrase::Scrypt; 1' or croak "Failed to load Authen::Passphrase::Scrypt: $@\n"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 2 | my $self = bless \%self, $class; | |||||||||||||||||||||||||||
| 73 | 2 | $self | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 2 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=6 SCAR=18.7
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 78 | 2 | ▶ | $self->{dbh} = DBI->connect(@{$self->{dbi_connect}}) or croak $DBI::errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | $self->{post_connect_cb}->($self) if $self->{post_connect_cb}; # uncoverable branch false | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 2 | ▶ | $self->{insert_sth} = $self->{dbh}->prepare($self->{insert_user}) or croak $self->{dbh}->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 2 | ▶ | $self->{select_sth} = $self->{dbh}->prepare($self->{select_user}) or croak $self->{dbh}->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 2 | ▶ | $self->{update_sth} = $self->{dbh}->prepare($self->{update_pass}) or croak $self->{dbh}->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 2 | ▶ | sub create_user { | ||||||||||||||||||||||||||
|
Subroutine
create_user: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 2 | my ($self, $parms) = @_; | |||||||||||||||||||||||||||
| 87 | 2 | my %parms = $parms->flatten; | |||||||||||||||||||||||||||
| 88 | 2 | ▶ | $self->{insert_sth}->execute($parms{username}, $self->hash_passphrase($parms{password}), $parms{email}) or croak $self->{insert_sth}->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 60 | ▶ | sub get_user { | ||||||||||||||||||||||||||
|
Subroutine
get_user: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 60 | my ($self, $user) = @_; | |||||||||||||||||||||||||||
| 93 | 60 | ▶ | $self->{select_sth}->execute($user) or croak $self->{select_sth}->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | $self->{select_sth}->fetchrow_hashref | ||||||||||||||||||||||||||||
| 95 | 60 | } | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 26 | ▶ | sub check_passphrase { | ||||||||||||||||||||||||||
|
Subroutine
check_passphrase: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 26 | my ($self, $username, $passphrase) = @_; | |||||||||||||||||||||||||||
| 99 | 26 | ▶ | unless ($self->{cache}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | ## no critic (ProhibitTies) | ||||||||||||||||||||||||||||
| 101 | 2 | tie my %cache, 'Tie::Hash::Expire', {expire_seconds => $self->{cache_max_age}}; | |||||||||||||||||||||||||||
| 102 | 2 | $self->{cache} = \%cache; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 26 | my $cachekey = sha256 "$username:$passphrase"; | |||||||||||||||||||||||||||
| 105 | 26 | ▶ | return $self->{cache}{$cachekey} if exists $self->{cache}{$cachekey}; # uncoverable branch true | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 26 | my $user = $self->get_user($username); | |||||||||||||||||||||||||||
| 107 | 26 | ▶ | return 0 unless $user; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 24 | my $ret; | |||||||||||||||||||||||||||
| 109 | 24 | ▶ | if ($user->{passphrase} =~ /^{SCRYPT}/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 12 | ▶ | croak "$username has a scrypt password but use_scrypt is false\n" unless $self->{use_scrypt}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | $ret = Authen::Passphrase::Scrypt->from_rfc2307($user->{passphrase}) | ||||||||||||||||||||||||||||
| 112 | 12 | } else { | |||||||||||||||||||||||||||
| 113 | 12 | $ret = Authen::Passphrase->from_rfc2307($user->{passphrase}); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 24 | $ret = $ret->match($passphrase); | |||||||||||||||||||||||||||
| 116 | 24 | ▶ | $self->{cache}{$cachekey} = $ret if $ret || $self->{cache_fail}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ret or $self->{"cache_fail"}
MC/DC50%1/2
$ret or $self->{"cache_fail"}
$ret
$self->{"cache_fail"}
Truth table66%2/3
A or B
A= $retB= $self->{"cache_fail"}
| |||||||||||||||||||||||||||||
| 117 | 24 | $ret | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 6 | ▶ | sub hash_passphrase { | ||||||||||||||||||||||||||
|
Subroutine
hash_passphrase: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 6 | my ($self, $passphrase) = @_; | |||||||||||||||||||||||||||
| 122 | 6 | ▶ | if ($self->{use_scrypt}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 3 | Authen::Passphrase::Scrypt->new({ | |||||||||||||||||||||||||||
| 124 | passphrase => $passphrase, | ||||||||||||||||||||||||||||
| 125 | })->as_rfc2307 | ||||||||||||||||||||||||||||
| 126 | } else { | ||||||||||||||||||||||||||||
| 127 | 3 | Authen::Passphrase::BlowfishCrypt->new( | |||||||||||||||||||||||||||
| 128 | cost => 10, | ||||||||||||||||||||||||||||
| 129 | passphrase => $passphrase, | ||||||||||||||||||||||||||||
| 130 | salt_random => 1, | ||||||||||||||||||||||||||||
| 131 | )->as_rfc2307 | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 4 | ▶ | sub set_passphrase { | ||||||||||||||||||||||||||
|
Subroutine
set_passphrase: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 136 | 4 | my ($self, $username, $passphrase) = @_; | |||||||||||||||||||||||||||
| 137 | 4 | ▶ | $self->{update_sth}->execute($self->hash_passphrase($passphrase), $username) or croak $self->{update_sth}->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 10 | ▶ | sub make_reset_hmac { | ||||||||||||||||||||||||||
|
Subroutine
make_reset_hmac: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 10 | my ($self, $username, @data) = @_; | |||||||||||||||||||||||||||
| 142 | 10 | ▶ | $self->{hmackey} //= $self->{entropy_source}->get_bits(8 * 512); # uncoverable condition false | ||||||||||||||||||||||||||
|
Condition100%2/3
$self->{"hmackey"} //= $self->{"entropy_source"}->get_bits(4096)
MC/DC100%0/2
$self->{"hmackey"} //= $self->{"entropy_source"}->get_bits(4096)
$self->{"hmackey"}
$self->{"entropy_source"}->get_bits(4096)
Truth table100%2/3
A //= B
A= $self->{"hmackey"}B= $self->{"entropy_source"}->get_bits(4096)
| |||||||||||||||||||||||||||||
| 143 | 10 | my $user = $self->get_user($username); | |||||||||||||||||||||||||||
| 144 | 10 | my $message = join ' ', $username, $user->{passphrase}, @data; | |||||||||||||||||||||||||||
| 145 | 10 | hmac_sha1_base64 $message, $self->{hmackey}; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 2 | ▶ | sub mail_body { | ||||||||||||||||||||||||||
|
Subroutine
mail_body: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 2 | my ($self, $username, $token) = @_; | |||||||||||||||||||||||||||
| 150 | 2 | my $hours = $self->{token_max_age} / 60 / 60; | |||||||||||||||||||||||||||
| 151 | 2 | ▶ | $hours .= $hours == 1 ? ' hour' : ' hours'; # uncoverable branch false | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 2 | <<"EOF"; | |||||||||||||||||||||||||||
| 153 | Someone has requested a password reset for your account. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | To reset your password, please submit the reset password form on the | ||||||||||||||||||||||||||||
| 156 | website using the following information: | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Username: $username | ||||||||||||||||||||||||||||
| 159 | Password: <your new password> | ||||||||||||||||||||||||||||
| 160 | Reset token: $token | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | The token is valid for $hours. | ||||||||||||||||||||||||||||
| 163 | EOF | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 2 | ▶ | sub send_reset_email { | ||||||||||||||||||||||||||
|
Subroutine
send_reset_email: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 167 | 2 | my ($self, $username) = @_; | |||||||||||||||||||||||||||
| 168 | 2 | my $expire = time + $self->{token_max_age}; | |||||||||||||||||||||||||||
| 169 | 2 | my $token = $self->make_reset_hmac($username, $expire) . ":$expire"; | |||||||||||||||||||||||||||
| 170 | 2 | my $user = $self->get_user($username); | |||||||||||||||||||||||||||
| 171 | sendmail (Email::Simple->create( | ||||||||||||||||||||||||||||
| 172 | header => [ | ||||||||||||||||||||||||||||
| 173 | From => $self->{mail_from}, | ||||||||||||||||||||||||||||
| 174 | To => $user->{email}, | ||||||||||||||||||||||||||||
| 175 | Subject => $self->{mail_subject}, | ||||||||||||||||||||||||||||
| 176 | 2 | ], | |||||||||||||||||||||||||||
| 177 | body => $self->mail_body($username, $token), | ||||||||||||||||||||||||||||
| 178 | )); | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | ################################################## | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 38 | ▶ | sub response { | ||||||||||||||||||||||||||
|
Subroutine
response: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 184 | 38 | my ($self, $code, $body) = @_; | |||||||||||||||||||||||||||
| 185 | return [ | ||||||||||||||||||||||||||||
| 186 | 38 | $code, | |||||||||||||||||||||||||||
| 187 | ['Content-Type' => 'text/plain', | ||||||||||||||||||||||||||||
| 188 | 'Content-Length' => length $body], | ||||||||||||||||||||||||||||
| 189 | [ $body ], | ||||||||||||||||||||||||||||
| 190 | ]; | ||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 8 | ▶ | sub reply { shift->response(200, $_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
reply: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 194 | 28 | ▶ | sub bad_request { shift->response(400, $_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
bad_request: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 195 | 2 | ▶ | sub internal_server_error { shift->response(500, $_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
internal_server_error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 2 | ▶ | sub unauthorized { | ||||||||||||||||||||||||||
|
Subroutine
unauthorized: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 198 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 199 | 2 | my $body = 'Authorization required'; | |||||||||||||||||||||||||||
| 200 | return [ | ||||||||||||||||||||||||||||
| 201 | 401, | ||||||||||||||||||||||||||||
| 202 | ['Content-Type' => 'text/plain', | ||||||||||||||||||||||||||||
| 203 | 'Content-Length' => length $body, | ||||||||||||||||||||||||||||
| 204 | 2 | 'WWW-Authenticate' => 'Basic realm="' . $self->{realm} . '"' ], | |||||||||||||||||||||||||||
| 205 | [ $body ], | ||||||||||||||||||||||||||||
| 206 | ]; | ||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | ################################################## | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 10 | ▶ | sub call_register { | ||||||||||||||||||||||||||
|
Subroutine
call_register: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 212 | 10 | my ($self, $req) = @_; | |||||||||||||||||||||||||||
| 213 | 10 | my %parms; | |||||||||||||||||||||||||||
| 214 | 10 | for (qw/username password confirm_password email/) { | |||||||||||||||||||||||||||
| 215 | 34 | $parms{$_} = $req->param($_); | |||||||||||||||||||||||||||
| 216 | 34 | ▶ | return $self->bad_request("Missing parameter $_") unless $parms{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 8 | ▶ | return $self->bad_request($self->{invalid_username}) unless $parms{username} =~ $self->{username_regex}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 6 | ▶ | return $self->bad_request('Username already in use') if $self->get_user($parms{username}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 4 | ▶ | return $self->bad_request('The two passwords do not match') unless $parms{password} eq $parms{confirm_password}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 2 | $self->create_user($req->parameters); | |||||||||||||||||||||||||||
| 224 | 2 | return $self->reply('Registered successfully') | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 10 | ▶ | sub call_passwd { | ||||||||||||||||||||||||||
|
Subroutine
call_passwd: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 228 | 10 | my ($self, $req) = @_; | |||||||||||||||||||||||||||
| 229 | 10 | ▶ | return $self->unauthorized unless $req->user; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 8 | my %parms; | |||||||||||||||||||||||||||
| 231 | 8 | for (qw/password new_password confirm_new_password/) { | |||||||||||||||||||||||||||
| 232 | 20 | $parms{$_} = $req->param($_); | |||||||||||||||||||||||||||
| 233 | 20 | ▶ | return $self->bad_request("Missing parameter $_") unless $parms{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 6 | ▶ | return $self->bad_request('Incorrect password') unless $self->check_passphrase($req->user, $parms{password}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 4 | ▶ | return $self->bad_request('The two passwords do not match') unless $parms{new_password} eq $parms{confirm_new_password}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 2 | $self->set_passphrase($req->user, $parms{new_password}); | |||||||||||||||||||||||||||
| 239 | 2 | return $self->reply('Password changed successfully'); | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 8 | ▶ | sub call_request_reset { | ||||||||||||||||||||||||||
|
Subroutine
call_request_reset: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 243 | 8 | my ($self, $req) = @_; | |||||||||||||||||||||||||||
| 244 | 8 | ▶ | return $self->internal_server_error('Password resets are disabled') unless $self->{mail_from}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 6 | my $username = $req->param('username'); | |||||||||||||||||||||||||||
| 246 | 6 | ▶ | my $user = $self->get_user($username) or return $self->bad_request('No such user'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | 2 | ▶ | eval { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | 2 | $self->send_reset_email($username); | |||||||||||||||||||||||||||
| 249 | 2 | 1 | |||||||||||||||||||||||||||
| 250 | } or return $self->internal_server_error($@); | ||||||||||||||||||||||||||||
| 251 | 2 | $self->reply('Email sent'); | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 12 | ▶ | sub call_reset { | ||||||||||||||||||||||||||
|
Subroutine
call_reset: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 255 | 12 | my ($self, $req) = @_; | |||||||||||||||||||||||||||
| 256 | 12 | my %parms; | |||||||||||||||||||||||||||
| 257 | 12 | for (qw/username new_password confirm_new_password token/) { | |||||||||||||||||||||||||||
| 258 | 42 | $parms{$_} = $req->param($_); | |||||||||||||||||||||||||||
| 259 | 42 | ▶ | return $self->bad_request("Missing parameter $_") unless $parms{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 10 | ▶ | my $user = $self->get_user($parms{username}) or return $self->bad_request('No such user'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | 8 | ▶ | return $self->bad_request('The two passwords do not match') unless $parms{new_password} eq $parms{confirm_new_password}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 6 | my ($token, $exp) = split /:/, $parms{token}; | |||||||||||||||||||||||||||
| 265 | 6 | my $goodtoken = $self->make_reset_hmac($parms{username}, $exp); | |||||||||||||||||||||||||||
| 266 | 6 | ▶ | return $self->bad_request('Bad reset token') unless $token eq $goodtoken; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 4 | ▶ | return $self->bad_request('Reset token has expired') if time >= $exp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 2 | $self->set_passphrase($parms{username}, $parms{new_password}); | |||||||||||||||||||||||||||
| 269 | 2 | return $self->reply('Password reset successfully'); | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 60 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 273 | 60 | my ($self, $env) = @_; | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 60 | ▶ | unless ($self->{init_done}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | 2 | $self->init; | |||||||||||||||||||||||||||
| 277 | 2 | $self->{init_done} = 1; | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 60 | my $auth = $env->{HTTP_AUTHORIZATION}; | |||||||||||||||||||||||||||
| 281 | 60 | ▶ | if ($auth && $auth =~ /^Basic (.*)$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$auth and $auth =~ /^Basic (.*)$/isu
MC/DC100%2/2
$auth and $auth =~ /^Basic (.*)$/isu
$auth
$auth =~ /^Basic (.*)$/isu
Truth table100%3/3
A and B
A= $authB= $auth =~ /^Basic (.*)$/isu
| |||||||||||||||||||||||||||||
| 282 | 20 | my ($user, $pass) = split /:/, decode_base64($1), 2; | |||||||||||||||||||||||||||
| 283 | 20 | ▶ | $env->{REMOTE_USER} = $user if $self->check_passphrase($user, $pass); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 60 | my $req = Plack::Request->new($env); | |||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | 60 | ▶ | if ($req->method eq 'POST') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | 42 | ▶ | return $self->call_register($req) if $req->path eq $self->{register_url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | 32 | ▶ | return $self->call_passwd($req) if $req->path eq $self->{passwd_url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | 22 | ▶ | return $self->call_request_reset($req) if $req->path eq $self->{request_reset_url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 14 | ▶ | return $self->call_reset($req) if $req->path eq $self->{reset_url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 20 | $env->{authcomplex} = $self; | |||||||||||||||||||||||||||
| 296 | 20 | $self->app->($env); | |||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 1 | 1; | |||||||||||||||||||||||||||
| 300 | __END__ | ||||||||||||||||||||||||||||