Portal.pm 15.8 KB
Newer Older
Yadd's avatar
Yadd committed
1
2
3
4
5
## @file
# Alias for Lemonldap::NG::SharedConf

## @class
# Alias for Lemonldap::NG::SharedConf
6
7
package Lemonldap::NG::Portal;

Clément OUDOT's avatar
Clément OUDOT committed
8
our $VERSION = '1.9.0';
Yadd's avatar
Yadd committed
9
10
use Lemonldap::NG::Portal::SharedConf;
use base 'Lemonldap::NG::Portal::SharedConf';
11
12
13
14
15
16
17

1;

__END__

=pod

Yadd's avatar
Yadd committed
18
19
=encoding utf8

20
21
22
23
24
25
26
=head1 NAME

Lemonldap::NG::Portal - The authentication portal part of Lemonldap::NG Web-SSO
system.

=head1 SYNOPSIS

Yadd's avatar
Yadd committed
27
28
  use Lemonldap::NG::Portal::SharedConf;
  my $portal = new Lemonldap::NG::Portal::SharedConf (
Yadd's avatar
Yadd committed
29
         configStorage => {
Yadd's avatar
Yadd committed
30
            type          => 'DBI',
Yadd's avatar
Yadd committed
31
32
33
            dbiChain      => "dbi:mysql:database=lmSessions;host=1.2.3.4",
            dbiUser       => "lemon",
            dbiPassword   => "pass",
Yadd's avatar
Yadd committed
34
35
36
         },
         # Activate SOAP service
         Soap           => 1
37
38
39
40
41
42
43
        );


  if($portal->process()) {
      # Write here the menu with CGI methods. This page is displayed ONLY IF
      # the user was not redirected here by a Lemonldap::NG::Handler,
      # else, the process sub redirect the user to the initial requested URI.
Yadd's avatar
Yadd committed
44
      print $portal->header('text/html; charset=utf8'); # DON'T FORGET THIS (see L<CGI(3)>)
45
46
47
48
      print "...";
  
      # or redirect the user to the menu
      print $portal->redirect( -uri => 'https://portal/menu');
49
50
51
      
      # You can also add a "Logout" link:
      print "<a href=\"$ENV{SCRIPT_NAME}?logout=1\">";
52
53
54
  }
  else {
      # Write here the html form used to authenticate with CGI methods.
Yadd's avatar
Yadd committed
55
      # $portal->error returns the error message if authentication failed
Yadd's avatar
Yadd committed
56
57
      # You can force the language in error like this :
      #   print $portal->error('fr')
58
      # Warning: by defaut, input names are "user" and "password"
Yadd's avatar
Yadd committed
59
      print $portal->header('text/html; charset=utf8'); # DON'T FORGET THIS (see L<CGI(3)>)
60
61
62
63
64
65
66
67
68
69
70
      print "<html> ...";
      print '<form method="POST">';
      # In your form, the following value is required for redirection
      print '<input type="hidden" name="url" value="'.$portal->param('url').'">';
      # Next, login and password
      print 'Login : <input name="user"><br>';
      print 'Password : <input name="password" type="password" autocomplete="off"><br>';
      print '<input type=submit value="OK">';
      print '</form>';
  }

Yadd's avatar
Yadd committed
71
72
73
74
75
76
77
78
79
SOAP mode authentication (client) :

  #!/usr/bin/perl -l
  
  use SOAP::Lite;
  use Data::Dumper;
  
  my $soap =
    SOAP::Lite->proxy('http://auth.example.com/')
Yadd's avatar
Yadd committed
80
    ->uri('urn:/Lemonldap::NG::Common::CGI::SOAPService');
Yadd's avatar
Yadd committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  my $r = $soap->getCookies( 'user', 'password' );
  
  # Catch SOAP errors
  if ( $r->fault ) {
      print STDERR "SOAP Error: " . $r->fault->{faultstring};
  }
  else {
      my $res = $r->result();
  
      # If authentication failed, display error
      if ( $res->{error} ) {
          print STDERR "Error: " . $soap->error( 'fr', $res->{error} )->result();
      }
  
      # print session-ID
      else {
          print "Cookie: lemonldap=" . $res->{cookies}->{lemonldap};
      }
  }

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
=head1 DESCRIPTION

Lemonldap::NG is a modular Web-SSO based on Apache::Session modules. It
simplifies the build of a protected area with a few changes in the application.

It manages both authentication and authorization and provides headers for
accounting. So you can have a full AAA protection for your web space as
described below.

The portal part inherits from L<CGI> so yo can use it both with Apache 1 and 2
and use all L<CGI> features.

=head2 Authentication, Autorization, Accounting

=head3 B<Authentication>

Yadd's avatar
Yadd committed
117
If a user isn't authenticated and attempts to connect to an area protected by a
118
119
120
121
122
Lemonldap::NG compatible handler, he is redirected to a portal. The portal
authenticates user with a ldap bind by default, but you can also use another
authentication sheme like using x509 user certificates (see
L<Lemonldap::NG::Portal::AuthSSL> for more).

123
Lemonldap::NG use session cookies generated by L<Apache::Session> so as secure as a
124
128-bit random cookie. You may use the C<securedCookie> options of
Yadd's avatar
Yadd committed
125
126
L<Lemonldap::NG::Portal> to avoid session hijacking if all your protected sites
use https.
127

128
You have to manage life of sessions by yourself since Lemonldap::NG knows nothing
129
130
131
about the L<Apache::Session> module you've choosed, but it's very easy using a
simple cron script because L<Lemonldap::NG::Portal> stores the start time in the
C<_utime> field.
Yadd's avatar
Yadd committed
132
The purgeCentralCache provided in C<example/> directory can help you to do it.
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
By default, a session stay 10 minutes in the Handler local storage, so in the
worth case, a user is authorized 10 minutes after he lost his rights.

=head3 B<Authorization>

Authorization is controled only by handlers because the portal knows nothing
about the way the user will choose. When configuring your Web-SSO, you have to:

=over

=item * choose the ldap attributes you want to use to manage accounting and
authorization (see C<exportedHeaders> parameter in L<Lemonldap::NG::Portal>
documentation),

=item * create Perl expression to define user groups (using ldap attributes):
Yadd's avatar
Yadd committed
148
optional, this mechanism is available with Lemonldap::NG::*::SharedConf
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
modules,

=item * create an array foreach virtual host associating URI regular
expressions and Perl expressions to use to grant access.

=back

=head4 Example

Exported variables (in Lemonldap::NG::Portal, will be stored in
configuration database):

  exportedVars => {
      cn            => "cn",
      departmentUID => "departmentUID",
      login         => "uid",
  },

User groups (stored in configuration database with L<Lemonldap::NG::Manager>):

  groups => {
      group1 => '{ $departmentUID eq "unit1" or $login = "xavier.guimard" }',
      ...
  },

Area protection (stored in configuration database with
L<Lemonldap::NG::Manager>):

  locationRules => {
      www1.domain.com => {
          '^/protected/.*$' => '$groups =~ /\bgroup1\b/',
          default           => 'accept',
      },
      www2.domain.com => {
          '^/site/.*$' => '$uid eq "xavier.guimard" or $groups =~ /\bgroup2\b/',
          '^/(js|css)' => 'accept',
          default      => 'deny',
      },
  },

=head4 Performance

You can use Perl expressions as complicated as you want and you can use all
the exported LDAP attributes (and create your own attributes: see examples in
L<Lemonldap::NG::Portal> distribution) both in groups evaluations and area
protections (you just have to call them with a "$").

You have to be careful when choosing your expressions:

=over

=item * C<groups> are evaluated each time a user is redirected to the portal,

=item * C<locationRules> are evaluated for each request.

=back

Yadd's avatar
Yadd committed
206
It is also recommended to use the C<groups> mechanism to avoid having to
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
evaluate a long expression at each HTTP request:

  locationRules => {
      www1.domain.com => {
          '^/protected/.*$' => '$groups =~ /\bgroup1\b/',
      },
  },

You can also use ldap filters in C<groups> parameter, or Perl expression or
mixed expressions. Perl expressions has to be enclosed with C<{}>:

=over

=item * C<group1 =E<gt> '(|(uid=xavier.guimard)(ou=unit1))'>

=item * C<group1 =E<gt> '{$uid eq "xavier.guimard" or $ou eq "unit1"}'>

=item * C<group1 =E<gt> '(|(uid=xavier.guimard){$ou eq "unit1"})'>

=back

Yadd's avatar
Yadd committed
228
It is also recommended to use Perl expressions to avoid requiring the LDAP
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
server more than 2 times per authentication.

=head3 B<Accounting>

=head4 I<Logging portal access>

L<Lemonldap::NG::Portal> doesn't log anything by default, but it's easy to overload
C<log> method for normal portal access or using C<error> method to know what
was wrong if C<process> method has failed.

=head4 I<Logging application access>

Because an handler knows nothing about the protected application, it can't do
more than logging URL. As Apache does this fine, L<Lemonldap::NG::Handler> gives it
the name to used in logs. The C<whatToTrace> parameters indicates which
variable Apache has to use (C<$uid> by default).

The real accounting has to be done by the application itself which knows the
result of SQL transaction for example.

249
Lemonldap::NG can export http headers either using a proxy or protecting directly
250
251
252
253
254
255
the application. By default, the C<User-Auth> field is used but you can change
it using the C<exportedHeaders> parameters (stored in the configuration
database). This parameters contains an associative array:

=over

Yadd's avatar
Yadd committed
256
=item * B<keys> are the names of the chosen headers
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300

=item * B<values> are perl expressions where you can use user datas stored in
the global store by calling them C<$E<lt>varnameE<gt>>.

=back

Example:

  exportedHeaders => {
      www1.domain.com => {
          'Auth-User' => '$uid',
          'Unit'      => '$ou',
      },
      www2.domain.com => {
          'Authorization' => '"Basic ".encode_base64($employeeNumber.":dummy")',
      },
  }

=head2 Storage systems

Lemonldap::NG use 3 levels of cache for authenticated users:

=over

=item * an Apache::Session::* module choosed with the C<globalStorage>
parameter (completed with C<globalglobalStorageOptions>) and used by
L<lemonldap::NG::Portal> to store authenticated user parameters,

=item * a L<Cache::Cache> module choosed with the C<localStorage> parameter
(completed with C<localStorageOptions> and used to share authenticated users
between Apache's threads or processus and of course between virtual hosts,

=item * Lemonldap::NG variables: if the same user use the same thread or
processus a second time, no request are needed to grant or refuse access. This
is very efficient with HTTP/1.1 Keep-Alive system.

=back

So the number of request to the central storage is limited to 1 per user each
10 minutes.

Lemonldap::NG is very fast, but you can increase performance using a
L<Cache::Cache> module that does not use disk access.

301
302
303
304
=head2 Logout system

Lemonldap::NG provides a single logout system: you can use it by adding a link
to the portal with "logout=1" parameter (See Synopsis) and/or by configuring
Yadd's avatar
Yadd committed
305
306
Handler to intercept some URL directly in the manager interface and/or in
Apache configuration file (See L<Lemonldap::NG::Handler>). The logout system:
307
308
309
310
311
312
313
314
315
316
317
318
319
320

=over

=item * delete session in the global session storage,

=item * replace Lemonldap::NG cookie by '',

=item * delete handler caches only if logout action was started from a
protected application and only in the current Apache server. So in other
servers, session is still in cache for 10 minutes maximum if the user was
connected on it in the last 10 minutes.

=back

321
322
323
324
325
326
327
328
329
330
331
332
333
334
=head2 Existing sessions

By default, when a user tries to connect to the portal with a valid cookie,
the portal proposes a new authentication. This behaviour can be change easily
by changing C<existingSession> sub :

  use Lemonldap::NG::Portal::SharedConf qw(:all);
  
  my $portal = Lemonldap::NG::Portal::SharedConf->new ( {
                existingSession => sub {PE_DONE},
                configStorage => ...
                ...
  });

Yadd's avatar
Yadd committed
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
=head1 PORTAL OPTIONS

=head2 Manager options

All options here can be set both in the manager interface and in the
constructor ($portal->new()). Local arguments have the precedence on
arguments set by the manager.

=over

=item * ldapPort: tcp port used by ldap server.

=item * ldapBase: base of the ldap directory.

=item * managerDn: dn to used to connect to ldap server. By default, anonymous
bind is used.

=item * managerPassword: password to used to connect to ldap server. By
default, anonymous bind is used.

=item * securedCookie : set it to 1 if you want to protect user cookies.

=item * cookieName : name of the cookie used by Lemonldap::NG (lemonldap by
default).

=item * domain : cookie domain. You may have to give it else the SSO will work
only on your server.

=item * globalStorage : required: L<Apache::Session> library to used to store
Yadd's avatar
Yadd committed
364
session information.
Yadd's avatar
Yadd committed
365
366
367
368

=item * globalStorageOptions : parameters to bind to L<Apache::Session> module

=item * locationRules : this parameter is used by L<Lemonldap::NG::Handler> to
369
read the rules.
Yadd's avatar
Yadd committed
370
371
372
373
374
375

=item * authentication: sheme to authenticate users (default: "ldap"). It can
be set to:

=over

Yadd's avatar
Yadd committed
376
=item * ldap : authentication is done by LDAP bind
Yadd's avatar
Yadd committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404

=item * SSL : the portal reads SSL variables issued from Apache SSL
authentication. See L<Lemonldap::NG::Portal::AuthSSL> for more.

=item * CAS : use CAS system to authenticate users. See
L<Lemonldap::NG::Portal::AuthCAS> for more.

=item * Apache : the authentication is done by Apache configuration and the
portal reads the environment variable C<REMOTE_USER> to search the user in
the LDAP server. This can be used to authenticate users by Kerberos.

=item * LA : Liberty-Alliance mechanism. WARNING : at the moment, you have to
use directly L<Lemonldap::NG::Portal::AuthLA> because this target does not
work.

=back

=back

=head2 Local options

Those options can not be set using the manager but have to be passed to the
constructor (new()).

=over

=item * configStorage I<(required for Lemonldap::NG::Portal::SharedConf)> :
describe how to find configuration generated by L<Lemonldap::NG::Manager>. See
405
L<Lemonldap::NG::Common::Conf> for more
Yadd's avatar
Yadd committed
406
407

=item * caPath, caFile : if you use ldap+tls you can overwrite cafile or capath
Yadd's avatar
Yadd committed
408
options with those parameters. This is useful if you use a shared
Yadd's avatar
Yadd committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
configuration.

=item * ldapPpolicyControl : set it to 1 if you want to use LDAP Password
Policy

=item * ldapGroupBase : this parameter can be used to store in the Lemonldap::NG
groups system all the LDAP groups that contains the user. Set here the LDAP base
of the LDAP groups. Example : C<"dmdName=groups,dc=example,dc=com">.

=item * I<methodName> : all method used by the process sub can be overloaded
simply using an option that referenced a sub. See
L<Lemonldap::NG::Portal::Simple> for more.

=back

CAS authentication options :

=over

=item * CAS_url, CAS_CAFile, CAS_loginUrl, CAS_validationUrl : see
L<Lemonldap::NG::Portal::AuthCAS> for more

=back

Liberty-Alliance authentication options :

=over

=item * laSp, laIdpsFile, laStorage, laLdapLoginAttribute, localStorage,
localStorageOptions, laDebug : see L<Lemonldap::NG::Portal::AuthLA> for more.

=back

SSL authentication options :

=over

=item * SSLVar : the name of the SSL variable to read. Default to
SSL_CLIENT_S_DN_Email.

=back

451
452
453
454
455
456
457
458
=head1 USING LEMONLDAP::NG::PORTAL FOR DEVELOPMENT

Lemonldap::NG::Portal provides different modules:

=over

=item * L<Lemonldap::NG::Portal::Simple>: base module to build a portal,

Yadd's avatar
Yadd committed
459
460
461
462
463
=item * L<Lemonldap::NG::Portal::Auth*>: authentication modules that modify
authentication scheme.

=item * L<Lemonldap::NG::Portal::AuthLA>: specific module to used
Liberty-Alliance mechanisms to authenticate users
464
465
466

=item * L<Lemonldap::NG::Portal::SharedConf>: this module provide the ability
to read portal configuration from a central database. It inherits from
Yadd's avatar
Yadd committed
467
L<Lemonldap::NG::Portal::Simple>. It's the more used module.
468
469
470
471
472

=back

=head1 SEE ALSO

473
L<Lemonldap::NG::Portal::SharedConf>, L<Lemonldap::NG::Portal::Simple>
Yadd's avatar
Yadd committed
474
L<Lemonldap::NG::Handler>, L<Lemonldap::NG::Manager>,
Yadd's avatar
Yadd committed
475
L<http://lemonldap-ng.org/>
476
477
478

=head1 AUTHOR

479
480
481
482
=over

=item Clement Oudot, E<lt>clem.oudot@gmail.comE<gt>

Yadd's avatar
Yadd committed
483
=item François-Xavier Deltombe, E<lt>fxdeltombe@gmail.com.E<gt>
484
485
486
487

=item Xavier Guimard, E<lt>x.guimard@free.frE<gt>

=back
488

Yadd's avatar
Yadd committed
489
490
491
=head1 BUG REPORT

Use OW2 system to report bug or ask for features:
Yadd's avatar
Yadd committed
492
L<http://jira.ow2.org>
Yadd's avatar
Yadd committed
493
494
495
496
497
498

=head1 DOWNLOAD

Lemonldap::NG is available at
L<http://forge.objectweb.org/project/showfiles.php?group_id=274>

499
500
=head1 COPYRIGHT AND LICENSE

501
502
=over

Yadd's avatar
Yadd committed
503
=item Copyright (C) 2005-2010 by Xavier Guimard, E<lt>x.guimard@free.frE<gt>
504

Yadd's avatar
Yadd committed
505
=item Copyright (C) 2012 by François-Xavier Deltombe, E<lt>fxdeltombe@gmail.com.E<gt>
506

Yadd's avatar
Yadd committed
507
=item Copyright (C) 2006-2015 by Clement Oudot, E<lt>clem.oudot@gmail.comE<gt>
508
509

=back
510
511

This library is free software; you can redistribute it and/or modify
512
513
514
515
516
517
518
519
520
521
522
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see L<http://www.gnu.org/licenses/>.
523
524

=cut