Extensible/Provider/Config.pm

filter: Statementstmt 100% 33 / 33 Branchbran 100% 10 / 10 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 100% 60 / 60 CC 7 SCAR 8.5
CC
7
Cov
100%
authenticate_user
4.0
get_user_roles
3.0
get_user_details
2.0
CRAP 2.3
« Extensible.pm ↑ Summary Extensible/Test/App.pm »
1 package Dancer2::Plugin::Auth::Extensible::Provider::Config;
2  
3 11 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Dancer2::Core::Types qw/ArrayRef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 with "Dancer2::Plugin::Auth::Extensible::Role::Provider";
8 11 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 our $VERSION = '0.714';
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 11 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 187 ▶sub authenticate_user {
Subroutine authenticate_user: called CC=4 SCAR=13.9
Pod documented
77 187 my ($self, $username, $password) = @_;
78  
79 187 ▶ croak "username and password must be defined"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $username and defined $password 6 181 187
Condition100%3/3
defined $username and defined $password
!ll&&!rl&&r
42181
MC/DC100%2/2
defined $username and defined $password
defined $username defined $password
Truth table100%3/3
A and B
A= defined $usernameB= defined $password
ABresult
0X0
100
111
80 unless defined $username && defined $password;
81  
82 181 ▶ my $user_details = $self->get_user_details($username) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $user_details = $self->get_user_details($username) or return 62 119 181
83 62 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 405 ▶sub get_user_details {
Subroutine get_user_details: called CC=2 SCAR=6.9
Pod documented
93 405 my ($self, $username) = @_;
94  
95 405 ▶ croak "username must be defined"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $username 2 403 405
96 unless defined $username;
97  
98 1044 ▶ my $user = first {
Subroutine __ANON__: called CC=1 SCAR=0.0
99 1044 $_->{user} eq $username
100 403 } @{ $self->users };
101 403 return $user;
102 }
103  
104 =head2 get_user_roles $username
105  
106 =cut
107  
108 37 ▶sub get_user_roles {
Subroutine get_user_roles: called CC=3 SCAR=11.0
Pod documented
109 37 my ($self, $username) = @_;
110  
111 37 ▶ croak "username must be defined"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $username 2 35 37
112 unless defined $username;
113  
114 35 ▶ my $user_details = $self->get_user_details($username) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $user_details = $self->get_user_details($username) or return 31 4 35
115 31 return $user_details->{roles};
116 }
117  
118 11 1;
« Extensible.pm ↑ Summary Extensible/Test/App.pm »