« blib/lib/Lemonldap/NG/Portal/Plugins/SamlFederation.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Issuer/OpenID.pm »
| 1 | package Lemonldap::NG::Portal::Plugins::GlobalLogout; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse; | ||
| 5 | use JSON qw(from_json to_json); | ||
| 6 | use Time::Local; | ||
| 7 | use Lemonldap::NG::Common::Session 'id2storage'; | ||
| 8 | use Lemonldap::NG::Portal::Main::Constants qw( | ||
| 9 | PE_OK | ||
| 10 | PE_ERROR | ||
| 11 | PE_NOTOKEN | ||
| 12 | PE_TOKENEXPIRED | ||
| 13 | PE_SENDRESPONSE | ||
| 14 | ); | ||
| 15 | |||
| 16 | our $VERSION = '2.22.0'; | ||
| 17 | |||
| 18 | extends qw( | ||
| 19 | Lemonldap::NG::Portal::Main::Plugin | ||
| 20 | Lemonldap::NG::Portal::Lib::OtherSessions | ||
| 21 | ); | ||
| 22 | |||
| 23 | # INTERFACE | ||
| 24 | use constant beforeLogout => 'run'; | ||
| 25 | |||
| 26 | # INITIALIZATION | ||
| 27 | has rule => ( is => 'rw', default => sub { 0 } ); | ||
| 28 | has ott => ( | ||
| 29 | is => 'rw', | ||
| 30 | lazy => 1, | ||
| 31 | default => sub { | ||
| 32 | my $ott = | ||
| 33 | $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken'); | ||
| 34 | $ott->timeout( $_[0]->conf->{formTimeout} ); | ||
| 35 | return $ott; | ||
| 36 | } | ||
| 37 | ); | ||
| 38 | |||
| 39 | sub init { | ||
| 40 | my ($self) = @_; | ||
| 41 | $self->addAuthRoute( globallogout => 'globalLogout', [ 'POST', 'GET' ] ); | ||
| 42 | |||
| 43 | # Parse activation rule | ||
| 44 | $self->rule( | ||
| 45 | $self->p->buildRule( $self->conf->{globalLogoutRule}, 'globalLogout' ) | ||
| 46 | ); | ||
| 47 | return 0 unless $self->rule; | ||
| 48 | |||
| 49 | return 1; | ||
| 50 | } | ||
| 51 | |||
| 52 | # RUNNING METHODS | ||
| 53 | # Look for user active SSO sessions and suggest to close them | ||
| 54 | sub run { | ||
| 55 | my ( $self, $req ) = @_; | ||
| 56 | my $user = $req->{userData}->{ $self->conf->{whatToTrace} }; | ||
| 57 | |||
| 58 | # Check activation rule | ||
| 59 | unless ( $self->rule->( $req, $req->userData ) ) { | ||
| 60 | $self->userLogger->info("GlobaLogout not allowed for $user"); | ||
| 61 | return PE_OK; | ||
| 62 | } | ||
| 63 | |||
| 64 | # Looking for active sessions | ||
| 65 | my $sessions = $self->activeSessions($req); | ||
| 66 | my $nbr = @{$sessions}; | ||
| 67 | $self->logger->debug("GlobalLogout: $nbr session(s) found") if $nbr; | ||
| 68 | return PE_OK unless ( $nbr > 1 ); | ||
| 69 | |||
| 70 | # Force GlobalLogout if timer is disabled | ||
| 71 | if ( $req->param('confirm') && $req->param('confirm') == 1 | ||
| 72 | || !$self->conf->{globalLogoutTimer} ) | ||
| 73 | { | ||
| 74 | my $msg = | ||
| 75 | $self->conf->{globalLogoutTimer} | ||
| 76 | ? 'GlobalLogout: confirm parameter detected' | ||
| 77 | : 'GlobalLogout: timer disabled'; | ||
| 78 | $self->logger->debug($msg); | ||
| 79 | $self->userLogger->info("GlobalLogout: force global logout for $user"); | ||
| 80 | $nbr = $self->removeOtherActiveSessions( $req, $sessions ); | ||
| 81 | $self->userLogger->info("$nbr remaining session(s) removed"); | ||
| 82 | |||
| 83 | return PE_OK; | ||
| 84 | } | ||
| 85 | |||
| 86 | # Prepare token | ||
| 87 | my $token = $self->ott->createToken( { | ||
| 88 | user => $user, | ||
| 89 | sessions => to_json($sessions) | ||
| 90 | } | ||
| 91 | ); | ||
| 92 | |||
| 93 | # Prepare form | ||
| 94 | $self->logger->debug("Prepare global logout confirmation"); | ||
| 95 | my $tmp = $self->p->sendHtml( | ||
| 96 | $req, | ||
| 97 | 'globallogout', | ||
| 98 | params => { | ||
| 99 | FORM_ACTION => | ||
| 100 | $self->p->relativeUrl( $req, 'globallogout', { all => 1 } ), | ||
| 101 | SESSIONS => $sessions, | ||
| 102 | TOKEN => $token, | ||
| 103 | LOGIN => $user, | ||
| 104 | CUSTOMPRM => $self->conf->{globalLogoutCustomParam} | ||
| 105 | } | ||
| 106 | ); | ||
| 107 | |||
| 108 | $req->response( | ||
| 109 | $req->wantJSON | ||
| 110 | ? $self->sendJSONresponse( $req, | ||
| 111 | { globalLogout => $nbr, confirmationRequired => 1 } ) | ||
| 112 | : $tmp | ||
| 113 | ); | ||
| 114 | |||
| 115 | return PE_SENDRESPONSE; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub globalLogout { | ||
| 119 | my ( $self, $req ) = @_; | ||
| 120 | my $res = PE_OK; | ||
| 121 | my $count = 0; | ||
| 122 | |||
| 123 | if ( $req->param('all') ) { | ||
| 124 | if ( my $token = $req->param('token') ) { | ||
| 125 | if ( $token = $self->ott->getToken($token) ) { | ||
| 126 | my $storeId = | ||
| 127 | $self->conf->{hashedSessionStore} | ||
| 128 | ? id2storage( $req->{userData}->{_session_id} ) | ||
| 129 | : $req->{userData}->{_session_id}; | ||
| 130 | |||
| 131 | # Read active sessions from token | ||
| 132 | my $sessions = eval { from_json( $token->{sessions} ) }; | ||
| 133 | if ($@) { | ||
| 134 | $self->logger->error( | ||
| 135 | "GlobalLogout: bad encoding in OTT ($@)"); | ||
| 136 | $res = PE_ERROR; | ||
| 137 | } | ||
| 138 | my $as; | ||
| 139 | my $user = $token->{user}; | ||
| 140 | my $req_user = | ||
| 141 | $req->{userData}->{ $self->{conf}->{whatToTrace} }; | ||
| 142 | if ( $req_user eq $user ) { | ||
| 143 | foreach (@$sessions) { | ||
| 144 | unless ( | ||
| 145 | |||
| 146 | # searchOn() returns sessions indexed by their | ||
| 147 | # storage ID, then it is required to set hashStore | ||
| 148 | # to 0 | ||
| 149 | $as = $self->p->getApacheSession( | ||
| 150 | $_->{id}, hashStore => 0, | ||
| 151 | ) | ||
| 152 | ) | ||
| 153 | { | ||
| 154 | $self->userLogger->info( | ||
| 155 | "GlobalLogout: session $_->{id} expired"); | ||
| 156 | next; | ||
| 157 | } | ||
| 158 | unless ( $storeId eq $_->{id} ) { | ||
| 159 | $self->userLogger->info( | ||
| 160 | "Remove \"$user\" session: $_->{id}"); | ||
| 161 | $as->remove; | ||
| 162 | $count++; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | } | ||
| 166 | else { | ||
| 167 | $self->userLogger->warn( | ||
| 168 | "GlobalLogout called with an invalid token: $req_user is NOT $user" | ||
| 169 | ); | ||
| 170 | $res = PE_TOKENEXPIRED; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | else { | ||
| 174 | $self->userLogger->error( | ||
| 175 | "GlobalLogout called with an expired token"); | ||
| 176 | $res = PE_TOKENEXPIRED; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | else { | ||
| 180 | $self->userLogger->error('GlobalLogout called without token'); | ||
| 181 | $res = PE_NOTOKEN; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | return $self->p->doPE( $req, $res ) if $res; | ||
| 186 | $self->userLogger->info("$count remaining session(s) removed"); | ||
| 187 | return $self->p->do( $req, [ 'authLogout', 'deleteSession' ] ); | ||
| 188 | } | ||
| 189 | |||
| 190 | sub activeSessions { | ||
| 191 | my ( $self, $req ) = @_; | ||
| 192 | my $activeSessions = []; | ||
| 193 | my $sessions = {}; | ||
| 194 | my $user = $req->{userData}->{ $self->conf->{whatToTrace} }; | ||
| 195 | my $customParam = $self->conf->{globalLogoutCustomParam} || ''; | ||
| 196 | |||
| 197 | # Try to retrieve sessions from sessions DB | ||
| 198 | if ($user) { | ||
| 199 | $self->logger->debug('Try to retrieve sessions from DB'); | ||
| 200 | my $moduleOptions = $self->conf->{globalStorageOptions} || {}; | ||
| 201 | $moduleOptions->{backend} = $self->conf->{globalStorage}; | ||
| 202 | $self->logger->debug("Looking for \"$user\" sessions..."); | ||
| 203 | $sessions = | ||
| 204 | $self->module->searchOn( $moduleOptions, $self->conf->{whatToTrace}, | ||
| 205 | $user ); | ||
| 206 | |||
| 207 | $self->logger->debug('Skip non-SSO session(s)...'); | ||
| 208 | my $other = 0; | ||
| 209 | foreach ( keys %$sessions ) { | ||
| 210 | unless ( $sessions->{$_}->{_session_kind} eq 'SSO' ) { | ||
| 211 | delete $sessions->{$_}; | ||
| 212 | $other++; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | $self->logger->info("$other non-SSO session(s) skipped") | ||
| 216 | if $other; | ||
| 217 | |||
| 218 | $self->logger->debug('Build an array ref with sessions info...'); | ||
| 219 | @$activeSessions = | ||
| 220 | map { | ||
| 221 | my $epoch; | ||
| 222 | my $regex = '^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$'; | ||
| 223 | if ( $_->{startTime} =~ /$regex/ ) { | ||
| 224 | $epoch = timelocal( $6, $5, $4, $3, $2 - 1, $1 ); | ||
| 225 | $_->{startTime} = $epoch; | ||
| 226 | } | ||
| 227 | else { | ||
| 228 | delete $_->{startTime}; | ||
| 229 | } | ||
| 230 | if ( $_->{updateTime} ) { | ||
| 231 | if ( $_->{updateTime} =~ /$regex/ ) { | ||
| 232 | |||
| 233 | $epoch = timelocal( $6, $5, $4, $3, $2 - 1, $1 ); | ||
| 234 | $_->{updateTime} = $epoch; | ||
| 235 | } | ||
| 236 | else { | ||
| 237 | delete $_->{updateTime}; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | $_; | ||
| 241 | } | ||
| 242 | sort { $b->{startTime} cmp $a->{startTime} } map { | ||
| 243 | { | ||
| 244 | id => $_, | ||
| 245 | customParam => $sessions->{$_}->{$customParam}, | ||
| 246 | ipAddr => $sessions->{$_}->{ipAddr}, | ||
| 247 | authLevel => $sessions->{$_}->{authenticationLevel}, | ||
| 248 | startTime => $sessions->{$_}->{_startTime}, | ||
| 249 | updateTime => $sessions->{$_}->{_updateTime} | ||
| 250 | }; | ||
| 251 | } keys %$sessions; | ||
| 252 | } | ||
| 253 | |||
| 254 | return $activeSessions; | ||
| 255 | } | ||
| 256 | |||
| 257 | sub removeOtherActiveSessions { | ||
| 258 | my ( $self, $req, $sessions ) = @_; | ||
| 259 | my $count = 0; | ||
| 260 | my $as; | ||
| 261 | |||
| 262 | my $storeId = | ||
| 263 | $self->conf->{hashedSessionStore} | ||
| 264 | ? id2storage( $req->{userData}->{_session_id} ) | ||
| 265 | : $req->{userData}->{_session_id}; | ||
| 266 | foreach (@$sessions) { | ||
| 267 | |||
| 268 | # searchOn() returns sessions indexed by their storage ID, then | ||
| 269 | # it is required to set hashStore to 0 | ||
| 270 | unless ( $as = $self->p->getApacheSession( $_->{id}, hashStore => 0, ) ) | ||
| 271 | { | ||
| 272 | $self->userLogger->info("GlobalLogout: session $_->{id} expired"); | ||
| 273 | next; | ||
| 274 | } | ||
| 275 | unless ( $storeId eq $_->{id} ) { | ||
| 276 | $self->userLogger->info( | ||
| 277 | "Remove \"$req->{userData}->{ $self->conf->{whatToTrace} }\" session: $_->{id}" | ||
| 278 | ); | ||
| 279 | $as->remove; | ||
| 280 | $count++; | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | return $count; | ||
| 285 | } | ||
| 286 | |||
| 287 | 1; |