« blib/lib/Lemonldap/NG/Portal/Auth/OpenID.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/2F/Engines/Default.pm »
| 1 | # Check state plugin | ||
| 2 | # | ||
| 3 | # test if portal is well loaded. If user/pasword parameters are set, it tests | ||
| 4 | # also login process | ||
| 5 | |||
| 6 | package Lemonldap::NG::Portal::Plugins::CheckState; | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use Mouse; | ||
| 10 | use Lemonldap::NG::Portal; | ||
| 11 | |||
| 12 | our $VERSION = '2.21.0'; | ||
| 13 | |||
| 14 | extends 'Lemonldap::NG::Portal::Main::Plugin'; | ||
| 15 | |||
| 16 | # INITIALIZATION | ||
| 17 | |||
| 18 | sub init { | ||
| 19 | my ($self) = @_; | ||
| 20 | unless ( $self->conf->{checkStateSecret} ) { | ||
| 21 | $self->logger->error( | ||
| 22 | 'checkStateSecret is required for "check state" plugin'); | ||
| 23 | return 0; | ||
| 24 | } | ||
| 25 | $self->addUnauthRoute( checkstate => 'check', [ 'GET', 'POST' ] ) | ||
| 26 | ->addAuthRoute( checkstate => 'check', [ 'GET', 'POST' ] ); | ||
| 27 | |||
| 28 | return 1; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub check { | ||
| 32 | my ( $self, $req ) = @_; | ||
| 33 | my @rep; | ||
| 34 | return $self->p->sendError( $req, 'Bad secret' ) | ||
| 35 | unless ( $req->param('secret') | ||
| 36 | and $req->param('secret') eq $self->conf->{checkStateSecret} ); | ||
| 37 | $req->steps( [ 'controlUrl', @{ $self->p->beforeAuth } ] ); | ||
| 38 | my $res = $self->p->process($req); | ||
| 39 | if ( $res && $res > 0 ) { | ||
| 40 | push @rep, "Bad result before auth: $res"; | ||
| 41 | } | ||
| 42 | |||
| 43 | if ( !$self->p->HANDLER->checkConf ) { | ||
| 44 | push @rep, 'Unable to get conf'; | ||
| 45 | } | ||
| 46 | |||
| 47 | if ( defined $req->param('sessions') ) { | ||
| 48 | foreach (qw(global oidc saml cas)) { | ||
| 49 | my $module = $self->conf->{"${_}Storage"}; | ||
| 50 | my $moduleOpts = $self->conf->{"${_}StorageOptions"}; | ||
| 51 | my $type = $_ eq 'global' ? 'SSO' : uc($_); | ||
| 52 | if ( $module and $moduleOpts and %$moduleOpts ) { | ||
| 53 | my $session = Lemonldap::NG::Common::Session->new( { | ||
| 54 | storageModule => $module, | ||
| 55 | storageModuleOptions => $moduleOpts, | ||
| 56 | kind => 'TEST', | ||
| 57 | info => { | ||
| 58 | test => 'Test', | ||
| 59 | } | ||
| 60 | } | ||
| 61 | ); | ||
| 62 | if ( $session and $session->data ) { | ||
| 63 | unless ( $session->remove ) { | ||
| 64 | push @rep, "Unable to delete $type session"; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | else { | ||
| 68 | push @rep, "Unable to create $type session"; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | my $choice = $req->param('choice'); | ||
| 75 | if ($choice) { | ||
| 76 | $req->data->{_authChoice} = $choice; | ||
| 77 | } | ||
| 78 | |||
| 79 | my $keepUser = $req->user; | ||
| 80 | |||
| 81 | if ( my $user = $req->param('user') and my $pwd = $req->param('password') ) | ||
| 82 | { | ||
| 83 | $req->parameters->{user} = ($user); | ||
| 84 | $req->parameters->{password} = $pwd; | ||
| 85 | $req->data->{skipToken} = 1; | ||
| 86 | |||
| 87 | # This makes Auth::Choice use authChoiceAuthBasic if defined | ||
| 88 | $req->data->{_pwdCheck} = 1; | ||
| 89 | |||
| 90 | # Not launched methods: | ||
| 91 | # - "buildCookie" useless here | ||
| 92 | $req->steps( [ | ||
| 93 | @{ $self->p->beforeAuth }, | ||
| 94 | $self->p->authProcess, | ||
| 95 | @{ $self->p->betweenAuthAndData }, | ||
| 96 | $self->p->sessionData, | ||
| 97 | @{ $self->p->afterData }, | ||
| 98 | 'storeHistory', | ||
| 99 | @{ $self->p->endAuth } | ||
| 100 | ] | ||
| 101 | ); | ||
| 102 | if ( $res = $self->p->process( $req, ) ) { | ||
| 103 | push @rep, "Bad result during auth: $res"; | ||
| 104 | } | ||
| 105 | $self->p->deleteSession($req); | ||
| 106 | } | ||
| 107 | elsif ( $user = $req->param('user') ) { | ||
| 108 | $req->user($user); | ||
| 109 | if ( $self->p->getUser($req) ) { | ||
| 110 | push @rep, "Unable to find user $user" | ||
| 111 | . ( $choice ? " (choice: $choice)" : '' ); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | # Drop test parameters | ||
| 116 | delete $req->data->{_authChoice}; | ||
| 117 | $req->user($keepUser); | ||
| 118 | |||
| 119 | return $self->p->sendError( $req, join( ",\n", @rep ), 500 ) if (@rep); | ||
| 120 | return $self->p->sendJSONresponse( $req, | ||
| 121 | { result => 1, version => $Lemonldap::NG::Portal::VERSION } ); | ||
| 122 | } | ||
| 123 | |||
| 124 | 1; |