File Coverage

blib/lib/Articulate/Authentication.pm
Criterion Covered Total %
statement 12 27 44.4
branch 0 6 0.0
condition n/a
subroutine 4 6 66.6
pod 2 2 100.0
total 18 41 43.9


line stmt bran cond sub pod time code
1             package Articulate::Authentication;
2 4     4   5386 use strict;
  4         11  
  4         171  
3 4     4   23 use warnings;
  4         8  
  4         124  
4              
5 4     4   21 use Moo;
  4         6  
  4         28  
6             with 'Articulate::Role::Component';
7 4     4   1934 use Articulate::Syntax qw(credentials instantiate_array);
  4         10  
  4         39  
8              
9             =head1 NAME
10              
11             Articulate::Authentication - determine if a user who they claim to be
12              
13             =head1 SYNOPSIS
14              
15             # in config:
16             components:
17             authentication:
18             Articulate::Authentication:
19             providers:
20             - Articulate::Authentication::AlwaysAllow
21              
22             # then any component can dp
23             $component->authentication->login($credentials);
24             $component->authentication->login($user_id, $password);
25              
26             =head1 ATTRIBUTE
27              
28             =head3 providers
29              
30             A list of providers which can respond to C.
31              
32             =cut
33              
34             has providers =>
35             is => 'rw',
36             default => sub { [] },
37             coerce => sub { instantiate_array(@_) };
38              
39             =head3 login
40              
41             $authentication->login($credentials);
42             $authentication->login( $user_id, $password );
43              
44             Asks each provider if the credentials supplied match a known user. Credentials may be in whatever form will satisfy the C function in L (username and password, hashref or credentials object).
45              
46             Each provider must respond true, false, or undef. A true value means the user is authenticated. A false value means that the user exists but is explicitly refused access (this should only be used in exceptional circumstances) and an undef value means the user cannot be authenticated by the provider (but could be authenticated by some other provider).
47              
48             =cut
49              
50             sub login {
51 0     0 1   my $self = shift;
52 0           my $credentials = credentials @_;
53 0           foreach my $provider ( @{ $self->providers } ) {
  0            
54 0 0         return $credentials if $provider->authenticate( $credentials );
55 0 0         return $credentials if $credentials->rejected;
56             }
57 0           return $credentials->deny('No provider authenticated these credentials');
58             }
59              
60             =head3 create_user
61              
62             $authentication->create_user( $user_id, $password );
63              
64             Requests that a new user is created. Each provider must respond true, false, or undef.
65              
66             =cut
67              
68             sub create_user {
69 0     0 1   my $self = shift;
70 0           my $user_id = shift;
71 0           my $password = shift;
72 0           foreach my $provider ( @{ $self->providers } ) {
  0            
73 0 0         if (
74             defined ( $provider->create_user( $user_id, $password ) )
75             ) {
76 0           return ($user_id);
77             }
78             }
79 0           return (undef);
80             }
81              
82             =head1 SEE ALSO
83              
84             =over
85              
86             =item * L
87              
88             =item * L
89              
90             =back
91              
92             =cut
93              
94             1;