blib/lib/Dancer2/Plugin/HTTP/Auth/Extensible.pm

filter: Statementstmt 45.5% 98 / 215 Branchbran 20.4% 20 / 98 Conditioncond 16.1% 5 / 31 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 75.0% 21 / 28 Podpod 53.3% 8 / 15 totaltotal 37.5% 152 / 405 CC 65 SCAR 41.6
CC
65
Cov
36%
__ANON__
252.0
http_authenticate_user
64.6
auth_provider
56.0
CRAP 64.2
↑ Summary
1 package Dancer2::Plugin::HTTP::Auth::Extensible;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Dancer2::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Class::Load qw(try_load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use HTTP::Headers::ActionPack::Authorization;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use HTTP::Headers::ActionPack::WWWAuthenticate;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 our $VERSION = '0.121';
14  
15  
16 =head1 NAME
17  
18 Dancer2::Plugin::HTTP::Auth::Extensible - extensible authentication framework for Dancer2 apps
19  
20 =head1 DESCRIPTION
21  
22 A user authentication and authorisation framework plugin for Dancer2 apps.
23  
24 Makes it easy to require a user to be logged in to access certain routes,
25 provides role-based access control, and supports various authentication
26 methods/sources (config file, database, Unix system users, etc).
27  
28 Designed to support multiple authentication realms and to be as extensible as
29 possible, and to make secure password handling easy (the base class for auth
30 providers makes handling C<RFC2307>-style hashed passwords really simple, so you
31 have no excuse for storing plain-text passwords).
32  
33  
34 =head1 SYNOPSIS
35  
36 Configure the plugin to use the authentication provider class you wish to use:
37  
38 plugins:
39 HTTP::Auth::Extensible:
40 realms:
41 users:
42 provider: Example
43 ....
44  
45 The configuration you provide will depend on the authentication provider module
46 in use. For a simple example, see
47 L<Dancer2::Plugin::Auth::Extensible::Provider::Config>.
48  
49 Define that a user must be logged in and have the proper permissions to
50 access a route:
51  
52 get '/secret' => http_require_role Confidant => sub { tell_secrets(); };
53  
54 Define that a user must be logged in to access a route - and find out who is
55 logged in with the C<logged_in_user> keyword:
56  
57 get '/users' => http_require_authentication sub {
58 my $user = http_authenticated_user;
59 return "Hi there, $user->{username}";
60 };
61  
62 =head1 AUTHENTICATION PROVIDERS
63  
64 This framework builds on top of L<Dancer2::Plugin::Auth::Extensible>. For a
65 full explenation of the providers check that manual.
66  
67 For flexibility, that authentication framework uses simple authentication
68 provider classes, which implement a simple interface and do whatever is required
69 to authenticate a user against the chosen source of authentication.
70  
71 For an example of how simple provider classes are, so you can build your own if
72 required or just try out this authentication framework plugin easily,
73 see L<Dancer2::Plugin::Auth::Extensible::Provider::Example>.
74  
75 That framework supplies the following providers out-of-the-box:
76  
77 =over 4
78  
79 =item L<Dancer2::Plugin::Auth::Extensible::Provider::Unix>
80  
81 Authenticates users using system accounts on Linux/Unix type boxes
82  
83 =item L<Dancer2::Plugin::Auth::Extensible::Provider::Database>
84  
85 Authenticates users stored in a database table
86  
87 =item L<Dancer2::Plugin::Auth::Extensible::Provider::Config>
88  
89 Authenticates users stored in the app's config
90  
91 =back
92  
93 Need to write your own? Just subclass
94 L<Dancer2::Plugin::Auth::Extensible::Provider::Base> and implement the required
95 methods, and you're good to go!
96  
97 =head1 CONTROLLING ACCESS TO ROUTES
98  
99 Keywords are provided to check if a user is logged in / has appropriate roles.
100  
101 =over
102  
103 =item http_require_authentication - require the user to be authenticated
104  
105 get '/dashboard' => http_require_authentication sub { .... };
106  
107 If the user can not be authenticated, they will be recieve a HTTP response
108 status of C</401 Not Authorized>. Remember, it should actualy say 'Not
109 Authenticated'.
110  
111 Optionally, a realm name can be specified as an extra argument:
112  
113 get 'outer_space'
114 => http_require_authentication 'outer_space'
115 => sub { .... };
116  
117 =item http_require_role - require the user to have a specified role
118  
119 get '/beer' => http_require_role BeerDrinker => sub { ... };
120  
121 Requires that the user can be authenticated as a user who has the specified
122 role. If the user can not be authenticated, they will get a C<401 Unautorized>
123 response. If they are logged in, but do not have the required role, they will
124 recieve a C<403 Forbidden> response.
125  
126 =item http_require_any_roles - require the user to have one of a list of roles
127  
128 get '/drink' => require_any_role [qw(BeerDrinker VodaDrinker)] => sub {
129 ...
130 };
131  
132 Same as L<http_require_role> except that a user has any one (or more) of the
133 roles listed.
134  
135 =item require_all_roles - require the user to have all roles listed
136  
137 get '/foo' => require_all_roles [qw(Foo Bar)] => sub { ... };
138  
139 Same as L<http_require_role> except that a user has all of the roles listed.
140  
141 =back
142  
143 =head2 Replacing the Default C< 401 > and C< 403 > Pages
144  
145  
146 =head2 Keywords
147  
148 =over
149  
150 =item http_require_authentication
151  
152 Used to wrap a route which requires a user can be authenticated to access
153 it.
154  
155 get '/secret' => http_require_authentication sub { .... };
156 get '/secret' => http_require_authentication 'realm-name' sub { .... };
157  
158 =cut
159  
160 9 ▶sub http_require_authentication {
Subroutine http_require_authentication: called CC=2 SCAR=9.7
Pod documented
161 9 my $dsl = shift;
162 9 ▶ my $realm = (@_ == 2) ? shift : http_default_realm($dsl);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 3 6 9
163 9 my $coderef = shift;
164  
165 11 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=23.7
166 11 ▶ if (!$coderef || ref $coderef ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $coderef or ref $coderef ne "CODE") 0 11 11
Condition33%1/3
not $coderef or ref $coderef ne "CODE"
l!l&&r!l&&!r
0011
MC/DC0%0/2
not $coderef or ref $coderef ne "CODE"
not $coderef ref $coderef ne "CODE"
Truth table33%1/3
not(A) or B
A= not $coderefB= ref $coderef ne "CODE"
ABresult
1X1
011
000
167 0 warn "Invalid http_require_authentication usage, please see docs";
168 }
169
170 11 my $user = http_authenticated_user($dsl, $realm);
171 0 ▶ if (!$user) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user) 0 0 0
172 # $dsl->execute_hook('http_authentication_required', $coderef);
173 # # TODO: see if any code executed by that hook set up a response
174 0 $dsl->header('WWW-Authenticate' =>
175 0 qq|@{[ http_default_scheme($dsl) ]} realm="$realm"|
176 );
177 0 $dsl->status(401); # Unauthorized
178 return
179 0 qq|Authentication required to access realm: |
180 . qq|'$realm'|;
181 }
182 0 return $coderef->($dsl);
183 9 };
184 }
185  
186 5 register http_require_authentication => \&http_require_authentication;
187 5 register http_requires_authentication => \&http_require_authentication;
188  
189 =item require_role
190  
191 Used to wrap a route which requires a user can be authenticated with the
192 specified role in order to access it.
193  
194 get '/beer' => require_role BeerDrinker => sub { ... };
195 get '/beer' => require_role BeerDrinker 'realm-name' => sub { ... };
196  
197 You can also provide a regular expression, if you need to match the role using a
198 regex - for example:
199  
200 get '/beer' => http_require_role qr/Drinker$/ => sub { ... };
201  
202 =cut
203  
204 5 ▶sub http_require_role {
Subroutine http_require_role: called CC=1 SCAR=0.0
Pod documented
205 5 return _build_wrapper(@_, 'single');
206 }
207  
208 5 register http_require_role => \&http_require_role;
209 5 register http_requires_role => \&http_require_role;
210  
211 =item http_require_any_role
212  
213 Used to wrap a route which requires a user can be authenticated with any
214 one (or more) of the specified roles in order to access it.
215  
216 get '/foo' => http_require_any_role [qw(Foo Bar)] => sub { ... };
217 get '/foo' => http_require_any_role [qw(Foo Bar)] 'realm-name' => sub { ... };
218  
219 =cut
220  
221 1 ▶sub http_require_any_role {
Subroutine http_require_any_role: called CC=1 SCAR=0.0
Pod documented
222 1 return _build_wrapper(@_, 'any');
223 }
224  
225 5 register http_require_any_role => \&http_require_any_role;
226 5 register http_requires_any_role => \&http_require_any_role;
227  
228 =item http_require_all_roles
229  
230 Used to wrap a route which requires a user can be authenticated with all
231 of the roles listed in order to access it.
232  
233 get '/foo' => http_require_all_roles [qw(Foo Bar)] => sub { ... };
234 get '/foo' => http_require_all_roles [qw(Foo Bar)] 'realm-name' => sub { ... };
235  
236 =cut
237  
238 2 ▶sub http_require_all_roles {
Subroutine http_require_all_roles: called CC=1 SCAR=0.0
Pod documented
239 2 return _build_wrapper(@_, 'all');
240 }
241  
242 5 register http_require_all_roles => \&http_require_all_roles;
243 5 register http_requires_all_roles => \&http_require_all_roles;
244  
245  
246 8 ▶sub _build_wrapper {
Subroutine _build_wrapper: called CC=2 SCAR=14.7
247 8 my $dsl = shift;
248 8 my $require_role = shift;
249 8 ▶ my $realm = (@_ == 3) ? shift : http_default_realm($dsl);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 ? : 0 8 8
250 8 my $coderef = shift;
251 8 my $mode = shift;
252  
253 8 ▶ return sub {
Subroutine __ANON__: called CC=17 SCAR=55.3
254 8 ▶ if (!$coderef || ref $coderef ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $coderef or ref $coderef ne "CODE") 0 8 8
Condition33%1/3
not $coderef or ref $coderef ne "CODE"
l!l&&r!l&&!r
008
MC/DC0%0/2
not $coderef or ref $coderef ne "CODE"
not $coderef ref $coderef ne "CODE"
Truth table33%1/3
not(A) or B
A= not $coderefB= ref $coderef ne "CODE"
ABresult
1X1
011
000
255 0 warn "Invalid http_require_authentication usage, please see docs";
256 }
257
258 8 my $user = http_authenticated_user($dsl, $realm);
259 0 ▶ if (!$user) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user) 0 0 0
260 # $dsl->execute_hook('http_authentication_required', $coderef);
261 # # TODO: see if any code executed by that hook set up a response
262 0 $dsl->header('WWW-Authenticate' =>
263 0 qq|@{[ http_default_scheme($dsl) ]} realm="$realm"|
264 );
265 0 $dsl->status(401); # Unauthorized
266 return
267 0 qq|Authentication required to access realm: |
268 . qq|'$realm'|;
269 }
270
271 0 ▶ my @role_list = ref $require_role eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $require_role eq 'ARRAY' ? : 0 0 0
272 ? @$require_role
273 : $require_role;
274 0 my $role_match;
275 0 ▶ if ($mode eq 'single') {
Branch: 3 branches
BranchTrueFalseTotal
if ($mode eq 'single') { } 0 0 0
elsif ($mode eq 'any') { } 0 0 0
elsif ($mode eq 'all') { } 0 0 0
276 0 for (user_roles($dsl)) {
277 0 ▶ $role_match++ and last if _smart_match($_, $require_role);
Branch: 1 branch
BranchTrueFalseTotal
if _smart_match($_, $require_role) 0 0 0
Condition0%0/5
_smart_match($_, $require_role) && ($role_match++ && (last))
!ll&&!rl&&r
000
$role_match++ and last
!ll
00
MC/DC0%0/2
_smart_match($_, $require_role) && ($role_match++ && (last))
_smart_match($_, $require_role) $role_match++
Truth table0%0/3
A && B
A= _smart_match($_, $require_role)B= $role_match++
ABresult
0X0
100
111
278 }
279 } elsif ($mode eq 'any') {
280 0 my %role_ok = map { $_ => 1 } @role_list;
281 0 for (user_roles($dsl)) {
282 0 ▶ $role_match++ and last if $role_ok{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $role_ok{$_} 0 0 0
Condition0%0/5
$role_ok{$_} && ($role_match++ && (last))
!ll&&!rl&&r
000
$role_match++ and last
!ll
00
MC/DC0%0/2
$role_ok{$_} && ($role_match++ && (last))
$role_ok{$_} $role_match++
Truth table0%0/3
A && B
A= $role_ok{$_}B= $role_match++
ABresult
0X0
100
111
283 }
284 } elsif ($mode eq 'all') {
285 0 $role_match++;
286 0 for my $role (@role_list) {
287 0 ▶ if (!user_has_role($dsl, $role)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (user_has_role($dsl, $role)) 0 0 0
288 0 $role_match = 0;
289 0 last;
290 }
291 }
292 }
293 0 ▶ if (!$role_match) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($role_match) 0 0 0
294  
295 # $dsl->execute_hook('http_permission_denied', $coderef);
296 # # TODO: see if any code executed by that hook set up a response
297 0 $dsl->status(403); # Forbidden
298 return
299 0 qq|Permission denied for resource: |
300 0 . qq|'@{[ $dsl->request->path ]}'|;
301 }
302
303 # We're happy with their roles, so go head and execute the route
304 # handler coderef.
305 0 return $coderef->($dsl);
306  
307 8 }; # return sub
308 } # _build_wrapper
309  
310  
311  
312 =item authenticated_user
313  
314 Returns a hashref of details of the currently authenticated user, if there is one.
315  
316 The details you get back will depend upon the authentication provider in use.
317  
318 =cut
319  
320 19 ▶sub http_authenticated_user {
Subroutine http_authenticated_user: called CC=3 SCAR=16.7
Pod undocumented
321 19 my $dsl = shift;
322 19 ▶ my $realm = shift || http_default_realm($dsl);
Condition33%1/3
shift() || http_default_realm($dsl)
l!l&&r!l&&!r
1900
MC/DC0%0/2
shift() || http_default_realm($dsl)
shift() http_default_realm($dsl)
Truth table33%1/3
A || B
A= shift()B= http_default_realm($dsl)
ABresult
1X1
011
000
323
324 19 ▶ if ( http_authenticate_user($dsl, $realm) ) { # undef unless http_authenticate_user
Branch: 1 branch
BranchTrueFalseTotal
if (http_authenticate_user($dsl, $realm)) { } 0 0 0
325 0 my $provider = auth_provider($dsl, http_realm($dsl));
326 0 return $provider->get_user_details(
327 http_username($dsl),
328 http_realm($dsl)
329 );
330 } else {
331 0 return;
332 }
333 }
334 5 register http_authenticated_user => \&http_authenticated_user;
335  
336 =item user_has_role
337  
338 Check if a user has the role named.
339  
340 By default, the currently-logged-in user will be checked, so you need only name
341 the role you're looking for:
342  
343 if (user_has_role('BeerDrinker')) { pour_beer(); }
344  
345 You can also provide the username to check;
346  
347 if (user_has_role($user, $role)) { .... }
348  
349 =cut
350  
351 0 ▶sub user_has_role {
Subroutine user_has_role: not called CC=5 SCAR=34.0
Pod documented
352 0 my $dsl = shift;
353 0 my $session = $dsl->app->session;
354  
355 0 my ($username, $want_role);
356 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
357 0 ($username, $want_role) = @_;
358 } else {
359 0 $username = http_username($dsl);
360 0 $want_role = shift;
361 }
362  
363 0 ▶ return unless defined $username;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $username 0 0 0
364  
365 0 my $roles = user_roles($dsl, $username);
366  
367 0 for my $has_role (@$roles) {
368 0 ▶ return 1 if $has_role eq $want_role;
Branch: 1 branch
BranchTrueFalseTotal
if $has_role eq $want_role 0 0 0
369 }
370  
371 0 return 0;
372 }
373 5 register user_has_role => \&user_has_role;
374  
375 =item user_roles
376  
377 Returns a list of the roles of a user.
378  
379 By default, roles for the currently-logged-in user will be checked;
380 alternatively, you may supply a username to check.
381  
382 Returns a list or arrayref depending on context.
383  
384 =cut
385  
386 0 ▶sub user_roles {
Subroutine user_roles: not called CC=5 SCAR=34.0
Pod documented
387 0 my ($dsl, $username, $realm) = @_;
388 0 my $session = $dsl->app->session;
389  
390 0 ▶ $username = http_username($dsl) unless defined $username;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $username 0 0 0
391  
392 0 ▶ my $search_realm = ($realm ? $realm : '');
Branch: 1 branch
BranchTrueFalseTotal
$realm ? : 0 0 0
393  
394 0 my $roles = auth_provider($dsl, $search_realm)->get_user_roles($username);
395 0 ▶ return unless defined $roles;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $roles 0 0 0
396 0 ▶ return wantarray ? @$roles : $roles;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
397 }
398 5 register user_roles => \&user_roles;
399  
400  
401 =item authenticate_user
402  
403 Usually you'll want to let the built-in authentication handling code deal with
404 authenticating users, but in case you need to do it yourself, this keyword
405 accepts a username and password, and optionally a specific realm, and checks
406 whether the username and password are valid.
407  
408 For example:
409  
410 if (authenticate_user($username, $password)) {
411 ...
412 }
413  
414 If you are using multiple authentication realms, by default each realm will be
415 consulted in turn. If you only wish to check one of them (for instance, you're
416 authenticating an admin user, and there's only one realm which applies to them),
417 you can supply the realm as an optional third parameter.
418  
419 In boolean context, returns simply true or false; in list context, returns
420 C<($success, $realm)>.
421  
422 =cut
423  
424 19 ▶sub http_authenticate_user {
Subroutine http_authenticate_user: called CC=9 SCAR=41.7
Pod undocumented
425 19 my $dsl = shift;
426 19 ▶ my $realm = shift || http_default_realm($dsl);
Condition33%1/3
shift() || http_default_realm($dsl)
l!l&&r!l&&!r
1900
MC/DC0%0/2
shift() || http_default_realm($dsl)
shift() http_default_realm($dsl)
Truth table33%1/3
A || B
A= shift()B= http_default_realm($dsl)
ABresult
1X1
011
000
427
428 19 http_realm_exists($dsl, $realm);
429  
430 0 ▶ unless ($dsl->request->header('Authorization')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dsl->request->header("Authorization")) 0 0 0
431 0 ▶ return wantarray ? (0, undef) : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
432 }
433 # my ($username, $password) = $dsl->request->headers->authorization_basic;
434
435 0 my $auth
436 = HTTP::Headers::ActionPack::Authorization::Basic
437 ->new_from_string($dsl->request->header('Authorization'));
438 0 my $username = $auth->username;
439 0 my $password = $auth->password;
440
441 # TODO For now it only does Basic authentication
442 # Once we have Digest and others, it needs to choose itself
443
444 0 ▶ my @realms_to_check = $realm ? ($realm) : (keys %{ plugin_setting->{realms} });
Branch: 1 branch
BranchTrueFalseTotal
$realm ? : 0 0 0
445  
446 0 for my $realm (@realms_to_check) { # XXX we only should have 1 ????
447 0 $dsl->app->log ( debug => "Attempting to authenticate $username against realm $realm");
448 0 my $provider = auth_provider($dsl, $realm);
449 0 ▶ if ($provider->authenticate_user($username, $password)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($provider->authenticate_user($username, $password)) 0 0 0
450 0 $dsl->app->log ( debug => "$realm accepted user $username");
451 0 $dsl->vars->{'http_username'} = $username;
452 # don't do `http_username($dsl, $username)`, SECURITY BREACH
453 0 $dsl->vars->{'http_realm' } = $realm;
454 # don't do `http_username($dsl, $username)`, SECURITY BREACH
455 0 ▶ return wantarray ? ($username, $realm) : $username;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
456 }
457 }
458  
459 # If we get to here, we failed to authenticate against any realm using the
460 # details provided.
461 # TODO: allow providers to raise an exception if something failed, and catch
462 # that and do something appropriate, rather than just treating it as a
463 # failed login.
464 0 ▶ return wantarray ? (0, undef) : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
465 }
466  
467 5 register http_authenticate_user => \&http_authenticate_user;
468  
469 14 ▶sub http_default_realm {
Subroutine http_default_realm: called CC=3 SCAR=11.3
Pod undocumented
470 14 my $dsl = shift;
471
472 14 ▶ if (1 == keys %{ plugin_setting->{realms} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == keys %{plugin_setting()->{"realms"};}) 13 1 14
473 13 return (keys %{ plugin_setting->{realms} })[0]; # only the first key in scalar context
474 }
475 1 ▶ if (exists plugin_setting->{default_realm} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists plugin_setting()->{"default_realm"}) 1 0 1
476 1 return plugin_setting->{default_realm};
477 }
478  
479 die
480 0 qq|Internal Server Error: |
481 . qq|"multiple realms without default"|;
482
483 0 return;
484
485 } # http_default_realm
486  
487 #register http_default_realm => \&http_default_realm;
488  
489 19 ▶sub http_realm_exists {
Subroutine http_realm_exists: called CC=3 SCAR=15.7
Pod undocumented
490 19 my $dsl = shift;
491 19 ▶ my $realm = shift || http_default_realm($dsl);
Condition33%1/3
shift() || http_default_realm($dsl)
l!l&&r!l&&!r
1900
MC/DC0%0/2
shift() || http_default_realm($dsl)
shift() http_default_realm($dsl)
Truth table33%1/3
A || B
A= shift()B= http_default_realm($dsl)
ABresult
1X1
011
000
492  
493 0 ▶ unless (grep {$realm eq $_} keys %{ plugin_setting->{realms} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$realm eq $_;} keys %{plugin_setting()->{"realms"};}) 0 0 0
494 0 die
495 qq|Internal Server Error: |
496 . qq|"required realm does not exist: '$realm'"|;
497 }
498
499 0 return $realm;
500
501 } # http_realm_exists
502  
503 #register http_realm_exists => \&http_realm_exists;
504  
505  
506 0 ▶sub http_default_scheme {
Subroutine http_default_scheme: not called CC=3 SCAR=24.8
Pod undocumented
507 0 my $dsl = shift;
508 0 ▶ my $realm = shift || http_default_realm($dsl);
Condition0%0/3
shift() || http_default_realm($dsl)
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || http_default_realm($dsl)
shift() http_default_realm($dsl)
Truth table0%0/3
A || B
A= shift()B= http_default_realm($dsl)
ABresult
1X1
011
000
509  
510 0 http_realm_exists($dsl, $realm);
511
512 0 my $scheme;
513
514 0 ▶ if (exists plugin_setting->{realms}->{$realm}->{scheme} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists plugin_setting()->{'realms'}{$realm}{'scheme'}) { } 0 0 0
515 0 $scheme = plugin_setting->{realms}->{$realm}->{scheme};
516 }
517 else {
518 0 $scheme = "Basic";
519 }
520
521 0 return $scheme;
522
523 } # http_default_schema
524  
525 #register http_default_scheme => \&http_default_scheme;
526  
527  
528 0 ▶sub http_scheme_known {
Subroutine http_scheme_known: not called CC=2 SCAR=17.9
Pod undocumented
529 0 my $dsl = shift;
530 0 my $scheme = shift;
531  
532 0 ▶ unless (grep $scheme eq $_, ('Basic', 'Digest')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep(($scheme eq $_), ("Basic", "Digest"))) 0 0 0
533 0 warn
534 qq|unknown scheme '$scheme'!|;
535 0 return;
536 }
537
538 0 return $scheme;
539
540 } # http_scheme_known
541  
542 #register http_scheme_known => \&http_scheme_known;
543  
544 =item http_username - gets or sets the name of the authenticated user
545  
546 WARNING: setting the username will issue a "SECURITY BREACH" warning. You
547 rarely want to impersonate another user.
548  
549 $my username = http_username;
550 http_username('new name');
551 http_username 'new name';
552  
553 If not inside an authenticated route (there is no authenticated user),
554 C< http_username > returns undef.
555  
556 =cut
557  
558 3 ▶sub http_username {
Subroutine http_username: called CC=4 SCAR=13.9
Pod documented
559 3 my $dsl = shift;
560
561 3 ▶ unless ( exists $dsl->vars->{http_username} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $dsl->vars->{"http_username"}) 2 1 3
562 2 $dsl->app->log( warning =>
563 qq|'http_username' should only be used in an authenticated route|
564 );
565 }
566
567 3 ▶ if (@_ == 1) { # CAUTION: use with care
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 1 2 3
568 1 $dsl->vars->{http_username} = shift;
569 my $message
570 = qq|POTENTIONAL SECURITY BREACH: |
571 . qq|"impersonating different user: '|
572 . $dsl->vars->{http_username}
573 1 . qq|'"|;
574 1 warn $message;
575 1 $dsl->app->log ( warning => $message );
576 }
577
578 3 ▶ return unless exists $dsl->vars->{http_username};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $dsl->vars->{"http_username"} 1 2 3
579 2 return $dsl->vars->{http_username};
580
581 } # http_username
582  
583 5 register http_username => \&http_username;
584  
585 =item http_realm - gets or sets the real of the current request
586  
587 WARNING: setting the realm will issue a "SECURITY BREACH" warning. You
588 rarely want to switch to another realm
589  
590 $my realm = http_realm;
591 http_realm('new name');
592 http_realm 'new name';
593  
594 If not inside an authenticated route (there is no authenticated user),
595 C< http_realm > returns undef.
596  
597 =cut
598  
599 3 ▶sub http_realm {
Subroutine http_realm: called CC=4 SCAR=13.9
Pod documented
600 3 my $dsl = shift;
601
602 3 ▶ unless ( exists $dsl->vars->{http_realm} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $dsl->vars->{"http_realm"}) 2 1 3
603 2 $dsl->app->log( warning =>
604 qq|'http_realm' should only be used in an authenticated route|
605 );
606 }
607
608 3 ▶ if (@_ == 1) { # CAUTION: use with care
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 1 2 3
609 1 $dsl->vars->{http_realm} = shift;
610 my $message
611 = qq|POTENTIONAL SECURITY BREACH: |
612 . qq|"switching to different realm: '|
613 . $dsl->vars->{http_realm}
614 1 . qq|'"|;
615 1 warn $message;
616 1 $dsl->app->log ( warning => $message );
617 }
618
619 3 ▶ return unless exists $dsl->vars->{http_realm};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $dsl->vars->{"http_realm"} 1 2 3
620 2 return $dsl->vars->{http_realm};
621
622 } # http_realm
623  
624 5 register http_realm => \&http_realm;
625  
626 =back
627  
628 =head2 SAMPLE CONFIGURATION
629  
630 In your application's configuation file:
631  
632 plugins:
633 HTTP::Auth::Extensible:
634 # Set to 1 if you want to disable the use of roles (0 is default)
635 disable_roles: 0
636 # After /login: If no return_url is given: land here ('/' is default)
637 user_home_page: '/user'
638 # After /logout: If no return_url is given: land here (no default)
639 exit_page: '/'
640
641 # List each authentication realm, with the provider to use and the
642 # provider-specific settings (see the documentation for the provider
643 # you wish to use)
644 realms:
645 realm_one:
646 provider: Database
647 db_connection_name: 'foo'
648
649 default_realm: realm_xxx
650 # If there is more than one realm, is needed if no 'realm' is
651 # specified in http_requires_authentication.
652  
653 B<Please note> that you B<not have to> have a session provider configured. The
654 authentication framework B<does not> require sessions in order to track information about
655 the currently logged in user.
656  
657 =cut
658  
659 {
660 # Given a realm, returns a configured and ready to use instance of the provider
661 # specified by that realm's config.
662 5 my %realm_provider;
663 0 ▶sub auth_provider {
Subroutine auth_provider: not called CC=7 SCAR=40.3
Pod undocumented
664 0 my $dsl = shift;
665 0 ▶ my $realm = shift || http_default_realm($dsl);
Condition0%0/3
shift() || http_default_realm($dsl)
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || http_default_realm($dsl)
shift() http_default_realm($dsl)
Truth table0%0/3
A || B
A= shift()B= http_default_realm($dsl)
ABresult
1X1
011
000
666
667 0 http_realm_exists($dsl, $realm); # can be in void, it dies when false
668  
669 # First, if we already have a provider for this realm, go ahead and use it:
670 0 ▶ return $realm_provider{$realm} if exists $realm_provider{$realm};
Branch: 1 branch
BranchTrueFalseTotal
if exists $realm_provider{$realm} 0 0 0
671  
672 # OK, we need to find out what provider this realm uses, and get an instance
673 # of that provider, configured with the settings from the realm.
674 0 ▶ my $realm_settings = plugin_setting->{realms}->{$realm}
Branch: 1 branch
BranchTrueFalseTotal
my $realm_settings = plugin_setting()->{"realms"}{$realm} or die "Invalid realm $realm" 0 0 0
675 or die "Invalid realm $realm";
676 my $provider_class = $realm_settings->{provider}
677 0 ▶ or die "No provider configured - consult documentation for "
Branch: 1 branch
BranchTrueFalseTotal
my $provider_class = $realm_settings->{"provider"} or die "No provider configured - consult documentation for Dancer2::Plugin::Auth::Extensible" 0 0 0
678 . "Dancer2::Plugin::Auth::Extensible";
679  
680 0 ▶ if ($provider_class !~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($provider_class =~ /::/) 0 0 0
681 0 $provider_class = "Dancer2::Plugin::Auth::Extensible" . "::Provider::$provider_class";
682 }
683 0 my ($ok, $error) = try_load_class($provider_class);
684  
685 0 ▶ if (! $ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
686 0 die "Cannot load provider $provider_class: $error";
687 }
688  
689 0 return $realm_provider{$realm} = $provider_class->new($realm_settings);
690 }
691 }
692  
693 5 register_hook qw(http_authentication_required http_permission_denied);
694 5 register_plugin for_versions => [qw(1 2)];
695  
696  
697 # Given a class method name and a set of parameters, try calling that class
698 # method for each realm in turn, arranging for each to receive the configuration
699 # defined for that realm, until one returns a non-undef, then return the realm which
700 # succeeded and the response.
701 # Note: all provider class methods return a single value; if any need to return
702 # a list in future, this will need changing)
703 0 ▶sub _try_realms {
Subroutine _try_realms: not called CC=4 SCAR=30.0
704 0 my ($method, @args);
705 0 for my $realm (keys %{ plugin_setting->{realms} }) {
706 0 my $provider = auth_provider($realm);
707 0 ▶ if (!$provider->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($provider->can($method)) 0 0 0
708 0 die "Provider $provider does not provide a $method method!";
709 }
710 0 ▶ if (defined(my $result = $provider->$method(@args))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $result = $provider->$method(@args))) 0 0 0
711 0 return $result;
712 }
713 }
714 0 return;
715 }
716  
717 4 ▶on_plugin_import {
Subroutine __ANON__: called CC=1 SCAR=0.0
718 4 my $dsl = shift;
719 4 my $app = $dsl->app;
720  
721 5 };
722  
723  
724 # Replacement for much maligned and misunderstood smartmatch operator
725 0 ▶sub _smart_match {
Subroutine _smart_match: not called CC=4 SCAR=30.0
726 0 my ($got, $want) = @_;
727 0 ▶ if (!ref $want) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $want) { } 0 0 0
elsif (ref $want eq 'Regexp') { } 0 0 0
elsif (ref $want eq 'ARRAY') { } 0 0 0
728 0 return $got eq $want;
729 } elsif (ref $want eq 'Regexp') {
730 0 return $got =~ $want;
731 } elsif (ref $want eq 'ARRAY') {
732 0 return grep { $_ eq $got } @$want;
733 } else {
734 0 carp "Don't know how to match against a " . ref $want;
735 }
736 }
737  
738  
739  
740  
741 =head1 AUTHOR
742  
743 Theo van Hoesel, C<< <Th.J.v.Hoesel at THEMA-MEDIA dot nl> >>
744  
745 HTTP Autneticate implementation based on:
746  
747 David Precious, C<< <davidp at preshweb.co.uk> >>
748  
749 Dancer2 port of Dancer::Plugin::Auth::Extensible by:
750  
751 Stefan Hornburg (Racke), C<< <racke at linuxia.de> >>
752  
753 =head1 BUGS / FEATURE REQUESTS
754  
755 This is an early version; there may still be bugs present or features missing.
756  
757 This is developed on GitHub - please feel free to raise issues or pull requests
758 against the repo at:
759 L<https://github.com/THEMA-MEDIA/Dancer2-Plugin-HTTP-Auth-Extensible>
760  
761  
762  
763 =head1 ACKNOWLEDGEMENTS
764  
765 Valuable feedback on the early design of this module came from many people,
766 including Matt S Trout (mst), David Golden (xdg), Damien Krotkine (dams),
767 Daniel Perrett, and others.
768  
769 Configurable login/logout URLs added by Rene (hertell)
770  
771 Regex support for require_role by chenryn
772  
773 Support for user_roles looking in other realms by Colin Ewen (casao)
774  
775 LDAP provider added by Mark Meyer (ofosos)
776  
777 Config options for default login/logout handlers by Henk van Oers (hvoers)
778  
779 =head1 LICENSE AND COPYRIGHT
780  
781  
782 Copyright 2014 THEMA-MEDIA, Th.J. van Hoesel
783  
784 This program is free software; you can redistribute it and/or modify it
785 under the terms of either: the GNU General Public License as published
786 by the Free Software Foundation; or the Artistic License.
787  
788 See http://dev.perl.org/licenses/ for more information.
789  
790  
791 =cut
792  
793 5 1; # End of Dancer2::Plugin::HTTP::Auth::Extensible
↑ Summary