File Coverage

blib/lib/Dancer2/Plugin/Auth/Extensible/Provider/Config.pm
Criterion Covered Total %
statement 29 29 100.0
branch 10 10 100.0
condition 3 3 100.0
subroutine 9 9 100.0
pod 3 3 100.0
total 54 54 100.0


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::Auth::Extensible::Provider::Config;
2              
3 8     8   836937 use Carp qw/croak/;
  8         19  
  8         556  
4 8     8   81 use Dancer2::Core::Types qw/ArrayRef/;
  8         22  
  8         116  
5 8     8   10586 use List::Util qw/first/;
  8         20  
  8         491  
6 8     8   52 use Moo;
  8         18  
  8         50  
7             with "Dancer2::Plugin::Auth::Extensible::Role::Provider";
8 8     8   4747 use namespace::clean;
  8         36524  
  8         56  
9              
10             our $VERSION = '0.709';
11              
12             =head1 NAME
13              
14             Dancer2::Plugin::Auth::Extensible::Provider::Config - example auth provider using app config
15              
16              
17             =head1 DESCRIPTION
18              
19             This is a simple authentication provider which authenticates based on a list of
20             usernames, passwords (crypted, preferably - see below) and role specifications
21             provided in the realm definition in your app's config file.
22              
23             This class is primarily intended as an example of what an authentication
24             provider class should do; however, if you just want simple user authentication
25             with user details stored in your app's config file, it may well suit your needs.
26              
27             See L<Dancer2::Plugin::Auth::Extensible> for details on how to use the
28             authentication framework.
29              
30             =head1 SYNOPSIS
31              
32             In your app's C<config.yml>:
33              
34             plugins:
35             Auth::Extensible:
36             realms:
37             config:
38             provider: Config
39             users:
40             - user: dave
41             pass: supersecret
42             roles:
43             - Developer
44             - Manager
45             - BeerDrinker
46             - user: bob
47             pass: '{SSHA}+2u1HpOU7ak6iBR6JlpICpAUvSpA/zBM'
48             roles:
49             - Tester
50              
51             As you can see, you can define the usernames, passwords (please use crypted
52             passwords, RFC2307-style, not plain text (although plain text *is* supported,
53             but really not a good idea), and the roles for each user (if you're
54             not planning to use roles, omit the roles section from each user entirely).
55              
56             =head1 ATTRIBUTES
57              
58             =head2 users
59              
60             Array reference containing users from configuration.
61              
62             =cut
63              
64             has users => (
65             is => 'ro',
66             isa => ArrayRef,
67             required => 1,
68             );
69              
70             =head1 METHODS
71              
72             =head2 authenticate_user $username, $password
73              
74             =cut
75              
76             sub authenticate_user {
77 175     175 1 3617 my ($self, $username, $password) = @_;
78              
79 175 100 100     1685 croak "username and password must be defined"
80             unless defined $username && defined $password;
81              
82 169 100       578 my $user_details = $self->get_user_details($username) or return;
83 58         372 return $self->match_password($password, $user_details->{pass});
84             }
85              
86             =head2 get_user_details $username
87              
88             =cut
89              
90             # Just return the whole user definition from the config; this way any additional
91             # fields defined for users will just get passed through.
92             sub get_user_details {
93 381     381 1 13075 my ($self, $username) = @_;
94              
95 381 100       1192 croak "username must be defined"
96             unless defined $username;
97              
98             my $user = first {
99 1004     1004   2406 $_->{user} eq $username
100 379         1539 } @{ $self->users };
  379         1752  
101 379         1736 return $user;
102             }
103              
104             =head2 get_user_roles $username
105              
106             =cut
107              
108             sub get_user_roles {
109 37     37 1 107 my ($self, $username) = @_;
110              
111 37 100       300 croak "username must be defined"
112             unless defined $username;
113              
114 35 100       93 my $user_details = $self->get_user_details($username) or return;
115 31         113 return $user_details->{roles};
116             }
117              
118             1;