File Coverage

blib/lib/Authen/Pluggable.pm
Criterion Covered Total %
statement 46 53 86.7
branch 7 12 58.3
condition 4 12 33.3
subroutine 6 7 85.7
pod 2 3 66.6
total 65 87 74.7


line stmt bran cond sub pod time code
1             package Authen::Pluggable;
2             $Authen::Pluggable::VERSION = '0.01';
3 5     5   279464 use Mojo::Base -base, -signatures;
  5         797960  
  5         34  
4 5     5   17773 use Mojo::Loader qw/load_class/;
  5         148021  
  5         3094  
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 3     3 1 1337 sub provider($s, $provider, $plugin=undef) {
  3         5  
  3         4  
  3         5  
  3         5  
16 3   66     12 $plugin //= $provider;
17 3         8 my %v = (provider => $plugin);
18             $s->_load_provider($provider, provider => $plugin)
19 3 50       9 unless exists($s->_providers->{$provider});
20 3         8 return $s->_providers->{$provider};
21             }
22              
23 1     1 1 261 sub providers ( $s, @providers ) {
  1         3  
  1         2  
  1         1  
24 1         3 foreach my $provider (@providers) {
25 1 50       4 $provider = { $provider => undef } if (ref($provider) ne 'HASH');
26 1         5 while ( my ( $k, $v ) = each %$provider ) {
27 2         15 $s->_load_provider( $k, %$v );
28             }
29             }
30 1         13 return $s;
31             }
32              
33 5     5   12 sub _load_provider ( $s, $provider, %cfg ) {
  5         7  
  5         7  
  5         9  
  5         5  
34 5   33     14 my $class = delete($cfg{provider}) // $provider;
35 5         12 $class = __PACKAGE__ . "::$class";
36 5 50       21 unless ( my $e = load_class $class ) {
37 5   33     98 $s->_providers->{$provider} //= $class->new( parent => $s );
38 5 100       186 $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 0 3437 sub authen ( $s, $user, $pass ) {
  7         11  
  7         9  
  7         10  
  7         7  
45 7         8 foreach my $provider ( keys %{ $s->_providers } ) {
  7         17  
46 9         50 my $uinfo = $s->_providers->{$provider}->authen( $user, $pass );
47 9 100       26871 $uinfo && do {
48 5         10 $uinfo->{provider} = $provider;
49 5         17 return $uinfo;
50             }
51             }
52              
53 2         7 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.01
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 C<50-alias.t> in test folder for an example with two
144             different password files
145              
146             It always return the object itself.
147              
148             =head1 EXAMPLE FOR CONFIGURING PROVIDERS
149              
150             There are various methods to select the providers where autenticate and to configure it.
151             Here some example using chaining.
152              
153             This load and configure Passwd plugin
154              
155             $auth->provider('Passwd')->cfg(
156             'file' => ...
157             );
158              
159             This load and confgure AD plugin
160              
161             $auth->provider('AD')->cfg(%opt)
162              
163             Multiple configuration at one time via autoloaded methods
164              
165             $auth->providers( 'Passwd', 'AD' )
166             ->Passwd->cfg('file' => ...)
167             ->AD->cfg(%opt);
168              
169             Same but via providers hashref configuration
170              
171             $auth->providers({
172             'Passwd' => { 'file' => ... },
173             'AD' => \%opt,
174             });
175              
176             =head1 BUGS/CONTRIBUTING
177              
178             Please report any bugs through the web interface at L
179              
180             If you want to contribute changes or otherwise involve yourself in development, feel free to fork the Git repository from
181             L.
182              
183             =head1 SUPPORT
184              
185             You can find this documentation with the perldoc command too.
186              
187             perldoc Authen::Pluggable
188              
189             =head1 AUTHOR
190              
191             Emiliano Bruni
192              
193             =head1 COPYRIGHT AND LICENSE
194              
195             This software is copyright (c) 2022 by Emiliano Bruni.
196              
197             This is free software; you can redistribute it and/or modify it under
198             the same terms as the Perl 5 programming language system itself.
199              
200             =cut
201              
202             __END__