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

345 lines
11 KiB
Perl
Raw Normal View History

2018-09-05 22:24:23 +02:00
use lib 'inc';
2016-11-14 22:45:32 +01:00
use Test::More;
use strict;
2016-11-20 06:35:06 +01:00
use IO::String;
use LWP::UserAgent;
2018-09-05 22:24:23 +02:00
use LWP::Protocol::PSGI;
2016-12-05 21:53:36 +01:00
use MIME::Base64;
2016-11-14 22:45:32 +01:00
2016-11-29 22:44:33 +01:00
BEGIN {
require 't/test-lib.pm';
2017-03-15 07:29:44 +01:00
require 't/saml-lib.pm';
2016-11-29 22:44:33 +01:00
}
2016-11-14 22:45:32 +01:00
2021-06-08 22:55:09 +02:00
my $maintests = 22;
2021-06-09 23:06:03 +02:00
my $debug = 'error';
2016-12-13 20:21:36 +01:00
my ( $issuer, $sp, $res );
2016-11-14 22:45:32 +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-11-14 22:45:32 +01:00
SKIP: {
eval "use Lasso";
if ($@) {
2016-11-20 06:35:06 +01:00
skip 'Lasso not found', $maintests;
2016-11-14 22:45:32 +01:00
}
2016-11-20 06:35:06 +01:00
# Initialization
2020-02-20 23:34:02 +01:00
$issuer = register( 'issuer', \&issuer );
$sp = register( 'sp', \&sp );
2016-11-16 16:27:01 +01:00
2021-06-08 22:55:09 +02:00
# Try to authenticate
# -------------------
switch ('issuer');
2016-11-20 06:35:06 +01:00
my $res;
2021-06-08 22:55:09 +02:00
ok(
$res = $issuer->_post(
'/', IO::String->new('user=french&password=french'),
length => 27
),
'Auth query'
);
expectOK($res);
my $id = expectCookie($res);
# Simple SP access
switch ('sp');
2016-11-16 16:27:01 +01:00
ok(
2016-11-20 06:35:06 +01:00
$res = $sp->_get(
2016-11-16 16:27:01 +01:00
'/', accept => 'text/html',
),
'Unauth SP request'
);
2016-12-23 11:02:21 +01:00
expectOK($res);
2016-12-27 21:00:20 +01:00
my ( $host, $url, $s ) =
expectAutoPost( $res, 'auth.idp.com', '/saml/singleSignOn',
'SAMLRequest' );
2016-11-29 22:10:00 +01:00
# Push SAML request to IdP
2016-11-28 22:15:57 +01:00
switch ('issuer');
ok(
$res = $issuer->_post(
$url,
IO::String->new($s),
accept => 'text/html',
length => length($s)
),
'Post SAML request to IdP'
);
2016-12-23 11:02:21 +01:00
expectOK($res);
2020-08-17 16:49:23 +02:00
my $pdata = 'lemonldappdata=' . expectCookie( $res, 'lemonldappdata' );
my $rawCookie = getHeader( $res, 'Set-Cookie' );
ok( $rawCookie =~ /;\s*SameSite=None/, 'Found SameSite=None' );
2016-11-29 22:10:00 +01:00
2019-02-10 22:29:50 +01:00
# Try to authenticate with an unauthorized user to IdP
2019-02-10 11:17:23 +01:00
$s = "user=dwho&password=dwho&$s";
ok(
$res = $issuer->_post(
$url,
IO::String->new($s),
accept => 'text/html',
cookie => $pdata,
length => length($s),
),
'Post authentication'
);
2019-02-10 22:29:50 +01:00
ok( $res->[2]->[0] =~ /trmsg="89"/, 'Reject reason is 89' )
2019-03-07 18:22:16 +01:00
or print STDERR Dumper( $res->[2]->[0] );
2019-02-10 11:17:23 +01:00
# Simple SP access
ok(
$res = $sp->_get(
'/', accept => 'text/html',
),
'Unauth SP request'
);
expectOK($res);
( $host, $url, $s ) =
expectAutoPost( $res, 'auth.idp.com', '/saml/singleSignOn',
'SAMLRequest' );
# Push SAML request to IdP
ok(
$res = $issuer->_post(
$url,
IO::String->new($s),
accept => 'text/html',
length => length($s)
),
'Post SAML request to IdP'
);
expectOK($res);
$pdata = 'lemonldappdata=' . expectCookie( $res, 'lemonldappdata' );
2019-02-10 22:29:50 +01:00
# Try to authenticate with an authorized user to IdP
2017-03-02 21:15:53 +01:00
$s = "user=french&password=french&$s";
2016-11-28 22:15:57 +01:00
ok(
$res = $issuer->_post(
$url,
IO::String->new($s),
accept => 'text/html',
cookie => $pdata,
2016-11-29 22:10:00 +01:00
length => length($s),
2016-11-28 22:15:57 +01:00
),
'Post authentication'
);
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' )
or explain( $pdata, 'not issuerRequestsaml' );
2016-12-27 21:00:20 +01:00
( $host, $url, $s ) =
expectAutoPost( $res, 'auth.sp.com', '/saml/proxySingleSignOnPost',
'SAMLResponse' );
2016-11-29 22:10:00 +01:00
2016-12-13 20:21:36 +01:00
# Post SAML response to SP
2016-11-29 22:10:00 +01:00
switch ('sp');
ok(
$res = $sp->_post(
$url, IO::String->new($s),
accept => 'text/html',
length => length($s),
),
2016-12-14 09:49:30 +01:00
'Post SAML response to SP'
2016-11-29 22:10:00 +01:00
);
2016-12-14 09:49:30 +01:00
# Verify authentication on SP
2016-12-23 11:02:21 +01:00
expectRedirection( $res, 'http://auth.sp.com' );
2021-06-08 22:55:09 +02:00
my $spId = expectCookie($res);
2020-11-17 23:18:05 +01:00
$rawCookie = getHeader( $res, 'Set-Cookie' );
2020-08-17 16:49:23 +02:00
ok( $rawCookie =~ /;\s*SameSite=None/, 'Found SameSite=None' );
2016-12-01 20:39:35 +01:00
ok( $res = $sp->_get( '/', cookie => "lemonldap=$spId" ), 'Get / on SP' );
2016-12-23 11:02:21 +01:00
expectOK($res);
2017-03-02 21:15:53 +01:00
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-11-28 22:15:57 +01:00
2016-12-09 11:25:05 +01:00
# Logout initiated by SP
ok(
$res = $sp->_get(
'/',
query => 'logout',
cookie => "lemonldap=$spId",
accept => 'text/html'
),
'Query SP for logout'
);
2016-12-27 21:00:20 +01:00
( $host, $url, $s ) =
expectAutoPost( $res, 'auth.idp.com', '/saml/singleLogout',
'SAMLRequest' );
2016-12-09 11:25:05 +01:00
# Push SAML logout request to IdP
switch ('issuer');
ok(
$res = $issuer->_post(
$url,
IO::String->new($s),
accept => 'text/html',
cookie => "lemonldap=$idpId",
length => length($s)
),
2016-12-23 11:02:21 +01:00
'Post SAML logout request to IdP'
2016-12-09 11:25:05 +01:00
);
2016-12-27 21:00:20 +01:00
( $host, $url, $s ) =
expectAutoPost( $res, '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
# Post SAML response to SP
switch ('sp');
ok(
$res = $sp->_post(
$url, IO::String->new($s),
accept => 'text/html',
length => length($s),
),
'Post SAML response to SP'
);
2016-12-23 11:02:21 +01:00
expectRedirection( $res, 'http://auth.sp.com' );
2016-12-15 22:22:15 +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'
);
2017-01-01 13:32:38 +01:00
expectOK($res);
2018-07-18 08:02:48 +02:00
expectAutoPost( $res, 'auth.idp.com', '/saml/singleSignOn', 'SAMLRequest' );
2016-11-14 22:45:32 +01:00
}
2016-11-20 06:35:06 +01:00
count($maintests);
2016-11-22 21:55:10 +01:00
clean_sessions();
2016-11-14 22:45:32 +01:00
done_testing( count() );
sub issuer {
2019-02-07 09:27:56 +01:00
return LLNG::Manager::Test->new( {
2016-11-14 22:45:32 +01:00
ini => {
2016-11-15 22:00:30 +01:00
logLevel => $debug,
2016-11-14 22:45:32 +01:00
domain => 'idp.com',
2016-11-28 22:15:57 +01:00
portal => 'http://auth.idp.com',
2016-11-14 22:45:32 +01:00
authentication => 'Demo',
userDB => 'Same',
2021-06-08 22:55:09 +02:00
globalLogoutRule => 1,
globalLogoutTimer => 0,
issuerDBSAMLActivation => 1,
2019-02-10 11:17:23 +01:00
issuerDBSAMLRule => '$uid eq "french"',
2016-11-15 22:00:30 +01:00
samlSPMetaDataOptions => {
2016-11-14 22:45:32 +01:00
'sp.com' => {
2016-12-16 07:03:30 +01:00
samlSPMetaDataOptionsEncryptionMode => 'none',
samlSPMetaDataOptionsSignSSOMessage => 1,
samlSPMetaDataOptionsSignSLOMessage => 1,
samlSPMetaDataOptionsCheckSSOMessageSignature => 1,
samlSPMetaDataOptionsCheckSLOMessageSignature => 1,
2016-11-14 22:45:32 +01:00
}
},
2016-12-01 20:39:35 +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',
}
},
2016-11-14 22:45:32 +01:00
samlOrganizationDisplayName => "IDP",
samlOrganizationName => "IDP",
2016-11-28 22:15:57 +01:00
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-11-14 22:45:32 +01:00
"sp.com" => {
samlSPMetaDataXML =>
samlSPMetaDataXML( 'sp', 'HTTP-POST' )
2016-11-14 22:45:32 +01:00
},
},
}
}
);
}
sub sp {
2019-02-07 09:27:56 +01:00
return LLNG::Manager::Test->new( {
2016-11-14 22:45:32 +01:00
ini => {
2016-11-15 22:00:30 +01:00
logLevel => $debug,
domain => 'sp.com',
2016-11-28 22:15:57 +01:00
portal => 'http://auth.sp.com',
2016-11-15 22:00:30 +01:00
authentication => 'SAML',
userDB => 'Same',
issuerDBSAMLActivation => 0,
2017-03-02 21:15:53 +01:00
restSessionServer => 1,
2016-11-15 22:00:30 +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 => 'post',
samlIDPMetaDataOptionsSLOBinding => 'post',
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-11-15 22:00:30 +01:00
}
},
2017-03-02 21:15:53 +01:00
samlIDPMetaDataExportedAttributes => {
idp => {
"uid" => "0;uid;;",
"cn" => "1;cn;;",
},
},
2016-11-15 22:00:30 +01:00
samlIDPMetaDataXML => {
idp => {
samlIDPMetaDataXML =>
samlIDPMetaDataXML( 'idp', 'HTTP-POST' )
2016-11-15 22:00:30 +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-11-15 22:00:30 +01:00
samlSPSSODescriptorAuthnRequestsSigned => 1,
},
2016-11-14 22:45:32 +01:00
}
);
}