line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::BasicAuth; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
2324
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
58
|
|
4
|
1
|
|
|
1
|
|
8
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
54
|
|
5
|
1
|
|
|
1
|
|
25
|
use Mojo::ByteStream; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
107
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.08'; |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
8
|
use base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
555
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub register { |
12
|
1
|
|
|
1
|
1
|
53
|
my ($plugin, $app) = @_; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
$app->renderer->add_helper( |
15
|
|
|
|
|
|
|
basic_auth => sub { |
16
|
13
|
|
|
13
|
|
256425
|
my $self = shift; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# |
19
|
|
|
|
|
|
|
# Sent Credentials |
20
|
13
|
|
100
|
|
|
52
|
my $auth = $self->req->url->to_abs->userinfo || ''; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Required credentials |
23
|
13
|
|
|
|
|
7064
|
my ($realm, $password, $username) = $plugin->_expected_auth(@_); |
24
|
13
|
100
|
|
|
|
38
|
my $callback = $password if ref $password eq 'CODE'; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# No credentials entered |
27
|
13
|
100
|
66
|
|
|
55
|
return $plugin->_password_prompt($self, $realm) |
28
|
|
|
|
|
|
|
if !$auth and !$callback; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Verification within callback |
31
|
11
|
100
|
100
|
|
|
63
|
return 1 if $callback and $callback->(split /:/, $auth, 2); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Verified with realm => username => password syntax |
34
|
8
|
100
|
100
|
|
|
77
|
return 1 if $auth eq ($username || '') . ":$password"; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Not verified |
37
|
5
|
|
|
|
|
18
|
return $plugin->_password_prompt($self, $realm); |
38
|
|
|
|
|
|
|
} |
39
|
1
|
|
|
|
|
40
|
); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub _expected_auth { |
43
|
13
|
|
|
13
|
|
28
|
my $self = shift; |
44
|
13
|
|
|
|
|
19
|
my $realm = shift; |
45
|
|
|
|
|
|
|
|
46
|
13
|
50
|
|
|
|
48
|
return @$realm{qw/ realm password username /} if ref $realm eq "HASH"; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# realm, pass, user || realm, pass, undef || realm, callback |
49
|
13
|
|
|
|
|
42
|
return $realm, reverse @_; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub _password_prompt { |
53
|
7
|
|
|
7
|
|
13
|
my ($self, $c, $realm) = @_; |
54
|
|
|
|
|
|
|
|
55
|
7
|
|
|
|
|
30
|
$c->res->headers->www_authenticate("Basic realm=$realm"); |
56
|
7
|
|
|
|
|
885
|
$c->res->code(401); |
57
|
7
|
|
|
|
|
395
|
$c->rendered; |
58
|
|
|
|
|
|
|
|
59
|
7
|
|
|
|
|
2319
|
return; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
1; |
63
|
|
|
|
|
|
|
__END__ |