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

filter: Statementstmt 56.0% 37 / 66 Branchbran 20.0% 2 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 72.2% 13 / 18 Podpod 0.0% 0 / 10 totaltotal 48.6% 53 / 109 CC 8 SCAR 17.3
CC
8
Cov
51%
createNotification
20.0
addSessionDataToRemember
12.0
_addRoute
2.0
CRAP 5.6
« blib/lib/Lemonldap/NG/Portal/Main/Auth.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/2F/Mail2F.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 432 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 432 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 432 ▶use HTML::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 432 use Lemonldap::NG::Portal::Main::Constants qw(
9 PE_OK
10 PE_INFO
11 PE_ERROR
12 432 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 432 our $VERSION = '2.19.0';
15  
16 432 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 7 ▶ my $self = shift;
Subroutine sendJSONresponse: called CC=1 SCAR=0.0
Pod undocumented
25 7 return $self->p->sendJSONresponse(@_);
26 }
27  
28 sub addAuthRoute {
29 4204 ▶ my $self = shift;
Subroutine addAuthRoute: called CC=1 SCAR=0.0
Pod undocumented
30 4204 return $self->_addRoute( 'addAuthRoute', @_ );
31 }
32  
33 sub addUnauthRoute {
34 1995 ▶ my $self = shift;
Subroutine addUnauthRoute: called CC=1 SCAR=0.0
Pod undocumented
35 1995 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 6199 ▶ my ( $self, $type, $word, $subName, $methods, $transform ) = @_;
Subroutine _addRoute: called CC=2 SCAR=7.0
45 $transform //= sub {
46 7422 ▶ my ($sub) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
47 7422 ▶ if ( ref $sub ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sub) { } 134 7288 7422
48 return sub {
49 47 ▶ shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
50 47 return $sub->( $self, @_ );
51 }
52 134 }
53 else {
54 return sub {
55 78 ▶ shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
56 78 return $self->$sub(@_);
57 }
58 7288 }
59 6199 ▶ };
Condition33%1/3
$transform //= sub {
l!l&&r!l&&!r
061990
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 6199 $self->p->$type( $word, $subName, $methods, $transform );
61 6199 return $self;
62 }
63  
64 sub loadTemplate {
65 0 ▶ my $self = shift;
Subroutine loadTemplate: not called CC=1 SCAR=6.9
Pod undocumented
66 0 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 1470 ▶ my ( $self, %entryPointDescription ) = @_;
Subroutine addEntryPoint: called CC=1 SCAR=0.0
Pod undocumented
125 1470 return $self->p->_addPluginEntryPoint(%entryPointDescription, _pkg => ref($self));
126 }
127  
128 432 1;
129 __END__
130  
131 =pod
132  
133 =encoding utf8
134  
135 =head1 NAME
136  
137 Lemonldap::NG::Portal::Main::Plugin - Base class for
138 L<Lemonldap::NG::Portal> modules I<(plugins, authentication modules,...)>.
139  
140 =head1 SYNOPSIS
141  
142 package Lemonldap::NG::Portal::My::Plugin;
143 use Mouse;
144 extends 'Lemonldap::NG::Portal::Main::Plugin';
145
146 use constant beforeAuth => 'verifyIP';
147
148 sub init {
149 my ($self) = @_;
150 $self->addUnauthRoute( mypath => 'hello', [ 'GET', 'PUT' ] );
151 $self->addAuthRoute( mypath => 'welcome', [ 'GET', 'PUT' ] );
152 return 1;
153 }
154 sub verifyIP {
155 my ($self, $req) = @_;
156 return PE_ERROR if($req->address !~ /^10/);
157 return PE_OK;
158 }
159 sub hello {
160 my ($self, $req) = @_;
161 ...
162 return $self->p->sendJSONresponse($req, { hello => 1 });
163 }
164 sub welcome {
165 my ($self, $req) = @_;
166 ...
167 return $self->p->sendHtml($req, 'template', params => { WELCOME => 1 });
168 }
169  
170 =head1 DESCRIPTION
171  
172 Lemonldap::NG::Portal::Main::Plugin provides many methods to easily write
173 Lemonldap::NG addons.
174  
175 init() is called for each plugin. If a plugin initialization fails (init()
176 returns 0), the portal responds a 500 status code for each request.
177  
178 =head1 Writing plugins
179  
180 Custom plugins can be inserted in portal by declaring them in
181 C<lemonldap-ng.ini> file, section C<[portal]>, key C<customPlugins>:
182  
183 [portal]
184 customPlugins = ::My::Plugin1, ::My::Plugin2
185  
186 Plugins must be valid packages well found in C<@INC>.
187  
188 =head2 Plugin entry points
189  
190 =head3 Entry point based on PATH_INFO
191  
192 Plugins can declare unauthRoutes/authRoutes during initialization (=
193 /path/info). Methods declared in this way must be declared in the plugin class.
194 They will be called with $req argument. $req is the HTTP request.
195 (See L<Lemonldap::NG::Portal::Main::Request>). These methods must return a valid
196 L<PSGI> response. You can also use sendJSONresponse() or sendHtml() methods
197 (see L<Lemonldap::NG::Common::PSGI>).
198  
199 Example:
200  
201 sub init {
202 my ($self) = @_;
203 $self->addUnauthRoute( mypath => 'hello', [ 'GET', 'PUT' ] );
204 $self->addAuthRoute( mypath => 'welcome', [ 'GET', 'PUT' ] );
205 return 1;
206 }
207 sub hello {
208 my ($self, $req) = @_;
209 ...
210 return $self->p->sendJSONresponse($req, { hello => 1 });
211 }
212 sub welcome {
213 my ($self, $req) = @_;
214 ...
215 return $self->p->sendHtml($req, 'template', params => { WELLCOME => 1 });
216 }
217  
218 If you want to get a "protected application" behavior, you can use
219 B<addAuthRouteWithRedirect>. This methods calls B<addAuthRoute> with given
220 arguments and build a "unAuth" route that build a redirection after
221 authentication.
222  
223 =head3 Entry point in auth process
224  
225 A plugin which wants to be inserted in authentication process has to declare
226 constants set with method name to run. Following entry points are available.
227  
228 =over
229  
230 =item C<beforeAuth>: method called before authentication process
231  
232 =item C<betweenAuthAndData>: method called after authentication and before
233 setting C<sessionInfo> provisionning
234  
235 =item C<afterData>: method called after C<sessionInfo> provisionning
236 I<(macros, groups,...)>. This entry point is called after 'storeHistory'
237 if login process fails and before 'validSession' if succeeds.
238  
239 =item C<endAuth>: method called when session is validated (after cookie build)
240  
241 =item C<authCancel>: method called when user click on "cancel" during auth
242 process
243  
244 =item C<forAuthUser>: method called for already authenticated users
245  
246 =item C<beforeLogout>: method called before logout
247  
248 =back
249  
250 B<Note>: methods inserted so must return a PE_* constant. See
251 Lemonldap::NG::Portal::Main::Constants.
252  
253 =head4 Advanced entry points
254  
255 These entry points are not stored in C<$req-E<gt>step> but launched on the fly:
256  
257 =over
258  
259 =item C<afterSub>: hash ref that give methods to call after given main method
260 is called. Example:
261  
262 use constant afterSub => {
263 getUser => 'mysub',
264 }
265 sub mysub {
266 my ( $self ,$req ) = @_;
267 # Do something
268 return PE_OK;
269 }
270  
271 =item C<aroundSub>: hash ref that give methods to call instead of given main
272 method. Example:
273  
274 use constant aroundSub => {
275 getUser => 'mysub',
276 };
277 sub mysub {
278 my ( $self, $sub, $req ) = @_;
279 # Do something before
280 my $ret = $sub->($req);
281 # Do something after
282 return $ret;
283 }
284  
285 Do not launch "getUser" but use the given C<$sub>. This permits multiple
286 plugins to use "aroundSub" in the same time.
287  
288 =item C<hook>: hash ref that gives methods to call when a hook is triggered in the
289 LemonLDAP::NG code. Example:
290  
291 use constant hook => {
292 oidcGenerateIDToken => 'addClaimToIDToken'
293 };
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/Main/Auth.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/2F/Mail2F.pm »