2018-09-05 22:24:23 +02:00
|
|
|
use lib 'inc';
|
2017-10-31 09:53:33 +01:00
|
|
|
use Test::More;
|
|
|
|
use strict;
|
|
|
|
use IO::String;
|
2018-04-09 22:56:14 +02:00
|
|
|
use LWP::UserAgent;
|
2018-09-05 22:24:23 +02:00
|
|
|
use LWP::Protocol::PSGI;
|
2017-10-31 09:53:33 +01:00
|
|
|
use MIME::Base64;
|
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
require 't/test-lib.pm';
|
|
|
|
require 't/saml-lib.pm';
|
|
|
|
}
|
2019-08-29 10:04:06 +02:00
|
|
|
my $userdb = tempdb();
|
2017-10-31 09:53:33 +01:00
|
|
|
|
2020-02-05 17:12:07 +01:00
|
|
|
my $maintests = 10;
|
2017-10-31 10:51:48 +01:00
|
|
|
my $debug = 'error';
|
2017-10-31 09:53:33 +01:00
|
|
|
my ( $issuer, $sp, $res );
|
|
|
|
|
2018-04-09 22:56:14 +02: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, [], [] ];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-10-31 09:53:33 +01:00
|
|
|
SKIP: {
|
|
|
|
eval "use Lasso";
|
|
|
|
if ($@) {
|
2018-02-08 21:55:21 +01:00
|
|
|
skip 'Lasso not found', $maintests;
|
2017-10-31 09:53:33 +01:00
|
|
|
}
|
|
|
|
eval { require DBI; require DBD::SQLite; };
|
|
|
|
if ($@) {
|
2018-02-08 21:55:21 +01:00
|
|
|
skip 'DBD::SQLite not found', $maintests;
|
2017-10-31 09:53:33 +01:00
|
|
|
}
|
2019-08-29 10:04:06 +02:00
|
|
|
my $dbh = DBI->connect("dbi:SQLite:dbname=$userdb");
|
2018-03-13 07:14:01 +01:00
|
|
|
$dbh->do(
|
|
|
|
'CREATE TABLE users (user text,password text,name text,uid text,cn text,mail text)'
|
|
|
|
);
|
|
|
|
$dbh->do(
|
|
|
|
"INSERT INTO users VALUES ('dwho','dwho','Doctor who','dwho','Doctor who','dwho\@badwolf.org')"
|
|
|
|
);
|
2017-10-31 09:53:33 +01:00
|
|
|
|
|
|
|
# Initialization
|
2020-02-05 17:12:07 +01:00
|
|
|
$issuer = register('issuer', \&issuer);
|
|
|
|
$sp = register('sp', \&sp);
|
2017-10-31 09:53:33 +01:00
|
|
|
|
|
|
|
# Simple authentication on IdP
|
|
|
|
switch ('issuer');
|
|
|
|
ok(
|
|
|
|
$res = $issuer->_post(
|
|
|
|
'/', IO::String->new('user=dwho&password=dwho&test=sql'),
|
|
|
|
length => 32
|
|
|
|
),
|
|
|
|
'Auth query'
|
|
|
|
);
|
|
|
|
expectOK($res);
|
|
|
|
my $idpId = expectCookie($res);
|
|
|
|
pass('Waiting timeout');
|
2019-11-04 14:54:41 +01:00
|
|
|
Time::Fake->offset("+30s");
|
2017-10-31 09:53:33 +01:00
|
|
|
|
|
|
|
# Simple SP access
|
|
|
|
my $res;
|
|
|
|
ok(
|
|
|
|
$res = $sp->_get(
|
|
|
|
'/', accept => 'text/html',
|
|
|
|
),
|
|
|
|
'Unauth SP request'
|
|
|
|
);
|
|
|
|
my ( $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),
|
2019-10-01 11:18:20 +02:00
|
|
|
cookie => "lemonldap=$idpId",
|
2017-10-31 09:53:33 +01:00
|
|
|
),
|
|
|
|
'Post SAML request to IdP'
|
|
|
|
);
|
2018-07-04 22:54:09 +02:00
|
|
|
my $pdata = 'lemonldappdata=' . expectCookie( $res, 'lemonldappdata' );
|
2017-10-31 09:53:33 +01:00
|
|
|
my $tmp;
|
|
|
|
( $host, $tmp, $query ) =
|
|
|
|
expectForm( $res, undef, '/upgradesession', 'confirm' );
|
|
|
|
ok( $res->[2]->[0] =~ /trspan="askToRenew"/, 'Propose to renew session' );
|
|
|
|
ok(
|
|
|
|
$res = $issuer->_post(
|
|
|
|
'/upgradesession',
|
|
|
|
IO::String->new($query),
|
|
|
|
accept => 'text/html',
|
|
|
|
length => length($query),
|
2019-10-01 11:18:20 +02:00
|
|
|
cookie => "lemonldap=$idpId;$pdata",
|
2017-10-31 09:53:33 +01:00
|
|
|
),
|
|
|
|
'Ask to renew'
|
|
|
|
);
|
|
|
|
( $host, $tmp, $query ) =
|
|
|
|
expectForm( $res, '#', undef, 'upgrading', 'url' );
|
2018-07-04 22:54:09 +02:00
|
|
|
$pdata = 'lemonldappdata=' . expectCookie( $res, 'lemonldappdata' );
|
2017-10-31 09:53:33 +01:00
|
|
|
$query =~ s/user=[^&]*//;
|
|
|
|
$query =~ s/^&//;
|
|
|
|
$query =~ s/&$//;
|
|
|
|
$query =~ s/&&/&/;
|
|
|
|
$query .= '&user=dwho&password=dwho';
|
|
|
|
ok(
|
|
|
|
$res = $issuer->_post(
|
|
|
|
'/upgradesession',
|
|
|
|
IO::String->new($query),
|
|
|
|
accept => 'text/html',
|
|
|
|
length => length($query),
|
2019-10-01 11:18:20 +02:00
|
|
|
cookie => "lemonldap=$idpId;$pdata",
|
2017-10-31 09:53:33 +01:00
|
|
|
),
|
|
|
|
'Re auth'
|
|
|
|
);
|
2018-07-04 22:54:09 +02:00
|
|
|
$pdata = 'lemonldappdata=' . expectCookie( $res, 'lemonldappdata' );
|
2019-07-02 20:03:40 +02:00
|
|
|
$tmp = expectCookie($res);
|
2017-10-31 09:53:33 +01:00
|
|
|
ok( $tmp ne $idpId, 'Get a new session' );
|
|
|
|
$idpId = $tmp;
|
|
|
|
( $url, $query ) = expectRedirection( $res,
|
2018-07-04 22:54:09 +02:00
|
|
|
qr#http://auth.idp.com(/+saml/singleSignOn)(?:\?(.*))?# );
|
2017-10-31 09:53:33 +01:00
|
|
|
ok(
|
|
|
|
$res = $issuer->_get(
|
|
|
|
$url,
|
|
|
|
query => $query,
|
|
|
|
accept => 'text/html',
|
2019-10-01 11:18:20 +02:00
|
|
|
cookie => "lemonldap=$idpId;$pdata",
|
2017-10-31 09:53:33 +01:00
|
|
|
),
|
|
|
|
'Follow redirection'
|
|
|
|
);
|
|
|
|
( $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),
|
|
|
|
),
|
|
|
|
'Post SAML response to SP'
|
|
|
|
);
|
|
|
|
|
|
|
|
# Verify authentication on SP
|
|
|
|
my $spId = expectCookie($res);
|
|
|
|
expectRedirection( $res, 'http://auth.sp.com' );
|
|
|
|
|
|
|
|
clean_sessions();
|
|
|
|
}
|
|
|
|
|
2018-02-08 21:55:21 +01:00
|
|
|
count($maintests);
|
2017-10-31 09:53:33 +01:00
|
|
|
done_testing( count() );
|
|
|
|
|
|
|
|
sub issuer {
|
2019-02-07 09:27:56 +01:00
|
|
|
return LLNG::Manager::Test->new( {
|
2017-10-31 09:53:33 +01:00
|
|
|
ini => {
|
|
|
|
logLevel => $debug,
|
|
|
|
domain => 'idp.com',
|
|
|
|
portal => 'http://auth.idp.com',
|
|
|
|
authentication => 'Choice',
|
|
|
|
userDB => 'Same',
|
2019-11-04 14:54:41 +01:00
|
|
|
portalForceAuthnInterval => 5,
|
2017-10-31 09:53:33 +01:00
|
|
|
authChoiceParam => 'test',
|
|
|
|
authChoiceModules => {
|
|
|
|
demo => 'Demo;Demo;Demo',
|
|
|
|
sql => 'DBI;DBI;DBI',
|
|
|
|
},
|
2019-08-29 10:04:06 +02:00
|
|
|
dbiAuthChain => "dbi:SQLite:dbname=$userdb",
|
2017-10-31 09:53:33 +01:00
|
|
|
dbiAuthUser => '',
|
|
|
|
dbiAuthPassword => '',
|
|
|
|
dbiAuthTable => 'users',
|
|
|
|
dbiAuthLoginCol => 'user',
|
|
|
|
dbiAuthPasswordCol => 'password',
|
|
|
|
dbiAuthPasswordHash => '',
|
|
|
|
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/",
|
2020-02-05 20:05:20 +01:00
|
|
|
samlServicePrivateKeyEnc => saml_key_idp_private_enc,
|
|
|
|
samlServicePrivateKeySig => saml_key_idp_private_sig,
|
|
|
|
samlServicePublicKeyEnc => saml_key_idp_public_enc,
|
|
|
|
samlServicePublicKeySig => saml_key_idp_public_sig,
|
2017-10-31 09:53:33 +01:00
|
|
|
samlSPMetaDataXML => {
|
|
|
|
"sp.com" => {
|
|
|
|
samlSPMetaDataXML =>
|
|
|
|
samlSPMetaDataXML( 'sp', 'HTTP-POST' )
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub sp {
|
2019-02-07 09:27:56 +01:00
|
|
|
return LLNG::Manager::Test->new( {
|
2017-10-31 09:53:33 +01:00
|
|
|
ini => {
|
|
|
|
logLevel => $debug,
|
|
|
|
domain => 'sp.com',
|
|
|
|
portal => 'http://auth.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 => 'post',
|
|
|
|
samlIDPMetaDataOptionsSLOBinding => 'post',
|
|
|
|
samlIDPMetaDataOptionsSignSSOMessage => 1,
|
|
|
|
samlIDPMetaDataOptionsSignSLOMessage => 1,
|
|
|
|
samlIDPMetaDataOptionsCheckSSOMessageSignature => 1,
|
|
|
|
samlIDPMetaDataOptionsCheckSLOMessageSignature => 1,
|
|
|
|
samlIDPMetaDataOptionsForceUTF8 => 1,
|
|
|
|
samlIDPMetaDataOptionsForceAuthn => 1,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
samlIDPMetaDataExportedAttributes => {
|
|
|
|
idp => {
|
|
|
|
"uid" => "0;uid;;",
|
|
|
|
"cn" => "1;cn;;",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
samlIDPMetaDataXML => {
|
|
|
|
idp => {
|
|
|
|
samlIDPMetaDataXML =>
|
|
|
|
samlIDPMetaDataXML( 'idp', 'HTTP-POST' )
|
|
|
|
}
|
|
|
|
},
|
|
|
|
samlOrganizationDisplayName => "SP",
|
|
|
|
samlOrganizationName => "SP",
|
|
|
|
samlOrganizationURL => "http://www.sp.com",
|
2020-02-05 20:05:20 +01:00
|
|
|
samlServicePublicKeySig => saml_key_sp_public_sig,
|
|
|
|
samlServicePrivateKeyEnc => saml_key_sp_private_enc,
|
|
|
|
samlServicePrivateKeySig => saml_key_sp_private_sig,
|
|
|
|
samlServicePublicKeyEnc => saml_key_sp_public_enc,
|
2017-10-31 09:53:33 +01:00
|
|
|
samlSPSSODescriptorAuthnRequestsSigned => 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|