« blib/lib/Lemonldap/NG/Portal/Plugins/Register.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Plugins/MailPasswordReset.pm »
| 1 | package Lemonldap::NG::Portal::Plugins::CertificateResetByMail; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Encode; | ||
| 5 | use Mouse; | ||
| 6 | use Net::SSLeay; | ||
| 7 | use DateTime::Format::RFC3339; | ||
| 8 | use Digest::SHA qw(sha256_hex); | ||
| 9 | use MIME::Base64; | ||
| 10 | use POSIX qw(strftime); | ||
| 11 | use Lemonldap::NG::Common::FormEncode; | ||
| 12 | use Lemonldap::NG::Portal::Main::Constants qw( | ||
| 13 | PE_OK | ||
| 14 | PE_MAILOK | ||
| 15 | PE_NOTOKEN | ||
| 16 | PE_MAILERROR | ||
| 17 | PE_BADMAILTOKEN | ||
| 18 | PE_CAPTCHAEMPTY | ||
| 19 | PE_CAPTCHAERROR | ||
| 20 | PE_MAILNOTFOUND | ||
| 21 | PE_TOKENEXPIRED | ||
| 22 | PE_USERNOTFOUND | ||
| 23 | PE_MAILCONFIRMOK | ||
| 24 | PE_MAILFORMEMPTY | ||
| 25 | PE_MALFORMEDUSER | ||
| 26 | PE_BADCREDENTIALS | ||
| 27 | PE_MAILFIRSTACCESS | ||
| 28 | PE_RESETCERTIFICATE_INVALID | ||
| 29 | PE_RESETCERTIFICATE_FORMEMPTY | ||
| 30 | PE_RESETCERTIFICATE_FIRSTACCESS | ||
| 31 | PE_MAILCONFIRMATION_ALREADY_SENT | ||
| 32 | ); | ||
| 33 | |||
| 34 | our $VERSION = '2.19.0'; | ||
| 35 | |||
| 36 | extends qw( | ||
| 37 | Lemonldap::NG::Portal::Lib::SMTP | ||
| 38 | Lemonldap::NG::Portal::Main::Plugin | ||
| 39 | Lemonldap::NG::Portal::Lib::_tokenRule | ||
| 40 | ); | ||
| 41 | |||
| 42 | # PROPERTIES | ||
| 43 | |||
| 44 | # Mail timeout token generator | ||
| 45 | # Form timout token generator (used even if requireToken is not set) | ||
| 46 | has ott => ( | ||
| 47 | is => 'rw', | ||
| 48 | lazy => 1, | ||
| 49 | default => sub { | ||
| 50 | my $ott = | ||
| 51 | $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken'); | ||
| 52 | $ott->timeout( $_[0]->conf->{formTimeout} ); | ||
| 53 | return $ott; | ||
| 54 | } | ||
| 55 | ); | ||
| 56 | |||
| 57 | # Sub module (Demo, LDAP,...) | ||
| 58 | has registerModule => ( is => 'rw' ); | ||
| 59 | |||
| 60 | # Captcha generator | ||
| 61 | has captcha => ( is => 'rw' ); | ||
| 62 | |||
| 63 | # Mail timeout token generator | ||
| 64 | has mailott => ( | ||
| 65 | is => 'rw', | ||
| 66 | lazy => 1, | ||
| 67 | default => sub { | ||
| 68 | my $ott = | ||
| 69 | $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken'); | ||
| 70 | $ott->cache(0); | ||
| 71 | $ott->timeout( $_[0]->conf->{formTimeout} || $_[0]->conf->{timeout} ); | ||
| 72 | return $ott; | ||
| 73 | } | ||
| 74 | ); | ||
| 75 | |||
| 76 | # INITIALIZATION | ||
| 77 | |||
| 78 | sub init { | ||
| 79 | my ($self) = @_; | ||
| 80 | |||
| 81 | # Declare REST route | ||
| 82 | $self->addUnauthRoute( | ||
| 83 | certificateReset => 'certificateReset', | ||
| 84 | [ 'POST', 'GET' ] | ||
| 85 | ); | ||
| 86 | |||
| 87 | # Initialize Captcha if needed | ||
| 88 | $self->captcha( | ||
| 89 | $self->p->buildRule( | ||
| 90 | $self->conf->{captcha_mail_enabled}, | ||
| 91 | 'captchaMail' | ||
| 92 | ) | ||
| 93 | ); | ||
| 94 | |||
| 95 | # Load registered module | ||
| 96 | $self->registerModule( | ||
| 97 | $self->p->loadPlugin( | ||
| 98 | '::CertificateResetByMail::' . $self->conf->{registerDB} | ||
| 99 | ) | ||
| 100 | ) or return 0; | ||
| 101 | |||
| 102 | return 1; | ||
| 103 | } | ||
| 104 | |||
| 105 | # RUNNIG METHODS | ||
| 106 | |||
| 107 | # Handle reset requests | ||
| 108 | sub certificateReset { | ||
| 109 | my ( $self, $req ) = @_; | ||
| 110 | |||
| 111 | $self->p->controlUrl($req); | ||
| 112 | |||
| 113 | # Check parameters | ||
| 114 | $req->error( $self->_certificateReset($req) ); | ||
| 115 | |||
| 116 | # Display form | ||
| 117 | my ( $tpl, $prms ) = $self->display($req); | ||
| 118 | return $self->p->sendHtml( $req, $tpl, params => $prms ); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub _certificateReset { | ||
| 122 | my ( $self, $req ) = @_; | ||
| 123 | my ($mailToken); | ||
| 124 | |||
| 125 | # CertificatReset FORM => modifyCertificate() | ||
| 126 | if ( $req->method =~ /^POST$/i | ||
| 127 | and ( $req->uploads->{certif} ) ) | ||
| 128 | { | ||
| 129 | my $upload = $req->uploads->{certif}; | ||
| 130 | |||
| 131 | return $self->modifyCertificate($req); | ||
| 132 | } | ||
| 133 | |||
| 134 | # FIRST FORM | ||
| 135 | $mailToken = $req->data->{mailToken} = $req->param('mail_token'); | ||
| 136 | unless ( $req->param('mail') || $mailToken ) { | ||
| 137 | $self->setSecurity($req); | ||
| 138 | return PE_MAILFIRSTACCESS if ( $req->method eq 'GET' ); | ||
| 139 | return PE_MAILFORMEMPTY; | ||
| 140 | } | ||
| 141 | |||
| 142 | my $searchByMail = 1; | ||
| 143 | |||
| 144 | # OTHER FORMS | ||
| 145 | if ($mailToken) { | ||
| 146 | $self->logger->debug("Token given for certificate reset: $mailToken"); | ||
| 147 | |||
| 148 | # Check if token is valid | ||
| 149 | my $mailSession = | ||
| 150 | $self->p->getApacheSession( $mailToken, kind => "TOKEN" ); | ||
| 151 | unless ($mailSession) { | ||
| 152 | $self->userLogger->warn('Bad reset token'); | ||
| 153 | return PE_BADMAILTOKEN; | ||
| 154 | } | ||
| 155 | |||
| 156 | $req->{user} = $mailSession->data->{user}; | ||
| 157 | $req->data->{mailAddress} = | ||
| 158 | $mailSession->data->{ $self->conf->{mailSessionKey} }; | ||
| 159 | $self->logger->debug( 'User associated to: ' . $req->{user} ); | ||
| 160 | |||
| 161 | # Restore pdata if any | ||
| 162 | $req->pdata( $mailSession->data->{_pdata} || {} ); | ||
| 163 | $mailSession->remove; | ||
| 164 | $searchByMail = 0 unless ( $req->{user} =~ /\@/ ); | ||
| 165 | } | ||
| 166 | |||
| 167 | # Check for posted values | ||
| 168 | else { | ||
| 169 | |||
| 170 | # Use submitted value | ||
| 171 | $req->{user} = $req->param('mail'); | ||
| 172 | |||
| 173 | # Captcha for register form | ||
| 174 | if ( $self->captcha->( $req, {} ) ) { | ||
| 175 | my $result = $self->p->getService('captcha')->check_captcha($req); | ||
| 176 | if ($result) { | ||
| 177 | $self->logger->debug("Captcha code verified"); | ||
| 178 | } | ||
| 179 | else { | ||
| 180 | $self->setSecurity($req); | ||
| 181 | $self->userLogger->warn("Captcha failed"); | ||
| 182 | return PE_CAPTCHAERROR; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | elsif ( $self->ottRule->( $req, {} ) ) { | ||
| 186 | my $token = $req->param('token'); | ||
| 187 | unless ($token) { | ||
| 188 | $self->setSecurity($req); | ||
| 189 | $self->userLogger->warn('Reset try without token'); | ||
| 190 | return PE_NOTOKEN; | ||
| 191 | } | ||
| 192 | unless ( $self->ott->getToken($token) ) { | ||
| 193 | $self->setSecurity($req); | ||
| 194 | $self->userLogger->warn('Reset try with expired/bad token'); | ||
| 195 | return PE_TOKENEXPIRED; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | unless ( $req->{user} =~ /$self->{conf}->{userControl}/ ) { | ||
| 200 | $self->setSecurity($req); | ||
| 201 | return PE_MALFORMEDUSER; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | # Search user in database | ||
| 206 | $req->steps( [ | ||
| 207 | 'getUser', 'setSessionInfo', | ||
| 208 | $self->p->groupsAndMacros, 'setPersistentSessionInfo', | ||
| 209 | 'setLocalGroups' | ||
| 210 | ] | ||
| 211 | ); | ||
| 212 | if ( my $error = $self->p->process( $req, useMail => $searchByMail ) ) { | ||
| 213 | if ( $error == PE_USERNOTFOUND or $error == PE_BADCREDENTIALS ) { | ||
| 214 | $self->userLogger->warn( 'Reset asked for an invalid user (' | ||
| 215 | . $req->param('mail') | ||
| 216 | . ')' ); | ||
| 217 | |||
| 218 | # To avoid mail enumeration, return OK | ||
| 219 | # unless portalErrorOnMailNotFound is set | ||
| 220 | |||
| 221 | if ( $self->conf->{portalErrorOnMailNotFound} ) { | ||
| 222 | $self->setSecurity($req); | ||
| 223 | return PE_MAILNOTFOUND; | ||
| 224 | } | ||
| 225 | |||
| 226 | my $mailTimeout = | ||
| 227 | $self->conf->{mailTimeout} || $self->conf->{timeout}; | ||
| 228 | my $expTimestamp = time() + $mailTimeout; | ||
| 229 | $req->data->{expMailDate} = | ||
| 230 | strftime( '%d/%m/%Y', localtime $expTimestamp ); | ||
| 231 | $req->data->{expMailTime} = | ||
| 232 | strftime( '%H:%M', localtime $expTimestamp ); | ||
| 233 | return PE_MAILCONFIRMOK; | ||
| 234 | } | ||
| 235 | return $error; | ||
| 236 | } | ||
| 237 | |||
| 238 | # Build temporary session | ||
| 239 | my $mailSession = $self->getCertificateSession( $req->{user} ); | ||
| 240 | unless ( $mailSession or $mailToken ) { | ||
| 241 | |||
| 242 | ## Create a new session | ||
| 243 | my $infos = {}; | ||
| 244 | |||
| 245 | # Set _utime for session autoremove | ||
| 246 | # Use default session timeout and mail session timeout to compute it | ||
| 247 | my $time = time(); | ||
| 248 | my $timeout = $self->conf->{timeout}; | ||
| 249 | my $mailTimeout = $self->conf->{mailTimeout} || $timeout; | ||
| 250 | |||
| 251 | $infos->{_utime} = $time + ( $mailTimeout - $timeout ); | ||
| 252 | |||
| 253 | # Store expiration timestamp for further use | ||
| 254 | $infos->{mailSessionTimeoutTimestamp} = $time + $mailTimeout; | ||
| 255 | |||
| 256 | # Store start timestamp for further use | ||
| 257 | $infos->{mailSessionStartTimestamp} = $time; | ||
| 258 | |||
| 259 | # Store mail | ||
| 260 | $infos->{ $self->conf->{mailSessionKey} } = | ||
| 261 | $self->p->getFirstValue( | ||
| 262 | $req->{sessionInfo}->{ $self->conf->{mailSessionKey} } ); | ||
| 263 | |||
| 264 | # Store user | ||
| 265 | $infos->{user} = $req->{user}; | ||
| 266 | |||
| 267 | # Store type | ||
| 268 | $infos->{_type} = 'certificate'; | ||
| 269 | |||
| 270 | # Store pdata | ||
| 271 | $infos->{_pdata} = $req->pdata; | ||
| 272 | |||
| 273 | # create session | ||
| 274 | $mailSession = | ||
| 275 | $self->p->getApacheSession( undef, kind => "TOKEN", info => $infos ); | ||
| 276 | |||
| 277 | $req->id( $mailSession->id ); | ||
| 278 | } | ||
| 279 | elsif ($mailSession) { | ||
| 280 | $self->logger->debug( 'Mail session found: ' . $mailSession->id ); | ||
| 281 | $req->id( $mailSession->id ); | ||
| 282 | $req->data->{mailAlreadySent} = 1; | ||
| 283 | } | ||
| 284 | |||
| 285 | # Send confirmation mail | ||
| 286 | unless ($mailToken) { | ||
| 287 | |||
| 288 | # Mail session expiration date | ||
| 289 | my $expTimestamp = $mailSession->data->{mailSessionTimeoutTimestamp}; | ||
| 290 | |||
| 291 | $self->logger->debug("Mail expiration timestamp: $expTimestamp"); | ||
| 292 | |||
| 293 | $req->data->{expMailDate} = | ||
| 294 | strftime( '%d/%m/%Y', localtime $expTimestamp ); | ||
| 295 | $req->data->{expMailTime} = | ||
| 296 | strftime( '%H:%M', localtime $expTimestamp ); | ||
| 297 | |||
| 298 | # Mail session start date | ||
| 299 | my $startTimestamp = $mailSession->data->{mailSessionStartTimestamp}; | ||
| 300 | |||
| 301 | $self->logger->debug("Mail start timestamp: $startTimestamp"); | ||
| 302 | $req->data->{startMailDate} = | ||
| 303 | strftime( '%d/%m/%Y', localtime $startTimestamp ); | ||
| 304 | $req->data->{startMailTime} = | ||
| 305 | strftime( '%H:%M', localtime $startTimestamp ); | ||
| 306 | |||
| 307 | # Ask if user wants an another confirmation email | ||
| 308 | if ( $req->data->{mailAlreadySent} | ||
| 309 | and not $req->param('resendconfirmation') ) | ||
| 310 | { | ||
| 311 | $self->userLogger->notice( | ||
| 312 | 'Reset mail already sent to ' . $req->{user} ); | ||
| 313 | |||
| 314 | # Return mail already sent only if it is allowed at previous step | ||
| 315 | if ( $self->conf->{portalErrorOnMailNotFound} ) { | ||
| 316 | $self->setSecurity($req); | ||
| 317 | return PE_MAILCONFIRMATION_ALREADY_SENT; | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | # Get mail address | ||
| 322 | $req->data->{mailAddress} ||= | ||
| 323 | $self->p->getFirstValue( | ||
| 324 | $req->{sessionInfo}->{ $self->conf->{mailSessionKey} } ); | ||
| 325 | return PE_MAILERROR unless ( $req->data->{mailAddress} ); | ||
| 326 | |||
| 327 | # Build confirmation url | ||
| 328 | my $req_url = $req->data->{_url}; | ||
| 329 | my $skin = $self->p->getSkin($req); | ||
| 330 | my $url = $self->p->buildUrl( ( | ||
| 331 | $self->conf->{certificateResetByMailURL} | ||
| 332 | || ( $req->portal, "certificateReset" ) | ||
| 333 | ), | ||
| 334 | { | ||
| 335 | mail_token => $req->{id}, | ||
| 336 | skin => $skin, | ||
| 337 | ( $req_url ? ( url => $req_url ) : () ), | ||
| 338 | } | ||
| 339 | ); | ||
| 340 | |||
| 341 | unless ( | ||
| 342 | $self->sendEmail( | ||
| 343 | $req, | ||
| 344 | subject => $self->conf->{certificateResetByMailStep1Subject}, | ||
| 345 | subject_trmsg => 'certificateResetByMailStep1Subject', | ||
| 346 | body => $self->conf->{certificateResetByMailStep1Body}, | ||
| 347 | body_template => 'mail_certificateConfirm', | ||
| 348 | dest => $req->data->{mailAddress}, | ||
| 349 | params => { | ||
| 350 | expMailDate => $req->data->{expMailDate}, | ||
| 351 | expMailTime => $req->data->{expMailTime}, | ||
| 352 | url => $url, | ||
| 353 | } | ||
| 354 | ) | ||
| 355 | ) | ||
| 356 | { | ||
| 357 | $self->logger->debug('Unable to send reset mail'); | ||
| 358 | |||
| 359 | # Don't return an error here to avoid enumeration | ||
| 360 | } | ||
| 361 | return PE_MAILCONFIRMOK; | ||
| 362 | } | ||
| 363 | |||
| 364 | # User has a valid mailToken, allow to reset certificate | ||
| 365 | # A token is required | ||
| 366 | $self->ott->setToken( | ||
| 367 | $req, | ||
| 368 | { | ||
| 369 | %{ $req->sessionInfo }, certificateResetAllowed => 1 | ||
| 370 | } | ||
| 371 | ); | ||
| 372 | return PE_RESETCERTIFICATE_FIRSTACCESS if ( $req->method eq 'GET' ); | ||
| 373 | return PE_RESETCERTIFICATE_FORMEMPTY; | ||
| 374 | } | ||
| 375 | |||
| 376 | sub modifyCertificate { | ||
| 377 | my ( $self, $req ) = @_; | ||
| 378 | my $nbio; | ||
| 379 | my $x509; | ||
| 380 | my $notAfter; | ||
| 381 | |||
| 382 | $self->logger->debug('Change your certificate form response'); | ||
| 383 | |||
| 384 | if ( my $token = $req->param('token') ) { | ||
| 385 | $req->sessionInfo( $self->ott->getToken($token) ); | ||
| 386 | unless ( $req->sessionInfo ) { | ||
| 387 | $self->userLogger->warn( | ||
| 388 | 'User tries to change certificate with an invalid or expired token' | ||
| 389 | ); | ||
| 390 | return PE_NOTOKEN; | ||
| 391 | } | ||
| 392 | } | ||
| 393 | |||
| 394 | # These 2 cases means that a user tries to reset certificate without | ||
| 395 | # following valid links!!! | ||
| 396 | else { | ||
| 397 | $self->userLogger->error( | ||
| 398 | 'User tries to reset certificate without token'); | ||
| 399 | return PE_NOTOKEN; | ||
| 400 | } | ||
| 401 | |||
| 402 | unless ( $req->sessionInfo->{certificateResetAllowed} ) { | ||
| 403 | $self->userLogger->error( | ||
| 404 | 'User tries to use another token to reset certificate'); | ||
| 405 | return PE_NOTOKEN; | ||
| 406 | } | ||
| 407 | |||
| 408 | #Updload certificate | ||
| 409 | my $upload = $req->uploads->{certif}; | ||
| 410 | return PE_RESETCERTIFICATE_FORMEMPTY unless ( $upload->size > 0 ); | ||
| 411 | |||
| 412 | # Get certificate | ||
| 413 | my $file = $upload->path; | ||
| 414 | $self->userLogger->debug( "Temporaly file " . $file ); | ||
| 415 | |||
| 416 | # Convert certificate file uploaded on DER format with openssl library | ||
| 417 | |||
| 418 | #my $certifbase64 =`openssl x509 -outform der -in $file -out $file`; | ||
| 419 | |||
| 420 | # load certificate from file with openssl library | ||
| 421 | $nbio = Net::SSLeay::BIO_new_file( $file, 'r' ) or die $!; | ||
| 422 | |||
| 423 | # for PEM certificate | ||
| 424 | $x509 = Net::SSLeay::PEM_read_bio_X509($nbio); | ||
| 425 | |||
| 426 | Net::SSLeay::BIO_free($nbio); | ||
| 427 | unless ($x509) { | ||
| 428 | $self->userLogger->debug( "Unable to decode certificate for user " | ||
| 429 | . Net::SSLeay::ERR_error_string( Net::SSLeay::ERR_get_error() ) ); | ||
| 430 | return PE_RESETCERTIFICATE_INVALID; | ||
| 431 | } | ||
| 432 | $self->userLogger->debug("Certificate successfully decoded"); | ||
| 433 | $notAfter = Net::SSLeay::P_ASN1_TIME_get_isotime( | ||
| 434 | Net::SSLeay::X509_get_notAfter($x509) ); | ||
| 435 | |||
| 436 | my $x509issuer = Net::SSLeay::X509_NAME_oneline( | ||
| 437 | Net::SSLeay::X509_get_issuer_name($x509) ); | ||
| 438 | |||
| 439 | my $x509serial = Net::SSLeay::P_ASN1_INTEGER_get_hex( | ||
| 440 | Net::SSLeay::X509_get_serialNumber($x509) ); | ||
| 441 | |||
| 442 | $self->userLogger->debug( | ||
| 443 | "Certificate will expire after $notAfter, Issuer $x509issuer and serialNumber $x509serial" | ||
| 444 | ); | ||
| 445 | |||
| 446 | # Check certificate validity before store | ||
| 447 | if ( | ||
| 448 | $self->checkCertificateValidity( $notAfter, | ||
| 449 | $self->conf->{certificateResetByMailValidityDelay} ) == 0 | ||
| 450 | ) | ||
| 451 | { | ||
| 452 | $self->userLogger->debug( | ||
| 453 | "Your certificate is no longer valid in $self->conf->{certificateValidityDelay}" | ||
| 454 | ); | ||
| 455 | return PE_RESETCERTIFICATE_INVALID; | ||
| 456 | } | ||
| 457 | |||
| 458 | # Build serial number hex: example f3:08:52:63:28:29:fa:e2 | ||
| 459 | my @numberstring = split //, lc($x509serial); | ||
| 460 | my $serial = ""; | ||
| 461 | for ( my $i = 0 ; $i <= $#numberstring ; $i += 2 ) { | ||
| 462 | $serial = $serial . $numberstring[$i] . $numberstring[ $i + 1 ]; | ||
| 463 | if ( $i + 2 < $#numberstring ) { $serial = $serial . ":"; } | ||
| 464 | } | ||
| 465 | |||
| 466 | # format issuer in the good format example "CN=CA,OU=CISIRH,O=MINEFI,L=Paris,ST=France,C=FR" | ||
| 467 | my @issuertab = split /\//, $x509issuer; | ||
| 468 | shift(@issuertab); | ||
| 469 | my $issuer = join( ",", reverse(@issuertab) ); | ||
| 470 | |||
| 471 | #$issuer = lc($issuer); | ||
| 472 | |||
| 473 | my $certificatExactAssertion = | ||
| 474 | '{ serialNumber ' . $serial . ', issuer rdnSequence:"' . $issuer . '" }'; | ||
| 475 | $self->userLogger->debug( "Description:: " . $certificatExactAssertion ); | ||
| 476 | |||
| 477 | # Get attribut userCertificate;binary value | ||
| 478 | my $cert = $self->certificateHash($file); | ||
| 479 | |||
| 480 | # Modify ldap certificate attribute | ||
| 481 | $req->user( $req->{sessionInfo}->{_user} ); | ||
| 482 | my $result = | ||
| 483 | $self->registerModule->modifCertificate( $req, $certificatExactAssertion, | ||
| 484 | $cert ); | ||
| 485 | $self->{user} = undef; | ||
| 486 | |||
| 487 | # Mail token can be used only one time, delete the session if all is ok | ||
| 488 | return $result unless ( $result == PE_OK ); | ||
| 489 | |||
| 490 | # Send mail to notify the certificate has been successfully reset | ||
| 491 | $req->data->{mailAddress} ||= | ||
| 492 | $self->p->getFirstValue( | ||
| 493 | $req->{sessionInfo}->{ $self->conf->{mailSessionKey} } ); | ||
| 494 | |||
| 495 | # Send mail | ||
| 496 | return PE_MAILERROR | ||
| 497 | unless $self->sendEmail( | ||
| 498 | $req, | ||
| 499 | subject => $self->conf->{certificateResetByMailStep2Subject}, | ||
| 500 | subject_trmsg => 'certificateResetByMailStep2Subject', | ||
| 501 | body => $self->conf->{certificateResetByMailStep2Body}, | ||
| 502 | body_template => 'mail_certificateReset', | ||
| 503 | dest => $req->data->{mailAddress}, | ||
| 504 | ); | ||
| 505 | |||
| 506 | return PE_MAILOK; | ||
| 507 | } | ||
| 508 | |||
| 509 | sub setSecurity { | ||
| 510 | my ( $self, $req ) = @_; | ||
| 511 | |||
| 512 | if ( $self->captcha->( $req, {} ) ) { | ||
| 513 | $self->p->getService('captcha')->init_captcha($req); | ||
| 514 | } | ||
| 515 | |||
| 516 | elsif ( $self->ottRule->( $req, {} ) ) { | ||
| 517 | $self->ott->setToken($req); | ||
| 518 | } | ||
| 519 | return 1; | ||
| 520 | } | ||
| 521 | |||
| 522 | sub display { | ||
| 523 | my ( $self, $req ) = @_; | ||
| 524 | $self->logger->debug( 'Display called with code: ' . $req->error ); | ||
| 525 | my %tplPrm = ( | ||
| 526 | $self->p->getErrorTplParams($req), | ||
| 527 | SKIN_PATH => $self->conf->{staticPrefix}, | ||
| 528 | SKIN => $self->p->getSkin($req), | ||
| 529 | SKIN_BG => $self->conf->{portalSkinBackground}, | ||
| 530 | MAIN_LOGO => $self->conf->{portalMainLogo}, | ||
| 531 | AUTH_URL => $req->data->{_url}, | ||
| 532 | CHOICE_VALUE => $req->{_authChoice}, | ||
| 533 | EXPMAILDATE => $req->data->{expMailDate}, | ||
| 534 | EXPMAILTIME => $req->data->{expMailTime}, | ||
| 535 | STARTMAILDATE => $req->data->{startMailDate}, | ||
| 536 | STARTMAILTIME => $req->data->{startMailTime}, | ||
| 537 | MAILALREADYSENT => $req->data->{mailAlreadySent}, | ||
| 538 | ( | ||
| 539 | $req->data->{customScript} | ||
| 540 | ? ( CUSTOM_SCRIPT => $req->data->{customScript} ) | ||
| 541 | : () | ||
| 542 | ), | ||
| 543 | MAIL => ( | ||
| 544 | $self->p->checkXSSAttack( 'mail', $req->{user} ) ? '' | ||
| 545 | : $req->{user} | ||
| 546 | ), | ||
| 547 | DISPLAY_FORM => 0, | ||
| 548 | DISPLAY_RESEND_FORM => 0, | ||
| 549 | DISPLAY_CONFIRMMAILSENT => 0, | ||
| 550 | DISPLAY_MAILSENT => 0, | ||
| 551 | DISPLAY_CERTIF_FORM => 0, | ||
| 552 | ); | ||
| 553 | if ( $req->data->{mailToken} | ||
| 554 | and | ||
| 555 | not $self->p->checkXSSAttack( 'mail_token', $req->data->{mailToken} ) ) | ||
| 556 | { | ||
| 557 | $tplPrm{MAIL_TOKEN} = $req->data->{mailToken}; | ||
| 558 | } | ||
| 559 | |||
| 560 | # Display captcha if enabled | ||
| 561 | if ( $req->captchaHtml ) { | ||
| 562 | $tplPrm{CAPTCHA_HTML} = $req->captchaHtml; | ||
| 563 | } | ||
| 564 | if ( $req->token ) { | ||
| 565 | $tplPrm{TOKEN} = $req->token; | ||
| 566 | } | ||
| 567 | |||
| 568 | # DEPRECATED: This is only used for compatibility with existing templates | ||
| 569 | if ( $req->captcha ) { | ||
| 570 | $tplPrm{CAPTCHA_SRC} = $req->captcha; | ||
| 571 | $tplPrm{CAPTCHA_SIZE} = $self->conf->{captcha_size}; | ||
| 572 | } | ||
| 573 | |||
| 574 | # Display form the first time | ||
| 575 | if ( ( | ||
| 576 | $req->error == PE_MAILFORMEMPTY | ||
| 577 | or $req->error == PE_MAILFIRSTACCESS | ||
| 578 | or $req->error == PE_MAILNOTFOUND | ||
| 579 | or $req->error == PE_CAPTCHAERROR | ||
| 580 | or $req->error == PE_CAPTCHAEMPTY | ||
| 581 | ) | ||
| 582 | and not $req->data->{mailToken} | ||
| 583 | ) | ||
| 584 | { | ||
| 585 | $self->logger->debug('Display form'); | ||
| 586 | $tplPrm{DISPLAY_FORM} = 1; | ||
| 587 | } | ||
| 588 | |||
| 589 | # Display mail confirmation resent form | ||
| 590 | elsif ( $req->error == PE_MAILCONFIRMATION_ALREADY_SENT ) { | ||
| 591 | $self->logger->debug('Display resend form'); | ||
| 592 | $tplPrm{DISPLAY_RESEND_FORM} = 1; | ||
| 593 | } | ||
| 594 | |||
| 595 | # Display confirmation mail sent | ||
| 596 | elsif ( $req->error == PE_MAILCONFIRMOK ) { | ||
| 597 | $self->logger->debug('Display "confirm mail sent"'); | ||
| 598 | $tplPrm{DISPLAY_CONFIRMMAILSENT} = 1; | ||
| 599 | } | ||
| 600 | |||
| 601 | # Display mail sent | ||
| 602 | elsif ( $req->error == PE_MAILOK ) { | ||
| 603 | $self->logger->debug('Display "mail sent"'); | ||
| 604 | $tplPrm{DISPLAY_MAILSENT} = 1; | ||
| 605 | } | ||
| 606 | |||
| 607 | # Display certificate reset form | ||
| 608 | elsif ( $req->data->{mailToken} | ||
| 609 | and $req->error != PE_MAILERROR | ||
| 610 | and $req->error != PE_BADMAILTOKEN | ||
| 611 | and $req->error != PE_MAILOK ) | ||
| 612 | { | ||
| 613 | $self->logger->debug('Display certificate reset form'); | ||
| 614 | $tplPrm{DISPLAY_CERTIF_FORM} = 1; | ||
| 615 | } | ||
| 616 | |||
| 617 | # Display certificate reset form again if certificate invalid | ||
| 618 | elsif ($req->error == PE_RESETCERTIFICATE_FORMEMPTY | ||
| 619 | || $req->error == PE_RESETCERTIFICATE_INVALID ) | ||
| 620 | { | ||
| 621 | $self->logger->debug('Display Certificate Reset form'); | ||
| 622 | $tplPrm{DISPLAY_CERTIF_FORM} = 1; | ||
| 623 | } | ||
| 624 | |||
| 625 | return 'certificateReset', \%tplPrm; | ||
| 626 | } | ||
| 627 | |||
| 628 | #string getCertifResetSession (string mail) | ||
| 629 | # Check if a certificate reset session exists | ||
| 630 | # @param mail the value of the mail key in session | ||
| 631 | # @return the first session id found or nothing if no session | ||
| 632 | sub getCertificateSession { | ||
| 633 | my ( $self, $user ) = @_; | ||
| 634 | my $moduleOptions = $self->conf->{globalStorageOptions} || {}; | ||
| 635 | $moduleOptions->{backend} = $self->conf->{globalStorage}; | ||
| 636 | my $module = "Lemonldap::NG::Common::Apache::Session"; | ||
| 637 | |||
| 638 | # Search on modifyaccount sessions | ||
| 639 | my $sessions = $module->searchOn( $moduleOptions, "user", $user ); | ||
| 640 | |||
| 641 | # Browse found sessions to check if it's a modifyaccount session | ||
| 642 | foreach my $id ( keys %$sessions ) { | ||
| 643 | my $certificateResetSession = | ||
| 644 | $self->p->getApacheSession( $id, ( kind => "TOKEN" ) ); | ||
| 645 | next unless ($certificateResetSession); | ||
| 646 | return $certificateResetSession | ||
| 647 | if ( $certificateResetSession->data->{_type} | ||
| 648 | and $certificateResetSession->data->{_type} =~ /^certificate$/ ); | ||
| 649 | } | ||
| 650 | |||
| 651 | # No modifyaccount session found, return empty string | ||
| 652 | return ""; | ||
| 653 | } | ||
| 654 | |||
| 655 | sub checkCertificateValidity { | ||
| 656 | my ( $self, $notAfter, $delay ) = @_; | ||
| 657 | my $dtNow; # now in format DateTime | ||
| 658 | my $days; # difference between NotAfter and now | ||
| 659 | my $f = DateTime::Format::RFC3339->new(); | ||
| 660 | my $dtNotAfter = $f->parse_datetime($notAfter); | ||
| 661 | $self->userLogger->debug("Not After Date: $dtNotAfter"); | ||
| 662 | |||
| 663 | $dtNow = DateTime->now; | ||
| 664 | $days = $dtNotAfter->delta_days($dtNow)->delta_days; | ||
| 665 | $dtNow->add_duration( DateTime::Duration->new( days => $delay ) ); | ||
| 666 | |||
| 667 | # test if ( now + $validity ) > certificate_expiration | ||
| 668 | if ( DateTime::compare( $dtNow, $dtNotAfter ) >= 0 ) { | ||
| 669 | |||
| 670 | # certificate is about to expire | ||
| 671 | $self->userLogger->debug( | ||
| 672 | "Certificate is about to expire or already expired"); | ||
| 673 | return 0; | ||
| 674 | } | ||
| 675 | else { | ||
| 676 | # certificate is still valid | ||
| 677 | $self->userLogger->debug("Certificate is still valid for $days days"); | ||
| 678 | return 1; | ||
| 679 | } | ||
| 680 | } | ||
| 681 | |||
| 682 | sub certificateHash { | ||
| 683 | my ( $self, $file ) = @_; | ||
| 684 | my $cert; | ||
| 685 | |||
| 686 | { | ||
| 687 | local $/ = undef; # Slurp mode | ||
| 688 | open CERT, "<", "$file" or die; | ||
| 689 | $cert = <CERT>; | ||
| 690 | close CERT; | ||
| 691 | } | ||
| 692 | |||
| 693 | # Normalize certificate | ||
| 694 | $cert =~ s/-----(BEGIN|END) CERTIFICATE-----//gi; | ||
| 695 | $cert =~ s/["]//gi; | ||
| 696 | $cert = decode_base64($cert); | ||
| 697 | |||
| 698 | #$self->userLogger->debug( "UserBinary::".$cert); | ||
| 699 | return $cert; | ||
| 700 | } | ||
| 701 | |||
| 702 | 1; |