our
$VERSION
=
'2.16.3'
;
has
cdc_name
=> (
is
=>
'rw'
);
has
cdc_domain
=> (
is
=>
'rw'
);
has
httpOnly
=> (
is
=>
'rw'
);
has
cookieExpiration
=> (
is
=>
'rw'
);
has
oldStyleUrl
=> (
is
=>
'rw'
);
has
cdc_values
=> (
is
=>
'rw'
);
sub
init {
my
(
$self
,
$args
) =
@_
;
my
$tmp
= Lemonldap::NG::Common::Conf->new(
$args
->{configStorage} );
unless
(
$tmp
) {
$self
->error(
"Unable to access configuration: $Lemonldap::NG::Common::Conf::msg"
);
return
0;
}
my
$lconf
=
$tmp
->getLocalConf(
'portal'
) // {};
my
$conf
=
$tmp
->getConf();
unless
(
ref
(
$conf
) ) {
$self
->error(
"Unable to load configuration: $Lemonldap::NG::Common::Conf::msg"
);
return
0;
}
$lconf
->{
$_
} =
$args
->{
$_
}
foreach
(
keys
%$args
);
$conf
->{
$_
} =
$lconf
->{
$_
}
foreach
(
keys
%$lconf
);
$self
->SUPER::init(
$lconf
) or
return
0;
$self
->cdc_name(
$conf
->{samlCommonDomainCookieName} ||
'_saml_idp'
);
$self
->cdc_domain(
$conf
->{samlCommonDomainCookieDomain} );
$self
->logger->debug(
"[CDC] Cookie name: "
.
$self
->cdc_name );
$self
->logger->debug(
"[CDC] Domain name: "
. (
$self
->cdc_domain ?
$self
->cdc_domain :
'<host name>'
) );
foreach
(
qw(httpOnly cookieExpiration oldStyleUrl)
) {
$self
->
$_
(
$conf
->{
$_
} );
}
return
1;
}
sub
handler {
my
(
$self
,
$req
) =
@_
;
my
$cdc_idp
=
""
;
my
$cdc_cookie
=
""
;
my
$cdc_domain
=
$self
->cdc_domain ||
$req
->hostname;
my
$action
=
$req
->param(
'action'
) ||
""
;
my
$idp
=
$req
->param(
'idp'
);
$cdc_cookie
=
$req
->cookies->{
$self
->cdc_name };
if
(
$cdc_cookie
) {
$self
->logger->debug(
"[CDC] Cookie found with value $cdc_cookie"
);
}
if
(
$action
eq
'write'
) {
$self
->logger->debug(
"[CDC] Write request detected"
);
unless
(
$idp
) {
return
$self
->sendError(
$req
,
"[CDC] No IDP given"
, 400 );
}
$self
->logger->debug(
"[CDC] Will add IDP $idp to IDP list"
);
my
$encoded_idp
= encode_base64(
$idp
,
''
);
$cdc_cookie
=~ s/
$encoded_idp
(\s+)?//g
if
(
$cdc_cookie
);
$cdc_cookie
.= (
$cdc_cookie
?
" "
:
""
);
$cdc_cookie
.=
$encoded_idp
;
$self
->logger->debug(
"[CDC] Build cookie $self->{cdc_name} with value $cdc_cookie"
);
push
@{
$req
->respHeaders },
'Set-Cookie'
=>
$self
->cdc_name .
'='
.
$cdc_cookie
.
"; domain=$cdc_domain; secure=1"
;
}
elsif
(
$action
eq
'read'
) {
$self
->logger->debug(
"[CDC] Read request detected"
);
if
(
$cdc_cookie
) {
$cdc_idp
= decode_base64( (
split
/\s+/,
$cdc_cookie
)[-1] );
$self
->logger->debug(
"[CDC] Get value $cdc_idp"
);
}
else
{
$self
->logger->debug(
"[CDC] No cookie, set a default value"
);
$cdc_idp
=
'notfound'
;
}
}
if
(
my
$url
=
$req
->param(
'url'
) ) {
if
(
$url
=~ m
return
$self
->sendError(
$req
,
"Bad URL"
, 400 );
}
my
$urldc
= decode_base64(
$url
);
$urldc
.= (
$cdc_idp
? ( (
$urldc
=~ /\?/
? (
$self
->{oldStyleUrl} ?
'&'
:
';'
)
:
'?'
)
. build_urlencoded(
idp
=>
$cdc_idp
)
)
:
''
);
return
[
302, [
Location
=> URI->new(
$urldc
)->as_string,
$req
->spliceHdrs ],
[]
];
}
if
(
$cdc_cookie
) {
my
@cdc_values
=
map
( decode_base64(
$_
), (
split
( /\s+/,
$cdc_cookie
) ) );
$self
->{cdc_values} = \
@cdc_values
;
}
return
[
200,
[
'Content-Type'
=>
'text/plain'
,
'Content-Length'
=> 2,
$req
->spliceHdrs,
],
[
'OK'
]
];
}
1;