Add buildurl method (#2594)

This commit is contained in:
Yadd 2021-08-27 14:35:07 +02:00
parent a489e4a76b
commit 3ed77a01c2
3 changed files with 71 additions and 3 deletions

View File

@ -479,6 +479,7 @@ site/templates/common/oidc_checksession.tpl
site/templates/common/registerBrowser.tpl
site/templates/common/script.tpl
t/01-AuthDemo.t
t/01-BuildUrl.t
t/01-CSP-and-CORS-headers.t
t/01-EnablePasswordDisplay.t
t/01-Handler-redirection-and-URL-check-by-portal.t
@ -547,7 +548,6 @@ t/31-Auth-and-issuer-CAS-declared-app-userattr.t
t/31-Auth-and-issuer-CAS-declared-app.t
t/31-Auth-and-issuer-CAS-declared-apps.t
t/31-Auth-and-issuer-CAS-default.t
t/31-Auth-and-issuer-CAS-gateway.t
t/31-Auth-and-issuer-CAS-Logout-20.t
t/31-Auth-and-issuer-CAS-Logout-30.t
t/31-Auth-and-issuer-CAS-proxied.t
@ -566,6 +566,7 @@ t/32-Auth-and-issuer-OIDC-implicit-no-token.t
t/32-Auth-and-issuer-OIDC-implicit.t
t/32-Auth-and-issuer-OIDC-sorted.t
t/32-CAS-10.t
t/32-CAS-Gateway.t
t/32-CAS-Hooks.t
t/32-CAS-Macros.t
t/32-CAS-Prefix.t

View File

@ -583,7 +583,9 @@ sub updateSession {
$self->logger->debug("Update sessionInfo $_");
$self->_dump( $infos->{$_} );
$req->{sessionInfo}->{$_} = $infos->{$_};
if ( $self->HANDLER->data->{_session_id} && $id eq $self->HANDLER->data->{_session_id} ) {
if ( $self->HANDLER->data->{_session_id}
&& $id eq $self->HANDLER->data->{_session_id} )
{
$self->HANDLER->data->{$_} = $infos->{$_};
}
}
@ -1083,7 +1085,7 @@ sub registerLogin {
}
my $history = $req->sessionInfo->{_loginHistory} ||= {};
my $type = ( $req->authResult > 0 ? 'failed' : 'success' ) . 'Login';
my $type = ( $req->authResult > 0 ? 'failed' : 'success' ) . 'Login';
$history->{$type} ||= [];
$self->logger->debug("Current login saved into $type");
@ -1221,4 +1223,29 @@ sub cspGetHost {
$uri->scheme . "://" . ( $uri->_port ? $uri->host_port : $uri->host ) );
}
sub buildUrl {
my $self = shift;
return $self->portal unless @_;
# URL base is $self->portal unless first arg is an URL
my $uri =
URI->new( ( $_[0] =~ m#^https?://# ) ? shift(@_) : $self->portal );
my @pathSg = grep { $_ ne '' } $uri->path_segments;
while (@_) {
my $s = shift;
if ( ref $s ) {
$uri->query_form($s);
if (@_) {
require Carp;
Carp::confess('Query must be the last arg of buildUrl');
}
}
else {
push @pathSg, $s;
}
}
$uri->path_segments(@pathSg);
return $uri;
}
1;

View File

@ -0,0 +1,40 @@
use Test::More;
use strict;
require 't/test-lib.pm';
my $app = LLNG::Manager::Test->new( {
ini => {
logLevel => 'error',
}
}
)->p;
my @tests = (
[] => 'http://auth.example.com/',
['foo'] => 'http://auth.example.com/foo',
[ 'foo', 'bar' ] => 'http://auth.example.com/foo/bar',
[ { p => 1 } ] => 'http://auth.example.com/?p=1',
['https://foo'] => 'https://foo',
[ 'https://foo', 'bar' ] => 'https://foo/bar',
[ 'https://foo', 'bar', 'baz' ] => 'https://foo/bar/baz',
[ 'https://foo', { p => 1 } ] => 'https://foo?p=1',
[ 'https://foo', 'bar', { p => 1 } ] => 'https://foo/bar?p=1',
);
{
no warnings;
$Data::Dumper::Indent = 0;
$Data::Dumper::Terse = 1;
}
for ( my $i = 0 ; $i < @tests ; $i += 2 ) {
my @args = @{ $tests[$i] };
my $expected = $tests[ $i + 1 ];
ok( $app->buildUrl(@args) eq $expected,
Dumper( \@args ) . "\t=>\t" . $tests[ $i + 1 ] )
or explain( $app->buildUrl(@args) . '', $expected );
count(1);
}
done_testing( count() );