« blib/lib/Lemonldap/NG/Portal/Lib/SMTP.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Plugins/SamlFederation.pm »
| 1 | package Lemonldap::NG::Portal::Plugins::FindUser; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse; | ||
| 5 | use Lemonldap::NG::Portal::Main::Constants qw( | ||
| 6 | PE_OK | ||
| 7 | PE_NOTOKEN | ||
| 8 | PE_FIRSTACCESS | ||
| 9 | PE_TOKENEXPIRED | ||
| 10 | ); | ||
| 11 | |||
| 12 | our $VERSION = '2.22.0'; | ||
| 13 | |||
| 14 | extends qw( | ||
| 15 | Lemonldap::NG::Portal::Main::Plugin | ||
| 16 | Lemonldap::NG::Portal::Lib::_tokenRule | ||
| 17 | ); | ||
| 18 | |||
| 19 | # INITIALIZATION | ||
| 20 | has ott => ( | ||
| 21 | is => 'rw', | ||
| 22 | lazy => 1, | ||
| 23 | default => sub { | ||
| 24 | my $ott = | ||
| 25 | $_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken'); | ||
| 26 | $ott->timeout( $_[0]->{conf}->{formTimeout} ); | ||
| 27 | return $ott; | ||
| 28 | } | ||
| 29 | ); | ||
| 30 | |||
| 31 | sub init { | ||
| 32 | my ($self) = @_; | ||
| 33 | ( my $imp = grep /::Plugins::Impersonation$/, $self->p->enabledPlugins ) | ||
| 34 | ? $self->addUnauthRoute( finduser => 'provideUser', ['POST'] ) | ||
| 35 | ->addAuthRoute( | ||
| 36 | finduser => 'provideUser', | ||
| 37 | ['POST'] | ||
| 38 | ) # Allow findUser with reAuth | ||
| 39 | : $self->logger->warn('FindUser plugin enabled without Impersonation'); | ||
| 40 | $self->logger->warn('FindUser plugin enabled without searching attribute') | ||
| 41 | unless keys %{ $self->conf->{findUserSearchingAttributes} }; | ||
| 42 | |||
| 43 | return 1; | ||
| 44 | } | ||
| 45 | |||
| 46 | # RUNNING METHOD | ||
| 47 | sub provideUser { | ||
| 48 | my ( $self, $req ) = @_; | ||
| 49 | my $error; | ||
| 50 | |||
| 51 | # Check token | ||
| 52 | if ( $self->ottRule->( $req, {} ) ) { | ||
| 53 | if ( my $token = $req->param('token') ) { | ||
| 54 | unless ( $self->ott->getToken($token) ) { | ||
| 55 | $self->userLogger->warn( | ||
| 56 | 'FindUser called with an expired/bad token'); | ||
| 57 | $error = PE_TOKENEXPIRED; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | $self->userLogger->warn('FindUser called without token'); | ||
| 62 | $error = PE_NOTOKEN; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | return $self->_sendResult( $req, $error ) if $error; | ||
| 66 | |||
| 67 | $req->steps( ['findUser'] ); | ||
| 68 | $req->data->{findUserChoice} = $self->conf->{authChoiceFindUser}; | ||
| 69 | if ( $error = $self->p->process($req) ) { | ||
| 70 | $self->logger->debug("Process returned error: $error"); | ||
| 71 | return $self->_sendResult( $req, $error ); | ||
| 72 | } | ||
| 73 | return $self->_sendResult($req); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub retreiveFindUserParams { | ||
| 77 | my ( $self, $req ) = @_; | ||
| 78 | my ( $searching, $excluding, @required ) = ( [], [], () ); | ||
| 79 | |||
| 80 | $self->logger->debug("FindUser: reading parameters..."); | ||
| 81 | @$searching = map { | ||
| 82 | my ( $key, $value, $null ) = split '#', $_; | ||
| 83 | $key =~ s/^(?:\d+_)?//; | ||
| 84 | my $param = $req->params($key) // ''; | ||
| 85 | my @values = grep s/^(?:\d+_)?//, | ||
| 86 | split( $self->conf->{multiValuesSeparator}, | ||
| 87 | $self->conf->{findUserSearchingAttributes}->{$_} || '' ); | ||
| 88 | my $select = scalar @values > 1 && not scalar @values % 2; | ||
| 89 | my %values = @values if $select; | ||
| 90 | my $defined = length $param; | ||
| 91 | my $regex = '^(?:' . join( '|', keys %values ) . ')$'; | ||
| 92 | my $checked = | ||
| 93 | $select | ||
| 94 | ? $param =~ /$regex/ | ||
| 95 | : $param =~ /$self->{conf}->{findUserControl}/; | ||
| 96 | push @required, $key unless $null; | ||
| 97 | |||
| 98 | # For <select>, accept only set values or empty if allowed | ||
| 99 | if ( $defined && $checked ) { | ||
| 100 | $self->logger->debug("Append searching parameter: $key => $param"); | ||
| 101 | { key => $key, value => $param }; | ||
| 102 | } | ||
| 103 | else { | ||
| 104 | if ($defined) { | ||
| 105 | my $warn = | ||
| 106 | "Parameter $key has been rejected by findUserControl: "; | ||
| 107 | $warn .= $select ? $regex : $self->conf->{findUserControl}; | ||
| 108 | $self->logger->warn($warn); | ||
| 109 | } | ||
| 110 | (); | ||
| 111 | } | ||
| 112 | } sort keys %{ $self->conf->{findUserSearchingAttributes} }; | ||
| 113 | |||
| 114 | if ( scalar @required ) { | ||
| 115 | my $test = 0; | ||
| 116 | foreach my $ref (@$searching) { | ||
| 117 | foreach (@required) { | ||
| 118 | $test++ if $ref->{key} eq $_; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | unless ( scalar @required == $test ) { | ||
| 122 | $self->logger->warn( 'A required parameter is missing (' | ||
| 123 | . join( '|', @required ) | ||
| 124 | . ')' ); | ||
| 125 | $searching = []; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | if ( scalar @$searching | ||
| 130 | && keys %{ $self->conf->{findUserExcludingAttributes} } ) | ||
| 131 | { | ||
| 132 | $self->logger->debug("FindUser: reading excluding parameters..."); | ||
| 133 | @$excluding = map { | ||
| 134 | my $key = $_; | ||
| 135 | map { | ||
| 136 | $self->logger->debug("Push excluding parameter: $key => $_"); | ||
| 137 | { # Allow multivalued excluding parameters | ||
| 138 | key => $key, | ||
| 139 | value => $_ | ||
| 140 | } | ||
| 141 | } split $self->conf->{multiValuesSeparator}, | ||
| 142 | $self->conf->{findUserExcludingAttributes}->{$_}; | ||
| 143 | } sort keys %{ $self->conf->{findUserExcludingAttributes} }; | ||
| 144 | } | ||
| 145 | |||
| 146 | return ( $searching, $excluding ); | ||
| 147 | } | ||
| 148 | |||
| 149 | sub buildForm { | ||
| 150 | my $self = shift; | ||
| 151 | my ( $fields, @required ) = ( [], () ); | ||
| 152 | |||
| 153 | $self->logger->debug('Building array ref with searching fields...'); | ||
| 154 | @$fields = | ||
| 155 | map { | ||
| 156 | my ( $key, $value, $null ) = split '#', $_; | ||
| 157 | my @values = split $self->conf->{multiValuesSeparator}, | ||
| 158 | $self->conf->{findUserSearchingAttributes}->{$_} || $key; | ||
| 159 | $key =~ s/^(?:\d+_)?//; | ||
| 160 | push @required, $key unless $null; | ||
| 161 | my $nbr = scalar @values; | ||
| 162 | if ( $nbr > 1 ) { | ||
| 163 | if ( $nbr % 2 ) { () } | ||
| 164 | else { | ||
| 165 | my %hash = @values; | ||
| 166 | my $choices = []; | ||
| 167 | $nbr /= 2; | ||
| 168 | $self->logger->debug( | ||
| 169 | "Building $key with type 'select' and $nbr entries..."); | ||
| 170 | @$choices = map { | ||
| 171 | my $k = $_; | ||
| 172 | $k =~ s/^(?:\d+_)?//; | ||
| 173 | { key => $k, value => $hash{$_} } | ||
| 174 | } sort keys %hash; | ||
| 175 | { | ||
| 176 | select => 1, | ||
| 177 | key => $key, | ||
| 178 | value => $value ? $value : $key, | ||
| 179 | choices => $choices, | ||
| 180 | null => $null | ||
| 181 | }; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | else { | ||
| 185 | { | ||
| 186 | select => 0, | ||
| 187 | key => $key, | ||
| 188 | value => $values[0], | ||
| 189 | null => $null | ||
| 190 | }; | ||
| 191 | } | ||
| 192 | } sort keys %{ $self->conf->{findUserSearchingAttributes} }; | ||
| 193 | $self->logger->debug('Mandatory field(s) required') if scalar @required; | ||
| 194 | |||
| 195 | return ( $fields, scalar @required ); | ||
| 196 | } | ||
| 197 | |||
| 198 | sub _sendResult { | ||
| 199 | my ( $self, $req, $error ) = @_; | ||
| 200 | |||
| 201 | eval { $self->p->_authentication->setSecurity($req) }; | ||
| 202 | my $res = | ||
| 203 | $error | ||
| 204 | ? { | ||
| 205 | user => '', | ||
| 206 | error => $error, | ||
| 207 | } | ||
| 208 | : { | ||
| 209 | result => 1, | ||
| 210 | user => ( $req->data->{findUser} ? $req->data->{findUser} : '' ) | ||
| 211 | }; | ||
| 212 | $res = { | ||
| 213 | %$res, | ||
| 214 | ( $req->token ? ( token => $req->token ) : () ), | ||
| 215 | ( $req->captcha ? ( captcha => $req->captcha ) : () ) | ||
| 216 | }; | ||
| 217 | $error ||= PE_FIRSTACCESS; | ||
| 218 | |||
| 219 | return $req->wantJSON | ||
| 220 | ? $self->p->sendJSONresponse( $req, $res ) | ||
| 221 | : $self->p->doPE( $req, $error ); | ||
| 222 | } | ||
| 223 | |||
| 224 | 1; |