File Coverage

blib/lib/PlugAuth/Role/Auth.pm
Criterion Covered Total %
statement 22 30 73.3
branch 6 14 42.8
condition n/a
subroutine 8 10 80.0
pod 6 6 100.0
total 42 60 70.0


line stmt bran cond sub pod time code
1             package PlugAuth::Role::Auth;
2              
3 41     41   33697 use strict;
  41         91  
  41         1646  
4 41     41   258 use warnings;
  41         81  
  41         1604  
5 41     41   318 use Role::Tiny;
  41         87  
  41         372  
6              
7             # ABSTRACT: Role for PlugAuth authentication plugins
8             our $VERSION = '0.35'; # VERSION
9              
10              
11             requires qw( check_credentials );
12              
13              
14 10     10 1 217 sub all_users { () }
15              
16              
17             sub create_user
18             {
19 6     6 1 29 my $next_auth = shift->next_auth;
20 6 50       23 return 0 unless defined $next_auth;
21 6         37 $next_auth->create_user(@_);
22             }
23              
24              
25             sub _find_create_user_cb
26             {
27 3     3   8 my $self = shift;
28 3 50       36 return $self if $self->can('create_user_cb');
29 0         0 my $next = $self->next_auth;
30 0 0       0 return $next ? $next->_find_create_user_cb : ();
31             }
32              
33              
34             sub change_password
35             {
36 0     0 1 0 my $next_auth = shift->next_auth;
37 0 0       0 return 0 unless defined $next_auth;
38 0         0 $next_auth->change_password(@_);
39             }
40              
41              
42             sub delete_user
43             {
44 0     0 1 0 my $next_auth = shift->next_auth;
45 0 0       0 return 0 unless defined $next_auth;
46 0         0 $next_auth->delete_user(@_);
47             }
48              
49              
50             sub next_auth
51             {
52 293     293 1 585 my($self, $new_value) = @_;
53 293 100       981 $self->{next_auth} = $new_value if defined $new_value;
54 293         897 $self->{next_auth};
55             }
56              
57              
58             sub deligate_check_credentials
59             {
60 45     45 1 127 my($self, $user, $pass) = @_;
61 45         191 my $next_auth = $self->next_auth;
62 45 100       336 return 0 unless defined $next_auth;
63 7         84 return $next_auth->check_credentials($user, $pass);
64             }
65              
66             around all_users => sub {
67             my($orig, $self) = @_;
68             my $next_auth = $self->next_auth;
69             return $orig->($self) unless defined $next_auth;
70             return ($orig->($self), $next_auth->all_users);
71             };
72              
73             1;
74              
75             __END__
76              
77             =pod
78              
79             =encoding UTF-8
80              
81             =head1 NAME
82              
83             PlugAuth::Role::Auth - Role for PlugAuth authentication plugins
84              
85             =head1 VERSION
86              
87             version 0.35
88              
89             =head1 SYNOPSIS
90              
91             package PlugAuth::Plugin::MyAuth;
92            
93             use Role::Tiny::With;
94            
95             with 'PlugAuth::Role::Plugin';
96             with 'PlugAuth::Role::Auth';
97            
98             # accept user = larry and pass = wall only.
99             sub check_credentials {
100             my($self, $user, $pass) = @_;
101             return 1 if $user eq 'larry' && $pass eq 'wall';
102             return $self->deligate_check_credentials($user, $pass);
103             }
104            
105             # only one user, larry
106             sub all_users { qw( larry ) }
107            
108             1;
109              
110             =head1 DESCRIPTION
111              
112             Use this role when writing PlugAuth plugins that manage
113             authentication (ie. determine the identify of the user).
114              
115             =head1 REQUIRED ABSTRACT METHODS
116              
117             These methods must be implemented by your class.
118              
119             =head2 $plugin-E<gt>check_credentials( $user, $pass )
120              
121             Return 1 if the password is correct for the given user.
122              
123             Return 0 otherwise.
124              
125             =head1 OPTIONAL ABSTRACT METHODS
126              
127             These methods may be implemented by your class.
128              
129             =head2 $plugin-E<gt>all_users
130              
131             Returns the list of all users known to your plugin. If
132             this cannot be determined, then return an empty list.
133              
134             =head2 $plugin-E<gt>create_user( $user, $password )
135              
136             Create the given user with the given password. Return 1
137             on success, return 0 on failure.
138              
139             =head2 $plugin-E<gt>create_user_cb( $user, $password, $cb )
140              
141             Create user with call back. This works like C<create_user>, but
142             it calls the callback while your plugin still has a lock on the
143             user database (if applicable). If this method is implemented,
144             then L<PlugAuth> can create users who belong to specific groups
145             as one atomic action. If you do not implement this method then
146             the server will return 501 Not Implemented.
147              
148             =head2 $plugin-E<gt>change_password( $user, $password )
149              
150             Change the password of the given user. Return 1 on
151             success, return 0 on failure.
152              
153             =head2 $plugin-E<gt>delete_user( $user )
154              
155             Delete the given user. Return 1 on success, return 0 on failure.
156              
157             =head1 METHODS
158              
159             =head2 $plugin-E<gt>next_auth
160              
161             Returns the next authentication plugin. May be undef if
162             there is no next authentication plugin.
163              
164             =head2 $plugin-E<gt>deligate_check_credentials( $user, $pass )
165              
166             Delegate to the next authentication plugin. Call this method if your plugins
167             authentication has failed if your plugin is not authoritative.
168              
169             =head1 SEE ALSO
170              
171             L<PlugAuth>,
172             L<PlugAuth::Guide::Plugin>,
173             L<Test::PlugAuth::Plugin::Auth>
174              
175             =head1 AUTHOR
176              
177             Graham Ollis <gollis@sesda3.com>
178              
179             =head1 COPYRIGHT AND LICENSE
180              
181             This software is copyright (c) 2012 by NASA GSFC.
182              
183             This is free software; you can redistribute it and/or modify it under
184             the same terms as the Perl 5 programming language system itself.
185              
186             =cut