blib/lib/Lemonldap/NG/Portal/Main/Plugin.pm

filter: Statementstmt 59.0% 39 / 66 Branchbran 20.0% 2 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 77.7% 14 / 18 Podpod 0.0% 0 / 10 totaltotal 51.3% 56 / 109 CC 8 SCAR 17.2
CC
8
Cov
53%
createNotification
20.0
addSessionDataToRemember
12.0
_addRoute
2.0
CRAP 5.6
« blib/lib/Lemonldap/NG/Portal/Plugins/AutoSignin.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Main/Auth.pm »
1 # Base package for LLNG portal plugins. It adds somme wrapper to
2 # Lemonldap::NG::Handler::PSGI::Try (base of portal)
3 package Lemonldap::NG::Portal::Main::Plugin;
4  
5 445 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 445 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 445 ▶use HTML::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 445 use Lemonldap::NG::Portal::Main::Constants qw(
9 PE_OK
10 PE_INFO
11 PE_ERROR
12 445 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 445 our $VERSION = '2.23.0';
15  
16 445 extends 'Lemonldap::NG::Common::Module';
17  
18 sub sendError {
19 3 ▶ my $self = shift;
Subroutine sendError: called CC=1 SCAR=0.0
Pod undocumented
20 3 return $self->p->sendError(@_);
21 }
22  
23 sub sendJSONresponse {
24 9 ▶ my $self = shift;
Subroutine sendJSONresponse: called CC=1 SCAR=0.0
Pod undocumented
25 9 return $self->p->sendJSONresponse(@_);
26 }
27  
28 sub addAuthRoute {
29 6043 ▶ my $self = shift;
Subroutine addAuthRoute: called CC=1 SCAR=0.0
Pod undocumented
30 6043 return $self->_addRoute( 'addAuthRoute', @_ );
31 }
32  
33 sub addUnauthRoute {
34 3084 ▶ my $self = shift;
Subroutine addUnauthRoute: called CC=1 SCAR=0.0
Pod undocumented
35 3084 return $self->_addRoute( 'addUnauthRoute', @_ );
36 }
37  
38 sub addAuthRouteWithRedirect {
39 0 ▶ my $self = shift;
Subroutine addAuthRouteWithRedirect: not called CC=1 SCAR=6.9
Pod undocumented
40 0 return $self->_addRoute( 'addAuthRouteWithRedirect', @_ );
41 }
42  
43 sub _addRoute {
44 9127 ▶ my ( $self, $type, $word, $subName, $methods, $transform ) = @_;
Subroutine _addRoute: called CC=2 SCAR=7.0
45 $transform //= sub {
46 12103 ▶ my ($sub) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
47 12103 ▶ if ( ref $sub ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sub) { } 140 11963 12103
48 return sub {
49 83 ▶ shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
50 83 return $sub->( $self, @_ );
51 }
52 140 }
53 else {
54 return sub {
55 230 ▶ shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
56 230 return $self->$sub(@_);
57 }
58 11963 }
59 9127 ▶ };
Condition33%1/3
$transform //= sub {
l!l&&r!l&&!r
091270
MC/DC0%0/2
$transform //= sub { my($sub) = @_; if (ref $sub) { return sub { shift(); return &$sub($self, @_); } ; } else { return sub { shift(); return $self->$sub(@_); } ; } ; }
$transform sub { my($sub) = @_; if (ref $sub) { return sub { shift(); return &$sub($self, @_); } ; } else { return sub { shift(); return $self->$sub(@_); } ; } ; }
Truth table33%1/3
A //= B
A= $transformB= sub { my($sub) = @_; if (ref $sub) { return sub { shift(); return &$sub($self, @_); } ; } else { return sub { shift(); return $self->$sub(@_); } ; } ; }
ABresult
1X1
011
000
60 9127 $self->p->$type( $word, $subName, $methods, $transform );
61 9127 return $self;
62 }
63  
64 sub loadTemplate {
65 13 ▶ my $self = shift;
Subroutine loadTemplate: called CC=1 SCAR=0.0
Pod undocumented
66 13 return $self->p->loadTemplate(@_);
67 }
68  
69 sub displayTemplate {
70 0 ▶ my ( $self, $req, $template, $params ) = @_;
Subroutine displayTemplate: not called CC=1 SCAR=6.9
Pod undocumented
71 0 $self->logger->debug("Return $template template");
72 0 $req->info( $self->loadTemplate( $req, $template, params => $params ) );
73 0 return PE_INFO;
74 }
75  
76 sub createNotification {
77 0 ▶ my ( $self, $req, $uid, $date, $ref, $title, $msg ) = @_;
Subroutine createNotification: not called CC=4 SCAR=30.0
Pod undocumented
78 my $notifEngine = $self->p->loadedModules->{
79 0 'Lemonldap::NG::Portal::Plugins::Notifications'};
80  
81 0 $self->logger->debug("Loading Notifications plugin...");
82 0 ▶ return PE_ERROR unless $notifEngine;
Branch: 1 branch
BranchTrueFalseTotal
unless $notifEngine 0 0 0
83  
84 # Prepare notification
85 my $content =
86 $self->conf->{oldNotifFormat}
87 0 ▶ ? '<?xml version="1.0" encoding="UTF-8"?><root><notification uid="_uid_" date="_date_" reference="_ref_"><title>_title_</title><text>_msg_</text></notification></root>'
Branch: 1 branch
BranchTrueFalseTotal
$self->conf->{'oldNotifFormat'} ? : 0 0 0
88 : '[{"uid":"_uid_","date":"_date_","title":"_title_","reference":"_ref_","text":"_msg_"}]';
89 0 $content =~ s/_uid_/$uid/;
90 0 $content =~ s/_date_/$date/;
91 0 $content =~ s/_ref_/$ref/;
92 0 $content =~ s/_title_/$title/;
93 0 $content =~ s/_msg_/$msg/;
94  
95 0 ▶ if ( $notifEngine->module->notifObject->newNotification($content) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notifEngine->module->notifObject->newNotification($content)) { } 0 0 0
96 0 $self->logger->debug("Notification $ref successfully created");
97 0 $self->auditLog(
98 $req,
99 message =>
100 ("Notification $ref / $date successfully created for $uid"),
101 code => "NOTIFICATION_CREATED",
102 ref => $ref,
103 date => $date,
104 user => $uid,
105 );
106 0 return PE_OK;
107 }
108 else {
109 0 $self->logger->debug("Notification $ref NOT created!");
110 0 return PE_ERROR;
111 }
112 }
113  
114 sub addSessionDataToRemember {
115 0 ▶ my ( $self, $newData ) = @_;
Subroutine addSessionDataToRemember: not called CC=3 SCAR=24.8
Pod undocumented
116 0 ▶ for my $sessionAttr ( keys %{ $newData || {} } ) {
Branch: 1 branch
BranchTrueFalseTotal
$newData or {} 0 0 0
117 $self->p->pluginSessionDataToRemember->{$sessionAttr} =
118 0 $newData->{$sessionAttr};
119 }
120 0 return;
121 }
122  
123 sub addEntryPoint {
124 1980 ▶ my ( $self, %entryPointDescription ) = @_;
Subroutine addEntryPoint: called CC=1 SCAR=0.0
Pod undocumented
125 1980 return $self->p->_addPluginEntryPoint( %entryPointDescription,
126 _pkg => ref($self) );
127 }
128  
129 445 1;
130 __END__
131  
132 =pod
133  
134 =encoding utf8
135  
136 =head1 NAME
137  
138 Lemonldap::NG::Portal::Main::Plugin - Base class for
139 L<Lemonldap::NG::Portal> modules I<(plugins, authentication modules,...)>.
140  
141 =head1 SYNOPSIS
142  
143 package Lemonldap::NG::Portal::My::Plugin;
144 use Mouse;
145 extends 'Lemonldap::NG::Portal::Main::Plugin';
146
147 use constant beforeAuth => 'verifyIP';
148
149 sub init {
150 my ($self) = @_;
151 $self->addUnauthRoute( mypath => 'hello', [ 'GET', 'PUT' ] );
152 $self->addAuthRoute( mypath => 'welcome', [ 'GET', 'PUT' ] );
153 return 1;
154 }
155 sub verifyIP {
156 my ($self, $req) = @_;
157 return PE_ERROR if($req->address !~ /^10/);
158 return PE_OK;
159 }
160 sub hello {
161 my ($self, $req) = @_;
162 ...
163 return $self->p->sendJSONresponse($req, { hello => 1 });
164 }
165 sub welcome {
166 my ($self, $req) = @_;
167 ...
168 return $self->p->sendHtml($req, 'template', params => { WELCOME => 1 });
169 }
170  
171 =head1 DESCRIPTION
172  
173 Lemonldap::NG::Portal::Main::Plugin provides many methods to easily write
174 Lemonldap::NG addons.
175  
176 init() is called for each plugin. If a plugin initialization fails (init()
177 returns 0), the portal responds a 500 status code for each request.
178  
179 =head1 Writing plugins
180  
181 Custom plugins can be inserted in portal by declaring them in
182 C<lemonldap-ng.ini> file, section C<[portal]>, key C<customPlugins>:
183  
184 [portal]
185 customPlugins = ::My::Plugin1, ::My::Plugin2
186  
187 Plugins must be valid packages well found in C<@INC>.
188  
189 =head2 Plugin entry points
190  
191 =head3 Entry point based on PATH_INFO
192  
193 Plugins can declare unauthRoutes/authRoutes during initialization (=
194 /path/info). Methods declared in this way must be declared in the plugin class.
195 They will be called with $req argument. $req is the HTTP request.
196 (See L<Lemonldap::NG::Portal::Main::Request>). These methods must return a valid
197 L<PSGI> response. You can also use sendJSONresponse() or sendHtml() methods
198 (see L<Lemonldap::NG::Common::PSGI>).
199  
200 Example:
201  
202 sub init {
203 my ($self) = @_;
204 $self->addUnauthRoute( mypath => 'hello', [ 'GET', 'PUT' ] );
205 $self->addAuthRoute( mypath => 'welcome', [ 'GET', 'PUT' ] );
206 return 1;
207 }
208 sub hello {
209 my ($self, $req) = @_;
210 ...
211 return $self->p->sendJSONresponse($req, { hello => 1 });
212 }
213 sub welcome {
214 my ($self, $req) = @_;
215 ...
216 return $self->p->sendHtml($req, 'template', params => { WELLCOME => 1 });
217 }
218  
219 If you want to get a "protected application" behavior, you can use
220 B<addAuthRouteWithRedirect>. This methods calls B<addAuthRoute> with given
221 arguments and build a "unAuth" route that build a redirection after
222 authentication.
223  
224 =head3 Entry point in auth process
225  
226 A plugin which wants to be inserted in authentication process has to declare
227 constants set with method name to run. Following entry points are available.
228  
229 =over
230  
231 =item C<beforeAuth>: method called before authentication process
232  
233 =item C<betweenAuthAndData>: method called after authentication and before
234 setting C<sessionInfo> provisionning
235  
236 =item C<afterData>: method called after C<sessionInfo> provisionning
237 I<(macros, groups,...)>. This entry point is called after 'storeHistory'
238 if login process fails and before 'validSession' if succeeds.
239  
240 =item C<endAuth>: method called when session is validated (after cookie build)
241  
242 =item C<authCancel>: method called when user click on "cancel" during auth
243 process
244  
245 =item C<forAuthUser>: method called for already authenticated users
246  
247 =item C<beforeLogout>: method called before logout
248  
249 =back
250  
251 B<Note>: methods inserted so must return a PE_* constant. See
252 Lemonldap::NG::Portal::Main::Constants.
253  
254 =head4 Advanced entry points
255  
256 These entry points are not stored in C<$req-E<gt>step> but launched on the fly:
257  
258 =over
259  
260 =item C<afterSub>: hash ref that give methods to call after given main method
261 is called. Example:
262  
263 use constant afterSub => {
264 getUser => 'mysub',
265 };
266 sub mysub {
267 my ( $self ,$req ) = @_;
268 # Do something
269 return PE_OK;
270 }
271  
272 =item C<aroundSub>: hash ref that give methods to call instead of given main
273 method. Example:
274  
275 use constant aroundSub => {
276 getUser => 'mysub',
277 };
278 sub mysub {
279 my ( $self, $sub, $req ) = @_;
280 # Do something before
281 my $ret = $sub->($req);
282 # Do something after
283 return $ret;
284 }
285  
286 Do not launch "getUser" but use the given C<$sub>. This permits multiple
287 plugins to use "aroundSub" in the same time.
288  
289 =item C<hook>: hash ref that gives methods to call when a hook is triggered in the
290 LemonLDAP::NG code. Example:
291  
292 use constant hook => {
293 oidcGenerateIDToken => 'addClaimToIDToken'
294 };
295 sub addClaimToIDToken {
296 my ( $self, $req, $payload, $rp ) = @_;
297 $payload->{"id_token_hook"} = 1;
298 return PE_OK;
299 }
300  
301 =back
302  
303 =head3 Registering new entrypoints
304  
305 Use the C<addEntryPoint> method to register a new entrypoint for future plugin loads.
306 This can be useful if you would like to extend your plugin with other plugins,
307 or interact with existing LemonLDAP::NG services in some way.
308  
309 C<addEntryPoint> takes a hash of options, for example:
310  
311 $self->addEntryPoint(
312 isa => "My::Base::Class",
313 callback => sub {
314 my ($plugin) = @_;
315 $self->do_domething_with($plugin);
316 }
317 );
318  
319 =over
320  
321 =item C<can>: trigger the entry point when the newly loaded plugin has a given method
322  
323 =item C<does>: trigger the entry point when the newly loaded plugin consumes a given role
324  
325 =item C<isa>: trigger the entry point when the newly loaded plugin extends a given class
326  
327 =item C<callback>: when triggered, run the given code reference, with the new
328 plugin instance as first argument, and optional extra arguments
329  
330 =item C<service> and C<method>: when triggered, run the given method of the
331 given portal service, with the new plugin instance as first argument, and
332 optional extra arguments
333  
334 =item C<args>: optional array reference of extra arguments to pass to the
335 callback or service
336  
337 =back
338  
339 =head1 LOGGING
340  
341 Logging is provided by $self->logger and $self->userLogger. The following rules
342 must be applied:
343  
344 =over
345  
346 =item logger->debug: technical debugging messages
347  
348 =item logger->info: simple technical information
349  
350 =item logger->notice: technical information that could interest administrators
351  
352 =item logger->warn: technical warning
353  
354 =item logger->error: error that must be reported to administrator
355  
356 =item userLogger->info: simple information about user's action
357  
358 =item userLogger->notice: information that may be registered (auth success,...)
359  
360 =item userLogger->warn: bad action of a user (auth failure). Auth/Combination
361 transform it to "info" when another authentication scheme is available
362  
363 =item userLogger->error: bad action of a user that must be reported, (even if
364 another backend is available with Combination)
365  
366 =back
367  
368 =head1 SEE ALSO
369  
370 L<http://lemonldap-ng.org>
371  
372 =head2 OTHER POD FILES
373  
374 =over
375  
376 =item Writing an authentication module: L<Lemonldap::NG::Portal::Auth>
377  
378 =item Writing a UserDB module: L<Lemonldap::NG::Portal::UserDB>
379  
380 =item Writing a second factor module: L<Lemonldap::NG::Portal::Main::SecondFactor>
381  
382 =item Writing an issuer module: L<Lemonldap::NG::Portal::Main::Issuer>
383  
384 =item Writing another plugin: L<Lemonldap::NG::Portal::Main::Plugin>
385  
386 =item Request object: L<Lemonldap::NG::Portal::Main::Request>
387  
388 =item Adding parameters in the manager: L<Lemonldap::NG::Manager::Build>
389  
390 =back
391  
392 =head1 AUTHORS
393  
394 =over
395  
396 =item LemonLDAP::NG team L<http://lemonldap-ng.org/team>
397  
398 =back
399  
400 =head1 BUG REPORT
401  
402 Use OW2 system to report bug or ask for features:
403 L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
404  
405 =head1 DOWNLOAD
406  
407 Lemonldap::NG is available at
408 L<https://lemonldap-ng.org/download>
409  
410 =head1 COPYRIGHT AND LICENSE
411  
412 See COPYING file for details.
413  
414 This library is free software; you can redistribute it and/or modify
415 it under the terms of the GNU General Public License as published by
416 the Free Software Foundation; either version 2, or (at your option)
417 any later version.
418  
419 This program is distributed in the hope that it will be useful,
420 but WITHOUT ANY WARRANTY; without even the implied warranty of
421 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
422 GNU General Public License for more details.
423  
424 You should have received a copy of the GNU General Public License
425 along with this program. If not, see L<http://www.gnu.org/licenses/>.
426  
427 =cut
« blib/lib/Lemonldap/NG/Portal/Plugins/AutoSignin.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Main/Auth.pm »