« blib/lib/Lemonldap/NG/Common/Conf/Compact.pm
↑ Summary
blib/lib/Lemonldap/NG/Common/Conf/Backends/LDAP.pm »
| 1 | package Lemonldap::NG::Common::Session::REST; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse; | ||
| 5 | use Lemonldap::NG::Common::Util qw(isHiddenAttr); | ||
| 6 | use Lemonldap::NG::Common::Conf::Constants; | ||
| 7 | use Lemonldap::NG::Common::Util qw/display2F getPSessionID/; | ||
| 8 | use JSON qw(from_json to_json); | ||
| 9 | |||
| 10 | our $VERSION = '2.21.0'; | ||
| 11 | |||
| 12 | has sessionTypes => ( is => 'rw' ); | ||
| 13 | |||
| 14 | # Boolean value to tell if storage ID is hashed or not | ||
| 15 | has hashedSessionStore => ( is => 'rw' ); | ||
| 16 | |||
| 17 | sub setTypes { | ||
| 18 | my ( $self, $conf ) = @_; | ||
| 19 | foreach my $type (@sessionTypes) { | ||
| 20 | if ( my $tmp = | ||
| 21 | $self->{ $type . 'Storage' } || $conf->{ $type . 'Storage' } ) | ||
| 22 | { | ||
| 23 | $self->{sessionTypes}->{$type}->{module} = $tmp; | ||
| 24 | $self->{sessionTypes}->{$type}->{options} = | ||
| 25 | $self->{ $type . 'StorageOptions' } | ||
| 26 | || $conf->{ $type . 'StorageOptions' } | ||
| 27 | || {}; | ||
| 28 | $self->{sessionTypes}->{$type}->{kind} = | ||
| 29 | ( $type eq 'global' ? 'SSO' : ucfirst($type) ); | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | my $offlinebackend = $self->{sessionTypes}->{oidc} ? 'oidc' : 'global'; | ||
| 34 | $self->{sessionTypes}->{offline}->{module} = | ||
| 35 | $self->{sessionTypes}->{$offlinebackend}->{module}; | ||
| 36 | $self->{sessionTypes}->{offline}->{options} = | ||
| 37 | $self->{sessionTypes}->{$offlinebackend}->{options}; | ||
| 38 | $self->{sessionTypes}->{offline}->{kind} = "OIDCI"; | ||
| 39 | $self->hashedSessionStore( $conf->{hashedSessionStore} ); | ||
| 40 | } | ||
| 41 | |||
| 42 | ### SEE LEMONLDAP::NG::COMMON::SESSION FOR AVAILABLE FUNCTIONS | ||
| 43 | |||
| 44 | sub delSession { | ||
| 45 | my ( $self, $req ) = @_; | ||
| 46 | my $type = $req->params('sessionType'); | ||
| 47 | my $mod = $self->getMod($req) | ||
| 48 | or return $self->sendError( $req, undef, 400 ); | ||
| 49 | my $id = $req->params('sessionId') | ||
| 50 | or return $self->sendError( $req, 'sessionId is missing', 400 ); | ||
| 51 | my $session = $self->getApacheSession( $mod, $id ); | ||
| 52 | $self->auditLog( | ||
| 53 | $req, | ||
| 54 | message => "Deleted $type session: $id", | ||
| 55 | code => "SESSION_DELETED", | ||
| 56 | type => $type, | ||
| 57 | id => $id | ||
| 58 | ); | ||
| 59 | $session->remove; | ||
| 60 | |||
| 61 | Lemonldap::NG::Handler::Main->publishEvent( $req, | ||
| 62 | { action => 'unlog', id => $id } ); | ||
| 63 | return $session->error | ||
| 64 | ? $self->sendError( $req, $session->error, 200 ) | ||
| 65 | : $self->sendJSONresponse( $req, { result => 1 } ); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub deleteOIDCConsent { | ||
| 69 | my ( $self, $req ) = @_; | ||
| 70 | my $mod = $self->getMod($req) | ||
| 71 | or return $self->sendError( $req, undef, 400 ); | ||
| 72 | my $id = $req->params('sessionId') | ||
| 73 | or return $self->sendError( $req, 'sessionId is missing', 400 ); | ||
| 74 | |||
| 75 | # Try to read session | ||
| 76 | $self->logger->debug("Loading session: $id"); | ||
| 77 | my $session = $self->getApacheSession( $mod, $id ) | ||
| 78 | or return $self->sendError( $req, undef, 400 ); | ||
| 79 | |||
| 80 | # Try to read OIDC Consent parameters | ||
| 81 | $self->logger->debug("Reading parameters..."); | ||
| 82 | my $params = $req->parameters(); | ||
| 83 | my $rp = $params->{rp} | ||
| 84 | or | ||
| 85 | return $self->sendError( $req, 'OIDC consent "RP" parameter is missing', | ||
| 86 | 400 ); | ||
| 87 | my $epoch = $params->{epoch} | ||
| 88 | or return $self->sendError( $req, | ||
| 89 | 'OIDC consent "epoch" parameter is missing', 400 ); | ||
| 90 | |||
| 91 | # Try to load OIDC Consents from session | ||
| 92 | $self->logger->debug("Looking for OIDC consent(s)..."); | ||
| 93 | my $_oidcConsents; | ||
| 94 | if ( $session->data->{_oidcConsents} ) { | ||
| 95 | $_oidcConsents = eval { | ||
| 96 | from_json( $session->data->{_oidcConsents}, { allow_nonref => 1 } ); | ||
| 97 | }; | ||
| 98 | if ($@) { | ||
| 99 | $self->logger->error("Corrupted session (_oidcConsents): $@"); | ||
| 100 | return $self->p->sendError( $req, "Corrupted session", 500 ); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | else { | ||
| 104 | $self->logger->debug("No OIDC consent found"); | ||
| 105 | $_oidcConsents = []; | ||
| 106 | } | ||
| 107 | |||
| 108 | # Delete OIDC Consent | ||
| 109 | $self->logger->debug("Reading OIDC consent(s)..."); | ||
| 110 | my @keep = (); | ||
| 111 | while (@$_oidcConsents) { | ||
| 112 | my $element = shift @$_oidcConsents; | ||
| 113 | |||
| 114 | $self->logger->debug( | ||
| 115 | "Searching for OIDC Consent to delete -> $rp / $epoch..."); | ||
| 116 | if ( defined $element->{rp} && defined $element->{epoch} ) { | ||
| 117 | push @keep, $element | ||
| 118 | unless ( ( $element->{rp} eq $rp ) | ||
| 119 | and ( $element->{epoch} eq $epoch ) ); | ||
| 120 | } | ||
| 121 | else { | ||
| 122 | $self->logger->error("Corrupted OIDC consent"); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | # Update session | ||
| 127 | $self->logger->debug("Saving OIDC consents..."); | ||
| 128 | $session->data->{_oidcConsents} = to_json( \@keep ); | ||
| 129 | $self->logger->debug("Update session"); | ||
| 130 | $session->update( \%{ $session->data } ); | ||
| 131 | |||
| 132 | Lemonldap::NG::Handler::PSGI::Main->localUnlog( $req, $id ); | ||
| 133 | return $session->error | ||
| 134 | ? $self->sendError( $req, $session->error, 200 ) | ||
| 135 | : $self->sendJSONresponse( $req, { result => 1 } ); | ||
| 136 | } | ||
| 137 | |||
| 138 | sub get2F { | ||
| 139 | my ( $self, $req, $session, $skey ) = @_; | ||
| 140 | |||
| 141 | # Case 1: only one session is required | ||
| 142 | if ($session) { | ||
| 143 | return $self->session( $req, $session, $skey ); | ||
| 144 | } | ||
| 145 | |||
| 146 | my $mod = $self->getMod($req) | ||
| 147 | or return $self->sendError( $req, 'Bad mode', 400 ); | ||
| 148 | my $params = $req->parameters(); | ||
| 149 | my $type = delete $params->{sessionType}; | ||
| 150 | $type = ucfirst($type); | ||
| 151 | |||
| 152 | # Case 2: list of sessions | ||
| 153 | my $res = $self->search2F( $req, $mod, $type, $params ); | ||
| 154 | |||
| 155 | if ( $res->{result} ) { | ||
| 156 | return $self->sendJSONresponse( $req, $res ); | ||
| 157 | } | ||
| 158 | else { | ||
| 159 | my $code = $res->{code} || 500; | ||
| 160 | my $msg = $res->{msg} || "Failed to search 2FAs"; | ||
| 161 | return $self->sendError( $req, $msg, $code ); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | sub search2F { | ||
| 166 | my ( $self, $req, $mod, $type, $params, $return_devices ) = @_; | ||
| 167 | |||
| 168 | my $whatToTrace = Lemonldap::NG::Handler::PSGI::Main->tsv->{whatToTrace}; | ||
| 169 | |||
| 170 | # 2.1 Get fields to require | ||
| 171 | my @fields = ( '_httpSessionType', $whatToTrace, '_2fDevices' ); | ||
| 172 | if ( my $groupBy = $params->{groupBy} ) { | ||
| 173 | $groupBy =~ s/^substr\((\w+)(?:,\d+(?:,\d+)?)?\)$/$1/; | ||
| 174 | $groupBy =~ s/^_whatToTrace$/$whatToTrace/o | ||
| 175 | or push @fields, $groupBy; | ||
| 176 | } | ||
| 177 | else { | ||
| 178 | push @fields, '_utime'; | ||
| 179 | } | ||
| 180 | |||
| 181 | # 2.2 Restrict query if possible: search for filters (any query arg that is | ||
| 182 | # not a keyword) | ||
| 183 | my $moduleOptions = $mod->{options}; | ||
| 184 | $moduleOptions->{backend} = $mod->{module}; | ||
| 185 | |||
| 186 | my @display_types = $params->get_all('type'); | ||
| 187 | $params->remove('type'); | ||
| 188 | |||
| 189 | my %filters = map { | ||
| 190 | my $s = $_; | ||
| 191 | $s =~ s/\b_whatToTrace\b/$whatToTrace/o; | ||
| 192 | /^groupBy$/ | ||
| 193 | ? () | ||
| 194 | : ( $s => $params->{$_} ); | ||
| 195 | } keys %$params; | ||
| 196 | $filters{_session_kind} = $type; | ||
| 197 | |||
| 198 | push @fields, keys(%filters); | ||
| 199 | { | ||
| 200 | my %seen; | ||
| 201 | @fields = grep { !$seen{$_}++ } @fields; | ||
| 202 | } | ||
| 203 | |||
| 204 | # For now, only one argument can be passed to | ||
| 205 | # Lemonldap::NG::Common::Apache::Session so just the first filter is | ||
| 206 | # used | ||
| 207 | my ($firstFilter) = sort { | ||
| 208 | $filters{$a} =~ m#^[\w:]+/\d+\*?$# ? 1 | ||
| 209 | : $filters{$b} =~ m#^[\w:]+/\d+\*?$# ? -1 | ||
| 210 | : $a eq '_session_kind' ? 1 | ||
| 211 | : $b eq '_session_kind' ? -1 | ||
| 212 | : $a cmp $b | ||
| 213 | } keys %filters; | ||
| 214 | |||
| 215 | # Check if a '*' is required | ||
| 216 | my $function = 'searchOn'; | ||
| 217 | $function = 'searchOnExpr' | ||
| 218 | if ( grep { /\*/ and not m#^[\w:]+/\d+\*?$# } | ||
| 219 | ( $filters{$firstFilter} ) ); | ||
| 220 | $self->logger->debug( | ||
| 221 | "First filter: $firstFilter = $filters{$firstFilter} ($function)"); | ||
| 222 | |||
| 223 | my $res = | ||
| 224 | Lemonldap::NG::Common::Apache::Session->$function( $moduleOptions, | ||
| 225 | $firstFilter, $filters{$firstFilter}, @fields ); | ||
| 226 | |||
| 227 | return { | ||
| 228 | result => 1, | ||
| 229 | count => 0, | ||
| 230 | total => 0, | ||
| 231 | values => [] | ||
| 232 | } | ||
| 233 | unless ( $res and %$res ); | ||
| 234 | |||
| 235 | delete $filters{$firstFilter} | ||
| 236 | unless ( grep { /\*/ and not m#^[\w:]+/\d+\*?$# } | ||
| 237 | ( $filters{$firstFilter} ) ); | ||
| 238 | foreach my $k ( keys %filters ) { | ||
| 239 | $self->logger->debug("Removing unless $k =~ /^$filters{$k}\$/"); | ||
| 240 | $filters{$k} =~ s/\./\\./g; | ||
| 241 | $filters{$k} =~ s/\*/\.\*/g; | ||
| 242 | foreach my $session ( keys %$res ) { | ||
| 243 | if ( $res->{$session}->{$k} ) { | ||
| 244 | delete $res->{$session} | ||
| 245 | unless ( $res->{$session}->{$k} =~ /^$filters{$k}$/ ); | ||
| 246 | } | ||
| 247 | } | ||
| 248 | } | ||
| 249 | |||
| 250 | # Remove sessions without at least one 2F device(s) | ||
| 251 | $self->logger->debug( | ||
| 252 | "Removing sessions without at least one 2F device(s)..."); | ||
| 253 | foreach my $session ( keys %$res ) { | ||
| 254 | delete $res->{$session} | ||
| 255 | unless ( defined $res->{$session}->{_2fDevices} | ||
| 256 | and $res->{$session}->{_2fDevices} =~ /"type"/s ); | ||
| 257 | } | ||
| 258 | |||
| 259 | my $all = ( keys %$res ); | ||
| 260 | |||
| 261 | # Filter 2FA sessions if needed | ||
| 262 | if (@display_types) { | ||
| 263 | $self->logger->debug("Filtering 2F sessions..."); | ||
| 264 | foreach (@display_types) { | ||
| 265 | foreach my $session ( keys %$res ) { | ||
| 266 | delete $res->{$session} | ||
| 267 | unless ( defined $res->{$session}->{_2fDevices} | ||
| 268 | and $res->{$session}->{_2fDevices} =~ /"type":\s*"$_"/s ); | ||
| 269 | } | ||
| 270 | $self->logger->debug( | ||
| 271 | "Removing sessions unless a $_ device is registered"); | ||
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | my $total = ( keys %$res ); | ||
| 276 | $self->logger->debug("2FA session(s) left : $total / $all"); | ||
| 277 | |||
| 278 | if ( my $group = $req->params('groupBy') ) { | ||
| 279 | my $r; | ||
| 280 | $group =~ s/\b_whatToTrace\b/$whatToTrace/o; | ||
| 281 | |||
| 282 | # Substrings | ||
| 283 | if ( $group =~ /^substr\((\w+)(?:,(\d+)(?:,(\d+))?)?\)$/ ) { | ||
| 284 | my ( $field, $length, $start ) = ( $1, $2, $3 ); | ||
| 285 | $start ||= 0; | ||
| 286 | $length = 1 if ( $length < 1 ); | ||
| 287 | foreach my $k ( keys %$res ) { | ||
| 288 | $r->{ substr $res->{$k}->{$field}, $start, $length }++ | ||
| 289 | if ( $res->{$k}->{$field} ); | ||
| 290 | } | ||
| 291 | $group = $field; | ||
| 292 | } | ||
| 293 | |||
| 294 | # Simple field groupBy query | ||
| 295 | elsif ( $group =~ /^\w+$/ ) { | ||
| 296 | eval { | ||
| 297 | foreach my $k ( keys %$res ) { | ||
| 298 | $r->{ $res->{$k}->{$group} }++; | ||
| 299 | } | ||
| 300 | }; | ||
| 301 | return { | ||
| 302 | result => 0, | ||
| 303 | msg => | ||
| 304 | qq{Use of an uninitialized attribute "$group" to group sessions}, | ||
| 305 | code => 400, | ||
| 306 | } | ||
| 307 | if ($@); | ||
| 308 | } | ||
| 309 | else { | ||
| 310 | return { | ||
| 311 | result => 0, | ||
| 312 | msg => 'Syntax error in groupBy', | ||
| 313 | code => 400, | ||
| 314 | }; | ||
| 315 | } | ||
| 316 | |||
| 317 | # Build result | ||
| 318 | $res = [ | ||
| 319 | sort { | ||
| 320 | my @a = ( $a->{value} =~ /^(\d+)(?:\.(\d+))*$/ ); | ||
| 321 | my @b = ( $b->{value} =~ /^(\d+)(?:\.(\d+))*$/ ); | ||
| 322 | ( @a and @b ) | ||
| 323 | ? ( $a[0] <=> $b[0] | ||
| 324 | or $a[1] <=> $b[1] | ||
| 325 | or $a[2] <=> $b[2] | ||
| 326 | or $a[3] <=> $b[3] ) | ||
| 327 | : $a->{value} cmp $b->{value} | ||
| 328 | } | ||
| 329 | map { { value => $_, count => $r->{$_} } } keys %$r | ||
| 330 | ]; | ||
| 331 | } | ||
| 332 | |||
| 333 | # Else, $res elements will be like: | ||
| 334 | # { session => <sessionId>, userId => <_session_uid> } | ||
| 335 | else { | ||
| 336 | $res = [ | ||
| 337 | sort { $a->{userId} cmp $b->{userId} } | ||
| 338 | map { | ||
| 339 | { | ||
| 340 | session => $_, | ||
| 341 | userId => $res->{$_}->{_session_uid}, | ||
| 342 | ( | ||
| 343 | $return_devices | ||
| 344 | ? ( | ||
| 345 | _2fDevices => eval { | ||
| 346 | from_json( | ||
| 347 | $res->{$_}->{_2fDevices}, | ||
| 348 | { allow_nonref => 1 } | ||
| 349 | ); | ||
| 350 | } | ||
| 351 | ) | ||
| 352 | : () | ||
| 353 | ) | ||
| 354 | } | ||
| 355 | } | ||
| 356 | keys %$res | ||
| 357 | ]; | ||
| 358 | } | ||
| 359 | |||
| 360 | return { | ||
| 361 | result => 1, | ||
| 362 | count => scalar(@$res), | ||
| 363 | total => $total, | ||
| 364 | values => $res | ||
| 365 | }; | ||
| 366 | } | ||
| 367 | |||
| 368 | sub delete2F { | ||
| 369 | my ( $self, $req ) = @_; | ||
| 370 | my $mod = $self->getMod($req) | ||
| 371 | or return $self->sendError( $req, undef, 400 ); | ||
| 372 | my $id = $req->params('sessionId') | ||
| 373 | or return $self->sendError( $req, 'sessionId is missing', 400 ); | ||
| 374 | |||
| 375 | # Try to read session | ||
| 376 | $self->logger->debug("Loading session : $id"); | ||
| 377 | my $session = $self->getApacheSession( $mod, $id ) | ||
| 378 | or return $self->sendError( $req, undef, 400 ); | ||
| 379 | |||
| 380 | # Try to read 2F parameters | ||
| 381 | $self->logger->debug("Reading parameters..."); | ||
| 382 | my $params = $req->parameters(); | ||
| 383 | my $type = $params->{type} | ||
| 384 | or return $self->sendError( $req, '2F device "type" parameter is missing', | ||
| 385 | 400 ); | ||
| 386 | my $epoch = $params->{epoch} | ||
| 387 | or | ||
| 388 | return $self->sendError( $req, '2F device "epoch" parameter is missing', | ||
| 389 | 400 ); | ||
| 390 | |||
| 391 | # Try to load 2F device(s) from session | ||
| 392 | $self->logger->debug("Looking for 2F device(s)..."); | ||
| 393 | my $_2fDevices; | ||
| 394 | if ( $session->data->{_2fDevices} ) { | ||
| 395 | $_2fDevices = eval { | ||
| 396 | from_json( $session->data->{_2fDevices}, { allow_nonref => 1 } ); | ||
| 397 | }; | ||
| 398 | if ($@) { | ||
| 399 | $self->logger->error("Corrupted session (_2fDevices) : $@"); | ||
| 400 | return $self->p->sendError( $req, "Corrupted session", 500 ); | ||
| 401 | } | ||
| 402 | } | ||
| 403 | else { | ||
| 404 | $self->logger->debug("No 2F device found"); | ||
| 405 | $_2fDevices = []; | ||
| 406 | } | ||
| 407 | |||
| 408 | # Delete 2F device | ||
| 409 | $self->logger->debug("Reading 2F device(s)..."); | ||
| 410 | my @keep = (); | ||
| 411 | while (@$_2fDevices) { | ||
| 412 | my $element = shift @$_2fDevices; | ||
| 413 | |||
| 414 | $self->logger->debug("Looking for 2F device to delete: [$type]$epoch"); | ||
| 415 | if ( defined $element->{type} && defined $element->{epoch} ) { | ||
| 416 | if ( ( $element->{type} eq $type ) | ||
| 417 | and ( $element->{epoch} eq $epoch ) ) | ||
| 418 | { | ||
| 419 | my $uid = $session->data->{_session_uid}; | ||
| 420 | $self->auditLog( | ||
| 421 | $req, | ||
| 422 | message => | ||
| 423 | ( "2FA deletion for $uid: " . display2F($element) ), | ||
| 424 | code => "2FA_DELETED", | ||
| 425 | user => $uid, | ||
| 426 | sfa => display2F($element) | ||
| 427 | ); | ||
| 428 | } | ||
| 429 | else { | ||
| 430 | push @keep, $element; | ||
| 431 | } | ||
| 432 | } | ||
| 433 | else { | ||
| 434 | $self->logger->error("Corrupted _2fDevice"); | ||
| 435 | } | ||
| 436 | } | ||
| 437 | |||
| 438 | # Update session | ||
| 439 | $self->logger->debug("Saving 2F devices..."); | ||
| 440 | $session->data->{_2fDevices} = to_json( \@keep ); | ||
| 441 | $self->logger->debug("Update session"); | ||
| 442 | $session->update( \%{ $session->data } ); | ||
| 443 | |||
| 444 | Lemonldap::NG::Handler::PSGI::Main->localUnlog( $req, $id ); | ||
| 445 | return $session->error | ||
| 446 | ? $self->sendError( $req, $session->error, 200 ) | ||
| 447 | : $self->sendJSONresponse( $req, { result => 1 } ); | ||
| 448 | } | ||
| 449 | |||
| 450 | sub _session { | ||
| 451 | my ( $self, $raw, $req, $id, $skey ) = @_; | ||
| 452 | my ( %h, $res ); | ||
| 453 | |||
| 454 | # When requesting a session, depending on the context, the cann can be | ||
| 455 | # with the storage ID or the cookie value. the "hash=1' parameter indicates | ||
| 456 | # that the query uses the cookie value | ||
| 457 | return $self->sendError( $req, 'Bad request', 400 ) unless $id; | ||
| 458 | my $mod = $self->getMod($req) | ||
| 459 | or return $self->sendError( $req, undef, 400 ); | ||
| 460 | |||
| 461 | # Try to read session | ||
| 462 | my $hashed = $req->param('hash') ? 1 : 0; | ||
| 463 | my $apacheSession = | ||
| 464 | $self->getApacheSession( $mod, $id, hashStore => $hashed ) | ||
| 465 | or return $self->sendError( $req, undef, 400 ); | ||
| 466 | |||
| 467 | my %session = %{ $apacheSession->data }; | ||
| 468 | unless ($raw) { | ||
| 469 | foreach ( keys %session ) { | ||
| 470 | $session{$_} = '******' if isHiddenAttr( $self->conf, $_ ); | ||
| 471 | } | ||
| 472 | } | ||
| 473 | |||
| 474 | if ($skey) { | ||
| 475 | if ( $skey =~ s/^\[(.*)\]$/$1/ ) { | ||
| 476 | my @sk = split /,/, $skey; | ||
| 477 | my $res = {}; | ||
| 478 | $res->{$_} = $session{$_} foreach (@sk); | ||
| 479 | return $self->sendJSONresponse( $req, $res ); | ||
| 480 | } | ||
| 481 | return $self->sendJSONresponse( $req, $session{$skey} ); | ||
| 482 | } | ||
| 483 | else { | ||
| 484 | return $self->sendJSONresponse( $req, \%session ); | ||
| 485 | } | ||
| 486 | |||
| 487 | # TODO: check for utf-8 problems | ||
| 488 | } | ||
| 489 | |||
| 490 | sub session { | ||
| 491 | my $self = shift; | ||
| 492 | return $self->_session( 0, @_ ); | ||
| 493 | } | ||
| 494 | |||
| 495 | sub rawSession { | ||
| 496 | my $self = shift; | ||
| 497 | return $self->_session( 1, @_ ); | ||
| 498 | } | ||
| 499 | |||
| 500 | sub getApacheSession { | ||
| 501 | my ( $self, $mod, $id, %args ) = @_; | ||
| 502 | my $apacheSession = Lemonldap::NG::Common::Session->new( { | ||
| 503 | |||
| 504 | # The request is done with the storage ID by default unless | ||
| 505 | # hashStore is explicitly set to a true value | ||
| 506 | hashStore => $args{hashStore} && $self->hashedSessionStore, | ||
| 507 | storageModule => $mod->{module}, | ||
| 508 | storageModuleOptions => $mod->{options}, | ||
| 509 | cacheModule => | ||
| 510 | Lemonldap::NG::Handler::PSGI::Main->tsv->{sessionCacheModule}, | ||
| 511 | cacheModuleOptions => | ||
| 512 | Lemonldap::NG::Handler::PSGI::Main->tsv->{sessionCacheOptions}, | ||
| 513 | id => $id, | ||
| 514 | force => $args{force}, | ||
| 515 | ( $id ? () : ( kind => $mod->{kind} ) ), | ||
| 516 | ( $args{info} ? ( info => $args{info} ) : () ), | ||
| 517 | } | ||
| 518 | ); | ||
| 519 | if ( $apacheSession->error ) { | ||
| 520 | $self->logger->error( $apacheSession->error ); | ||
| 521 | return undef; | ||
| 522 | } | ||
| 523 | $self->logger->debug("Get session $id from Common::Session::REST") if ($id); | ||
| 524 | return $apacheSession; | ||
| 525 | } | ||
| 526 | |||
| 527 | sub getPersistentSession { | ||
| 528 | my ( $self, $mod, $uid, $info ) = @_; | ||
| 529 | |||
| 530 | # Compute persistent identifier | ||
| 531 | my $pid = getPSessionID($uid); | ||
| 532 | |||
| 533 | $info->{_session_uid} = $uid; | ||
| 534 | |||
| 535 | my $ps = Lemonldap::NG::Common::Session->new( { | ||
| 536 | storageModule => $mod->{module}, | ||
| 537 | storageModuleOptions => $mod->{options}, | ||
| 538 | id => $pid, | ||
| 539 | force => 1, | ||
| 540 | kind => "Persistent", | ||
| 541 | ( $info ? ( info => $info ) : () ), | ||
| 542 | } | ||
| 543 | ); | ||
| 544 | |||
| 545 | if ( $ps->error ) { | ||
| 546 | $self->logger->error( $ps->error ); | ||
| 547 | return undef; | ||
| 548 | } | ||
| 549 | else { | ||
| 550 | |||
| 551 | # Set _session_uid if not already present | ||
| 552 | unless ( defined $ps->data->{_session_uid} ) { | ||
| 553 | $ps->update( { _session_uid => $uid } ); | ||
| 554 | } | ||
| 555 | |||
| 556 | # Set _utime if not already present | ||
| 557 | unless ( defined $ps->data->{_utime} ) { | ||
| 558 | $ps->update( { _utime => time } ); | ||
| 559 | } | ||
| 560 | } | ||
| 561 | return $ps; | ||
| 562 | } | ||
| 563 | |||
| 564 | sub getMod { | ||
| 565 | my ( $self, $req ) = @_; | ||
| 566 | my ( $s, $m ); | ||
| 567 | unless ( $s = $req->params('sessionType') ) { | ||
| 568 | $self->error( $req->error('Session type is required') ); | ||
| 569 | return (); | ||
| 570 | } | ||
| 571 | unless ( $m = $self->sessionTypes->{$s} ) { | ||
| 572 | $self->error( $req->error('Unknown (or unconfigured) session type') ); | ||
| 573 | return (); | ||
| 574 | } | ||
| 575 | if ( my $kind = $req->params('kind') ) { | ||
| 576 | $m->{kind} = $kind; | ||
| 577 | } | ||
| 578 | return $m; | ||
| 579 | } | ||
| 580 | |||
| 581 | sub getGlobal { | ||
| 582 | my ($self) = @_; | ||
| 583 | return $self->sessionTypes->{global}; | ||
| 584 | } | ||
| 585 | |||
| 586 | 1; |