BEGIN {
require
't/test-lib.pm'
;
require
't/saml-lib.pm'
;
}
my
$maintests
= 15;
my
$debug
=
'error'
;
my
(
$issuer
,
$sp
,
$res
);
LWP::Protocol::PSGI->register(
sub
{
my
$req
= Plack::Request->new(
@_
);
fail(
'POST should not launch SOAP requests'
);
count(1);
return
[ 500, [], [] ];
}
);
SKIP: {
eval
"use Lasso"
;
if
($@) {
skip
'Lasso not found'
,
$maintests
;
}
$issuer
= register(
'issuer'
, \
&issuer
);
$sp
= register(
'sp'
, \
&sp
);
ok(
$res
=
$issuer
->_post(
'/'
, IO::String->new(
'user=russian&password=russian'
),
length
=> 29
),
'Auth query'
);
expectOK(
$res
);
my
$idpId
= expectCookie(
$res
);
ok(
$res
=
$issuer
->_get(
'/saml/singleSignOn'
,
query
=>
'IDPInitiated=1&spConfKey=sp.com'
,
cookie
=>
"lemonldap=$idpId"
,
accept
=>
'test/html'
),
'Query IdP to access to SP'
);
expectOK(
$res
);
ok(
$res
->[2]->[0] =~
m
'Form method is POST'
);
my
$url
= $1;
ok(
$res
->[2]->[0] =~
/<input type=
"hidden"
.+?name=
"SAMLResponse"
.+?value=
"(.+?)"
/s,
'Found SAML response'
);
my
$s
=
"SAMLResponse=$1"
;
ok(
$res
=
$sp
->_post(
$url
, IO::String->new(
$s
),
accept
=>
'text/html'
,
length
=>
length
(
$s
),
),
'Post SAML response to SP'
);
my
$spId
= expectCookie(
$res
);
ok(
$res
=
$sp
->_get(
'/'
,
cookie
=>
"lemonldap=$spId"
),
'Get / on SP'
);
expectOK(
$res
);
expectAuthenticatedAs(
$res
,
'ru@badwolf.org@idp'
);
ok( getSession(
$spId
)->data->{cn} eq
'Русский'
,
'UTF-8 values'
)
or explain(
$res
,
'cn => Frédéric Accents'
);
ok(
$res
=
$issuer
->_get(
'/'
,
query
=>
'logout'
,
cookie
=>
"lemonldap=$idpId"
,
accept
=>
'text/html'
),
'Query IdP for logout'
);
expectOK(
$res
);
ok(
$res
->[2]->[0] =~
m
'Get iframe request'
);
$url
= $1;
my
$query
= $2;
expectCspChildOK(
$res
,
"auth.sp.com"
);
my
$removedCookie
= expectCookie(
$res
);
is(
$removedCookie
, 0,
"SSO cookie removed"
);
ok(
$res
=
$sp
->_get(
$url
,
query
=>
$query
,
accept
=>
'text/html'
),
'Query SP for iframe'
);
(
$url
,
$query
) = expectRedirection(
$res
,
ok(
$res
=
$issuer
->_get(
$url
,
query
=>
$query
,
accept
=>
'text/html'
),
'Push SAML response to IdP'
);
ok(
!
defined
getHeader(
$res
,
'Content-Security-Policy'
)
|| getHeader(
$res
,
'Content-Security-Policy'
) !~ /frame-ancestors/,
' Frame can be embedded'
)
or explain(
$res
->[1],
'Content-Security-Policy does not contain a frame-ancestors'
);
ok(
$res
=
$issuer
->_get(
'/'
,
cookie
=>
"lemonldap=$idpId"
,
),
'Test if user is reject on IdP'
);
expectReject(
$res
);
ok(
$res
=
$sp
->_get(
'/'
,
accept
=>
'text/html'
,
cookie
=>
"lemonldap=$spId"
),
'Test if user is reject on SP'
);
expectRedirection(
$res
,
}
count(
$maintests
);
clean_sessions();
done_testing( count() );
sub
issuer {
return
LLNG::Manager::Test->new( {
ini
=> {
logLevel
=>
$debug
,
domain
=>
'idp.com'
,
authentication
=>
'Demo'
,
userDB
=>
'Same'
,
issuerDBSAMLActivation
=> 1,
samlSPMetaDataOptions
=> {
'sp.com'
=> {
samlSPMetaDataOptionsEncryptionMode
=>
'none'
,
samlSPMetaDataOptionsEnableIDPInitiatedURL
=> 1,
samlSPMetaDataOptionsSignSSOMessage
=> 1,
samlSPMetaDataOptionsSignSLOMessage
=> 1,
samlSPMetaDataOptionsCheckSSOMessageSignature
=> 1,
samlSPMetaDataOptionsCheckSLOMessageSignature
=> 1,
}
},
samlSPMetaDataExportedAttributes
=> {
'sp.com'
=> {
cn
=>
'1;cn;urn:oasis:names:tc:SAML:2.0:attrname-format:basic'
,
uid
=>
'1;uid;urn:oasis:names:tc:SAML:2.0:attrname-format:basic'
,
}
},
samlOrganizationDisplayName
=>
"IDP"
,
samlOrganizationName
=>
"IDP"
,
samlServicePrivateKeyEnc
=> saml_key_idp_private_enc,
samlServicePrivateKeySig
=> saml_key_idp_private_sig,
samlServicePublicKeyEnc
=> saml_key_idp_public_enc,
samlServicePublicKeySig
=> saml_key_idp_public_sig,
samlSPMetaDataXML
=> {
"sp.com"
=> {
samlSPMetaDataXML
=>
samlSPMetaDataXML(
'sp'
,
'HTTP-Redirect'
)
},
},
}
}
);
}
sub
sp {
return
LLNG::Manager::Test->new( {
ini
=> {
logLevel
=>
$debug
,
domain
=>
'sp.com'
,
authentication
=>
'SAML'
,
userDB
=>
'Same'
,
issuerDBSAMLActivation
=> 0,
restSessionServer
=> 1,
samlIDPMetaDataExportedAttributes
=> {
idp
=> {
mail
=>
"0;mail;;"
,
uid
=>
"1;uid"
,
cn
=>
"0;cn"
}
},
samlIDPMetaDataOptions
=> {
idp
=> {
samlIDPMetaDataOptionsEncryptionMode
=>
'none'
,
samlIDPMetaDataOptionsSSOBinding
=>
'redirect'
,
samlIDPMetaDataOptionsSLOBinding
=>
'redirect'
,
samlIDPMetaDataOptionsSignSSOMessage
=> 1,
samlIDPMetaDataOptionsSignSLOMessage
=> 1,
samlIDPMetaDataOptionsCheckSSOMessageSignature
=> 1,
samlIDPMetaDataOptionsCheckSLOMessageSignature
=> 1,
samlIDPMetaDataOptionsForceUTF8
=> 1,
samlIDPMetaDataOptionsAllowLoginFromIDP
=> 1,
}
},
samlIDPMetaDataExportedAttributes
=> {
idp
=> {
"uid"
=>
"0;uid;;"
,
"cn"
=>
"1;cn;;"
,
},
},
samlIDPMetaDataXML
=> {
idp
=> {
samlIDPMetaDataXML
=>
samlIDPMetaDataXML(
'idp'
,
'HTTP-Redirect'
)
}
},
samlOrganizationDisplayName
=>
"SP"
,
samlOrganizationName
=>
"SP"
,
samlServicePublicKeySig
=> saml_key_sp_public_sig,
samlServicePrivateKeyEnc
=> saml_key_sp_private_enc,
samlServicePrivateKeySig
=> saml_key_sp_private_sig,
samlServicePublicKeyEnc
=> saml_key_sp_public_enc,
samlSPSSODescriptorAuthnRequestsSigned
=> 1,
},
}
);
}