Debian Codegraph @

lemonldap-ng-common/lib/Lemonldap/NG/Common/Session/Purge.pm

raw
1 package Lemonldap::NG::Common::Session::Purge;
2
3 use strict;
4 use Lemonldap::NG::Common::Conf;
5 use Lemonldap::NG::Common::Conf::Constants;
6 use Lemonldap::NG::Common::Apache::Session;
7 use Lemonldap::NG::Common::Session;
8 use Lemonldap::NG::Common::Safelib;
9 use Lemonldap::NG::Common::PSGI::Request;
10 use JSON;
11 use Mouse;
12 use Time::HiRes;
13 use POSIX qw(strftime);
14
15 use constant defaultLogger => 'Lemonldap::NG::Common::Logger::Std';
16
17 has logLevel => ( is => 'rw' );
18 has force => ( is => 'rw' );
19 has audit => ( is => 'rw' );
20 has json => ( is => 'rw' );
21 has conf => (
22 is => 'ro',
23 default => sub {
24 my $ca = Lemonldap::NG::Common::Conf->new()
25 or die $Lemonldap::NG::Common::Conf::msg;
26 my $conf =
27 $ca->getConf
28 or die "Unable to get configuration ($!)"
29 or die "Unable to get configuration ($!)";
30 my $localconf = $ca->getLocalConf(PORTALSECTION)
31 or die "Unable to get local configuration ($!)";
32 if ($localconf) {
33 $conf->{$_} = $localconf->{$_} foreach ( keys %$localconf );
34 }
35 $conf->{logLevel} = $_[0]->logLevel || $conf->{logLevel} || 'info';
36 return $conf;
37 },
38 lazy => 1,
39 );
40
41 has logger => (
42 is => 'ro',
43 default => sub {
44 my $self = shift;
45 my $conf = $self->conf;
46 my $logger =
47 $self->conf->{logger} || $ENV{LLNG_DEFAULTLOGGER} || defaultLogger;
48 $logger =~ s/^::/Lemonldap::NG::Common::Logger::/;
49 $logger = defaultLogger
50 if $logger eq 'Lemonldap::NG::Common::Logger::Apache2';
51 eval "require $logger";
52 die $@ if ($@);
53 my $err;
54
55 unless ( $conf->{logLevel} =~ /^(?:debug|info|notice|warn|error)$/ ) {
56 $err = "Bad logLevel value $conf->{logLevel}, use 'info'";
57 $conf->{logLevel} = 'info';
58 }
59 $logger = $logger->new($conf);
60 $logger->error($err) if $err;
61 return $logger;
62 },
63 lazy => 1,
64 );
65
66 has userLogger => (
67 is => 'ro',
68 default => sub {
69 my $self = shift;
70 my $conf = $self->conf;
71 my $logger =
72 $self->conf->{userLogger} || $ENV{LLNG_USERLOGGER} || defaultLogger;
73 $logger =~ s/^::/Lemonldap::NG::Common::Logger::/;
74 $logger = defaultLogger
75 if $logger eq 'Lemonldap::NG::Common::Logger::Apache2';
76 eval "require $logger";
77 die $@ if ($@);
78 my $err;
79
80 unless ( $conf->{logLevel} =~ /^(?:debug|info|notice|warn|error)$/ ) {
81 $err = "Bad logLevel value $conf->{logLevel}, use 'info'";
82 $conf->{logLevel} = 'info';
83 }
84 $logger = $logger->new($conf);
85 $logger->error($err) if $err;
86 return $logger;
87 },
88 lazy => 1,
89 );
90
91 has _auditLogger => (
92 is => 'ro',
93 default => sub {
94 my $self = shift;
95 my $conf = $self->conf;
96 my $logger =
97 $self->conf->{auditLogger}
98 || $ENV{LLNG_AUDITLOGGER}
99 || "Lemonldap::NG::Common::AuditLogger::UserLoggerCompat";
100
101 eval "require $logger";
102 die $@ if ($@);
103
104 $logger = $logger->new($self);
105 return $logger;
106 },
107 lazy => 1,
108 );
109
110 has persistent_backend_options => (
111 is => 'ro',
112 default => sub {
113 my $conf = $_[0]->conf;
114 my $logger = $_[0]->logger;
115
116 my $type;
117 my $options;
118
119 # sessions
120 if ( $conf->{persistentStorage} ) {
121 $type = 'persistent';
122 }
123 else {
124 $type = 'global';
125 }
126
127 # load module
128 my $module = $conf->{"${type}Storage"};
129 eval "use $module";
130 die $@ if ($@);
131
132 $options = { %{ $conf->{"${type}StorageOptions"} // {} } };
133 $options->{backend} = $module;
134
135 $logger->debug("persistent session backend $module will be used");
136 return $options;
137 },
138 lazy => 1,
139 );
140
141 has backends => (
142 is => 'ro',
143 default => sub {
144 my $conf = $_[0]->conf;
145 my $logger = $_[0]->logger;
146 my %backends;
147 my $module;
148
149 # Sessions
150 if ( $conf->{globalStorage} ) {
151
152 # Load module
153 $module = $conf->{globalStorage};
154 eval "use $module";
155 die $@ if ($@);
156 $conf->{globalStorageOptions}->{backend} = $module;
157
158 # Add module in managed backends
159 $backends{SSO} = $conf->{globalStorageOptions};
160
161 $logger->debug("Session backend $module will be used");
162 }
163
164 # SAML
165 if ( $conf->{samlStorage}
166 or keys %{ $conf->{samlStorageOptions} ||= {} } )
167 {
168
169 # Load module
170 $module = $conf->{samlStorage} || $conf->{globalStorage};
171 eval "use $module";
172 die $@ if ($@);
173 $conf->{samlStorageOptions}->{backend} = $module;
174
175 # Add module in managed backends
176 $backends{SAML} = $conf->{samlStorageOptions};
177
178 $logger->debug("SAML backend $module will be used");
179 }
180
181 # CAS
182 if ( $conf->{casStorage}
183 or keys %{ $conf->{casStorageOptions} ||= {} } )
184 {
185
186 # Load module
187 $module = $conf->{casStorage} || $conf->{globalStorage};
188 eval "use $module";
189 die $@ if ($@);
190 $conf->{casStorageOptions}->{backend} = $module;
191
192 # Add module in managed backends
193 $backends{CAS} = $conf->{casStorageOptions};
194
195 $logger->debug("CAS backend $module will be used");
196 }
197
198 # Captcha
199 if ( $conf->{captchaStorage}
200 or keys %{ $conf->{captchaStorageOptions} ||= {} } )
201 {
202
203 # Load module
204 $module = $conf->{captchaStorage} || $conf->{globalStorage};
205 eval "use $module";
206 die $@ if ($@);
207 $conf->{captchaStorageOptions}->{backend} = $module;
208
209 # Add module in managed backends
210 $backends{Captcha} = $conf->{captchaStorageOptions};
211
212 $logger->debug("Captcha backend $module will be used");
213 }
214
215 # OpenIDConnect
216 if ( $conf->{oidcStorage}
217 or keys %{ $conf->{oidcStorageOptions} ||= {} } )
218 {
219
220 # Load module
221 $module = $conf->{oidcStorage} || $conf->{globalStorage};
222 eval "use $module";
223 die $@ if ($@);
224 $conf->{oidcStorageOptions}->{backend} = $module;
225
226 # Add module in managed backends
227 $backends{OIDC} = $conf->{oidcStorageOptions};
228
229 $logger->debug("OIDC backend $module will be used");
230 }
231 return \%backends;
232 },
233 lazy => 1,
234 );
235
236 has force => ( is => 'ro' );
237
238 my %PSESSION_FILTERS = (
239 age => {
240 filter => sub {
241 my ( $self, $entry, $value ) = @_;
242 return ( defined( $entry->{_utime} )
243 and $entry->{_utime} < ( time - $value ) );
244 }
245 },
246 update => {
247 filter => sub {
248 my ( $self, $entry, $value ) = @_;
249 my $min_updateTime =
250 strftime( "%Y%m%d%H%M%S", localtime( time - $value ) );
251 my $filter_result = ( defined( $entry->{_updateTime} )
252 and $entry->{_updateTime} < $min_updateTime );
253 }
254 },
255 login => {
256 filter => sub {
257 my ( $self, $entry, $value ) = @_;
258 my $lastLogin =
259 $entry->{_loginHistory}->{successLogin}->[0]->{_utime};
260
261 my $filter_result = (
262 not defined($lastLogin)
263 or ( defined($value) and ( $lastLogin < ( time - $value ) ) )
264 );
265 }
266 },
267 sfdevice => {
268 filter => sub {
269 my ( $self, $entry, $value ) = @_;
270 return !Lemonldap::NG::Common::Safelib::has2f_internal($entry);
271 }
272 },
273 );
274
275 sub purge {
276 my ($self) = @_;
277
278 my $internal_stats;
279
280 # Compute the list of backends here to avoid having big timing
281 # inconsistencies
282 my $backends = $self->backends;
283
284 # Trigger lazy init
285 my $conf = $self->conf;
286
287 $self->logger->info("Session purge started");
288 $internal_stats->{start_time}->{total} = Time::HiRes::time();
289
290 while ( my ( $type, $options ) = each %$backends ) {
291 $self->_purge_for_backend( $type, $options, $internal_stats );
292 }
293
294 $internal_stats->{end_time}->{total} = Time::HiRes::time();
295
296 my $total_purged = 0;
297 $total_purged += $_ for values %{ $internal_stats->{nb_purged} };
298 $internal_stats->{nb_purged}->{total} = $total_purged;
299
300 my $total_error = 0;
301 $total_error += $_ for values %{ $internal_stats->{nb_error} };
302 $internal_stats->{nb_error}->{total} = $total_error;
303
304 my $return_stats;
305 for my $type ( "total", sort keys %{ $self->backends } ) {
306 $return_stats->{$type}->{errors} = $internal_stats->{nb_error}->{$type};
307 $return_stats->{$type}->{purged} =
308 $internal_stats->{nb_purged}->{$type};
309 $return_stats->{$type}->{duration_u} = int(
310 1000000 * (
311 $internal_stats->{end_time}->{$type} -
312 $internal_stats->{start_time}->{$type}
313 )
314 );
315 }
316
317 my $log = "Session purge completed: ";
318 if ( $self->json ) {
319 $log .= to_json($return_stats);
320 }
321 else {
322 my @logentries;
323 for my $type ( "total", sort keys %{ $self->backends } ) {
324 my $logentry .= "$type (";
325 $logentry .= $return_stats->{$type}->{purged} . " purged, ";
326 $logentry .= $return_stats->{$type}->{errors} . " errors in ";
327 $logentry .= $return_stats->{$type}->{duration_u} . " us";
328 $logentry .= ")";
329 push @logentries, $logentry;
330 }
331 $log .= join( ', ', @logentries );
332 }
333
334 $self->logger->notice($log);
335 $self->logger->warn(
336 "$total_error sessions remaining, try to purge them with force (option -f)"
337 ) if $total_error;
338
339 return {
340 success => !($total_error),
341 purged => $total_purged,
342 errors => $total_error,
343 stats => $return_stats,
344 };
345 }
346
347 sub _purge_for_backend {
348 my ( $self, $type, $options, $internal_stats ) = @_;
349 my $conf = $self->conf;
350 $self->logger->debug("Cleaning $type sessions");
351
352 $internal_stats->{nb_error}->{$type} = 0;
353 $internal_stats->{nb_purged}->{$type} = 0;
354 $internal_stats->{start_time}->{$type} = Time::HiRes::time();
355
356 if ( $options->{backend} eq "Apache::Session::Memcached" ) {
357 $internal_stats->{end_time}->{$type} = Time::HiRes::time();
358 return;
359 }
360
361 my @t;
362
363 # Memorize some OIDC parameters
364 my ( $rpActivity, $rtMinTimeout );
365 foreach my $rp ( keys %{ $conf->{oidcRPMetaDataOptions} || {} } ) {
366 my $v = $conf->{oidcRPMetaDataOptions}->{$rp}
367 ->{oidcRPMetaDataOptionsRtActivity};
368 next unless $v;
369
370 # Store the mapping ClientID -> RT timeout
371 $rpActivity->{ $conf->{oidcRPMetaDataOptions}->{$rp}
372 ->{oidcRPMetaDataOptionsClientID} } = $v;
373 $rtMinTimeout = $v if !$rpActivity or $v < $rpActivity;
374 }
375
376 my $checkRtExpiration = sub {
377 my ( $id, $session ) = @_;
378 return unless $session->{_session_kind} eq 'OIDCI';
379 return unless $session->{_type} eq 'refresh_token';
380 my $v = $session->{_oidcRtUpdate} or return;
381 my $timeout = $rpActivity->{ $session->{client_id} } or return;
382 push @t, $id if $v + $timeout < time;
383 };
384
385 my $purge_done = 0;
386
387 # Real purge
388 if ( $options->{backend}->can('deleteIfLowerThan') ) {
389 $self->logger->debug("Found deleteIfLowerThan() in backend, using it");
390 my ( $success, $rows ) = $options->{backend}->deleteIfLowerThan(
391 $options,
392 {
393 not => { '_session_kind' => 'Persistent' },
394 or => {
395 _utime => time - $conf->{timeout},
396 (
397 $conf->{timeoutActivity}
398 ? ( _lastSeen => time - $conf->{timeoutActivity} )
399 : ()
400 )
401 }
402 }
403 );
404
405 if ($success) {
406 if ($rows) {
407 $internal_stats->{nb_purged}->{$type} += $rows;
408 }
409 if ($rtMinTimeout) {
410 my $rtSessions = $options->{backend}
411 ->searchLt( $options, '_oidcRtUpdate', time - $rtMinTimeout );
412 if ($rtSessions) {
413 foreach my $id ( keys %$rtSessions ) {
414 $checkRtExpiration->( $id, $rtSessions->{$id} );
415 }
416 }
417 }
418 $internal_stats->{end_time}->{$type} = Time::HiRes::time();
419
420 $purge_done = 1;
421 return unless @t;
422 }
423 else {
424 $self->logger->notice(
425 "deleteIfLowerThan failed during session purge"
426 . ", falling back to slower session enumeration" );
427 }
428 }
429
430 # If deleteIfLowerThan missing OR deleteIfLowerThan failed (#3656)
431 if ( !$purge_done ) {
432
433 # Get all expired sessions
434 Lemonldap::NG::Common::Apache::Session->get_key_from_all_sessions(
435 $options,
436 sub {
437 my $entry = shift;
438 my $id = shift;
439 my $time = time;
440
441 $self->logger->debug("Check session $id");
442
443 # Empty session need to be removed
444 unless ($entry) {
445 push @t, $id;
446 $self->logger->debug(
447 "Session $id is empty (corrupted?), delete forced");
448 }
449
450 # Do net check sessions without _utime
451 return undef unless $entry->{_utime};
452
453 # Do not expire persistent sessions
454 return undef if ( $entry->{_session_kind} eq "Persistent" );
455
456 # Session expired
457 if ( $time - $entry->{_utime} > $conf->{timeout} ) {
458 push @t, $id;
459 $self->logger->debug("Session $id expired");
460 }
461
462 # User has no activity, so considere the session has expired
463 elsif ( $conf->{timeoutActivity}
464 and $entry->{_lastSeen}
465 and $time - $entry->{_lastSeen} > $conf->{timeoutActivity} )
466 {
467 push @t, $id;
468 $self->logger->debug("Session $id inactive");
469 }
470 elsif ($rtMinTimeout) {
471 $checkRtExpiration->( $id, $entry );
472 }
473 undef;
474 }
475 );
476 }
477
478 # Delete sessions
479 my @errors;
480 for my $id (@t) {
481
482 my $session = Lemonldap::NG::Common::Session->new(
483 storageModule => $options->{backend},
484 storageModuleOptions => $options,
485 cacheModule => $conf->{localSessionStorage},
486 cacheModuleOptions => $conf->{localSessionStorageOptions},
487 id => $id,
488 );
489
490 unless ( $session->data ) {
491 $self->logger->debug("Error while opening session $id");
492 print STDERR "Error on session $id\n";
493 $internal_stats->{nb_error}->{$type}++;
494 push @errors, $id;
495 next;
496 }
497
498 unless ( $session->remove ) {
499 $self->logger->debug("Error while deleting session $id");
500 print STDERR "Error on session $id\n";
501 $internal_stats->{nb_error}->{$type}++;
502 push @errors, $id;
503 next;
504 }
505 $self->logger->debug("Session $id has been purged");
506 $internal_stats->{nb_purged}->{$type}++;
507 }
508
509 $self->_cleanup_lock_files( $type, $options, $internal_stats );
510 $self->_cleanup_errors( $type, $options, $internal_stats, \@errors );
511
512 $internal_stats->{end_time}->{$type} = Time::HiRes::time();
513 }
514
515 sub _cleanup_lock_files {
516 my ( $self, $type, $options, $internal_stats ) = @_;
517 my $conf = $self->conf;
518
519 # Remove lock files for File backend
520 if ( $options->{backend} =~ /^Apache::Session::(?:Browseable::)?File$/i ) {
521 require Apache::Session::Lock::File;
522 my $l = Apache::Session::Lock::File->new;
523 my $lock_directory = $options->{LockDirectory} || $options->{Directory};
524 $l->clean( $lock_directory, $conf->{timeout} );
525 }
526 return;
527 }
528
529 sub _cleanup_errors {
530 my ( $self, $type, $options, $internal_stats, $errors_ref ) = @_;
531 my @errors = @$errors_ref;
532 my $conf = $self->conf;
533
534 # Force deletion of corrupted sessions for File backend
535 if ( $options->{backend} =~ /^Apache::Session::(?:Browseable::)?File$/i
536 and $self->force )
537 {
538 foreach (@errors) {
539 my $id = $_;
540 eval { unlink $options->{Directory} . "/$id"; };
541 if ($@) {
542 $self->logger->error("Unable to remove session $id");
543 }
544 else {
545 $self->logger->warn("Session $id removed with force");
546 $internal_stats->{nb_error}->{$type}--;
547 }
548 }
549 }
550
551 # Force deletion of corrupted sessions for DBI backend
552 if ( $options->{backend} =~
553 /^Apache::Session::(?:Browseable::)?(MySQL|Postgres|DBI|Oracle|Informix|MySQLJSON|PgHstore|PgJSON|SQLLite|Sybase)$/i
554 and $self->force )
555 {
556 my $dbi = DBI->connect_cached( $options->{DataSource},
557 $options->{UserName}, $options->{Password} );
558 my $table = $options->{TableName} || "sessions";
559 my $req = $dbi->prepare("DELETE from $table WHERE id=?");
560 foreach (@errors) {
561 my $id = $_;
562 my $res = $req->execute($id);
563 unless ( $res == 1 ) {
564 $self->logger->error("Fail to delete session $id with force");
565 }
566 else {
567 $self->logger->warn("Session $id removed with force");
568 $internal_stats->{nb_error}->{$type}--;
569 }
570 }
571 }
572
573 # Force deletion of corrupted sessions for LDAP backend
574 if ( $options->{backend} =~ /^Apache::Session::(?:Browseable::)?LDAP$/i
575 and $self->force )
576 {
577 my $useTls = 0;
578 my $tlsParam;
579 my @servers = ();
580 foreach my $server ( split /[\s,]+/, $options->{ldapServer} ) {
581 if ( $server =~ m{^ldap\+tls://([^/]+)/?\??(.*)$} ) {
582 $useTls = 1;
583 $server = $1;
584 $tlsParam = $2 || "";
585 }
586 else {
587 $useTls = 0;
588 }
589 push @servers, $server;
590 }
591 my $ldap =
592 Net::LDAP->new( \@servers, keepalive => 1, onerror => undef, );
593 unless ($ldap) {
594 print STDERR "Unable to connect to LDAP server\n";
595 $internal_stats->{nb_error}->{$type}++;
596 return;
597 }
598
599 # Start TLS if needed
600 if ($useTls) {
601 my %h = split( /[&=]/, $tlsParam );
602 $h{verify} ||= $options->{ldapVerify} || "require";
603 $h{cafile} ||= $options->{ldapCAFile}
604 if ( $options->{ldapCAFile} );
605 $h{capath} ||= $options->{ldapCAPath}
606 if ( $options->{ldapCAPath} );
607 my $start_tls = $ldap->start_tls(%h);
608 if ( $start_tls->code ) {
609 print STDERR "STARTTLS error: "
610 . $start_tls->code . ': '
611 . $start_tls->error;
612 $internal_stats->{nb_error}->{$type}++;
613 return;
614 }
615 }
616
617 my $bind = $ldap->bind( $options->{ldapBindDN},
618 password => $options->{ldapBindPassword} );
619 my $attrId = $options->{ldapAttributeId} | "cn";
620 foreach (@errors) {
621 my $id = $_;
622 my $sessionDn =
623 $attrId . "=" . $id . "," . $options->{ldapConfBase};
624 my $delete = $ldap->delete($sessionDn);
625 if ( $delete->is_error ) {
626 $self->logger->error("Fail to delete session $id with force");
627 }
628 else {
629 $self->logger->warn("Session $id removed with force");
630 $internal_stats->{nb_error}->{$type}--;
631 }
632 }
633 }
634 return;
635 }
636
637 sub persistentPurge {
638 my ( $self, $filters ) = @_;
639
640 my $internal_stats;
641
642 $self->logger->info("Persistent session purge started");
643
644 $internal_stats->{start_time}->{total} = Time::HiRes::time();
645
646 $self->_purge_persistent( $filters, $internal_stats );
647
648 $internal_stats->{end_time}->{total} = Time::HiRes::time();
649
650 my $total_purged = 0;
651 $total_purged += $_ for values %{ $internal_stats->{nb_purged} };
652 $internal_stats->{nb_purged}->{total} = $total_purged;
653
654 my $total_error = 0;
655 $total_error += $_ for values %{ $internal_stats->{nb_error} };
656 $internal_stats->{nb_error}->{total} = $total_error;
657
658 my $return_stats;
659 for my $type ( "total", 'Persistent' ) {
660 $return_stats->{$type}->{errors} = $internal_stats->{nb_error}->{$type};
661 $return_stats->{$type}->{purged} =
662 $internal_stats->{nb_purged}->{$type};
663 $return_stats->{$type}->{duration_u} = int(
664 1000000 * (
665 $internal_stats->{end_time}->{$type} -
666 $internal_stats->{start_time}->{$type}
667 )
668 );
669 }
670
671 my $log = "Persistent session purge completed: ";
672 if ( $self->json ) {
673 $log .= to_json($return_stats);
674 }
675 else {
676 my @logentries;
677 for my $type ( "total", "Persistent" ) {
678 my $logentry .= "$type (";
679 $logentry .= $return_stats->{$type}->{purged} . " purged, ";
680 $logentry .= $return_stats->{$type}->{errors} . " errors in ";
681 $logentry .= $return_stats->{$type}->{duration_u} . " us";
682 $logentry .= ")";
683 push @logentries, $logentry;
684 }
685 $log .= join( ', ', @logentries );
686 }
687
688 $self->logger->notice($log);
689
690 return {
691 success => 1,
692
693 purged => $total_purged,
694 errors => $total_error,
695 stats => $return_stats,
696 };
697 }
698
699 sub _purge_persistent {
700 my ( $self, $filters, $internal_stats ) = @_;
701 my $type = "Persistent";
702 my $conf = $self->conf;
703 my $options = $self->persistent_backend_options;
704
705 my @t;
706
707 $internal_stats->{nb_error}->{$type} = 0;
708 $internal_stats->{nb_purged}->{$type} = 0;
709 $internal_stats->{start_time}->{$type} = Time::HiRes::time();
710
711 # Get all expired sessions
712 Lemonldap::NG::Common::Apache::Session->get_key_from_all_sessions(
713 $options,
714 sub {
715 my $entry = shift;
716 my $id = shift;
717 my $time = time;
718
719 $self->logger->debug("Check session $id");
720
721 unless ($entry) {
722 $self->logger->debug("Session $id is empty");
723 return;
724 }
725
726 # Ignore non-persistent sessions
727 return unless ( $entry->{_session_kind} eq "Persistent" );
728
729 # safety check: do not remove if there are no filters
730 my $remove = keys %$filters ? 1 : 0;
731
732 while ( my ( $filter_type, $value ) = each %$filters ) {
733 if ( my $filter_configuration =
734 $PSESSION_FILTERS{$filter_type} )
735 {
736 my $filter_result =
737 $filter_configuration->{filter}
738 ->( $self, $entry, $value );
739 $self->logger->debug(
740 "$filter_type filter result: " . int($filter_result) );
741 $remove &&= $filter_result;
742 }
743 else {
744 $self->logger->debug("Unknown filter $filter_type");
745 $remove = 0;
746 }
747 }
748
749 push @t, $id if $remove;
750
751 }
752 );
753
754 # Delete sessions
755 my @errors;
756 for my $id (@t) {
757
758 my $session = Lemonldap::NG::Common::Session->new(
759 storageModule => $options->{backend},
760 storageModuleOptions => $options,
761 cacheModule => $conf->{localSessionStorage},
762 cacheModuleOptions => $conf->{localSessionStorageOptions},
763 id => $id,
764 );
765
766 unless ( $session->data ) {
767 $self->logger->debug("Error while opening session $id");
768 print STDERR "Error on session $id\n";
769 $internal_stats->{nb_error}->{$type}++;
770 push @errors, $id;
771 next;
772 }
773
774 unless ( $session->remove ) {
775 $self->logger->debug("Error while deleting session $id");
776 print STDERR "Error on session $id\n";
777 $internal_stats->{nb_error}->{$type}++;
778 push @errors, $id;
779 next;
780 }
781 $self->logger->debug("Session $id has been purged");
782 $self->_auditPurgePsession( $session->data ) if $self->audit;
783 $internal_stats->{nb_purged}->{$type}++;
784 }
785 $internal_stats->{end_time}->{$type} = Time::HiRes::time();
786 }
787
788 sub localPurge {
789 my ($self) = @_;
790 $self->logger->debug( __PACKAGE__ . "::localPurge() called" );
791 my $conf = $self->conf;
792 if ( $conf->{localSessionStorage} ) {
793 eval "require $conf->{localSessionStorage}";
794 die $@ if $@;
795 $conf->{localSessionStorageOptions}->{default_expires_in} ||= 600;
796 my $s = $conf->{localSessionStorage}
797 ->new( $conf->{localSessionStorageOptions} );
798 $s->purge();
799 }
800 }
801
802 sub auditLog {
803 my ( $self, $req, %info ) = @_;
804 $self->_auditLogger->log( $req, %info );
805 }
806
807 sub _auditPurgePsession {
808 my ( $self, $session ) = @_;
809 my $dummy_req = Lemonldap::NG::Common::PSGI::Request->new(
810 { PATH_INFO => "", REQUEST_URI => "" } );
811 my $uid = $session->{_session_uid};
812 $self->auditLog(
813 $dummy_req,
814 message => ("Persistent session for $uid was removed"),
815 code => "PSESSION_REMOVED",
816 user => $uid,
817 );
818 }
819
820 1;