File Coverage

blib/lib/Dancer2/Plugin/Auth/HTTP/Basic/DWIW.pm
Criterion Covered Total %
statement 52 52 100.0
branch 14 18 77.7
condition 5 10 50.0
subroutine 9 9 100.0
pod n/a
total 80 89 89.8


line stmt bran cond sub pod time code
1 6     6   5093479 use strict;
  6         43  
  6         157  
2 6     6   29 use warnings;
  6         8  
  6         277  
3              
4             package Dancer2::Plugin::Auth::HTTP::Basic::DWIW;
5             # ABSTRACT: HTTP Basic authentication plugin for Dancer2 that does what I want.
6             $Dancer2::Plugin::Auth::HTTP::Basic::DWIW::VERSION = '0.07';
7 6     6   2510 use MIME::Base64;
  6         3746  
  6         349  
8 6     6   2820 use Dancer2::Plugin;
  6         265798  
  6         65  
9              
10             our $HANDLERS = {
11             check_login => undef,
12             no_auth => undef,
13             };
14              
15             register http_basic_auth => sub {
16 5     5   227 my ($dsl, $stuff, $sub, @other_stuff) = @_;
17              
18 5   50     25 my $realm = plugin_setting->{'realm'} // 'Please login';
19              
20             return sub {
21 11     11   926235 local $@ = undef;
22 11         29 eval {
23 11   100     117 my $header = $dsl->app->request->header('Authorization') || die \401;
24              
25 7         1426 my ($auth_method, $auth_string) = split(' ', $header);
26              
27 7 50 33     76 $auth_method ne 'Basic' || $auth_string || die \400;
28              
29 7         51 my ($username, $password) = split(':', decode_base64($auth_string), 2);
30              
31 7 50 33     38 $username || $password || die \401;
32              
33 7 100       72 if(my $handler = $HANDLERS->{check_login}) {
34 5 50       18 if(ref($handler) eq 'CODE') {
35 5         24 my $check_result = eval { $handler->($username, $password); };
  5         22  
36              
37 5 100       55 if($@) {
38 1         7 $dsl->error("Error while validating credentials: $@");
39 1         640 die \500;
40             }
41              
42 4 100       14 if(!$check_result) {
43 2         15 die \401;
44             }
45             }
46             }
47             };
48              
49 11 100       871 unless ($@) {
50 4         22 return $sub->($dsl, @other_stuff);
51             }
52             else {
53 7         14 my $error_code = ${$@};
  7         19  
54              
55 7         53 $dsl->header('WWW-Authenticate' => 'Basic realm="' . $realm . '"');
56 7         1385 $dsl->status($error_code);
57              
58 7 100       708 if(my $handler = $HANDLERS->{no_auth}) {
59 2 50       8 if(ref($handler) eq 'CODE') {
60 2         9 return $handler->();
61             }
62             }
63              
64 5         31 return;
65             }
66 5         806 };
67             };
68              
69             register http_basic_auth_login => sub {
70 4     4   44 my ($dsl) = @_;
71 4         17 my $app = $dsl->app;
72              
73 4         34 my @auth_header = split(' ', $dsl->app->request->header('Authorization'));
74 4         120 my $auth_string = $auth_header[1];
75 4         25 my @auth_parts = split(':', decode_base64($auth_string), 2);
76              
77 4         26 return @auth_parts;
78             },
79             {
80             is_global => 0
81             };
82              
83             register http_basic_auth_set_check_handler => sub {
84 2     2   192 my ($dsl, $handler) = @_;
85              
86 2         115 warn 'This is deprecated! Please use http_basic_auth_handler check_login => sub {}';
87 2         17 $dsl->http_basic_auth_handler(check_login => $handler);
88             };
89              
90             register http_basic_auth_handler => sub {
91 4     4   112 my ($dsl, $name, $handler) = @_;
92 4         17 $HANDLERS->{$name} = $handler;
93             };
94              
95             register_plugin for_versions => [2];
96             1;
97              
98             __END__