Debian Codegraph @

lemonldap-ng-manager/lib/Lemonldap/NG/Manager.pm

raw
1 # Manager main component
2 #
3 # This package contains these parts:
4 # - Properties and private methods
5 # - Initialization method (launched by Lemonldap::NG::Common::PSGI)
6 # that declares routes
7 # - Upload methods (launched by Lemonldap::NG::Common::PSGI::Router)
8 #
9 # It inherits from Conf.pm to responds to display methods and from
10 # Sessions.pm to manage sessions
11 package Lemonldap::NG::Manager;
12
13 use strict;
14 use utf8;
15 use Mouse;
16 use JSON;
17 use Lemonldap::NG::Common::Conf::Constants;
18 use Lemonldap::NG::Common::PSGI::Constants;
19
20 our $VERSION = '2.23.0';
21
22 extends qw(
23 Lemonldap::NG::Handler::PSGI::Router
24 Lemonldap::NG::Common::Conf::AccessLib
25 );
26
27 has csp => ( is => 'rw' );
28 has loadedPlugins => ( is => 'rw', default => sub { [] } );
29 has hLoadedPlugins => ( is => 'rw', default => sub { {} } );
30
31 ## @method boolean init($args)
32 # Launch initialization method
33 #
34 # @param $args hashref to merge with object
35 # @return 0 in case of error, 1 else
36 sub init {
37 my ( $self, $args ) = @_;
38 $args ||= {};
39
40 if ( my $localconf = $self->confAcc->getLocalConf(MANAGERSECTION) ) {
41 foreach ( keys %$localconf ) {
42 $args->{$_} //= $localconf->{$_};
43 $self->{$_} = $args->{$_} unless (/^(?:l|userL)ogger$/);
44 }
45 }
46
47 # Manager needs to keep new Ajax behaviour
48 $args->{noAjaxHook} = 0;
49
50 return 0
51 unless ( $self->Lemonldap::NG::Handler::PSGI::Router::init($args) );
52
53 # TODO: manage errors
54 unless ( -r $self->{templateDir} ) {
55 $self->error("Unable to read $self->{templateDir}");
56 return 0;
57 }
58
59 my $conf = $self->confAcc->getConf;
60 $conf->{$_} = $args->{$_} foreach ( keys %$args );
61
62 $self->{enabledModules} ||= "conf, sessions, notifications, 2ndFA, api";
63 my @links;
64 my @enabledModules =
65 map {
66 my @res = ( "Lemonldap::NG::Manager::" . ucfirst($_) );
67 if ( my $tmp = $self->loadPlugin( @res, $conf ) ) {
68 $self->logger->debug("Plugin $_ loaded");
69 push @links, $_;
70 push @{ $self->loadedPlugins }, $tmp;
71 $self->hLoadedPlugins->{$_} = $tmp;
72 }
73 else {
74 $self->logger->error("Unable to load $_, skipping");
75 @res = ();
76 }
77 (@res);
78 }
79 split( /[,\s]+/, $self->{enabledModules} );
80 unless (@enabledModules) {
81 $self->logger->error('No plugins loaded, aborting');
82 return 0;
83 }
84 unless ($conf) {
85 require Lemonldap::NG::Manager::Conf::Zero;
86 $conf = Lemonldap::NG::Manager::Conf::Zero::zeroConf();
87 }
88
89 # TODO: -> loadPlugin
90 #for ( my $i = 0 ; $i < @enabledModules ; $i++ ) {
91 # my $mod = $enabledModules[$i];
92 # no strict 'refs';
93 # if ( &{"${mod}::addRoutes"}( $self, $conf ) ) {
94 # $self->logger->debug("Module $mod enabled");
95 # push @working, $mod;
96 # }
97 # else {
98 # $links[$i] = undef;
99 # $self->logger->error(
100 # "Module $mod can not be enabled: " . $self->error );
101 # }
102 #}
103 $self->addRoute( links => 'links', ['GET'] );
104 $self->addRoute( 'psgi.js' => 'sendJs', ['GET'] );
105
106 my $portal = $conf->{portal};
107 $portal =~ s#https?://([^/]*).*#$1#;
108 $self->csp(
109 "default-src 'self' $portal;frame-ancestors 'none';form-action 'self';"
110 );
111
112 # Avoid restricted users to access configuration by default route
113 my $defaultMod = $self->{defaultModule} =
114 $self->{defaultModule} || $enabledModules[0];
115 $self->logger->debug("Default module -> $defaultMod");
116 my ($index) =
117 grep { $enabledModules[$_] eq $defaultMod } ( 0 .. $#enabledModules );
118 $index //= 0;
119 $self->logger->debug("Default index -> $index");
120 $self->defaultRoute( $self->loadedPlugins->[$index]->defaultRoute );
121
122 $self->links( [] );
123 for ( my $i = 0 ; $i < @links ; $i++ ) {
124 next unless ( defined $links[$i] );
125 push @{ $self->links },
126 {
127 target => $self->loadedPlugins->[$i]->defaultRoute,
128 title => $links[$i],
129 icon => $self->loadedPlugins->[$i]->icon,
130 };
131 }
132
133 $self->menuLinks( [] );
134 if (
135 my $portal =
136 $conf->{cfgNum}
137 ? Lemonldap::NG::Handler::PSGI::Main->tsv->{portal}->()
138 : $conf->{portal}
139 )
140 {
141 $portal = $self->customPortalUrl || $portal;
142 push @{ $self->menuLinks },
143 {
144 target => $portal,
145 title => 'backtoportal',
146 icon => 'home'
147 },
148 {
149 target => "$portal?logout=1",
150 title => 'logout',
151 icon => 'log-out'
152 };
153 }
154 1;
155 }
156
157 sub tplParams {
158 my ( $self, $req ) = @_;
159 my $res = eval {
160 $self->hLoadedPlugins->{viewer}->brwRule->( $req, $req->{userData} );
161 } || 0;
162 return (
163 VERSION => $VERSION,
164 ALLOWBROWSER => $res,
165 DOC_PREFIX => ( $self->{docPrefix} || '/doc' ),
166 );
167 }
168
169 sub javascript {
170 my ( $self, $req ) = @_;
171 my $res = eval {
172 $self->hLoadedPlugins->{viewer}
173 && $self->hLoadedPlugins->{viewer}
174 ->diffRule->( $req, $req->{userData} );
175 } || 0;
176 print STDERR $@ if $@;
177 my $impPrefix = $self->{impersonationPrefix} || 'real_';
178 my $ttl = $self->{timeout} || 72000;
179
180 return
181 'var formPrefix=staticPrefix+"forms/";var confPrefix=scriptname+"confs/";var viewPrefix=scriptname+"view/";'
182 . "var allowDiff=$res;"
183 . "var sessionTTL=$ttl;"
184 . "var impPrefix='$impPrefix';"
185 . ( $self->links ? 'var links=' . to_json( $self->links ) . ';' : '' )
186 . (
187 $self->menuLinks
188 ? 'var menulinks=' . to_json( $self->menuLinks ) . ';'
189 : ''
190 );
191 }
192
193 sub sendHtml {
194 my ( $self, $req, $template, %args ) = @_;
195 my $res = $self->SUPER::sendHtml( $req, $template, %args );
196 push @{ $res->[1] },
197 'Content-Security-Policy' => $self->csp,
198 'X-Content-Type-Options' => 'nosniff',
199 'X-Frame-Options' => 'DENY',
200 'X-XSS-Protection' => '1; mode=block';
201 return $res;
202 }
203
204 sub loadPlugin {
205 my ( $self, $plugin, $conf ) = @_;
206 unless ($plugin) {
207 require Carp;
208 Carp::confess('Calling loadPugin without arg !');
209 }
210 my $obj;
211 $plugin = "Lemonldap::NG::Manager$plugin" if ( $plugin =~ /^::/ );
212
213 eval "require $plugin";
214 if ($@) {
215 $self->logger->error("$plugin load error: $@");
216 return 0;
217 }
218 eval {
219 $obj = $plugin->new( { p => $self, conf => $conf } );
220 $self->logger->debug("Module $plugin loaded");
221 };
222 if ($@) {
223 $self->error("Unable to build $plugin object: $@");
224 return 0;
225 }
226 ( $obj and $obj->init($conf) ) or return 0;
227 return $obj;
228 }
229
230 1;
231 __END__
232
233 =head1 NAME
234
235 =encoding utf8
236
237 Lemonldap::NG::Manager - Perl extension for managing Lemonldap::NG Web-SSO
238 system.
239
240 =head1 SYNOPSIS
241
242 Use any of Plack launcher. Example:
243
244 #!/usr/bin/env plackup
245
246 use Lemonldap::NG::Manager;
247
248 # This must be the last instruction ! See PSGI for more
249 Lemonldap::NG::Manager->run($opts);
250
251 =head1 DESCRIPTION
252
253 Lemonldap::NG::Manager provides a web interface to manage Lemonldap::NG Web-SSO
254 system.
255
256 The Perl part of Lemonldap::NG::Manager is the REST server. Web interface is
257 written in Javascript, using AngularJS framework and can be found in `site`
258 directory. The REST API is described in REST-API.md file provided in source tree.
259
260 Lemonldap::NG Manager uses L<Plack> to be CGI, FastCGI and so on compatible.
261 It inherits of L<Lemonldap::NG::Handler::PSGI::Router>
262
263 =head1 ORGANIZATION
264
265 Lemonldap::NG Manager contains 6 parts:
266
267 =over
268
269 =item Configuration management
270
271 =item Session explorer
272
273 =item Notification explorer
274
275 =item Second Factors manager
276
277 =item Configuration builder (see L<Lemonldap::NG::Manager::Build>
278
279 =item Command line interface (see L<Lemonldap::NG::Manager::Cli>
280
281 =back
282
283 =head2 Static files generation
284
285 `scripts/jsongenerator.pl` file uses Lemonldap::NG::Manager::Build::Attributes,
286 Lemonldap::NG::Manager::Build::Tree and Lemonldap::NG::Manager::Build::CTrees to generate
287
288 =over
289
290 =item `site/htdocs/static/struct.json`:
291
292 main file containing the tree view;
293
294 =item `site/htdocs/static/js/conftree.js`:
295
296 generates Virtualhosts, SAML and OpenID-Connect partners sub-trees;
297
298 =item `Lemonldap::NG::Common::Conf::ReConstants`:
299
300 constants used by all Perl manager components;
301
302 =item `Lemonldap::NG::Common::Conf::DefaultValues`:
303
304 constants used to read configuration.
305
306 =back
307
308 =head1 PARAMETERS
309
310 You can use a hash ref to override any LemonLDAP::NG parameter. Currently, you
311 can specify where your lemonldap-ng.ini file is:
312
313 Lemonldap::NG::Manager->run( { confFile => '/path/to/lemonldap-ng.ini' } );
314
315 =head2 lemonldap-ng.ini parameters
316
317 You can override any configuration parameter in lemonldap-ng.ini, but some are
318 required and can't be set to global configuration (as any Lemonldap::NG module,
319 you can also fix them in $opts hash ref passed as argument to run() or new()).
320
321 [manager]
322 ;protection: choose one of none, authenticate, manager as explain in
323 ; Lemonldap::NG::Handler::PSGI::Router doc.
324 protection = manager
325
326 ;enabledModules: Modules to display. Default to `conf, sessions, notifications, 2ndFA`
327 enabledModules = conf, sessions, notifications, 2ndFA
328
329 ;logLevel: choose one of error, warn, notice, info, debug
330 ; See Lemonldap::NG::Common::PSGI doc for more
331 logLevel = notice
332
333 ;staticPrefix: set here the URI path to static content
334 ; See Lemonldap::NG::Common::PSGI doc for more
335 staticPrefix = static/
336
337 ;languages: Available interface languages
338 languages = en, fr
339
340 ;templateDir: path to the directory containing HTML templates
341 ; See Lemonldap::NG::Common::PSGI doc for more
342 templateDir = /usr/share/lemonldap-ng/manager/
343
344 =head1 SEE ALSO
345
346 L<http://lemonldap-ng.org/>
347
348 =head1 AUTHORS
349
350 =over
351
352 =item LemonLDAP::NG team L<http://lemonldap-ng.org/team>
353
354 =back
355
356 =head1 BUG REPORT
357
358 Use OW2 system to report bug or ask for features:
359 L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
360
361 Note that if you want to post a ticket for a conf upload problem, please
362 see L<Lemonldap::NG::Manager::Conf::Parser> before.
363
364 =head1 DOWNLOAD
365
366 Lemonldap::NG is available at
367 L<https://lemonldap-ng.org/download>
368
369 =head1 COPYRIGHT AND LICENSE
370
371 See COPYING file for details.
372
373 This library is free software; you can redistribute it and/or modify
374 it under the terms of the GNU General Public License as published by
375 the Free Software Foundation; either version 2, or (at your option)
376 any later version.
377
378 This program is distributed in the hope that it will be useful,
379 but WITHOUT ANY WARRANTY; without even the implied warranty of
380 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
381 GNU General Public License for more details.
382
383 You should have received a copy of the GNU General Public License
384 along with this program. If not, see L<http://www.gnu.org/licenses/>.
385
386 =cut