« blib/lib/Lemonldap/NG/Portal/Plugins/CheckDevOps.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Lib/OpenID/SREG.pm »
| 1 | package Lemonldap::NG::Portal::Plugins::CheckUser; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse; | ||
| 5 | use Lemonldap::NG::Common::Util qw(isHiddenAttr); | ||
| 6 | use Lemonldap::NG::Portal::Main::Constants qw( | ||
| 7 | PE_NOTOKEN | ||
| 8 | PE_TOKENEXPIRED | ||
| 9 | PE_MALFORMEDUSER | ||
| 10 | PE_BADCREDENTIALS | ||
| 11 | ); | ||
| 12 | |||
| 13 | our $VERSION = '2.23.0'; | ||
| 14 | |||
| 15 | extends qw( | ||
| 16 | Lemonldap::NG::Portal::Main::Plugin | ||
| 17 | Lemonldap::NG::Portal::Lib::_tokenRule | ||
| 18 | Lemonldap::NG::Portal::Lib::OtherSessions | ||
| 19 | ); | ||
| 20 | |||
| 21 | # INITIALIZATION | ||
| 22 | has ott => ( | ||
| 23 | is => 'rw', | ||
| 24 | lazy => 1, | ||
| 25 | default => sub { | ||
| 26 | my $ott = | ||
| 27 | $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken'); | ||
| 28 | $ott->timeout( $_[0]->{conf}->{formTimeout} ); | ||
| 29 | return $ott; | ||
| 30 | } | ||
| 31 | ); | ||
| 32 | |||
| 33 | has rules => ( is => 'rw', default => sub { {} } ); | ||
| 34 | has sorted => ( is => 'rw', default => sub { 0 } ); | ||
| 35 | has merged => ( is => 'rw', default => '' ); | ||
| 36 | |||
| 37 | # Prefix used for renaming session attributes during impersonation process | ||
| 38 | has prefix => ( | ||
| 39 | is => 'ro', | ||
| 40 | lazy => 1, | ||
| 41 | default => sub { | ||
| 42 | my $self = $_[0]; | ||
| 43 | return $self->conf->{impersonationPrefix}; | ||
| 44 | } | ||
| 45 | ); | ||
| 46 | |||
| 47 | sub persistentAttrs { | ||
| 48 | $_[0]->{conf}->{persistentSessionAttributes} | ||
| 49 | || '_loginHistory _2fDevices notification_ _oidcConsents'; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub init { | ||
| 53 | my ($self) = @_; | ||
| 54 | $self->addAuthRoute( checkuser => 'check', ['POST'] ) | ||
| 55 | ->addAuthRouteWithRedirect( checkuser => 'display', ['GET'] ); | ||
| 56 | |||
| 57 | # Compile rules | ||
| 58 | foreach ( | ||
| 59 | qw(DisplayEmptyValues DisplayEmptyHeaders DisplayPersistentInfo DisplayComputedSession DisplayNormalizedHeaders DisplayHistory DisplayHiddenAttributes IdRule UnrestrictedUsersRule) | ||
| 60 | ) | ||
| 61 | { | ||
| 62 | $self->logger->debug("CheckUser: Build rule $_"); | ||
| 63 | my $rule = | ||
| 64 | $self->p->buildRule( $self->conf->{"checkUser$_"}, "checkUser$_" ); | ||
| 65 | return 0 unless $rule; | ||
| 66 | $self->rules->{$_} = $rule; | ||
| 67 | } | ||
| 68 | |||
| 69 | # Init. other options | ||
| 70 | $self->sorted( $self->conf->{impersonationRule} | ||
| 71 | || $self->conf->{contextSwitchingRule} ); | ||
| 72 | $self->merged( $self->conf->{impersonationMergeSSOgroups} | ||
| 73 | && $self->conf->{impersonationRule} ? 'Merged' : '' ); | ||
| 74 | |||
| 75 | return 1; | ||
| 76 | } | ||
| 77 | |||
| 78 | # RUNNING METHODS | ||
| 79 | sub display { | ||
| 80 | my ( $self, $req ) = @_; | ||
| 81 | my $history = [ [], [] ]; | ||
| 82 | my ( $attrs, $array_attrs ) = ( $req->userData, [] ); | ||
| 83 | |||
| 84 | $self->logger->debug("Display current session data..."); | ||
| 85 | $self->userLogger->info("Using spoofed SSO groups if exist") | ||
| 86 | if ( $self->conf->{impersonationRule} ); | ||
| 87 | |||
| 88 | $history = $self->_concatHistory( $attrs->{_loginHistory} ) | ||
| 89 | if $self->rules->{DisplayHistory}->( $req, $req->userData ) | ||
| 90 | && $self->conf->{loginHistoryEnabled}; | ||
| 91 | |||
| 92 | $attrs = | ||
| 93 | $self->_removeKeys( $attrs, $self->persistentAttrs, | ||
| 94 | 'Remove persistent session attributes...' ) | ||
| 95 | unless $self->rules->{DisplayPersistentInfo}->( $req, $req->userData ); | ||
| 96 | |||
| 97 | # Create an array of hashes and dispatch attributes for template loop | ||
| 98 | # ARRAY_REF = [ A_REF GROUPS, A_REF MACROS, A_REF OTHERS ] | ||
| 99 | $array_attrs = $self->_dispatchAttributes( | ||
| 100 | $self->_createArray( $req, $attrs, $req->userData ) ); | ||
| 101 | |||
| 102 | my $params = { | ||
| 103 | FORM_ACTION => $self->p->relativeUrl( $req, 'checkuser' ), | ||
| 104 | MSG => 'checkUser' . $self->merged, | ||
| 105 | ALERTE => ( $self->merged ? 'alert-warning' : 'alert-info' ), | ||
| 106 | LOGIN => $req->userData->{ $self->conf->{whatToTrace} }, | ||
| 107 | HISTORY => ( @{ $history->[0] } || @{ $history->[1] } ) ? 1 : 0, | ||
| 108 | SUCCESS => $history->[0], | ||
| 109 | FAILED => $history->[1], | ||
| 110 | DISPLAY => ( | ||
| 111 | @{ $array_attrs->[0] } | ||
| 112 | || @{ $array_attrs->[1] } | ||
| 113 | || @{ $array_attrs->[2] } | ||
| 114 | ) ? 1 : 0, | ||
| 115 | ATTRIBUTES => $array_attrs->[2], | ||
| 116 | MACROS => $array_attrs->[1], | ||
| 117 | GROUPS => $array_attrs->[0], | ||
| 118 | TOKEN => ( | ||
| 119 | $self->ottRule->( $req, {} ) | ||
| 120 | ? $self->ott->createToken() | ||
| 121 | : '' | ||
| 122 | ) | ||
| 123 | }; | ||
| 124 | return $self->sendJSONresponse( $req, $params ) if $req->wantJSON; | ||
| 125 | |||
| 126 | # Display form | ||
| 127 | return $self->p->sendHtml( $req, 'checkuser', params => $params ); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub check { | ||
| 131 | my ( $self, $req ) = @_; | ||
| 132 | my ( $attrs, $array_attrs, $array_hdrs ) = ( {}, [], [] ); | ||
| 133 | my $msg = my $auth = my $computed = ''; | ||
| 134 | my $savedUserData = $req->userData; | ||
| 135 | my $unUser = | ||
| 136 | $self->rules->{UnrestrictedUsersRule}->( $req, $savedUserData ) || 0; | ||
| 137 | my $history = [ [], [] ]; | ||
| 138 | |||
| 139 | # Check token | ||
| 140 | if ( $self->ottRule->( $req, {} ) ) { | ||
| 141 | my $token; | ||
| 142 | if ( $token = $req->param('token') ) { | ||
| 143 | unless ( $self->ott->getToken($token) ) { | ||
| 144 | $self->userLogger->warn( | ||
| 145 | 'CheckUser called with an expired/bad token'); | ||
| 146 | $msg = PE_TOKENEXPIRED; | ||
| 147 | $token = $self->ott->createToken(); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | else { | ||
| 151 | $self->userLogger->warn('CheckUser called without token'); | ||
| 152 | $msg = PE_NOTOKEN; | ||
| 153 | $token = $self->ott->createToken(); | ||
| 154 | } | ||
| 155 | |||
| 156 | my $params = { | ||
| 157 | FORM_ACTION => $self->p->relativeUrl( $req, 'checkuser' ), | ||
| 158 | MSG => "PE$msg", | ||
| 159 | ALERTE => 'alert-warning', | ||
| 160 | LOGIN => '', | ||
| 161 | TOKEN => $token, | ||
| 162 | }; | ||
| 163 | return $self->p->sendJSONresponse( $req, $params ) | ||
| 164 | if $req->wantJSON && $msg; | ||
| 165 | |||
| 166 | # Display form | ||
| 167 | return $self->p->sendHtml( $req, 'checkuser', params => $params ) | ||
| 168 | if $msg; | ||
| 169 | } | ||
| 170 | |||
| 171 | ## Check user session datas | ||
| 172 | # Use submitted attributes if exists | ||
| 173 | my $url = $req->param('url') || ''; | ||
| 174 | my $user = $req->param('user') || ''; | ||
| 175 | |||
| 176 | if ( $user and $user !~ /$self->{conf}->{userControl}/ ) { | ||
| 177 | $user = ''; | ||
| 178 | $attrs = {}; | ||
| 179 | return $self->p->sendError( $req, 'Malformed user', 400 ) | ||
| 180 | if $req->wantJSON; | ||
| 181 | return $self->p->sendHtml( | ||
| 182 | $req, | ||
| 183 | 'checkuser', | ||
| 184 | params => { | ||
| 185 | FORM_ACTION => $self->p->relativeUrl( $req, 'checkuser' ), | ||
| 186 | MSG => 'PE' . PE_MALFORMEDUSER, | ||
| 187 | ALERTE => 'alert-warning', | ||
| 188 | LOGIN => '', | ||
| 189 | TOKEN => ( | ||
| 190 | $self->ottRule->( $req, {} ) | ||
| 191 | ? $self->ott->createToken() | ||
| 192 | : '' | ||
| 193 | ) | ||
| 194 | } | ||
| 195 | ); | ||
| 196 | } | ||
| 197 | |||
| 198 | if ( !$user || $req->{user} eq $user ) { | ||
| 199 | $self->userLogger->info("checkUser requested for himself"); | ||
| 200 | $self->userLogger->info("Using spoofed SSO groups if exist") | ||
| 201 | if $self->conf->{impersonationRule}; | ||
| 202 | $attrs = $req->userData; | ||
| 203 | $user = $req->{user}; | ||
| 204 | } | ||
| 205 | else { | ||
| 206 | $self->userLogger->info("checkUser requested for $user"); | ||
| 207 | |||
| 208 | # Try to retrieve session from sessions DB | ||
| 209 | $self->logger->debug('Try to retrieve session from DB...'); | ||
| 210 | my $moduleOptions = $self->conf->{globalStorageOptions} || {}; | ||
| 211 | $moduleOptions->{backend} = $self->conf->{globalStorage}; | ||
| 212 | |||
| 213 | my $sessions = {}; | ||
| 214 | my $searchAttrs = | ||
| 215 | $self->conf->{checkUserSearchAttributes} | ||
| 216 | ? $self->conf->{whatToTrace} . ' ' | ||
| 217 | . $self->conf->{checkUserSearchAttributes} | ||
| 218 | : $self->conf->{whatToTrace}; | ||
| 219 | |||
| 220 | foreach ( split /[,\s]+/, $searchAttrs ) { | ||
| 221 | $self->logger->debug("Searching with: $_ = $user"); | ||
| 222 | $sessions = $self->module->searchOn( $moduleOptions, $_, $user ); | ||
| 223 | last if ( keys %$sessions ); | ||
| 224 | } | ||
| 225 | |||
| 226 | my $age = '1'; | ||
| 227 | foreach my $id ( keys %$sessions ) { | ||
| 228 | |||
| 229 | # searchOn() returns sessions indexed by their storage ID, then | ||
| 230 | # it is required to set hashStore to 0 | ||
| 231 | my $session = $self->p->getApacheSession( $id, hashStore => 0, ) | ||
| 232 | or next; | ||
| 233 | |||
| 234 | if ( $session->{data}->{_utime} gt $age ) { | ||
| 235 | $attrs = $session->{data}; | ||
| 236 | $age = $session->{data}->{_utime}; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | unless ( defined $attrs->{_session_id} ) { | ||
| 240 | $req->{user} = $user; | ||
| 241 | $self->userLogger->info( | ||
| 242 | "No session found in DB. Compute userData..."); | ||
| 243 | $attrs = $self->_userData($req); | ||
| 244 | $computed = 1; | ||
| 245 | } | ||
| 246 | |||
| 247 | # Check identities rule | ||
| 248 | $self->logger->info( '"' | ||
| 249 | . $savedUserData->{ $self->conf->{whatToTrace} } | ||
| 250 | . '" is an unrestricted user!' ) | ||
| 251 | if $unUser; | ||
| 252 | unless ( $unUser || $self->rules->{IdRule}->( $req, $attrs ) ) { | ||
| 253 | $self->userLogger->warn( | ||
| 254 | "checkUser requested for an invalid user ($user)"); | ||
| 255 | $req->{sessionInfo} = {}; | ||
| 256 | $self->logger->debug('Identity not authorized'); | ||
| 257 | $req->error(PE_BADCREDENTIALS) | ||
| 258 | ; # Catch error to preserve protected Id | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | if ( $req->error ) { | ||
| 263 | $msg = 'PE' . $req->{error}; | ||
| 264 | $array_attrs = [ [], [], [] ]; | ||
| 265 | $attrs = {}; | ||
| 266 | } | ||
| 267 | else { | ||
| 268 | $msg = 'checkUser' . $self->merged; | ||
| 269 | $history = $self->_concatHistory( $attrs->{_loginHistory} ) | ||
| 270 | if $self->rules->{DisplayHistory}->( $req, $savedUserData ) | ||
| 271 | && $self->conf->{loginHistoryEnabled}; | ||
| 272 | |||
| 273 | $attrs = | ||
| 274 | $self->_removeKeys( $attrs, $self->persistentAttrs, | ||
| 275 | 'Remove persistent session attributes...' ) | ||
| 276 | unless $self->rules->{DisplayPersistentInfo} | ||
| 277 | ->( $req, $savedUserData ); | ||
| 278 | |||
| 279 | if ($computed) { | ||
| 280 | if ( | ||
| 281 | $self->rules->{DisplayComputedSession}->( $req, $savedUserData ) | ||
| 282 | ) | ||
| 283 | { | ||
| 284 | $msg = 'checkUserComputedSession'; | ||
| 285 | if ( $self->conf->{impersonationRule} ) { | ||
| 286 | $self->logger->debug("Map real attributes..."); | ||
| 287 | my %realAttrs = | ||
| 288 | map { $self->prefix . $_ => $attrs->{$_} } | ||
| 289 | keys %$attrs; | ||
| 290 | $attrs = { %$attrs, %realAttrs }; | ||
| 291 | |||
| 292 | # Compute groups and macros with real and spoofed attributes | ||
| 293 | $self->logger->debug( | ||
| 294 | "Compute groups and macros with real and spoofed attributes" | ||
| 295 | ); | ||
| 296 | $req->sessionInfo($attrs); | ||
| 297 | delete $req->sessionInfo->{groups}; | ||
| 298 | $req->steps( | ||
| 299 | [ $self->p->groupsAndMacros, 'setLocalGroups' ] ); | ||
| 300 | if ( my $error = $self->p->process($req) ) { | ||
| 301 | $self->logger->debug("Process returned error: $error"); | ||
| 302 | return $req->error($error); | ||
| 303 | } | ||
| 304 | } | ||
| 305 | } | ||
| 306 | else { | ||
| 307 | $attrs = {}; | ||
| 308 | $msg = 'checkUserNoSessionFound'; | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | # Create an array of hashes and dispatch attributes for template loop | ||
| 313 | # ARRAY_REF = [ A_REF GROUPS, A_REF MACROS, A_REF OTHERS ] | ||
| 314 | $array_attrs = $self->_dispatchAttributes( | ||
| 315 | $self->_createArray( $req, $attrs, $savedUserData ) ); | ||
| 316 | } | ||
| 317 | |||
| 318 | if ( $self->p->checkXSSAttack( 'CheckUser URL', $url ) ) { | ||
| 319 | $url = ''; | ||
| 320 | $auth = 'VHnotFound'; | ||
| 321 | } | ||
| 322 | |||
| 323 | # Check if user is allowed to access submitted URL and compute headers | ||
| 324 | if ( $url and %$attrs ) { | ||
| 325 | |||
| 326 | # Check url format | ||
| 327 | my $originalUrl; | ||
| 328 | ( $url, $originalUrl ) = $self->_resolveURL( $req, $url ); | ||
| 329 | |||
| 330 | # User is allowed ? | ||
| 331 | $self->logger->debug( | ||
| 332 | "checkUser requested for user: $attrs->{ $self->{conf}->{whatToTrace} } and URL: $url | alias: $originalUrl" | ||
| 333 | ); | ||
| 334 | $auth = $self->_authorization( $req, $originalUrl, $attrs ); | ||
| 335 | if ( $auth >= 0 ) { | ||
| 336 | $auth = $auth ? "allowed" : "forbidden"; | ||
| 337 | $self->logger->debug( | ||
| 338 | "checkUser: $attrs->{ $self->{conf}->{whatToTrace} } is " | ||
| 339 | . "$auth to access $url" ); | ||
| 340 | |||
| 341 | # Return VirtualHost headers | ||
| 342 | $array_hdrs = | ||
| 343 | $self->_headers( $req, $originalUrl, $attrs, $savedUserData ); | ||
| 344 | } | ||
| 345 | else { | ||
| 346 | $auth = 'VHnotFound'; | ||
| 347 | $self->userLogger->info("checkUser: $url has no configuration"); | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | my $alert_auth = 'alert-warning'; | ||
| 352 | if ( $auth eq 'allowed' ) { $alert_auth = 'alert-success' } | ||
| 353 | elsif ( $auth eq 'forbidden' ) { $alert_auth = 'alert-danger' } | ||
| 354 | |||
| 355 | # TODO: | ||
| 356 | my $params = { | ||
| 357 | FORM_ACTION => $self->p->relativeUrl( $req, 'checkuser' ), | ||
| 358 | MSG => $msg, | ||
| 359 | ALERTE => ( $msg eq 'checkUser' ? 'alert-info' : 'alert-warning' ), | ||
| 360 | LOGIN => $user, | ||
| 361 | URL => $url, | ||
| 362 | ALLOWED => $auth, | ||
| 363 | ALERTE_AUTH => $alert_auth, | ||
| 364 | HEADERS => $array_hdrs, | ||
| 365 | HISTORY => ( @{ $history->[0] } || @{ $history->[1] } ) ? 1 : 0, | ||
| 366 | SUCCESS => $history->[0], | ||
| 367 | FAILED => $history->[1], | ||
| 368 | DISPLAY => ( | ||
| 369 | @{ $array_attrs->[0] } | ||
| 370 | || @{ $array_attrs->[1] } | ||
| 371 | || @{ $array_attrs->[2] } | ||
| 372 | ) ? 1 : 0, | ||
| 373 | ATTRIBUTES => $array_attrs->[2], | ||
| 374 | MACROS => $array_attrs->[1], | ||
| 375 | GROUPS => $array_attrs->[0], | ||
| 376 | TOKEN => ( | ||
| 377 | $self->ottRule->( $req, {} ) | ||
| 378 | ? $self->ott->createToken() | ||
| 379 | : '' | ||
| 380 | ) | ||
| 381 | }; | ||
| 382 | return $self->p->sendJSONresponse( $req, $params ) if $req->wantJSON; | ||
| 383 | |||
| 384 | # Display form | ||
| 385 | return $self->p->sendHtml( $req, 'checkuser', params => $params ); | ||
| 386 | } | ||
| 387 | |||
| 388 | sub _resolveURL { | ||
| 389 | my ( $self, $req, $url ) = @_; | ||
| 390 | my ($proto) = $url =~ m#^(https?://).*#i; | ||
| 391 | my ( $vhost, $appuri ) = $url =~ m@^(?:https?://)?([^/#]*)(.*)@i; | ||
| 392 | my ($port) = $vhost =~ m#^.+(:\d+)$#; | ||
| 393 | $port ||= ''; | ||
| 394 | $vhost =~ s/:\d+$//; | ||
| 395 | $vhost .= $self->p->getCookieDomain($req) unless ( $vhost =~ /\./ ); | ||
| 396 | $proto = | ||
| 397 | $self->p->HANDLER->_isHttps( $req, $vhost ) ? 'https://' : 'http://' | ||
| 398 | unless $proto; | ||
| 399 | $self->logger->debug( 'VHost is ' . uc( ( split( /:/, $proto ) )[0] ) ); | ||
| 400 | my $originalVhost = $self->p->HANDLER->resolveAlias($vhost); | ||
| 401 | |||
| 402 | return ( | ||
| 403 | lc("$proto$vhost$port") . $appuri, | ||
| 404 | lc("$proto$originalVhost$port") . $appuri | ||
| 405 | ); | ||
| 406 | } | ||
| 407 | |||
| 408 | sub _userData { | ||
| 409 | my ( $self, $req ) = @_; | ||
| 410 | my $realAuthLevel = $req->userData->{authenticationLevel}; | ||
| 411 | |||
| 412 | # Compute session | ||
| 413 | my $steps = | ||
| 414 | defined $realAuthLevel | ||
| 415 | ? [ | ||
| 416 | 'getUser', 'setAuthSessionInfo', | ||
| 417 | 'setSessionInfo', $self->p->groupsAndMacros | ||
| 418 | ] | ||
| 419 | : [ 'getUser', 'setSessionInfo', $self->p->groupsAndMacros ]; | ||
| 420 | $self->conf->{checkUserDisplayPersistentInfo} | ||
| 421 | ? push @$steps, 'setPersistentSessionInfo', 'setLocalGroups' | ||
| 422 | : push @$steps, 'setLocalGroups'; | ||
| 423 | $req->steps($steps); | ||
| 424 | if ( my $error = $self->p->process($req) ) { | ||
| 425 | $self->userLogger->warn( | ||
| 426 | 'checkUser requested for an invalid user (' . $req->{user} . ")" ) | ||
| 427 | if ( $error == PE_BADCREDENTIALS ); | ||
| 428 | $self->logger->debug("Process returned error: $error"); | ||
| 429 | return $req->error(PE_BADCREDENTIALS); | ||
| 430 | } | ||
| 431 | |||
| 432 | unless ( defined $req->sessionInfo->{uid} ) { | ||
| 433 | |||
| 434 | # Avoid error with SAML, OIDC, etc... | ||
| 435 | $self->logger->debug("\"$req->{user}\" NOT found in userDB"); | ||
| 436 | return $req->error(PE_BADCREDENTIALS); | ||
| 437 | } | ||
| 438 | |||
| 439 | # Compute groups & macros again with real authenticationLevel | ||
| 440 | $req->sessionInfo->{authenticationLevel} = $realAuthLevel; | ||
| 441 | delete $req->sessionInfo->{groups}; | ||
| 442 | |||
| 443 | $req->steps( | ||
| 444 | [ 'setSessionInfo', $self->p->groupsAndMacros, 'setLocalGroups' ] ); | ||
| 445 | if ( my $error = $self->p->process($req) ) { | ||
| 446 | $self->logger->debug("CheckUser: process returned error ($error)"); | ||
| 447 | return $req->error($error); | ||
| 448 | } | ||
| 449 | |||
| 450 | $self->logger->debug("Return \"$req->{user}\" sessionInfo"); | ||
| 451 | return $req->{sessionInfo}; | ||
| 452 | } | ||
| 453 | |||
| 454 | sub _authorization { | ||
| 455 | my ( $self, $req, $uri, $attrs ) = @_; | ||
| 456 | my ( $vhost, $appuri ) = $uri =~ m@^https?://([^/#]*)(.*)@; | ||
| 457 | my $exist = 0; | ||
| 458 | |||
| 459 | $appuri = $self->p->HANDLER->canonicalUri($appuri); | ||
| 460 | |||
| 461 | $vhost =~ s/:\d+$//; | ||
| 462 | foreach my $vh ( keys %{ $self->conf->{locationRules} } ) { | ||
| 463 | if ( $vh eq $vhost ) { | ||
| 464 | $exist = 1; | ||
| 465 | $self->logger->debug("VirtualHost: $vh found in Conf"); | ||
| 466 | $req->env->{REQUEST_URI} = $appuri; | ||
| 467 | last; | ||
| 468 | } | ||
| 469 | } | ||
| 470 | |||
| 471 | $self->logger->debug( | ||
| 472 | "Return \"$attrs->{ $self->{conf}->{whatToTrace} }\" authorization"); | ||
| 473 | return $exist | ||
| 474 | ? $self->p->HANDLER->grant( $req, $attrs, $appuri, undef, $vhost ) | ||
| 475 | : -1; | ||
| 476 | } | ||
| 477 | |||
| 478 | sub _headers { | ||
| 479 | my ( $self, $req, $uri, $attrs, $savedUserData ) = @_; | ||
| 480 | my ($vhost) = $uri =~ m@^https?://([^/#]*).*@; | ||
| 481 | |||
| 482 | $vhost =~ s/:\d+$//; | ||
| 483 | $req->{env}->{HTTP_HOST} = $vhost; | ||
| 484 | $self->p->HANDLER->headersInit( $self->{conf} ); | ||
| 485 | my $headers = $self->p->HANDLER->checkHeaders( $req, $attrs ); | ||
| 486 | |||
| 487 | # Remove hidden headers relative to VHost if required | ||
| 488 | unless ( $self->rules->{UnrestrictedUsersRule}->( $req, $savedUserData ) ) { | ||
| 489 | my $keysToRemove = ''; | ||
| 490 | $keysToRemove = '__ALL__' | ||
| 491 | if exists $self->conf->{checkUserHiddenHeaders}->{$vhost}; | ||
| 492 | $keysToRemove = $self->conf->{checkUserHiddenHeaders}->{$vhost} | ||
| 493 | if ( $keysToRemove | ||
| 494 | && $self->conf->{checkUserHiddenHeaders}->{$vhost} =~ /\w+/ ); | ||
| 495 | |||
| 496 | if ( $keysToRemove && $keysToRemove eq '__ALL__' ) { | ||
| 497 | $self->logger->debug( | ||
| 498 | "Overwrite for VirtualHost: $vhost ALL valued header(s)..."); | ||
| 499 | @$headers = map { | ||
| 500 | $_->{value} =~ /\w+/ | ||
| 501 | ? { key => $_->{key}, value => '******' } | ||
| 502 | : $_ | ||
| 503 | } @$headers; | ||
| 504 | } | ||
| 505 | elsif ($keysToRemove) { | ||
| 506 | $self->logger->debug( | ||
| 507 | "Mask hidden header(s) for VirtualHost: $vhost"); | ||
| 508 | my $hash = { map { $_->{key} => $_->{value} } @$headers }; | ||
| 509 | $hash = $self->_removeKeys( $hash, $keysToRemove, | ||
| 510 | "Overwrite valued \"$keysToRemove\" header(s)...", 1 ); | ||
| 511 | @$headers = ( | ||
| 512 | map { { key => $_, value => $hash->{$_} } } | ||
| 513 | sort keys %$hash | ||
| 514 | ); | ||
| 515 | } | ||
| 516 | } | ||
| 517 | |||
| 518 | # Remove empty headers if required | ||
| 519 | unless ( $self->rules->{DisplayEmptyHeaders}->( $req, $savedUserData ) ) { | ||
| 520 | $self->logger->debug("Remove empty headers..."); | ||
| 521 | @$headers = grep $_->{value} =~ /.+/, @$headers; | ||
| 522 | } | ||
| 523 | |||
| 524 | # Normalize headers name if required | ||
| 525 | if ( $self->rules->{DisplayNormalizedHeaders}->( $req, $savedUserData ) ) { | ||
| 526 | $self->logger->debug("Normalize headers..."); | ||
| 527 | @$headers = map { | ||
| 528 | ; # Prevent compilation error with old Perl versions | ||
| 529 | no strict 'refs'; | ||
| 530 | { | ||
| 531 | key => &{ $self->p->HANDLER . '::cgiName' }( $_->{key} ), | ||
| 532 | value => $_->{value} | ||
| 533 | } | ||
| 534 | } @$headers; | ||
| 535 | } | ||
| 536 | |||
| 537 | $self->logger->debug( | ||
| 538 | "Return \"$attrs->{ $self->{conf}->{whatToTrace} }\" headers"); | ||
| 539 | return $headers; | ||
| 540 | } | ||
| 541 | |||
| 542 | sub _createArray { | ||
| 543 | my ( $self, $req, $attrs, $userData ) = @_; | ||
| 544 | my $array_attrs = []; | ||
| 545 | my @hidden = split /[,\s]+/, $self->conf->{checkUserHiddenAttributes}; | ||
| 546 | |||
| 547 | foreach ( sort keys %$attrs ) { | ||
| 548 | push @$array_attrs, | ||
| 549 | { key => $_, value => $attrs->{$_} } | ||
| 550 | unless ( ( | ||
| 551 | isHiddenAttr( $self->conf, $_, @hidden ) | ||
| 552 | && !$self->rules->{DisplayHiddenAttributes}->( $req, $userData ) | ||
| 553 | ) | ||
| 554 | || ( !$attrs->{$_} | ||
| 555 | && !$self->rules->{DisplayEmptyValues}->( $req, $userData ) ) | ||
| 556 | ); | ||
| 557 | } | ||
| 558 | |||
| 559 | return $array_attrs; | ||
| 560 | } | ||
| 561 | |||
| 562 | sub _dispatchAttributes { | ||
| 563 | my ( $self, $attrs ) = @_; | ||
| 564 | my ( $grps, $mcrs, $others ) = ( [], [], [] ); | ||
| 565 | my $macros = $self->{conf}->{macros}; | ||
| 566 | |||
| 567 | $self->logger->debug("Dispatch attributes..."); | ||
| 568 | while (@$attrs) { | ||
| 569 | my $element = shift @$attrs; | ||
| 570 | $self->logger->debug( "Processing element: $element->{key} => " | ||
| 571 | . ( $element->{value} // '' ) ); | ||
| 572 | my $ok = 0; | ||
| 573 | if ( $element->{key} eq 'groups' ) { | ||
| 574 | $self->logger->debug('Key "groups" found'); | ||
| 575 | my $separator = $self->{conf}->{multiValuesSeparator}; | ||
| 576 | my @tmp = split /\Q$separator/, $element->{value}; | ||
| 577 | $grps = [ map { { value => $_ } } sort @tmp ]; | ||
| 578 | next; | ||
| 579 | } | ||
| 580 | if (%$macros) { | ||
| 581 | foreach my $key ( sort keys %$macros ) { | ||
| 582 | if ( $element->{key} eq $key ) { | ||
| 583 | $self->logger->debug('Macro found'); | ||
| 584 | push @$mcrs, $element; | ||
| 585 | $ok = 1; | ||
| 586 | last; | ||
| 587 | } | ||
| 588 | } | ||
| 589 | } | ||
| 590 | push @$others, $element unless $ok; | ||
| 591 | } | ||
| 592 | |||
| 593 | # Sort real and spoofed attributes if required | ||
| 594 | if ( $self->sorted ) { | ||
| 595 | $self->logger->debug('Sort real and spoofed attributes...'); | ||
| 596 | my ( $realAttrs, $spoofedAttrs ) = ( [], [] ); | ||
| 597 | my $prefix = $self->prefix; | ||
| 598 | while (@$others) { | ||
| 599 | my $element = shift @$others; | ||
| 600 | $self->logger->debug( "Processing attribute $element->{key} => " | ||
| 601 | . ( $element->{value} // '' ) ); | ||
| 602 | if ( $element->{key} =~ /^$prefix.+$/ ) { | ||
| 603 | push @$realAttrs, $element; | ||
| 604 | $self->logger->debug(' -> Real attribute'); | ||
| 605 | } | ||
| 606 | else { | ||
| 607 | push @$spoofedAttrs, $element; | ||
| 608 | } | ||
| 609 | } | ||
| 610 | @$others = ( @$spoofedAttrs, @$realAttrs ); | ||
| 611 | } | ||
| 612 | |||
| 613 | return [ $grps, $mcrs, $others ]; | ||
| 614 | } | ||
| 615 | |||
| 616 | sub _removeKeys { | ||
| 617 | my ( $self, $attrs, $hidden, $msg, $mask ) = @_; | ||
| 618 | my $regex = '^(?:' . join( '|', split( /\s+/, $hidden ) ) . ')'; | ||
| 619 | my @keys = grep /$regex/, keys %$attrs; | ||
| 620 | |||
| 621 | $self->logger->debug($msg); | ||
| 622 | if ($mask) { | ||
| 623 | $self->userLogger->info('Hide some headers...'); | ||
| 624 | foreach (@keys) { | ||
| 625 | $attrs->{$_} = '******' if $attrs->{$_} =~ /\w+/; | ||
| 626 | } | ||
| 627 | } | ||
| 628 | else { | ||
| 629 | $self->userLogger->info('Remove some headers...'); | ||
| 630 | delete @$attrs{@keys}; | ||
| 631 | } | ||
| 632 | |||
| 633 | return $attrs; | ||
| 634 | } | ||
| 635 | |||
| 636 | sub _concatHistory { | ||
| 637 | my ( $self, $history ) = @_; | ||
| 638 | my ( $success, $failed ) = ( [], [] ); | ||
| 639 | |||
| 640 | $self->logger->debug('Concatenate history...'); | ||
| 641 | @$success = map { | ||
| 642 | my $element = $_; | ||
| 643 | my $utime = delete $element->{_utime}; | ||
| 644 | { | ||
| 645 | utime => $utime, | ||
| 646 | values => join $self->conf->{multiValuesSeparator}, | ||
| 647 | map "$_=$element->{$_}", sort keys %$element | ||
| 648 | } | ||
| 649 | } @{ $history->{successLogin} }; | ||
| 650 | |||
| 651 | @$failed = map { | ||
| 652 | my $element = $_; | ||
| 653 | my $utime = delete $element->{_utime}; | ||
| 654 | { | ||
| 655 | utime => $utime, | ||
| 656 | values => join $self->conf->{multiValuesSeparator}, | ||
| 657 | map "$_=$element->{$_}", sort keys %$element | ||
| 658 | } | ||
| 659 | } @{ $history->{failedLogin} }; | ||
| 660 | |||
| 661 | return [ $success, $failed ]; | ||
| 662 | } | ||
| 663 | |||
| 664 | 1; |