lemonldap-ng/lemonldap-ng-portal/t/30-SAML-SP-rule.t

192 lines
6.6 KiB
Perl
Raw Normal View History

2018-09-05 22:24:23 +02:00
use lib 'inc';
2017-03-22 21:20:30 +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;
2017-03-22 21:20:30 +01: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 = 4;
2017-03-22 21:20:30 +01:00
my $debug = 'error';
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, [], [] ];
}
);
2017-03-22 21:20:30 +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 );
2017-03-22 21:20:30 +01:00
# Simple SP access
my $res;
ok(
$res = $sp->_get(
'/',
accept => 'text/html',
query => 'url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw=='
),
'Unauth SP request'
);
2018-07-18 08:02:48 +02:00
my ( $url, $query ) = expectRedirection( $res,
2017-03-22 21:20:30 +01:00
qr#^http://auth.idp.com(/saml/singleSignOn)\?(SAMLRequest=.+)# );
# Push SAML request to IdP
switch ('issuer');
ok(
$res = $issuer->_get(
$url,
query => $query,
accept => 'text/html',
),
'Launch SAML request to IdP'
);
expectOK($res);
2018-07-05 23:00:40 +02:00
my $pdata = 'lemonldappdata=' . expectCookie( $res, 'lemonldappdata' );
2017-03-22 21:20:30 +01:00
# Try to authenticate to IdP with unautorizated user
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';
use URI::Escape;
$query =
join( '&', map { "$_=" . uri_escape( $fields{$_} ) } keys %fields );
ok(
$res = $issuer->_post(
$url,
IO::String->new($query),
length => length($query),
cookie => $pdata,
2017-03-22 21:20:30 +01:00
accept => 'text/html',
),
'Post authentication'
);
expectOK($res);
my $idpId = expectCookie($res);
ok( $res->[2]->[0] =~ /trmsg="84"/, 'Reject reason is 84' );
2017-03-22 21:20:30 +01:00
}
count($maintests);
clean_sessions();
done_testing( count() );
sub issuer {
2019-02-07 09:27:56 +01:00
return LLNG::Manager::Test->new( {
2017-03-22 21:20:30 +01:00
ini => {
logLevel => $debug,
domain => 'idp.com',
portal => 'http://auth.idp.com',
authentication => 'Demo',
userDB => 'Same',
issuerDBSAMLActivation => 1,
2019-02-06 22:54:41 +01:00
issuerDBSAMLRule => '$uid eq "french"',
2017-03-22 21:20:30 +01:00
samlSPMetaDataOptions => {
'sp.com' => {
samlSPMetaDataOptionsEncryptionMode => 'none',
samlSPMetaDataOptionsSignSSOMessage => 1,
samlSPMetaDataOptionsSignSLOMessage => 1,
samlSPMetaDataOptionsCheckSSOMessageSignature => 1,
samlSPMetaDataOptionsCheckSLOMessageSignature => 1,
samlSPMetaDataOptionsRule => '$uid eq "dwho"',
2017-03-22 21:20:30 +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 => {
2017-03-22 21:20:30 +01:00
"sp.com" => {
samlSPMetaDataXML =>
samlSPMetaDataXML( 'sp', 'HTTP-Redirect' )
},
},
}
}
);
}
sub sp {
2019-02-07 09:27:56 +01:00
return LLNG::Manager::Test->new( {
2017-03-22 21:20:30 +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 => 'redirect',
samlIDPMetaDataOptionsSLOBinding => 'redirect',
samlIDPMetaDataOptionsSignSSOMessage => 1,
samlIDPMetaDataOptionsSignSLOMessage => 1,
samlIDPMetaDataOptionsCheckSSOMessageSignature => 1,
samlIDPMetaDataOptionsCheckSLOMessageSignature => 1,
samlIDPMetaDataOptionsForceUTF8 => 1,
}
},
samlIDPMetaDataExportedAttributes => {
idp => {
"uid" => "0;uid;;",
"cn" => "1;cn;;",
},
},
samlIDPMetaDataXML => {
idp => {
samlIDPMetaDataXML =>
samlIDPMetaDataXML( 'idp', 'HTTP-Redirect' )
}
},
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,
2017-03-22 21:20:30 +01:00
samlSPSSODescriptorAuthnRequestsSigned => 1,
},
}
);
}