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

348 lines
11 KiB
Perl
Raw Normal View History

2018-09-05 22:24:23 +02:00
use lib 'inc';
2016-12-04 22:57:25 +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-05 21:53:38 +01:00
use MIME::Base64;
2016-12-04 22:57:25 +01:00
BEGIN {
require 't/test-lib.pm';
2017-03-15 07:29:44 +01:00
require 't/saml-lib.pm';
2016-12-04 22:57:25 +01:00
}
2020-02-05 17:12:07 +01:00
my $maintests = 19;
2016-12-15 22:22:15 +01:00
my $debug = 'error';
2016-12-13 20:21:36 +01:00
my ( $issuer, $sp, $res );
2016-12-04 22:57:25 +01:00
# 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-04 22:57:25 +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-04 22:57:25 +01:00
# Simple SP access
my $res;
ok(
$res = $sp->_get(
2017-03-20 07:16:56 +01:00
'/',
accept => 'text/html',
query => 'url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw=='
2016-12-04 22:57:25 +01:00
),
'Unauth SP request'
);
2018-07-18 08:02:48 +02:00
my ( $host, $url, $query );
2017-03-20 07:16:56 +01:00
( $url, $query ) = expectRedirection( $res,
2016-12-23 11:02:21 +01:00
qr#^http://auth.idp.com(/saml/singleSignOn)\?(SAMLRequest=.+)# );
2016-12-04 22:57:25 +01:00
# Push SAML request to IdP
switch ('issuer');
ok(
$res = $issuer->_get(
$url,
2016-12-05 21:53:38 +01:00
query => $query,
2016-12-04 22:57:25 +01:00
accept => 'text/html',
),
'Launch SAML request to IdP'
);
2016-12-23 11:02:21 +01:00
expectOK($res);
2018-07-05 23:00:40 +02:00
my $pdata = 'lemonldappdata=' . expectCookie( $res, 'lemonldappdata' );
2016-12-04 22:57:25 +01:00
2019-11-02 19:46:35 +01:00
# Try to authenticate with an expired OTT to IdP
# Waiting
Time::Fake->offset("+150s");
2016-12-04 22:57:25 +01:00
my $body = $res->[2]->[0];
$body =~ s/^.*?<form.*?>//s;
$body =~ s#</form>.*$##s;
my %fields =
( $body =~ /<input type="hidden".+?name="(.+?)".+?value="(.*?)"/sg );
$fields{user} = $fields{password} = 'french';
2016-12-04 22:57:25 +01:00
use URI::Escape;
2017-03-20 13:43:57 +01:00
$query =
join( '&', map { "$_=" . uri_escape( $fields{$_} ) } keys %fields );
2016-12-04 22:57:25 +01:00
ok(
$res = $issuer->_post(
$url,
2017-03-20 13:43:57 +01:00
IO::String->new($query),
2016-12-04 22:57:25 +01:00
accept => 'text/html',
cookie => $pdata,
2017-03-20 13:43:57 +01:00
length => length($query),
2016-12-04 22:57:25 +01:00
),
2019-11-02 19:46:35 +01:00
'Post delayed authentication request'
2016-12-04 22:57:25 +01:00
);
2019-11-02 19:46:35 +01:00
expectRedirection( $res, 'http://auth.idp.com/saml' );
2016-12-23 11:02:21 +01:00
my $idpId = expectCookie($res);
# Expect pdata to be cleared
$pdata = expectCookie( $res, 'lemonldappdata' );
ok( $pdata !~ 'issuerRequestsaml', 'SAML request cleared from pdata' );
2019-11-02 19:46:35 +01:00
# Simple SP access
ok(
$res = $sp->_get(
'/',
accept => 'text/html',
query => 'url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw=='
),
'Unauth SP request'
);
( $host, $url, $query );
( $url, $query ) = expectRedirection( $res,
qr#^http://auth.idp.com(/saml/singleSignOn)\?(SAMLRequest=.+)# );
# Push SAML request to IdP
Time::Fake->reset;
ok(
$res = $issuer->_get(
$url,
query => $query,
accept => 'text/html',
),
'Launch SAML request to IdP'
);
expectOK($res);
$pdata = 'lemonldappdata=' . expectCookie( $res, 'lemonldappdata' );
# Try to authenticate with a valid OTT to IdP
# Waiting
Time::Fake->offset("+100s");
$body = $res->[2]->[0];
$body =~ s/^.*?<form.*?>//s;
$body =~ s#</form>.*$##s;
%fields =
( $body =~ /<input type="hidden".+?name="(.+?)".+?value="(.*?)"/sg );
$fields{user} = $fields{password} = 'french';
use URI::Escape;
$query =
join( '&', map { "$_=" . uri_escape( $fields{$_} ) } keys %fields );
ok(
$res = $issuer->_post(
$url,
IO::String->new($query),
accept => 'text/html',
cookie => $pdata,
length => length($query),
),
'Post authentication request'
);
expectOK($res);
$idpId = expectCookie($res);
# Expect pdata to be cleared
$pdata = expectCookie( $res, 'lemonldappdata' );
ok( $pdata !~ 'issuerRequestsaml', 'SAML request cleared from pdata' );
2017-03-20 13:43:57 +01:00
( $host, $url, $query ) =
expectForm( $res, 'auth.sp.com', '/saml/proxySingleSignOnPost',
'SAMLResponse', 'RelayState' );
2016-12-06 20:04:39 +01:00
# Post SAML response to SP
2016-12-06 20:04:39 +01:00
switch ('sp');
ok(
$res = $sp->_post(
2017-03-20 13:43:57 +01:00
$url, IO::String->new($query),
2016-12-06 20:04:39 +01:00
accept => 'text/html',
2017-03-20 13:43:57 +01:00
length => length($query),
2016-12-06 20:04:39 +01:00
),
'Post SAML response to SP'
2016-12-06 20:04:39 +01:00
);
2016-12-23 11:02:21 +01:00
my $spId = expectCookie($res);
2017-03-20 13:43:57 +01:00
expectRedirection( $res, 'http://test1.example.com/' );
2016-12-06 20:04:39 +01:00
ok( $res = $sp->_get( '/', cookie => "lemonldap=$spId" ), 'Get / on SP' );
2016-12-23 11:02:21 +01:00
expectOK($res);
expectAuthenticatedAs( $res, 'fa@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 'Frédéric Accents', 'UTF-8 values' )
or explain( $res, 'cn => Frédéric Accents' );
2016-12-06 20:04:39 +01:00
2016-12-11 17:45:56 +01:00
# Logout initiated by SP
ok(
$res = $sp->_get(
'/',
query => 'logout',
cookie => "lemonldap=$spId",
accept => 'text/html'
),
'Query SP for logout'
);
2016-12-23 11:02:21 +01:00
( $url, $query ) = expectRedirection( $res,
qr#^http://auth.idp.com(/saml/singleLogout)\?(SAMLRequest=.+)# );
2016-12-11 17:45:56 +01:00
# Push SAML logout request to IdP
switch ('issuer');
ok(
$res = $issuer->_get(
$url,
query => $query,
accept => 'text/html',
cookie => "lemonldap=$idpId",
),
'Launch SAML logout request to IdP'
);
2016-12-23 11:02:21 +01:00
( $url, $query ) = expectRedirection( $res,
qr#^http://auth.sp.com(/saml/proxySingleLogoutReturn)\?(SAMLResponse=.+)#
);
2016-12-15 22:22:15 +01:00
my $removedCookie = expectCookie($res);
2020-02-20 23:34:02 +01:00
is( $removedCookie, 0, "IDP Cookie removed" );
2016-12-15 22:22:15 +01:00
# Send SAML response to SP
switch ('sp');
ok(
$res = $sp->_get(
$url,
query => $query,
accept => 'text/html',
),
'Launch SAML logout request to SP'
2016-12-15 22:22:15 +01:00
);
2016-12-23 11:02:21 +01:00
expectOK($res);
2016-12-13 20:21:36 +01:00
2016-12-23 18:00:15 +01:00
# Test if logout is done
switch ('issuer');
ok(
2017-01-01 18:56:46 +01:00
$res = $issuer->_get(
2016-12-23 18:00:15 +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 18:00:15 +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-04 22:57:25 +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-04 22:57:25 +01:00
ini => {
logLevel => $debug,
domain => 'idp.com',
portal => 'http://auth.idp.com',
authentication => 'Demo',
userDB => 'Same',
2019-11-02 19:46:35 +01:00
issuersTimeout => 120,
2016-12-04 22:57:25 +01:00
issuerDBSAMLActivation => 1,
samlSPMetaDataOptions => {
'sp.com' => {
2016-12-16 07:03:30 +01:00
samlSPMetaDataOptionsEncryptionMode => 'none',
samlSPMetaDataOptionsSignSSOMessage => 1,
samlSPMetaDataOptionsSignSLOMessage => 1,
samlSPMetaDataOptionsCheckSSOMessageSignature => 1,
samlSPMetaDataOptionsCheckSLOMessageSignature => 1,
2016-12-04 22:57:25 +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-04 22:57:25 +01:00
"sp.com" => {
2017-03-20 07:16:56 +01:00
samlSPMetaDataXML =>
samlSPMetaDataXML( 'sp', 'HTTP-Redirect' )
2016-12-04 22:57:25 +01:00
},
},
}
}
);
}
sub sp {
2019-02-07 09:27:56 +01:00
return LLNG::Manager::Test->new( {
2016-12-04 22:57:25 +01:00
ini => {
logLevel => $debug,
domain => 'sp.com',
portal => 'http://auth.sp.com',
authentication => 'SAML',
userDB => 'Same',
2016-12-04 22:57:25 +01:00
issuerDBSAMLActivation => 0,
restSessionServer => 1,
2016-12-04 22:57:25 +01:00
samlIDPMetaDataExportedAttributes => {
idp => {
mail => "0;mail;;",
uid => "1;uid",
cn => "0;cn"
}
},
samlIDPMetaDataOptions => {
idp => {
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-04 22:57:25 +01:00
}
},
samlIDPMetaDataExportedAttributes => {
idp => {
"uid" => "0;uid;;",
"cn" => "1;cn;;",
},
},
2016-12-04 22:57:25 +01:00
samlIDPMetaDataXML => {
idp => {
2017-03-20 07:16:56 +01:00
samlIDPMetaDataXML =>
samlIDPMetaDataXML( 'idp', 'HTTP-Redirect' )
2016-12-04 22:57:25 +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-04 22:57:25 +01:00
samlSPSSODescriptorAuthnRequestsSigned => 1,
},
}
);
}