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