blib/lib/Mojolicious/Plugin/HttpBasicAuth.pm

filter: Statementstmt 97.6% 41 / 42 Branchbran 87.5% 7 / 8 Conditioncond 58.3% 14 / 24 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 88.8% 8 / 9 Podpod 100% 1 / 1 totaltotal 76.5% 75 / 98 CC 14 SCAR 13.9
CC
14
Cov
84%
__ANON__
6.0
_auth_header
5.1
register
4.2
CRAP 4.0
↑ Summary
1 package Mojolicious::Plugin::HttpBasicAuth;
2  
3 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mojo::ByteStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mojo::Util qw{b64_encode b64_decode};
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.12';
8  
9 1 ▶sub register {
Subroutine register: called CC=4 SCAR=14.3
Pod documented
10 1 my ($plugin, $app, $user_defaults) = @_;
11  
12 1 push @{ $app->renderer->classes }, __PACKAGE__;
13  
14 1 my %defaults = %$user_defaults;
15 1 ▶ $defaults{realm} //= 'WWW';
Condition50%1/2
$defaults{"realm"} //= "WWW"
l!l
01
MC/DC0%0/1
$defaults{"realm"} //= "WWW"
$defaults{"realm"}
Truth table50%1/2
A
A= $defaults{"realm"}
Aresult
11
00
16 0 ▶ $defaults{validate} //= sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
17 0 die('please define a validate callback');
18 1 ▶ };
Condition33%1/3
$defaults{"validate"} //= sub { die "please define a validate callback"; }
l!l&&r!l&&!r
100
MC/DC0%0/2
$defaults{"validate"} //= sub { die "please define a validate callback"; }
$defaults{"validate"} sub { die "please define a validate callback"; }
Truth table33%1/3
A //= B
A= $defaults{"validate"}B= sub { die "please define a validate callback"; }
ABresult
1X1
011
000
19 126 ▶ $defaults{invalid} //= sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
20 126 my $controller = shift;
21 return (
22 126 json => { json => { error => 'HTTP 401: Unauthorized' } },
23 html => { template => 'auth/basic' },
24 any => { data => 'HTTP 401: Unauthorized' }
25 );
26 1 ▶ };
Condition33%1/3
$defaults{"invalid"} //= sub { my $controller = shift(); return "json", {"json", {"error", "HTTP 401: Unauthorized"}}, "html", {"template", "auth/basic"}, "any", {"data", "HTTP 401: Unauthorized"}; }
l!l&&r!l&&!r
010
MC/DC0%0/2
$defaults{"invalid"} //= sub { my $controller = shift(); return "json", {"json", {"error", "HTTP 401: Unauthorized"}}, "html", {"template", "auth/basic"}, "any", {"data", "HTTP 401: Unauthorized"}; }
$defaults{"invalid"} sub { my $controller = shift(); return "json", {"json", {"error", "HTTP 401: Unauthorized"}}, "html", {"template", "auth/basic"}, "any", {"data", "HTTP 401: Unauthorized"}; }
Truth table33%1/3
A //= B
A= $defaults{"invalid"}B= sub { my $controller = shift(); return "json", {"json", {"error", "HTTP 401: Unauthorized"}}, "html", {"template", "auth/basic"}, "any", {"data", "HTTP 401: Unauthorized"}; }
ABresult
1X1
011
000
27  
28 $app->renderer->add_helper(
29 162 ▶ basic_auth => sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
30 162 my $controller = shift;
31 162 ▶ my $params = shift // {};
Condition50%1/2
shift() // {}
l!l
1620
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
32 162 my %options = (%defaults, %$params);
33  
34 # Sent credentials
35 162 ▶ my $auth = b64_decode($plugin->_auth_header($controller) || '');
Condition100%2/2
$plugin->_auth_header($controller) || ''
l!l
14418
MC/DC100%1/1
$plugin->_auth_header($controller) || ''
$plugin->_auth_header($controller)
Truth table100%2/2
A
A= $plugin->_auth_header($controller)
Aresult
11
00
36  
37 # No credentials entered
38 162 ▶ return $plugin->_unauthorized($controller, $options{realm}, $options{invalid}) unless ($auth);
Branch: 1 branch
BranchTrueFalseTotal
unless $auth 18 144 162
39  
40 # Verification within callback
41 144 ▶ return 1 if $options{validate} and $options{validate}->($controller, split(/:/, $auth, 2), $options{realm});
Branch: 1 branch
BranchTrueFalseTotal
if $options{"validate"} and $options{"validate"}->($controller, split(/:/u, $auth, 2), $options{"realm"}) 36 108 144
Condition66%2/3
$options{"validate"} and $options{"validate"}->($controller, split(/:/u, $auth, 2), $options{"realm"})
!ll&&!rl&&r
010836
MC/DC50%1/2
$options{"validate"} and $options{"validate"}->($controller, split(/:/u, $auth, 2), $options{"realm"})
$options{"validate"} $options{"validate"}->($controller, split(/:/u, $auth, 2), $options{"realm"})
Truth table66%2/3
A and B
A= $options{"validate"}B= $options{"validate"}->($controller, split(/:/u, $auth, 2), $options{"realm"})
ABresult
0X0
100
111
42  
43 # Not verified
44 108 return $plugin->_unauthorized($controller, $options{realm}, $options{invalid});
45 }
46 1 );
47 }
48  
49 162 ▶sub _auth_header {
Subroutine _auth_header: called CC=5 SCAR=16.4
50 162 my $plugin = shift;
51 162 my $controller = shift;
52 162 ▶ my $auth = $controller->req->headers->authorization || $controller->req->env->{'X_HTTP_AUTHORIZATION'} || $controller->req->env->{'HTTP_AUTHORIZATION'};
Condition66%4/6
$controller->req->headers->authorization || $controller->req->env->{'X_HTTP_AUTHORIZATION'} || $controller->req->env->{'HTTP_AUTHORIZATION'}
l!l&&r!l&&!r
144018
$controller->req->headers->authorization or $controller->req->env->{"X_HTTP_AUTHORIZATION"}
l!l&&r!l&&!r
144018
MC/DC33%1/3
$controller->req->headers->authorization || $controller->req->env->{'X_HTTP_AUTHORIZATION'} || $controller->req->env->{'HTTP_AUTHORIZATION'}
$controller->req->headers->authorization $controller->req->env->{"X_HTTP_AUTHORIZATION"} $controller->req->env->{'HTTP_AUTHORIZATION'}
Truth table50%2/4
A or B || C
A= $controller->req->headers->authorizationB= $controller->req->env->{"X_HTTP_AUTHORIZATION"}C= $controller->req->env->{'HTTP_AUTHORIZATION'}
ABCresult
1XX1
01X1
0011
0000
53  
54 162 ▶ if ($auth && $auth =~ m/Basic (.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth and $auth =~ /Basic (.*)/u) 144 18 162
Condition66%2/3
$auth and $auth =~ /Basic (.*)/u
!ll&&!rl&&r
180144
MC/DC50%1/2
$auth and $auth =~ /Basic (.*)/u
$auth $auth =~ /Basic (.*)/u
Truth table66%2/3
A and B
A= $authB= $auth =~ /Basic (.*)/u
ABresult
0X0
100
111
55 144 $auth = $1;
56 }
57  
58 162 return $auth;
59 }
60  
61 126 ▶sub _unauthorized {
Subroutine _unauthorized: called CC=2 SCAR=7.0
62 126 my ($plugin, $controller, $realm, $callback) = @_;
63  
64 126 $controller->res->code(401);
65 126 $controller->res->headers->www_authenticate("Basic realm=\"$realm\"");
66 126 $controller->respond_to($callback->($controller));
67  
68 # Only render if not already rendered
69 126 ▶ if ($controller->tx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($controller->tx) 126 0 126
70 126 $controller->rendered;
71 }
72  
73 126 return;
74 }
75  
76 1 1;
77  
78 =pod
79  
80 =encoding utf8
81  
82 =head1 NAME
83  
84 Mojolicious::Plugin::HttpBasicAuth - Http-Basic-Authentication implementation for Mojolicious
85  
86 =head1 SYNOPSIS
87  
88 # in your startup
89 $self->plugin(
90 'http_basic_auth', {
91 validate => sub {
92 my $c = shift;
93 my $loginname = shift;
94 my $password = shift;
95 my $realm = shift;
96 return 1 if($realm eq 'Evergreen Terrace' && $loginname eq 'Homer' && $password eq 'Marge');
97 return 0;
98 },
99 realm => 'Evergreen Terrace'
100 }
101 );
102  
103 # in your routes
104 sub index {
105 my $self = shift;
106 return unless $self->basic_auth(\%options);
107 $self->render();
108 }
109  
110 # or bridged
111 my $foo = $r->bridge('/bridge')->to(cb => sub {
112 my $self = shift;
113 # Authenticated
114 return unless $self->basic_auth({realm => 'Castle Bridge', validate => sub {return 1;}});
115 });
116 $foo->route('/bar')->to(controller => 'foo', action => 'bar');
117  
118  
119 =head1 DESCRIPTION
120  
121 L<Mojolicious::Plugin::HttpBasicAuth> is a implementation of the Http-Basic-Authentication
122  
123 =head1 OPTIONS
124  
125 L<Mojolicious::Plugin::HttpBasicAuth> supports the following options.
126  
127 =head2 realm
128  
129 $self->plugin('http_basic_auth', {realm => 'My Castle!'});
130  
131 HTTP-Realm, defaults to 'WWW'
132  
133 =head2 validate
134  
135 $self->plugin('http_basic_auth', {
136 validate => sub {
137 my $c = shift;
138 my $loginname = shift;
139 my $password = shift;
140 my $realm = shift;
141 return 1 if($realm eq 'Springfield' && $loginname eq 'Homer' && $password eq 'Marge');
142 return 0;
143 }
144 });
145  
146 Validation callback to verify user. This option is B<mandatory>.
147  
148 =head2 invalid
149  
150 $self->plugin('http_basic_auth', {
151 invalid => sub {
152 my $controller = shift;
153 return (
154 json => { json => { error => 'HTTP 401: Unauthorized' } },
155 html => { template => 'auth/basic' },
156 any => { data => 'HTTP 401: Unauthorized' }
157 );
158 }
159 });
160  
161 Callback for invalid requests, default can be seen here. Return values are dispatched to L<Mojolicious::Controller/"respond_to">
162  
163 =head1 HELPERS
164  
165 L<Mojolicious::Plugin::HttpBasicAuth> implements the following helpers.
166  
167 =head2 basic_auth
168  
169 return unless $self->basic_auth({realm => 'Kitchen'});
170  
171 All default options can be overwritten in every call.
172  
173 =head1 METHODS
174  
175 L<Mojolicious::Plugin::HttpBasicAuth> inherits all methods from
176 L<Mojolicious::Plugin> and implements the following new ones.
177  
178 =head2 register
179  
180 my $route = $plugin->register(Mojolicious->new);
181 my $route = $plugin->register(Mojolicious->new, {realm => 'Fort Knox', validate => sub {
182 return 0;
183 }});
184  
185 Register renderer and helper in L<Mojolicious> application.
186  
187 =head1 SEE ALSO
188  
189 L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
190  
191 =head1 AUTHOR
192  
193 Patrick Grämer E<lt>pgraemer@cpan.orgE<gt>
194 , L<http://graemer.org>.
195  
196 =head1 CONTRIBUTOR
197  
198 Markus Michel E<lt>mmichel@cpan.orgE<gt>
199 , L<http://markusmichel.org>.
200  
201 =head1 COPYRIGHT
202  
203 Copyright 2015 Patrick Grämer
204  
205 =head1 LICENSE
206  
207 This library is free software; you can redistribute it and/or modify
208 it under the same terms as Perl itself.
209  
210 =cut
211  
212 __DATA__
↑ Summary