lemonldap-ng/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Redirect-IdP-initiated.t

267 lines
8.7 KiB
Perl
Raw Normal View History

2018-09-05 22:24:23 +02:00
use lib 'inc';
2016-12-14 13:09:55 +01:00
use Test::More;
use strict;
use IO::String;
use LWP::UserAgent;
2018-09-05 22:24:23 +02:00
use LWP::Protocol::PSGI;
2016-12-14 13:09:55 +01:00
use MIME::Base64;
BEGIN {
require 't/test-lib.pm';
2017-03-15 07:29:44 +01:00
require 't/saml-lib.pm';
2016-12-14 13:09:55 +01:00
}
2021-04-22 18:06:02 +02:00
my $maintests = 17;
2016-12-19 06:31:51 +01:00
my $debug = 'error';
2016-12-14 13:09:55 +01:00
my ( $issuer, $sp, $res );
# Redefine LWP methods for tests
LWP::Protocol::PSGI->register(
sub {
my $req = Plack::Request->new(@_);
fail('POST should not launch SOAP requests');
count(1);
return [ 500, [], [] ];
}
);
2016-12-14 13:09:55 +01:00
SKIP: {
eval "use Lasso";
if ($@) {
skip 'Lasso not found', $maintests;
}
# Initialization
2020-02-20 23:34:02 +01:00
$issuer = register( 'issuer', \&issuer );
$sp = register( 'sp', \&sp );
2016-12-14 13:09:55 +01:00
# Simple authentication on IdP
switch ('issuer');
ok(
$res = $issuer->_post(
2017-03-03 18:25:03 +01:00
'/', IO::String->new('user=russian&password=russian'),
length => 29
2016-12-14 13:09:55 +01:00
),
'Auth query'
);
2016-12-23 11:02:21 +01:00
expectOK($res);
my $idpId = expectCookie($res);
2016-12-14 13:09:55 +01:00
# Query IdP to access to SP
ok(
$res = $issuer->_get(
'/saml/singleSignOn',
query => 'IDPInitiated=1&spConfKey=sp.com',
cookie => "lemonldap=$idpId",
accept => 'test/html'
),
'Query IdP to access to SP'
);
2016-12-23 11:02:21 +01:00
expectOK($res);
2016-12-14 13:09:55 +01:00
ok(
$res->[2]->[0] =~
m#<form.+?action="http://auth.sp.com(.*?)".+?method="post"#,
'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";
2016-12-23 11:02:21 +01:00
# Post SAML response to SP
2016-12-14 13:09:55 +01:00
switch ('sp');
ok(
$res = $sp->_post(
$url, IO::String->new($s),
accept => 'text/html',
length => length($s),
),
'Post SAML response to SP'
);
# Verify authentication on SP
2016-12-23 11:02:21 +01:00
my $spId = expectCookie($res);
expectRedirection( $res, 'http://auth.sp.com' );
2016-12-14 13:09:55 +01:00
ok( $res = $sp->_get( '/', cookie => "lemonldap=$spId" ), 'Get / on SP' );
2016-12-23 11:02:21 +01:00
expectOK($res);
expectAuthenticatedAs( $res, 'ru@badwolf.org@idp' );
# Verify UTF-8
ok( $res = $sp->_get("/sessions/global/$spId"), 'Get UTF-8' );
expectOK($res);
ok( $res = eval { JSON::from_json( $res->[2]->[0] ) }, ' GET JSON' )
or print STDERR $@;
ok( $res->{cn} eq 'Русский', 'UTF-8 values' )
or explain( $res, 'cn => Frédéric Accents' );
2016-12-14 13:09:55 +01:00
# Logout initiated by IdP
switch ('issuer');
ok(
$res = $issuer->_get(
'/',
query => 'logout',
cookie => "lemonldap=$idpId",
accept => 'text/html'
),
2016-12-23 11:02:21 +01:00
'Query IdP for logout'
2016-12-14 13:09:55 +01:00
);
2016-12-23 11:02:21 +01:00
expectOK($res);
2016-12-19 06:31:51 +01:00
ok(
$res->[2]->[0] =~
2016-12-26 09:14:05 +01:00
m#iframe src="http://auth.sp.com(/saml/proxySingleLogout)\?(SAMLRequest=.*?)"#,
'Get iframe request'
2016-12-19 06:31:51 +01:00
);
2016-12-26 09:14:05 +01:00
$url = $1;
my $query = $2;
2022-02-16 17:43:29 +01:00
expectCspChildOK( $res, "auth.sp.com" );
2016-12-19 06:31:51 +01:00
my $removedCookie = expectCookie($res);
2020-02-20 23:34:02 +01:00
is( $removedCookie, 0, "SSO cookie removed" );
2016-12-26 09:14:05 +01:00
switch ('sp');
ok( $res = $sp->_get( $url, query => $query, accept => 'text/html' ),
'Query SP for iframe' );
( $url, $query ) = expectRedirection( $res,
qr#http://auth.idp.com(/saml/singleLogoutReturn)\?(SAMLResponse=.*)# );
# Push SAML logout response to IdP
switch ('issuer');
ok( $res = $issuer->_get( $url, query => $query, accept => 'text/html' ),
'Push SAML response to IdP' );
2019-05-11 20:18:43 +02:00
expectRedirection( $res, 'http://auth.idp.com/static/common/icons/ok.png' );
2017-01-21 10:50:59 +01:00
ok( getHeader( $res, 'Content-Security-Policy' ) !~ /frame-ancestors/,
2017-01-21 10:17:24 +01:00
' Frame can be embedded' )
or explain( $res->[1],
2017-01-21 10:50:59 +01:00
'Content-Security-Policy does not contain a frame-ancestors' );
2016-12-19 06:31:51 +01:00
2016-12-23 17:03:36 +01:00
# Test if logout is done
switch ('issuer');
ok(
2017-01-01 18:56:46 +01:00
$res = $issuer->_get(
2016-12-23 17:03:36 +01:00
'/', cookie => "lemonldap=$idpId",
),
'Test if user is reject on IdP'
);
expectReject($res);
switch ('sp');
ok(
$res = $sp->_get(
'/',
accept => 'text/html',
2019-10-01 11:18:20 +02:00
cookie => "lemonldap=$spId"
2016-12-23 17:03:36 +01:00
),
'Test if user is reject on SP'
);
2018-07-18 08:02:48 +02:00
expectRedirection( $res,
qr#^http://auth.idp.com(/saml/singleSignOn)\?(SAMLRequest=.+)# );
2016-12-14 13:09:55 +01:00
}
count($maintests);
clean_sessions();
done_testing( count() );
sub issuer {
2019-02-07 09:27:56 +01:00
return LLNG::Manager::Test->new( {
2016-12-14 13:09:55 +01:00
ini => {
logLevel => $debug,
domain => 'idp.com',
portal => 'http://auth.idp.com',
authentication => 'Demo',
userDB => 'Same',
2016-12-14 13:09:55 +01:00
issuerDBSAMLActivation => 1,
samlSPMetaDataOptions => {
'sp.com' => {
2016-12-16 07:03:30 +01:00
samlSPMetaDataOptionsEncryptionMode => 'none',
samlSPMetaDataOptionsEnableIDPInitiatedURL => 1,
samlSPMetaDataOptionsSignSSOMessage => 1,
samlSPMetaDataOptionsSignSLOMessage => 1,
samlSPMetaDataOptionsCheckSSOMessageSignature => 1,
samlSPMetaDataOptionsCheckSLOMessageSignature => 1,
2016-12-14 13:09:55 +01:00
}
},
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",
samlOrganizationURL => "http://www.idp.com/",
samlServicePrivateKeyEnc => saml_key_idp_private_enc,
2020-02-20 23:34:02 +01:00
samlServicePrivateKeySig => saml_key_idp_private_sig,
samlServicePublicKeyEnc => saml_key_idp_public_enc,
samlServicePublicKeySig => saml_key_idp_public_sig,
samlSPMetaDataXML => {
2016-12-14 13:09:55 +01:00
"sp.com" => {
samlSPMetaDataXML =>
samlSPMetaDataXML( 'sp', 'HTTP-Redirect' )
2016-12-14 13:09:55 +01:00
},
},
}
}
);
}
sub sp {
2019-02-07 09:27:56 +01:00
return LLNG::Manager::Test->new( {
2016-12-14 13:09:55 +01:00
ini => {
logLevel => $debug,
domain => 'sp.com',
portal => 'http://auth.sp.com',
authentication => 'SAML',
userDB => 'Same',
2016-12-14 13:09:55 +01:00
issuerDBSAMLActivation => 0,
restSessionServer => 1,
2016-12-14 13:09:55 +01:00
samlIDPMetaDataExportedAttributes => {
idp => {
mail => "0;mail;;",
uid => "1;uid",
cn => "0;cn"
}
},
samlIDPMetaDataOptions => {
idp => {
2016-12-16 07:03:30 +01:00
samlIDPMetaDataOptionsEncryptionMode => 'none',
2016-12-27 09:40:12 +01:00
samlIDPMetaDataOptionsSSOBinding => 'redirect',
samlIDPMetaDataOptionsSLOBinding => 'redirect',
2016-12-16 07:03:30 +01:00
samlIDPMetaDataOptionsSignSSOMessage => 1,
samlIDPMetaDataOptionsSignSLOMessage => 1,
samlIDPMetaDataOptionsCheckSSOMessageSignature => 1,
samlIDPMetaDataOptionsCheckSLOMessageSignature => 1,
2017-03-03 18:25:03 +01:00
samlIDPMetaDataOptionsForceUTF8 => 1,
2016-12-16 07:03:30 +01:00
samlIDPMetaDataOptionsAllowLoginFromIDP => 1,
2016-12-14 13:09:55 +01:00
}
},
samlIDPMetaDataExportedAttributes => {
idp => {
"uid" => "0;uid;;",
"cn" => "1;cn;;",
},
},
2016-12-14 13:09:55 +01:00
samlIDPMetaDataXML => {
idp => {
samlIDPMetaDataXML =>
samlIDPMetaDataXML( 'idp', 'HTTP-Redirect' )
2016-12-14 13:09:55 +01:00
}
},
samlOrganizationDisplayName => "SP",
samlOrganizationName => "SP",
samlOrganizationURL => "http://www.sp.com",
samlServicePublicKeySig => saml_key_sp_public_sig,
2020-02-20 23:34:02 +01:00
samlServicePrivateKeyEnc => saml_key_sp_private_enc,
samlServicePrivateKeySig => saml_key_sp_private_sig,
samlServicePublicKeyEnc => saml_key_sp_public_enc,
2016-12-14 13:09:55 +01:00
samlSPSSODescriptorAuthnRequestsSigned => 1,
},
}
);
}