lemonldap-ng/lemonldap-ng-portal/t/30-Auth-SAML-with-choice.t

395 lines
14 KiB
Perl
Raw Normal View History

2018-09-05 22:24:23 +02:00
use lib 'inc';
2018-06-18 12:16:00 +02:00
use Test::More;
use strict;
use IO::String;
use LWP::UserAgent;
2018-09-05 22:24:23 +02:00
use LWP::Protocol::PSGI;
2018-06-18 12:16:00 +02:00
use MIME::Base64;
BEGIN {
require 't/test-lib.pm';
require 't/saml-lib.pm';
}
2020-02-05 17:12:07 +01:00
my $maintests = 21;
2018-06-21 13:52:18 +02:00
my $debug = 'error';
2018-06-18 12:16:00 +02:00
2018-06-18 22:37:28 +02:00
SKIP: {
eval "use Lasso";
if ($@) {
skip 'Lasso not found', $maintests;
}
# Initialization
my ( $issuer, $sp );
2020-02-20 23:34:02 +01:00
$issuer = register( 'issuer', \&issuer );
$sp = register( 'sp', \&sp );
2018-06-18 22:37:28 +02:00
# Simple SP access
my $res;
ok(
$res = $sp->_get(
'/', accept => 'text/html',
),
'Unauth SP request'
);
expectOK($res);
2019-02-07 09:27:56 +01:00
ok(
$res->[2]->[0] =~
m%<form id="lformDemo" action="#" method="post" class="login Demo">%s,
'Found Demo choice'
) or print STDERR Dumper( $res->[2]->[0] );
2018-06-18 22:37:28 +02:00
ok( $res->[2]->[0] =~ m#<form[^>]+class="login SAML".*?</form>#s,
2019-02-07 09:27:56 +01:00
'Found SAML choice' )
or print STDERR Dumper( $res->[2]->[0] );
2018-06-18 22:37:28 +02:00
$res->[2]->[0] = $&;
my ( $host, $url, $query ) = expectForm( $res, undef, undef, 'test' );
# Post SAML choice
ok(
$res = $sp->_post(
'/' => IO::String->new($query),
accept => 'text/html',
length => length($query),
),
'Post SAML choice'
);
( $host, $url, $query ) = expectForm( $res, undef, undef, 'confirm', );
2019-04-10 22:50:34 +02:00
# IDP must be sorted
my @idp = map /val="http:\/\/(.+?)\/saml\/metadata">/g, $res->[2]->[0];
2019-05-11 20:18:43 +02:00
ok( $idp[0] eq 'auth.idp2.com', '1st = idp2' )
or print STDERR Dumper( \@idp );
ok( $idp[1] eq 'auth.idp2_z.com', '2nd = idp2_z' )
or print STDERR Dumper( \@idp );
ok( $idp[2] eq 'auth.idp3.com', '3rd = idp3' )
or print STDERR Dumper( \@idp );
ok( $idp[3] eq 'auth.idp.com', '4th= idp' ) or print STDERR Dumper( \@idp );
2019-04-10 23:03:17 +02:00
2019-02-07 09:27:56 +01:00
ok(
$res->[2]->[0] =~
m%<img src="http://auth.sp.com/static/common/icons/sfa_manager.png" class="mr-2" alt="IDP2" title="IDP2" />%,
2019-02-05 23:32:55 +01:00
'Found IDP icon and title tag'
) or print STDERR Dumper( $res->[2]->[0] );
2019-02-07 09:27:56 +01:00
ok( $res->[2]->[0] =~ /idp_Test_DisplayName/, 'Found IDP display name' )
or print STDERR Dumper( $res->[2]->[0] );
2019-02-05 23:32:55 +01:00
2018-07-05 23:00:40 +02:00
my $spPdata = 'lemonldappdata=' . expectCookie( $res, 'lemonldappdata' );
2018-06-18 22:37:28 +02:00
# Choose SAML issuer
$query .= '&idp=http%3A%2F%2Fauth.idp.com%2Fsaml%2Fmetadata';
ok(
$res = $sp->_post(
'/' => IO::String->new($query),
accept => 'text/html',
length => length($query),
2019-10-01 11:18:20 +02:00
cookie => "$spPdata",
2018-06-18 22:37:28 +02:00
),
'Post SAML choice'
);
2018-07-05 23:00:40 +02:00
$spPdata = 'lemonldappdata=' . expectCookie( $res, 'lemonldappdata' );
2018-06-18 22:37:28 +02:00
( $host, $url, $query ) =
expectAutoPost( $res, 'auth.idp.com', '/saml/singleSignOn',
'SAMLRequest' );
# Push SAML request to IdP
switch ('issuer');
ok(
$res = $issuer->_post(
$url,
IO::String->new($query),
accept => 'text/html',
length => length($query)
),
'Post SAML request to IdP'
);
expectOK($res);
2018-07-05 23:00:40 +02:00
my $pdata = 'lemonldappdata=' . expectCookie( $res, 'lemonldappdata' );
2018-06-18 22:37:28 +02:00
# Try to authenticate to IdP
$query = "user=french&password=french&$query";
ok(
$res = $issuer->_post(
$url,
IO::String->new($query),
accept => 'text/html',
cookie => $pdata,
2018-06-18 22:37:28 +02:00
length => length($query),
),
'Post authentication'
);
my $idpId = expectCookie($res);
( $host, $url, $query ) =
expectAutoPost( $res, 'auth.sp.com', '/saml/proxySingleSignOnPost',
'SAMLResponse' );
# Post SAML response to SP
switch ('sp');
ok(
$res = $sp->_post(
$url, IO::String->new($query),
accept => 'text/html',
length => length($query),
2019-10-01 11:18:20 +02:00
cookie => "$spPdata",
2018-06-18 22:37:28 +02:00
),
'Post SAML response to SP'
);
# Verify authentication on SP
2018-06-18 23:19:33 +02:00
expectRedirection( $res, 'http://auth.sp.com' );
my $spId = expectCookie($res);
2018-06-18 22:37:28 +02:00
2018-06-18 23:19:33 +02:00
ok( $res = $sp->_get( '/', cookie => "lemonldap=$spId" ), 'Get / on SP' );
expectOK($res);
expectAuthenticatedAs( $res, 'fa@badwolf.org@idp' );
2018-06-22 13:08:18 +02:00
# Logout initiated by SP
ok(
2018-06-22 13:08:18 +02:00
$res = $sp->_get(
'/',
query => 'logout',
2018-06-22 13:08:18 +02:00
cookie => "lemonldap=$spId",
accept => 'text/html'
),
2018-06-22 13:08:18 +02:00
'Query SP for logout'
);
2018-06-22 13:08:18 +02:00
( $host, $url, $query ) =
expectAutoPost( $res, 'auth.idp.com', '/saml/singleLogout',
'SAMLRequest' );
2018-06-22 13:08:18 +02:00
# Push SAML logout request to IdP
switch ('issuer');
ok(
2018-06-22 13:08:18 +02:00
$res = $issuer->_post(
$url,
2018-06-22 13:08:18 +02:00
IO::String->new($query),
accept => 'text/html',
cookie => "lemonldap=$idpId",
2018-06-22 13:08:18 +02:00
length => length($query)
),
2018-06-22 13:08:18 +02:00
'Post SAML logout request to IdP'
);
( $host, $url, $query ) =
2018-06-22 13:08:18 +02:00
expectAutoPost( $res, 'auth.sp.com', '/saml/proxySingleLogoutReturn',
'SAMLResponse' );
my $removedCookie = expectCookie($res);
2020-02-20 23:34:02 +01:00
is( $removedCookie, 0, "SSO cookie removed" );
2018-06-22 13:08:18 +02:00
# Post SAML response to SP
switch ('sp');
ok(
$res = $sp->_post(
$url, IO::String->new($query),
accept => 'text/html',
length => length($query),
),
2018-06-22 13:08:18 +02:00
'Post SAML response to SP'
);
2018-06-22 13:08:18 +02:00
expectRedirection( $res, 'http://auth.sp.com' );
# Test if logout is done
switch ('issuer');
ok(
$res = $issuer->_get(
'/', cookie => "lemonldap=$idpId",
),
'Test if user is reject on IdP'
);
expectReject($res);
switch ('sp');
ok(
$res = $sp->_get(
2019-10-01 11:18:20 +02:00
'/', cookie => "lemonldap=$spId"
2018-06-22 13:08:18 +02:00
),
'Test if user is reject on SP'
);
expectReject($res);
2018-06-18 22:37:28 +02:00
}
2018-06-18 12:16:00 +02:00
count($maintests);
clean_sessions();
done_testing( count() );
2018-06-18 22:37:28 +02:00
sub sp {
2019-02-07 09:27:56 +01:00
return LLNG::Manager::Test->new( {
2018-06-18 22:37:28 +02:00
ini => {
domain => 'sp.com',
portal => 'http://auth.sp.com',
logLevel => $debug,
authentication => 'Choice',
userDB => 'Same',
authChoiceParam => 'test',
authChoiceModules => {
demo => 'Demo;Demo;Demo',
saml => 'SAML;SAML;Null',
},
samlIDPMetaDataExportedAttributes => {
idp => {
mail => "0;mail;;",
uid => "1;uid",
cn => "0;cn"
},
idp2 => {
mail => "0;mail;;",
uid => "1;uid",
cn => "0;cn"
},
2019-04-10 22:50:34 +02:00
idp3 => {
mail => "0;mail;;",
uid => "1;uid",
cn => "0;cn"
},
2019-04-13 20:28:22 +02:00
idp2_z => {
2019-04-10 23:03:17 +02:00
mail => "0;mail;;",
uid => "1;uid",
cn => "0;cn"
},
2018-06-18 22:37:28 +02:00
},
samlIDPMetaDataOptions => {
idp => {
samlIDPMetaDataOptionsEncryptionMode => 'none',
samlIDPMetaDataOptionsSSOBinding => 'post',
samlIDPMetaDataOptionsSLOBinding => 'post',
samlIDPMetaDataOptionsSignSSOMessage => 1,
samlIDPMetaDataOptionsSignSLOMessage => 1,
samlIDPMetaDataOptionsCheckSSOMessageSignature => 1,
samlIDPMetaDataOptionsCheckSLOMessageSignature => 1,
samlIDPMetaDataOptionsForceUTF8 => 1,
2019-04-10 22:50:34 +02:00
samlIDPMetaDataOptionsSortNumber => 2,
2022-02-16 17:43:29 +01:00
samlIDPMetaDataOptionsDisplayName =>
2019-02-07 09:27:56 +01:00
'idp_Test_DisplayName',
2019-02-05 23:32:55 +01:00
2018-06-18 22:37:28 +02:00
},
idp2 => {
samlIDPMetaDataOptionsEncryptionMode => 'none',
samlIDPMetaDataOptionsSSOBinding => 'post',
samlIDPMetaDataOptionsSLOBinding => 'post',
samlIDPMetaDataOptionsSignSSOMessage => 1,
samlIDPMetaDataOptionsSignSLOMessage => 1,
samlIDPMetaDataOptionsCheckSSOMessageSignature => 1,
samlIDPMetaDataOptionsCheckSLOMessageSignature => 1,
samlIDPMetaDataOptionsForceUTF8 => 1,
2019-02-07 09:27:56 +01:00
samlIDPMetaDataOptionsIcon => 'icons/sfa_manager.png',
2018-06-18 22:37:28 +02:00
},
2019-04-10 22:50:34 +02:00
idp3 => {
samlIDPMetaDataOptionsEncryptionMode => 'none',
samlIDPMetaDataOptionsSSOBinding => 'post',
samlIDPMetaDataOptionsSLOBinding => 'post',
samlIDPMetaDataOptionsSignSSOMessage => 1,
samlIDPMetaDataOptionsSignSLOMessage => 1,
samlIDPMetaDataOptionsCheckSSOMessageSignature => 1,
samlIDPMetaDataOptionsCheckSLOMessageSignature => 1,
samlIDPMetaDataOptionsForceUTF8 => 1,
samlIDPMetaDataOptionsSortNumber => 1,
samlIDPMetaDataOptionsDisplayName => 'Test_Sort',
},
2019-04-13 20:28:22 +02:00
idp2_z => {
2019-04-10 23:03:17 +02:00
samlIDPMetaDataOptionsEncryptionMode => 'none',
samlIDPMetaDataOptionsSSOBinding => 'post',
samlIDPMetaDataOptionsSLOBinding => 'post',
samlIDPMetaDataOptionsSignSSOMessage => 1,
samlIDPMetaDataOptionsSignSLOMessage => 1,
samlIDPMetaDataOptionsCheckSSOMessageSignature => 1,
samlIDPMetaDataOptionsCheckSLOMessageSignature => 1,
samlIDPMetaDataOptionsForceUTF8 => 1,
},
2018-06-18 22:37:28 +02:00
},
samlIDPMetaDataExportedAttributes => {
idp => {
"uid" => "0;uid;;",
"cn" => "1;cn;;",
},
idp2 => {
"uid" => "0;uid;;",
"cn" => "1;cn;;",
},
2019-04-10 22:50:34 +02:00
idp3 => {
"uid" => "0;uid;;",
"cn" => "1;cn;;",
},
2019-04-13 20:28:22 +02:00
idp2_z => {
2019-04-10 23:03:17 +02:00
"uid" => "0;uid;;",
"cn" => "1;cn;;",
},
2018-06-18 22:37:28 +02:00
},
samlIDPMetaDataXML => {
idp => {
samlIDPMetaDataXML =>
samlIDPMetaDataXML( 'idp', 'HTTP-POST' )
},
idp2 => {
samlIDPMetaDataXML =>
samlIDPMetaDataXML( 'idp2', 'HTTP-POST' )
2019-04-10 22:50:34 +02:00
},
idp3 => {
samlIDPMetaDataXML =>
samlIDPMetaDataXML( 'idp3', 'HTTP-POST' )
},
2019-04-13 20:28:22 +02:00
idp2_z => {
2019-04-10 23:03:17 +02:00
samlIDPMetaDataXML =>
2019-04-13 20:28:22 +02:00
samlIDPMetaDataXML( 'idp2_z', 'HTTP-POST' )
2019-04-10 23:03:17 +02:00
},
2018-06-18 22:37:28 +02: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,
2018-06-18 22:37:28 +02:00
samlSPSSODescriptorAuthnRequestsSigned => 1,
}
}
);
}
sub issuer {
2019-02-07 09:27:56 +01:00
return LLNG::Manager::Test->new( {
2018-06-18 22:37:28 +02:00
ini => {
logLevel => $debug,
domain => 'idp.com',
portal => 'http://auth.idp.com',
authentication => 'Demo',
userDB => 'Same',
issuerDBSAMLActivation => 1,
samlSPMetaDataOptions => {
'sp.com' => {
samlSPMetaDataOptionsEncryptionMode => 'none',
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",
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 => {
2018-06-18 22:37:28 +02:00
"sp.com" => {
samlSPMetaDataXML =>
samlSPMetaDataXML( 'sp', 'HTTP-POST' )
},
},
}
}
);
}