File Coverage

blib/lib/Dancer/Plugin/Auth/Extensible/Provider/Config.pm
Criterion Covered Total %
statement 17 17 100.0
branch 3 4 75.0
condition n/a
subroutine 5 5 100.0
pod 0 3 0.0
total 25 29 86.2


line stmt bran cond sub pod time code
1             package Dancer::Plugin::Auth::Extensible::Provider::Config;
2              
3 1     1   1151 use strict;
  1         4  
  1         61  
4 1     1   9 use base "Dancer::Plugin::Auth::Extensible::Provider::Base";
  1         2  
  1         903  
5              
6             =head1 NAME
7              
8             Dancer::Plugin::Auth::Extensible::Config - example auth provider using app config
9              
10              
11             =head1 DESCRIPTION
12              
13             This is a simple authentication provider which authenticates based on a list of
14             usernames, passwords (crypted, preferably - see below) and role specifications
15             provided in the realm definition in your app's config file.
16              
17             This class is primarily intended as an example of what an authentication
18             provider class should do; however, if you just want simple user authentication
19             with user details stored in your app's config file, it may well suit your needs.
20              
21             See L for details on how to use the
22             authentication framework.
23              
24             =head1 SYNOPSIS
25              
26             In your app's C:
27              
28             plugins:
29             Auth::Extensible:
30             realms:
31             config:
32             provider: Config
33             users:
34             - user: dave
35             pass: supersecret
36             roles:
37             - Developer
38             - Manager
39             - BeerDrinker
40             - user: bob
41             pass: '{SSHA}+2u1HpOU7ak6iBR6JlpICpAUvSpA/zBM'
42             roles:
43             - Tester
44              
45             As you can see, you can define the usernames, passwords (please use crypted
46             passwords, RFC2307-style, not plain text (although plain text *is* supported,
47             but really not a good idea), and the roles for each user (if you're
48             not planning to use roles, omit the roles section from each user entirely).
49              
50             =cut
51              
52             sub authenticate_user {
53 8     8 0 20 my ($self, $username, $password) = @_;
54 8 100       27 my $user_details = $self->get_user_details($username) or return;
55 4         33 return $self->match_password($password, $user_details->{pass});
56             }
57              
58             # Just return the whole user definition from the config; this way any additional
59             # fields defined for users will just get passed through.
60             sub get_user_details {
61 34     34 0 51 my ($self, $username) = @_;
62 68         216 my ($user) = grep {
63 34         130 $_->{user} eq $username
64 34         54 } @{ $self->realm_settings->{users} };
65 34         142 return $user;
66             }
67              
68             sub get_user_roles {
69 9     9 0 13 my ($self, $username) = @_;
70              
71 9 50       22 my $user_details = $self->get_user_details($username) or return;
72 9         30 return $user_details->{roles};
73             }
74              
75             1;
76