Display.pm 21.8 KB
Newer Older
Xavier Guimard's avatar
Xavier Guimard committed
1 2 3 4
## @file
# Display functions for LemonLDAP::NG Portal
package Lemonldap::NG::Portal::Main::Display;

Xavier Guimard's avatar
Xavier Guimard committed
5
our $VERSION = '2.0.2';
Xavier Guimard's avatar
Xavier Guimard committed
6 7 8

package Lemonldap::NG::Portal::Main;
use strict;
9
use Mouse;
10
use JSON;
11
use Data::Dumper;
Xavier Guimard's avatar
Xavier Guimard committed
12

13 14 15 16 17 18
has skinRules => ( is => 'rw' );

sub displayInit {
    my ($self) = @_;
    $self->skinRules( [] );
    if ( $self->conf->{portalSkinRules} ) {
Clément OUDOT's avatar
Clément OUDOT committed
19
        foreach my $skinRule ( sort keys %{ $self->conf->{portalSkinRules} } ) {
20 21 22
            my $sub = HANDLER->buildSub( HANDLER->substitute($skinRule) );
            if ($sub) {
                push @{ $self->skinRules },
Clément OUDOT's avatar
Clément OUDOT committed
23
                  [ $self->conf->{portalSkinRules}->{$skinRule}, $sub ];
24 25
            }
            else {
26
                $self->logger->error(
27
                    qq(Skin rule "$skinRule" returns an error: )
Clément OUDOT's avatar
Clément OUDOT committed
28
                      . HANDLER->tsv->{jail}->error );
29 30 31 32 33
            }
        }
    }
}

Xavier Guimard's avatar
Xavier Guimard committed
34 35 36 37 38 39
# Call portal process and set template parameters
# @return template name and template parameters
sub display {
    my ( $self, $req ) = @_;

    my $skin_dir = $self->conf->{templatesDir};
Xavier Guimard's avatar
Xavier Guimard committed
40
    my ( $skinfile, %templateParams );
Xavier Guimard's avatar
Xavier Guimard committed
41

Xavier Guimard's avatar
Xavier Guimard committed
42
    # 1. Authentication not complete
Xavier Guimard's avatar
Xavier Guimard committed
43

Xavier Guimard's avatar
Xavier Guimard committed
44
    # 1.1 A notification has to be done (session is created but hidden and
Xavier Guimard's avatar
Xavier Guimard committed
45
    #     unusable until the user has accept the message)
Xavier Guimard's avatar
Xavier Guimard committed
46
    if ( my $notif = $req->data->{notification} ) {
Xavier Guimard's avatar
Xavier Guimard committed
47
        $self->logger->debug('Display: notification detected');
Xavier Guimard's avatar
Xavier Guimard committed
48 49
        $skinfile       = 'notification';
        %templateParams = (
50
            MAIN_LOGO       => $self->conf->{portalMainLogo},
51
            LANGS           => $self->conf->{showLanguages},
Xavier Guimard's avatar
Xavier Guimard committed
52 53
            AUTH_ERROR_TYPE => $req->error_type,
            NOTIFICATION    => $notif,
Xavier Guimard's avatar
Xavier Guimard committed
54
            HIDDEN_INPUTS   => $self->buildHiddenForm($req),
Xavier Guimard's avatar
Xavier Guimard committed
55
            AUTH_URL        => $req->{data}->{_url},
Xavier Guimard's avatar
Xavier Guimard committed
56
            CHOICE_PARAM    => $self->conf->{authChoiceParam},
Xavier Guimard's avatar
Xavier Guimard committed
57
            CHOICE_VALUE    => $req->data->{_authChoice},
Clément OUDOT's avatar
Clément OUDOT committed
58 59
            (
                $req->data->{customScript}
Xavier Guimard's avatar
Xavier Guimard committed
60
                ? ( CUSTOM_SCRIPT => $req->data->{customScript} )
61 62
                : ()
            ),
Xavier Guimard's avatar
Xavier Guimard committed
63 64 65
        );
    }

Clément OUDOT's avatar
Clément OUDOT committed
66
    # 1.2a An authentication (or userDB) module needs to ask a question
Xavier Guimard's avatar
Xavier Guimard committed
67 68
    #     before processing to the request
    elsif ( $req->{error} == PE_CONFIRM ) {
Xavier Guimard's avatar
Xavier Guimard committed
69
        $self->logger->debug('Display: confirm detected');
Xavier Guimard's avatar
Xavier Guimard committed
70 71
        $skinfile       = 'confirm';
        %templateParams = (
72
            MAIN_LOGO       => $self->conf->{portalMainLogo},
73
            LANGS           => $self->conf->{showLanguages},
Xavier Guimard's avatar
Xavier Guimard committed
74 75
            AUTH_ERROR      => $req->error,
            AUTH_ERROR_TYPE => $req->error_type,
Xavier Guimard's avatar
Xavier Guimard committed
76
            AUTH_URL        => $req->{data}->{_url},
Xavier Guimard's avatar
Xavier Guimard committed
77
            MSG             => $req->info,
Xavier Guimard's avatar
Xavier Guimard committed
78
            HIDDEN_INPUTS   => $self->buildHiddenForm($req),
Xavier Guimard's avatar
Xavier Guimard committed
79
            ACTIVE_TIMER    => $req->data->{activeTimer},
Xavier Guimard's avatar
Xavier Guimard committed
80 81
            FORM_METHOD     => $self->conf->{confirmFormMethod},
            CHOICE_PARAM    => $self->conf->{authChoiceParam},
Xavier Guimard's avatar
Xavier Guimard committed
82
            CHOICE_VALUE    => $req->data->{_authChoice},
Xavier Guimard's avatar
Xavier Guimard committed
83
            CHECK_LOGINS    => $self->conf->{portalCheckLogins}
Clément OUDOT's avatar
Clément OUDOT committed
84
              && $req->data->{login},
Xavier Guimard's avatar
Xavier Guimard committed
85
            ASK_LOGINS => $req->param('checkLogins') || 0,
Xavier Guimard's avatar
Xavier Guimard committed
86
            CONFIRMKEY => $self->stamp(),
Xavier Guimard's avatar
Xavier Guimard committed
87
            REMEMBER   => $req->data->{confirmRemember},
Clément OUDOT's avatar
Clément OUDOT committed
88 89
            (
                $req->data->{customScript}
Xavier Guimard's avatar
Xavier Guimard committed
90
                ? ( CUSTOM_SCRIPT => $req->data->{customScript} )
Clément OUDOT's avatar
Clément OUDOT committed
91 92 93 94 95 96 97 98 99 100 101
                : ()
            ),
        );
    }

    # 1.2b An authentication (or userDB) module needs to ask a question
    #     before processing to the request
    elsif ( $req->{error} == PE_IDPCHOICE ) {
        $self->logger->debug('Display: IDP choice detected');
        $skinfile       = 'idpchoice';
        %templateParams = (
102
            MAIN_LOGO       => $self->conf->{portalMainLogo},
103
            LANGS           => $self->conf->{showLanguages},
Clément OUDOT's avatar
Clément OUDOT committed
104 105
            AUTH_ERROR      => $req->error,
            AUTH_ERROR_TYPE => $req->error_type,
Xavier Guimard's avatar
Xavier Guimard committed
106
            AUTH_URL        => $req->{data}->{_url},
Clément OUDOT's avatar
Clément OUDOT committed
107
            HIDDEN_INPUTS   => $self->buildHiddenForm($req),
Xavier Guimard's avatar
Xavier Guimard committed
108
            ACTIVE_TIMER    => $req->data->{activeTimer},
Clément OUDOT's avatar
Clément OUDOT committed
109 110
            FORM_METHOD     => $self->conf->{confirmFormMethod},
            CHOICE_PARAM    => $self->conf->{authChoiceParam},
Xavier Guimard's avatar
Xavier Guimard committed
111
            CHOICE_VALUE    => $req->data->{_authChoice},
Clément OUDOT's avatar
Clément OUDOT committed
112
            CHECK_LOGINS    => $self->conf->{portalCheckLogins}
Clément OUDOT's avatar
Clément OUDOT committed
113
              && $req->data->{login},
Clément OUDOT's avatar
Clément OUDOT committed
114 115
            ASK_LOGINS => $req->param('checkLogins') || 0,
            CONFIRMKEY => $self->stamp(),
Xavier Guimard's avatar
Xavier Guimard committed
116
            LIST       => $req->data->{list} || [],
Xavier Guimard's avatar
Xavier Guimard committed
117
            REMEMBER   => $req->data->{confirmRemember},
Clément OUDOT's avatar
Clément OUDOT committed
118 119
            (
                $req->data->{customScript}
Xavier Guimard's avatar
Xavier Guimard committed
120
                ? ( CUSTOM_SCRIPT => $req->data->{customScript} )
121 122
                : ()
            ),
Xavier Guimard's avatar
Xavier Guimard committed
123 124 125
        );
    }

Xavier Guimard's avatar
Xavier Guimard committed
126 127
    # 1.3 There is a message to display
    elsif ( my $info = $req->info ) {
Xavier Guimard's avatar
Xavier Guimard committed
128
        $self->logger->debug('Display: info detected');
129 130
        $self->logger->debug(
            'Hidden values -> ' . Dumper( $req->{portalHiddenFormValues} ) );
Xavier Guimard's avatar
Xavier Guimard committed
131 132
        $skinfile       = 'info';
        %templateParams = (
133
            MAIN_LOGO       => $self->conf->{portalMainLogo},
134
            LANGS           => $self->conf->{showLanguages},
Xavier Guimard's avatar
Xavier Guimard committed
135 136 137 138
            AUTH_ERROR      => $self->error,
            AUTH_ERROR_TYPE => $req->error_type,
            MSG             => $info,
            URL             => $req->{urldc},
Xavier Guimard's avatar
Xavier Guimard committed
139
            HIDDEN_INPUTS   => $self->buildHiddenForm($req),
Xavier Guimard's avatar
Xavier Guimard committed
140
            ACTIVE_TIMER    => $req->data->{activeTimer},
Xavier Guimard's avatar
Xavier Guimard committed
141 142
            FORM_METHOD     => $self->conf->{infoFormMethod},
            CHOICE_PARAM    => $self->conf->{authChoiceParam},
Xavier Guimard's avatar
Xavier Guimard committed
143
            CHOICE_VALUE    => $req->data->{_authChoice},
Clément OUDOT's avatar
Clément OUDOT committed
144 145
            (
                $req->data->{customScript}
Xavier Guimard's avatar
Xavier Guimard committed
146
                ? ( CUSTOM_SCRIPT => $req->data->{customScript} )
147 148
                : ()
            ),
Xavier Guimard's avatar
Xavier Guimard committed
149 150 151
        );
    }

Xavier Guimard's avatar
Xavier Guimard committed
152
    # 1.4 OpenID menu page
Xavier Guimard's avatar
Xavier Guimard committed
153 154 155 156
    elsif ($req->{error} == PE_OPENID_EMPTY
        or $req->{error} == PE_OPENID_BADID )
    {
        $skinfile = 'openid';
Xavier Guimard's avatar
Xavier Guimard committed
157 158
        my $p = $self->conf->{portal} . $self->conf->{issuerDBOpenIDPath};
        $p =~ s#(?<!:)/?\^?/#/#g;
Xavier Guimard's avatar
Xavier Guimard committed
159
        my $id = $req->{sessionInfo}
Clément OUDOT's avatar
Clément OUDOT committed
160
          ->{ $self->conf->{openIdAttr} || $self->conf->{whatToTrace} };
Xavier Guimard's avatar
Xavier Guimard committed
161
        %templateParams = (
162
            MAIN_LOGO       => $self->conf->{portalMainLogo},
163
            LANGS           => $self->conf->{showLanguages},
Xavier Guimard's avatar
Xavier Guimard committed
164 165 166
            AUTH_ERROR      => $self->error,
            AUTH_ERROR_TYPE => $req->error_type,
            PROVIDERURI     => $p,
Xavier Guimard's avatar
Xavier Guimard committed
167
            MSG             => $req->info(),
Clément OUDOT's avatar
Clément OUDOT committed
168 169
            (
                $req->data->{customScript}
Xavier Guimard's avatar
Xavier Guimard committed
170
                ? ( CUSTOM_SCRIPT => $req->data->{customScript} )
171 172
                : ()
            ),
Xavier Guimard's avatar
Xavier Guimard committed
173
        );
Xavier Guimard's avatar
Xavier Guimard committed
174
        $templateParams{ID} = $req->data->{_openidPortal} . $id if ($id);
Xavier Guimard's avatar
Xavier Guimard committed
175 176
    }

Xavier Guimard's avatar
Xavier Guimard committed
177 178 179 180 181 182 183 184
    # 2. Good authentication

    # 2.1 Redirection
    elsif ( $req->{error} == PE_REDIRECT ) {
        $skinfile       = "redirect";
        %templateParams = (
            URL           => $req->{urldc},
            HIDDEN_INPUTS => $self->buildHiddenForm($req),
Xavier Guimard's avatar
Xavier Guimard committed
185
            FORM_METHOD   => $req->data->{redirectFormMethod} || 'get',
Clément OUDOT's avatar
Clément OUDOT committed
186 187
            (
                $req->data->{customScript}
Xavier Guimard's avatar
Xavier Guimard committed
188
                ? ( CUSTOM_SCRIPT => $req->data->{customScript} )
189 190
                : ()
            ),
Xavier Guimard's avatar
Xavier Guimard committed
191 192 193
        );
    }

194
    # 2.2 Case : display menu (with error or not)
195
    elsif ( $req->error == PE_OK ) {
Xavier Guimard's avatar
Xavier Guimard committed
196 197 198 199
        $skinfile = 'menu';

        #utf8::decode($auth_user);
        %templateParams = (
200
            MAIN_LOGO => $self->conf->{portalMainLogo},
201
            LANGS     => $self->conf->{showLanguages},
Clément OUDOT's avatar
Clément OUDOT committed
202 203
            AUTH_USER => $req->{sessionInfo}->{ $self->conf->{portalUserAttr} },
            NEWWINDOW => $self->conf->{portalOpenLinkInNewWindow},
204
            LOGOUT_URL          => $self->conf->{portal} . "?logout=1",
205
            APPSLIST_ORDER      => $req->{sessionInfo}->{'_appsListOrder'},
206 207 208
            PING                => $self->conf->{portalPingInterval},
            REQUIRE_OLDPASSWORD => $self->conf->{portalRequireOldPassword},
            HIDE_OLDPASSWORD    => 0,
Xavier Guimard's avatar
Xavier Guimard committed
209
            $self->menu->params($req),
Clément OUDOT's avatar
Clément OUDOT committed
210 211
            (
                $req->data->{customScript}
Xavier Guimard's avatar
Xavier Guimard committed
212
                ? ( CUSTOM_SCRIPT => $req->data->{customScript} )
213 214
                : ()
            ),
Xavier Guimard's avatar
Xavier Guimard committed
215 216 217
        );
    }

218
    elsif ( $req->error == PE_RENEWSESSION ) {
219
        $skinfile       = 'upgradesession';
220
        %templateParams = (
221
            MAIN_LOGO  => $self->conf->{portalMainLogo},
222
            LANGS      => $self->conf->{showLanguages},
223 224 225
            MSG        => 'askToRenew',
            CONFIRMKEY => $self->stamp,
            PORTAL     => $self->conf->{portal},
Xavier Guimard's avatar
Xavier Guimard committed
226
            URL        => $req->data->{_url},
Clément OUDOT's avatar
Clément OUDOT committed
227 228
            (
                $req->data->{customScript}
Xavier Guimard's avatar
Xavier Guimard committed
229
                ? ( CUSTOM_SCRIPT => $req->data->{customScript} )
230 231
                : ()
            ),
232 233 234
        );
    }

235 236 237
    elsif ( $req->error == PE_MUSTAUTHN ) {
        $skinfile       = 'updatesession';
        %templateParams = (
238
            MAIN_LOGO  => $self->conf->{portalMainLogo},
239
            LANGS      => $self->conf->{showLanguages},
240 241 242 243
            MSG        => 'PE87',
            CONFIRMKEY => $self->stamp,
            PORTAL     => $self->conf->{portal},
            URL        => $req->data->{_url},
Clément OUDOT's avatar
Clément OUDOT committed
244 245
            (
                $req->data->{customScript}
246 247 248 249 250 251
                ? ( CUSTOM_SCRIPT => $req->data->{customScript} )
                : ()
            ),
        );
    }

252
    # 2.3 Case : user authenticated but an error was returned (bad url,...)
253 254
    elsif (
        $req->noLoginDisplay
Xavier Guimard's avatar
Xavier Guimard committed
255
        or (    not $req->data->{noerror}
256 257
            and $req->userData
            and %{ $req->userData } )
Clément OUDOT's avatar
Clément OUDOT committed
258
      )
259
    {
260 261
        $skinfile       = 'error';
        %templateParams = (
262
            MAIN_LOGO       => $self->conf->{portalMainLogo},
263
            LANGS           => $self->conf->{showLanguages},
264 265
            AUTH_ERROR      => $req->error,
            AUTH_ERROR_TYPE => $req->error_type,
Clément OUDOT's avatar
Clément OUDOT committed
266 267
            (
                $req->data->{customScript}
Xavier Guimard's avatar
Xavier Guimard committed
268
                ? ( CUSTOM_SCRIPT => $req->data->{customScript} )
269 270
                : ()
            ),
271 272 273
        );
    }

274
    # 3 Authentication has been refused OR first access
Xavier Guimard's avatar
Xavier Guimard committed
275
    else {
276 277 278
        $skinfile = 'login';
        my $login = $self->userId($req);
        $login = '' if ( $login eq 'anonymous' );
Xavier Guimard's avatar
Xavier Guimard committed
279
        %templateParams = (
Clément OUDOT's avatar
Clément OUDOT committed
280
            MAIN_LOGO             => $self->conf->{portalMainLogo},
281
            LANGS                 => $self->conf->{showLanguages},
Clément OUDOT's avatar
Clément OUDOT committed
282 283 284 285 286 287 288 289
            AUTH_ERROR            => $req->error,
            AUTH_ERROR_TYPE       => $req->error_type,
            AUTH_URL              => $req->{data}->{_url},
            LOGIN                 => $login,
            CHECK_LOGINS          => $self->conf->{portalCheckLogins},
            ASK_LOGINS            => $req->param('checkLogins') || 0,
            DISPLAY_RESETPASSWORD => $self->conf->{portalDisplayResetPassword},
            DISPLAY_REGISTER      => $self->conf->{portalDisplayRegister},
290
            MAIL_URL              => $self->conf->{mailUrl},
Clément OUDOT's avatar
Clément OUDOT committed
291 292 293 294 295
            REGISTER_URL          => $self->conf->{registerUrl},
            HIDDEN_INPUTS         => $self->buildHiddenForm($req),
            STAYCONNECTED         => $self->conf->{stayConnected},
            (
                $req->data->{customScript}
Xavier Guimard's avatar
Xavier Guimard committed
296
                ? ( CUSTOM_SCRIPT => $req->data->{customScript} )
297 298
                : ()
            ),
Xavier Guimard's avatar
Xavier Guimard committed
299 300 301
        );

        # Display captcha if it's enabled
Xavier Guimard's avatar
Xavier Guimard committed
302
        if ( $req->captcha ) {
Xavier Guimard's avatar
Xavier Guimard committed
303 304
            %templateParams = (
                %templateParams,
Xavier Guimard's avatar
Xavier Guimard committed
305
                CAPTCHA_SRC  => $req->captcha,
306
                CAPTCHA_SIZE => $self->{conf}->{captcha_size} || 6
Xavier Guimard's avatar
Xavier Guimard committed
307 308 309
            );
        }
        if ( $req->token ) {
310
            %templateParams = ( %templateParams, TOKEN => $req->token, );
Xavier Guimard's avatar
Xavier Guimard committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324
        }

        # Show password form if password policy error
        if (

               $req->{error} == PE_PP_CHANGE_AFTER_RESET
            or $req->{error} == PE_PP_MUST_SUPPLY_OLD_PASSWORD
            or $req->{error} == PE_PP_INSUFFICIENT_PASSWORD_QUALITY
            or $req->{error} == PE_PP_PASSWORD_TOO_SHORT
            or $req->{error} == PE_PP_PASSWORD_TOO_YOUNG
            or $req->{error} == PE_PP_PASSWORD_IN_HISTORY
            or $req->{error} == PE_PASSWORD_MISMATCH
            or $req->{error} == PE_BADOLDPASSWORD
            or $req->{error} == PE_PASSWORDFORMEMPTY
325 326
            or (    $req->{error} == PE_PP_PASSWORD_EXPIRED
                and $self->conf->{ldapAllowResetExpiredPassword} )
Clément OUDOT's avatar
Clément OUDOT committed
327
          )
Xavier Guimard's avatar
Xavier Guimard committed
328 329 330 331
        {
            %templateParams = (
                %templateParams,
                REQUIRE_OLDPASSWORD =>
Clément OUDOT's avatar
Clément OUDOT committed
332
                  1,    # Old password is required to check user credentials
Xavier Guimard's avatar
Xavier Guimard committed
333 334 335 336 337 338 339
                DISPLAY_FORM          => 0,
                DISPLAY_OPENID_FORM   => 0,
                DISPLAY_YUBIKEY_FORM  => 0,
                DISPLAY_PASSWORD      => 1,
                DISPLAY_RESETPASSWORD => 0,
                AUTH_LOOP             => [],
                CHOICE_PARAM          => $self->conf->{authChoiceParam},
Xavier Guimard's avatar
Xavier Guimard committed
340
                CHOICE_VALUE          => $req->data->{_authChoice},
341
                OLDPASSWORD           => $self->checkXSSAttack( 'oldpassword',
Xavier Guimard's avatar
Xavier Guimard committed
342
                    $req->data->{oldpassword} )
Xavier Guimard's avatar
Xavier Guimard committed
343
                ? ""
Xavier Guimard's avatar
Xavier Guimard committed
344
                : $req->data->{oldpassword},
Xavier Guimard's avatar
Xavier Guimard committed
345 346 347 348 349 350 351 352
                HIDE_OLDPASSWORD => $self->conf->{hideOldPassword},
            );
        }

        # Disable all forms on:
        # * Logout message
        # * Bad URL error
        elsif ($req->{error} == PE_LOGOUT_OK
353
            or $req->{error} == PE_WAIT
Xavier Guimard's avatar
Xavier Guimard committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367
            or $req->{error} == PE_BADURL )
        {
            %templateParams = (
                %templateParams,
                DISPLAY_RESETPASSWORD => 0,
                DISPLAY_FORM          => 0,
                DISPLAY_OPENID_FORM   => 0,
                DISPLAY_YUBIKEY_FORM  => 0,
                AUTH_LOOP             => [],
                MSG                   => $req->info(),
            );

        }

368
        # Display authentication form
Xavier Guimard's avatar
Xavier Guimard committed
369 370 371
        else {

            # Authentication loop
Xavier Guimard's avatar
Xavier Guimard committed
372
            if ( $self->conf->{authentication} eq 'Choice'
373 374
                and my $authLoop = $self->_buildAuthLoop($req) )
            {
Xavier Guimard's avatar
Xavier Guimard committed
375 376
                %templateParams = (
                    %templateParams,
Xavier Guimard's avatar
Xavier Guimard committed
377
                    AUTH_LOOP            => $authLoop,
Xavier Guimard's avatar
Xavier Guimard committed
378
                    CHOICE_PARAM         => $self->conf->{authChoiceParam},
Xavier Guimard's avatar
Xavier Guimard committed
379
                    CHOICE_VALUE         => $req->data->{_authChoice},
Xavier Guimard's avatar
Xavier Guimard committed
380 381 382 383 384 385 386 387 388
                    DISPLAY_FORM         => 0,
                    DISPLAY_OPENID_FORM  => 0,
                    DISPLAY_YUBIKEY_FORM => 0,
                );
            }

            # Choose what form to display if not in a loop
            else {

Clément OUDOT's avatar
Clément OUDOT committed
389 390
                my $displayType =
                  eval { $self->_authentication->getDisplayType($req) };
Xavier Guimard's avatar
Xavier Guimard committed
391

392
                $self->logger->debug("Display type $displayType ");
Xavier Guimard's avatar
Xavier Guimard committed
393 394 395

                %templateParams = (
                    %templateParams,
396 397
                    DISPLAY_FORM => $displayType =~ /\bstandardform\b/ ? 1
                    : 0,
Clément OUDOT's avatar
Clément OUDOT committed
398
                    DISPLAY_OPENID_FORM => $displayType =~ /\bopenidform\b/ ? 1
399 400 401
                    : 0,
                    DISPLAY_YUBIKEY_FORM => $displayType =~ /\byubikeyform\b/
                    ? 1
Xavier Guimard's avatar
Xavier Guimard committed
402
                    : 0,
403
                    DISPLAY_SSL_FORM  => $displayType =~ /sslform/ ? 1 : 0,
Xavier Guimard's avatar
Xavier Guimard committed
404
                    DISPLAY_GPG_FORM  => $displayType =~ /gpgform/ ? 1 : 0,
405 406
                    DISPLAY_LOGO_FORM => $displayType eq "logo"    ? 1 : 0,
                    module            => $displayType eq "logo"
Xavier Guimard's avatar
Xavier Guimard committed
407
                    ? $self->getModule( $req, 'auth' )
Xavier Guimard's avatar
Xavier Guimard committed
408
                    : "",
Clément OUDOT's avatar
Clément OUDOT committed
409 410 411
                    AUTH_LOOP => [],
                    PORTAL_URL =>
                      ( $displayType eq "logo" ? $self->conf->{portal} : 0 ),
Xavier Guimard's avatar
Xavier Guimard committed
412 413 414 415 416 417 418 419 420
                    MSG => $req->info(),
                );

            }

        }

    }

Xavier Guimard's avatar
Xavier Guimard committed
421
    # Additional $req param
422
    %templateParams = ( %templateParams, %{ $req->{customParameters} // {} }, );
Xavier Guimard's avatar
Xavier Guimard committed
423

424 425 426 427 428
    for my $session_key ( keys %{ $req->{sessionInfo} } ) {
        $templateParams{ "session_" . $session_key } =
          $req->{sessionInfo}->{$session_key};
    }

429
    $self->logger->debug("Skin returned: $skinfile");
Xavier Guimard's avatar
Xavier Guimard committed
430
    return ( $skinfile, \%templateParams );
Xavier Guimard's avatar
Xavier Guimard committed
431 432 433 434 435 436 437 438
}

##@method public void printImage(string file, string type)
# Print image to STDOUT
# @param $file The path to the file to print
# @param $type The content-type to use (ie: image/png)
# @return void
sub staticFile {
Xavier Guimard's avatar
Xavier Guimard committed
439
    my ( $self, $req, $file, $type ) = @_;
Xavier Guimard's avatar
Xavier Guimard committed
440 441 442
    require Plack::Util;
    require Cwd;
    require HTTP::Date;
Clément OUDOT's avatar
Clément OUDOT committed
443 444
    open my $fh, '<:raw', $self->conf->{templatesDir} . "/$file"
      or return $self->sendError( $req,
445
        $self->conf->{templatesDir} . "/$file: $!", 403 );
Xavier Guimard's avatar
Xavier Guimard committed
446 447 448 449
    my @stat = stat $file;
    Plack::Util::set_io_path( $fh, Cwd::realpath($file) );
    return [
        200,
Clément OUDOT's avatar
Clément OUDOT committed
450 451
        [
            'Content-Type'   => $type,
Xavier Guimard's avatar
Xavier Guimard committed
452 453 454 455 456 457 458 459
            'Content-Length' => $stat[7],
            'Last-Modified'  => HTTP::Date::time2str( $stat[9] )
        ],
        $fh,
    ];
}

sub buildHiddenForm {
Xavier Guimard's avatar
Xavier Guimard committed
460 461 462
    my ( $self, $req ) = @_;
    my @keys = keys %{ $req->{portalHiddenFormValues} };
    my $val  = '';
Xavier Guimard's avatar
Xavier Guimard committed
463 464 465 466 467

    foreach (@keys) {

        # Check XSS attacks
        next
Clément OUDOT's avatar
Clément OUDOT committed
468
          if $self->checkXSSAttack( $_, $req->{portalHiddenFormValues}->{$_} );
Xavier Guimard's avatar
Xavier Guimard committed
469 470 471

        # Build hidden input HTML code
        $val .= qq{<input type="hidden" name="$_" id="$_" value="}
Clément OUDOT's avatar
Clément OUDOT committed
472
          . $req->{portalHiddenFormValues}->{$_} . '" />';
Xavier Guimard's avatar
Xavier Guimard committed
473 474 475 476 477 478 479
    }

    return $val;
}

# Return skin name
# @return skin name
Xavier Guimard's avatar
Xavier Guimard committed
480
# TODO: create property for skinRule
Xavier Guimard's avatar
Xavier Guimard committed
481 482 483 484 485 486 487
sub getSkin {
    my ( $self, $req ) = @_;

    my $skin = $self->conf->{portalSkin};

    # Fill sessionInfo to eval rule if empty (unauthenticated user)
    $req->{sessionInfo}->{_url}   ||= $req->{urldc};
Xavier Guimard's avatar
Xavier Guimard committed
488
    $req->{sessionInfo}->{ipAddr} ||= $req->address;
Xavier Guimard's avatar
Xavier Guimard committed
489 490

    # Load specific skin from skinRules
491
    foreach my $rule ( @{ $self->{skinRules} } ) {
492
        if ( $rule->[1]->( $req, $req->sessionInfo ) ) {
493 494 495 496 497
            if ( -d $self->conf->{templateDir} . '/' . $rule->[0] ) {
                $skin = $rule->[0];
                $self->logger->debug("Skin $skin selected from skin rule");
                last;
            }
Xavier Guimard's avatar
Xavier Guimard committed
498 499 500 501 502
        }
    }

    # Check skin GET/POST parameter
    my $skinParam = $req->param('skin');
503
    if ( defined $skinParam ) {
Xavier Guimard's avatar
Xavier Guimard committed
504
        if ( $skinParam =~ /^[\w\-]+$/ ) {
505 506 507 508 509 510 511 512 513
            if ( -d $self->conf->{templateDir} . '/' . $skinParam ) {
                $skin = $skinParam;
                $self->logger->debug(
                    "Skin $skin selected from GET/POST parameter");
            }
            else {
                $self->userLogger->error(
                    "User tries to access to unexistent skin dir $skinParam");
            }
514 515
        }
        else {
516
            $self->userLogger->error("Strange skin parameter: $skinParam");
517
        }
Xavier Guimard's avatar
Xavier Guimard committed
518 519 520 521 522
    }

    return $skin;
}

Xavier Guimard's avatar
Xavier Guimard committed
523
# Build an HTML array to display sessions
Xavier Guimard's avatar
Xavier Guimard committed
524
# @param $sessions Array ref of hash ref containing sessions data
Xavier Guimard's avatar
Xavier Guimard committed
525 526 527 528 529 530 531 532 533
# @param $title Title of the array
# @param $displayUser To display "User" column
# @param $displaError To display "Error" column
# @return HTML string
sub mkSessionArray {
    my ( $self, $sessions, $title, $displayUser, $displayError ) = @_;

    return "" unless ( ref $sessions eq "ARRAY" and @$sessions );

534 535 536 537 538 539 540 541 542
    my @fields = sort keys %{ $self->conf->{sessionDataToRemember} };
    return $self->loadTemplate(
        'sessionArray',
        params => {
            title        => $title,
            displayUser  => $displayUser,
            displayError => $displayError,
            fields       => [
                map { { name => $self->conf->{sessionDataToRemember}->{$_} } }
Clément OUDOT's avatar
Clément OUDOT committed
543
                  @fields
544 545 546 547
            ],
            sessions => [
                map {
                    my $session = $_;
Clément OUDOT's avatar
Clément OUDOT committed
548 549
                    {
                        user   => $session->{user},
550 551 552 553
                        utime  => $session->{_utime},
                        ip     => $session->{ipAddr},
                        values => [ map { { v => $session->{$_} } } @fields ],
                        error  => $session->{error},
554
                        displayError => $displayError,
555 556 557 558 559
                    }
                } @$sessions
            ],
        }
    );
Xavier Guimard's avatar
Xavier Guimard committed
560 561
}

Xavier Guimard's avatar
Xavier Guimard committed
562 563
sub mkOidcConsent {
    my ( $self, $session ) = @_;
564

565 566 567
    if ( defined( $self->conf->{oidcRPMetaDataOptions} )
        and ref( $self->conf->{oidcRPMetaDataOptions} ) )
    {
568

569
        # Set default RP displayname
570
        foreach my $oidc ( keys %{ $self->conf->{oidcRPMetaDataOptions} } ) {
571
            $self->conf->{oidcRPMetaDataOptions}->{$oidc}
Clément OUDOT's avatar
Clément OUDOT committed
572
              ->{oidcRPMetaDataOptionsDisplayName} ||= $oidc;
573 574 575
        }
    }

576
    # Loading existing oidcConsents
577 578
    $self->logger->debug("Searching OIDC Consents...");
    my $_oidcConsents;
579
    if ( exists $session->{_oidcConsents} ) {
580
        $_oidcConsents = eval {
581 582 583 584 585
            from_json( $session->{_oidcConsents}, { allow_nonref => 1 } );
        };
        if ($@) {
            $self->logger->error("Corrupted session (_oidcConsents): $@");
            return PE_ERROR;
586 587
        }
    }
588 589 590 591
    else {
        $self->logger->debug("No OIDC Consent found");
    }
    my $consents = {};
592
    foreach (@$_oidcConsents) {
593 594 595 596 597
        if ( defined $_->{rp} ) {
            my $rp = $_->{rp};
            $self->logger->debug("RP { $rp } Consent found");
            $consents->{$rp}->{epoch} = $_->{epoch};
            $consents->{$rp}->{scope} = $_->{scope};
Clément OUDOT's avatar
Clément OUDOT committed
598 599 600
            $consents->{$rp}->{displayName} =
              $self->conf->{oidcRPMetaDataOptions}->{$rp}
              ->{oidcRPMetaDataOptionsDisplayName};
601
        }
602 603
    }

604
    # Display existing oidcConsents
605 606 607 608
    return $self->loadTemplate(
        'oidcConsents',
        params => {
            partners => [
609
                map {
Clément OUDOT's avatar
Clément OUDOT committed
610 611
                    {
                        name        => $_,
612
                        epoch       => $consents->{$_}->{epoch},
613 614 615 616
                        scope       => $consents->{$_}->{scope},
                        displayName => $consents->{$_}->{displayName}
                    }
                } ( sort keys %$consents )
617
            ],
618
            consents => join( ",", keys %$consents ),
619 620
        }
    );
Xavier Guimard's avatar
Xavier Guimard committed
621 622
}

Xavier Guimard's avatar
Xavier Guimard committed
623
1;