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 # Real purge
386 if ( $options->{backend}->can('deleteIfLowerThan') ) {
387 $self->logger->debug("Found deleteIfLowerThan() in backend, using it");
388 my ( $success, $rows ) = $options->{backend}->deleteIfLowerThan(
389 $options,
390 {
391 not => { '_session_kind' => 'Persistent' },
392 or => {
393 _utime => time - $conf->{timeout},
394 (
395 $conf->{timeoutActivity}
396 ? ( _lastSeen => time - $conf->{timeoutActivity} )
397 : ()
398 )
399 }
400 }
401 );
402
403 if ($success) {
404 if ($rows) {
405 $internal_stats->{nb_purged}->{$type} += $rows;
406 }
407 if ($rtMinTimeout) {
408 my $rtSessions = $options->{backend}
409 ->searchLt( $options, '_oidcRtUpdate', time - $rtMinTimeout );
410 if ($rtSessions) {
411 foreach my $id ( keys %$rtSessions ) {
412 $checkRtExpiration->( $id, $rtSessions->{$id} );
413 }
414 }
415 }
416 $internal_stats->{end_time}->{$type} = Time::HiRes::time();
417 return unless @t;
418 }
419 }
420
421 else {
422 # Get all expired sessions
423 Lemonldap::NG::Common::Apache::Session->get_key_from_all_sessions(
424 $options,
425 sub {
426 my $entry = shift;
427 my $id = shift;
428 my $time = time;
429
430 $self->logger->debug("Check session $id");
431
432 # Empty session need to be removed
433 unless ($entry) {
434 push @t, $id;
435 $self->logger->debug(
436 "Session $id is empty (corrupted?), delete forced");
437 }
438
439 # Do net check sessions without _utime
440 return undef unless $entry->{_utime};
441
442 # Do not expire persistent sessions
443 return undef if ( $entry->{_session_kind} eq "Persistent" );
444
445 # Session expired
446 if ( $time - $entry->{_utime} > $conf->{timeout} ) {
447 push @t, $id;
448 $self->logger->debug("Session $id expired");
449 }
450
451 # User has no activity, so considere the session has expired
452 elsif ( $conf->{timeoutActivity}
453 and $entry->{_lastSeen}
454 and $time - $entry->{_lastSeen} > $conf->{timeoutActivity} )
455 {
456 push @t, $id;
457 $self->logger->debug("Session $id inactive");
458 }
459 elsif ($rtMinTimeout) {
460 $checkRtExpiration->( $id, $entry );
461 }
462 undef;
463 }
464 );
465 }
466
467 # Delete sessions
468 my @errors;
469 for my $id (@t) {
470
471 my $session = Lemonldap::NG::Common::Session->new(
472 storageModule => $options->{backend},
473 storageModuleOptions => $options,
474 cacheModule => $conf->{localSessionStorage},
475 cacheModuleOptions => $conf->{localSessionStorageOptions},
476 id => $id,
477 );
478
479 unless ( $session->data ) {
480 $self->logger->debug("Error while opening session $id");
481 print STDERR "Error on session $id\n";
482 $internal_stats->{nb_error}->{$type}++;
483 push @errors, $id;
484 next;
485 }
486
487 unless ( $session->remove ) {
488 $self->logger->debug("Error while deleting session $id");
489 print STDERR "Error on session $id\n";
490 $internal_stats->{nb_error}->{$type}++;
491 push @errors, $id;
492 next;
493 }
494 $self->logger->debug("Session $id has been purged");
495 $internal_stats->{nb_purged}->{$type}++;
496 }
497
498 $self->_cleanup_lock_files( $type, $options, $internal_stats );
499 $self->_cleanup_errors( $type, $options, $internal_stats, \@errors );
500
501 $internal_stats->{end_time}->{$type} = Time::HiRes::time();
502 }
503
504 sub _cleanup_lock_files {
505 my ( $self, $type, $options, $internal_stats ) = @_;
506 my $conf = $self->conf;
507
508 # Remove lock files for File backend
509 if ( $options->{backend} =~ /^Apache::Session::(?:Browseable::)?File$/i ) {
510 require Apache::Session::Lock::File;
511 my $l = Apache::Session::Lock::File->new;
512 my $lock_directory = $options->{LockDirectory} || $options->{Directory};
513 $l->clean( $lock_directory, $conf->{timeout} );
514 }
515 return;
516 }
517
518 sub _cleanup_errors {
519 my ( $self, $type, $options, $internal_stats, $errors_ref ) = @_;
520 my @errors = @$errors_ref;
521 my $conf = $self->conf;
522
523 # Force deletion of corrupted sessions for File backend
524 if ( $options->{backend} =~ /^Apache::Session::(?:Browseable::)?File$/i
525 and $self->force )
526 {
527 foreach (@errors) {
528 my $id = $_;
529 eval { unlink $options->{Directory} . "/$id"; };
530 if ($@) {
531 $self->logger->error("Unable to remove session $id");
532 }
533 else {
534 $self->logger->warn("Session $id removed with force");
535 $internal_stats->{nb_error}->{$type}--;
536 }
537 }
538 }
539
540 # Force deletion of corrupted sessions for DBI backend
541 if ( $options->{backend} =~
542 /^Apache::Session::(?:Browseable::)?(MySQL|Postgres|DBI|Oracle|Informix|MySQLJSON|PgHstore|PgJSON|SQLLite|Sybase)$/i
543 and $self->force )
544 {
545 my $dbi = DBI->connect_cached( $options->{DataSource},
546 $options->{UserName}, $options->{Password} );
547 my $table = $options->{TableName} || "sessions";
548 my $req = $dbi->prepare("DELETE from $table WHERE id=?");
549 foreach (@errors) {
550 my $id = $_;
551 my $res = $req->execute($id);
552 unless ( $res == 1 ) {
553 $self->logger->error("Fail to delete session $id with force");
554 }
555 else {
556 $self->logger->warn("Session $id removed with force");
557 $internal_stats->{nb_error}->{$type}--;
558 }
559 }
560 }
561
562 # Force deletion of corrupted sessions for LDAP backend
563 if ( $options->{backend} =~ /^Apache::Session::(?:Browseable::)?LDAP$/i
564 and $self->force )
565 {
566 my $useTls = 0;
567 my $tlsParam;
568 my @servers = ();
569 foreach my $server ( split /[\s,]+/, $options->{ldapServer} ) {
570 if ( $server =~ m{^ldap\+tls://([^/]+)/?\??(.*)$} ) {
571 $useTls = 1;
572 $server = $1;
573 $tlsParam = $2 || "";
574 }
575 else {
576 $useTls = 0;
577 }
578 push @servers, $server;
579 }
580 my $ldap =
581 Net::LDAP->new( \@servers, keepalive => 1, onerror => undef, );
582 unless ($ldap) {
583 print STDERR "Unable to connect to LDAP server\n";
584 $internal_stats->{nb_error}->{$type}++;
585 return;
586 }
587
588 # Start TLS if needed
589 if ($useTls) {
590 my %h = split( /[&=]/, $tlsParam );
591 $h{verify} ||= $options->{ldapVerify} || "require";
592 $h{cafile} ||= $options->{ldapCAFile}
593 if ( $options->{ldapCAFile} );
594 $h{capath} ||= $options->{ldapCAPath}
595 if ( $options->{ldapCAPath} );
596 my $start_tls = $ldap->start_tls(%h);
597 if ( $start_tls->code ) {
598 print STDERR "STARTTLS error: "
599 . $start_tls->code . ': '
600 . $start_tls->error;
601 $internal_stats->{nb_error}->{$type}++;
602 return;
603 }
604 }
605
606 my $bind = $ldap->bind( $options->{ldapBindDN},
607 password => $options->{ldapBindPassword} );
608 my $attrId = $options->{ldapAttributeId} | "cn";
609 foreach (@errors) {
610 my $id = $_;
611 my $sessionDn =
612 $attrId . "=" . $id . "," . $options->{ldapConfBase};
613 my $delete = $ldap->delete($sessionDn);
614 if ( $delete->is_error ) {
615 $self->logger->error("Fail to delete session $id with force");
616 }
617 else {
618 $self->logger->warn("Session $id removed with force");
619 $internal_stats->{nb_error}->{$type}--;
620 }
621 }
622 }
623 return;
624 }
625
626 sub persistentPurge {
627 my ( $self, $filters ) = @_;
628
629 my $internal_stats;
630
631 $self->logger->info("Persistent session purge started");
632
633 $internal_stats->{start_time}->{total} = Time::HiRes::time();
634
635 $self->_purge_persistent( $filters, $internal_stats );
636
637 $internal_stats->{end_time}->{total} = Time::HiRes::time();
638
639 my $total_purged = 0;
640 $total_purged += $_ for values %{ $internal_stats->{nb_purged} };
641 $internal_stats->{nb_purged}->{total} = $total_purged;
642
643 my $total_error = 0;
644 $total_error += $_ for values %{ $internal_stats->{nb_error} };
645 $internal_stats->{nb_error}->{total} = $total_error;
646
647 my $return_stats;
648 for my $type ( "total", 'Persistent' ) {
649 $return_stats->{$type}->{errors} = $internal_stats->{nb_error}->{$type};
650 $return_stats->{$type}->{purged} =
651 $internal_stats->{nb_purged}->{$type};
652 $return_stats->{$type}->{duration_u} = int(
653 1000000 * (
654 $internal_stats->{end_time}->{$type} -
655 $internal_stats->{start_time}->{$type}
656 )
657 );
658 }
659
660 my $log = "Persistent session purge completed: ";
661 if ( $self->json ) {
662 $log .= to_json($return_stats);
663 }
664 else {
665 my @logentries;
666 for my $type ( "total", "Persistent" ) {
667 my $logentry .= "$type (";
668 $logentry .= $return_stats->{$type}->{purged} . " purged, ";
669 $logentry .= $return_stats->{$type}->{errors} . " errors in ";
670 $logentry .= $return_stats->{$type}->{duration_u} . " us";
671 $logentry .= ")";
672 push @logentries, $logentry;
673 }
674 $log .= join( ', ', @logentries );
675 }
676
677 $self->logger->notice($log);
678
679 return {
680 success => 1,
681
682 purged => $total_purged,
683 errors => $total_error,
684 stats => $return_stats,
685 };
686 }
687
688 sub _purge_persistent {
689 my ( $self, $filters, $internal_stats ) = @_;
690 my $type = "Persistent";
691 my $conf = $self->conf;
692 my $options = $self->persistent_backend_options;
693
694 my @t;
695
696 $internal_stats->{nb_error}->{$type} = 0;
697 $internal_stats->{nb_purged}->{$type} = 0;
698 $internal_stats->{start_time}->{$type} = Time::HiRes::time();
699
700 # Get all expired sessions
701 Lemonldap::NG::Common::Apache::Session->get_key_from_all_sessions(
702 $options,
703 sub {
704 my $entry = shift;
705 my $id = shift;
706 my $time = time;
707
708 $self->logger->debug("Check session $id");
709
710 unless ($entry) {
711 $self->logger->debug("Session $id is empty");
712 return;
713 }
714
715 # Ignore non-persistent sessions
716 return unless ( $entry->{_session_kind} eq "Persistent" );
717
718 # safety check: do not remove if there are no filters
719 my $remove = keys %$filters ? 1 : 0;
720
721 while ( my ( $filter_type, $value ) = each %$filters ) {
722 if ( my $filter_configuration =
723 $PSESSION_FILTERS{$filter_type} )
724 {
725 my $filter_result =
726 $filter_configuration->{filter}
727 ->( $self, $entry, $value );
728 $self->logger->debug(
729 "$filter_type filter result: " . int($filter_result) );
730 $remove &&= $filter_result;
731 }
732 else {
733 $self->logger->debug("Unknown filter $filter_type");
734 $remove = 0;
735 }
736 }
737
738 push @t, $id if $remove;
739
740 }
741 );
742
743 # Delete sessions
744 my @errors;
745 for my $id (@t) {
746
747 my $session = Lemonldap::NG::Common::Session->new(
748 storageModule => $options->{backend},
749 storageModuleOptions => $options,
750 cacheModule => $conf->{localSessionStorage},
751 cacheModuleOptions => $conf->{localSessionStorageOptions},
752 id => $id,
753 );
754
755 unless ( $session->data ) {
756 $self->logger->debug("Error while opening session $id");
757 print STDERR "Error on session $id\n";
758 $internal_stats->{nb_error}->{$type}++;
759 push @errors, $id;
760 next;
761 }
762
763 unless ( $session->remove ) {
764 $self->logger->debug("Error while deleting session $id");
765 print STDERR "Error on session $id\n";
766 $internal_stats->{nb_error}->{$type}++;
767 push @errors, $id;
768 next;
769 }
770 $self->logger->debug("Session $id has been purged");
771 $self->_auditPurgePsession( $session->data ) if $self->audit;
772 $internal_stats->{nb_purged}->{$type}++;
773 }
774 $internal_stats->{end_time}->{$type} = Time::HiRes::time();
775 }
776
777 sub localPurge {
778 my ($self) = @_;
779 $self->logger->debug( __PACKAGE__ . "::localPurge() called" );
780 my $conf = $self->conf;
781 if ( $conf->{localSessionStorage} ) {
782 eval "require $conf->{localSessionStorage}";
783 die $@ if $@;
784 $conf->{localSessionStorageOptions}->{default_expires_in} ||= 600;
785 my $s = $conf->{localSessionStorage}
786 ->new( $conf->{localSessionStorageOptions} );
787 $s->purge();
788 }
789 }
790
791 sub auditLog {
792 my ( $self, $req, %info ) = @_;
793 $self->_auditLogger->log( $req, %info );
794 }
795
796 sub _auditPurgePsession {
797 my ( $self, $session ) = @_;
798 my $dummy_req = Lemonldap::NG::Common::PSGI::Request->new(
799 { PATH_INFO => "", REQUEST_URI => "" } );
800 my $uid = $session->{_session_uid};
801 $self->auditLog(
802 $dummy_req,
803 message => ("Persistent session for $uid was removed"),
804 code => "PSESSION_REMOVED",
805 user => $uid,
806 );
807 }
808
809 1;