File Coverage

blib/lib/Authen/Pluggable.pm
Criterion Covered Total %
statement 46 53 86.7
branch 8 12 66.6
condition 4 12 33.3
subroutine 6 7 85.7
pod 3 3 100.0
total 67 87 77.0


line stmt bran cond sub pod time code
1             package Authen::Pluggable;
2             $Authen::Pluggable::VERSION = '0.02';
3 6     6   743157 use Mojo::Base -base, -signatures;
  6         774680  
  6         39  
4 6     6   17322 use Mojo::Loader qw/load_class/;
  6         143224  
  6         3644  
5              
6             has '_providers' => sub { return {} };
7             has 'log';
8              
9 0     0   0 sub AUTOLOAD ($s) {
  0         0  
  0         0  
10 0         0 our $AUTOLOAD;
11 0         0 $AUTOLOAD =~ s/.*:://;
12 0         0 return $s->_providers->{$AUTOLOAD};
13             }
14              
15 5     5 1 22035 sub provider($s, $provider, $plugin=undef) {
  5         8  
  5         8  
  5         10  
  5         5  
16 5   66     19 $plugin //= $provider;
17 5         13 my %v = (provider => $plugin);
18             $s->_load_provider($provider, provider => $plugin)
19 5 100       16 unless exists($s->_providers->{$provider});
20 5         16 return $s->_providers->{$provider};
21             }
22              
23 1     1 1 240 sub providers ( $s, @providers ) {
  1         2  
  1         2  
  1         2  
24 1         2 foreach my $provider (@providers) {
25 1 50       4 $provider = { $provider => undef } if (ref($provider) ne 'HASH');
26 1         6 while ( my ( $k, $v ) = each %$provider ) {
27 2         11 $s->_load_provider( $k, %$v );
28             }
29             }
30 1         8 return $s;
31             }
32              
33 6     6   13 sub _load_provider ( $s, $provider, %cfg ) {
  6         9  
  6         8  
  6         11  
  6         7  
34 6   33     25 my $class = delete($cfg{provider}) // $provider;
35 6         16 $class = __PACKAGE__ . "::$class";
36 6 50       21 unless ( my $e = load_class $class ) {
37 6   33     98 $s->_providers->{$provider} //= $class->new( parent => $s );
38 6 100       217 $s->_providers->{$provider}->cfg(%cfg) if (%cfg);
39             } else {
40 0 0 0     0 ( $s->log && $s->log->error($e) ) || croak $e;
41             }
42             }
43              
44 7     7 1 3258 sub authen ( $s, $user, $pass ) {
  7         9  
  7         12  
  7         8  
  7         8  
45 7         8 foreach my $provider ( keys %{ $s->_providers } ) {
  7         15  
46 9         47 my $uinfo = $s->_providers->{$provider}->authen( $user, $pass );
47 9 100       26186 $uinfo && do {
48 5         11 $uinfo->{provider} = $provider;
49 5         13 return $uinfo;
50             }
51             }
52              
53 2         5 return undef;
54             }
55              
56             1;
57              
58             =pod
59              
60             =head1 NAME
61              
62             Authen::Pluggable - A Perl module to authenticate users via pluggable modules
63              
64             =for html

65            
66             github workflow tests
67            
68             Top language:
69             github last commit
70            

71              
72             =head1 VERSION
73              
74             version 0.02
75              
76             =head1 SYNOPSIS
77              
78             use Authen::Pluggable;
79              
80             my $auth = Authen::Pluggable->new();
81             $auth->provider('plugin1','plugin2');
82             $auth->plugin1->cfg({...});
83             $auth->plugin2->cfg({...});
84              
85             my $user_info = $auth->authen($username, $password) || die "Login failed";
86              
87             =head1 DESCRIPTION
88              
89             Authen::Pluggable is a Perl module to authenticate users via pluggable modules
90              
91             Every plugin class is in namespace C so you must omit it
92              
93             =encoding UTF-8
94              
95             =head1 METHODS
96              
97             =head2 new
98              
99             This method takes a hash of parameters. The following options are valid:
100              
101             =over
102              
103             =item log
104              
105             Any object that supports debug, info, error and warn.
106              
107             log => Log::Log4perl->get_logger('Authen::Simple::LDAP')
108              
109             =back
110              
111             =head2 provider($provider, $plugin [opt])
112              
113             If C<$plugin> is omitted C is loaded.
114             If C<$plugin> is set C is loaded with
115             C<$provider> as alias.
116              
117             It return the plugin object.
118              
119             =head2 providers(@providers)
120              
121             If C<@providers> items are scalar, they are considered as plugin name and they
122             are loaded. Else they can be hashref items. The hash key is considered as
123             plugin name if there isn't a provider key inside else it's considered as
124             alias name while provider key are considered as plugin name.
125              
126             $auth->providers('plugin1', 'plugin2')
127              
128             loads C and C
129              
130             $auth->providers(
131             { alias1 => {
132             provider => 'plugin1',
133             ... other configurations ...
134             },
135             alias2 => {
136             provider => 'plugin1',
137             ... other configurations ...
138             }
139             }
140             ),
141              
142             loads C two times, one with provider name C and
143             one with C. See L in test folder for an example with two
144             different password files
145              
146             It always return the object itself.
147              
148             =head2 authen($username, $password)
149              
150             Call all configured providers and return the first with a valid authentication.
151              
152             The structure returned is usually something like this
153              
154             { provider => $provider, user => $user, cn => $cn, gid => $gid };
155              
156             where C<$provider> is the alias of the provider which return the valid
157             authentication and C<$cn> is the common name of the user.
158              
159             If no plugins return a valid authentication, this method returns undef.
160              
161             =head1 EXAMPLE FOR CONFIGURING PROVIDERS
162              
163             There are various methods to select the providers where autenticate and to configure it.
164             Here some example using chaining.
165              
166             This load and configure Passwd plugin
167              
168             $auth->provider('Passwd')->cfg(
169             'file' => ...
170             );
171              
172             This load and confgure AD plugin
173              
174             $auth->provider('AD')->cfg(%opt)
175              
176             Multiple configuration at one time via autoloaded methods
177              
178             $auth->providers( 'Passwd', 'AD' )
179             ->Passwd->cfg('file' => ...)
180             ->AD->cfg(%opt);
181              
182             Same but via providers hashref configuration
183              
184             $auth->providers({
185             'Passwd' => { 'file' => ... },
186             'AD' => \%opt,
187             });
188              
189             =head1 BUGS/CONTRIBUTING
190              
191             Please report any bugs through the web interface at L
192              
193             If you want to contribute changes or otherwise involve yourself in development, feel free to fork the Git repository from
194             L.
195              
196             =head1 SUPPORT
197              
198             You can find this documentation with the perldoc command too.
199              
200             perldoc Authen::Pluggable
201              
202             =head1 AUTHOR
203              
204             Emiliano Bruni
205              
206             =head1 COPYRIGHT AND LICENSE
207              
208             This software is copyright (c) 2022 by Emiliano Bruni.
209              
210             This is free software; you can redistribute it and/or modify it under
211             the same terms as the Perl 5 programming language system itself.
212              
213             =cut
214              
215             __END__