2018-09-05 22:24:23 +02:00
|
|
|
use lib 'inc';
|
2017-04-13 09:28:15 +02:00
|
|
|
use Test::More; # skip_all => 'CAS is in rebuild';
|
2016-12-20 12:53:33 +01:00
|
|
|
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;
|
2016-12-20 12:53:33 +01:00
|
|
|
use MIME::Base64;
|
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
require 't/test-lib.pm';
|
|
|
|
}
|
|
|
|
|
2017-04-14 09:40:01 +02:00
|
|
|
my $debug = 'error';
|
2016-12-20 12:53:33 +01:00
|
|
|
my ( $issuer, $sp, $res );
|
|
|
|
|
2019-07-02 08:55:56 +02:00
|
|
|
eval { require XML::Simple };
|
|
|
|
plan skip_all => "Missing dependencies: $@" if ($@);
|
|
|
|
|
2018-04-09 22:56:14 +02:00
|
|
|
# Redefine LWP methods for tests
|
|
|
|
LWP::Protocol::PSGI->register(
|
|
|
|
sub {
|
|
|
|
my $req = Plack::Request->new(@_);
|
|
|
|
ok( $req->uri =~ m#http://auth.((?:id|s)p).com([^\?]*)(?:\?(.*))?$#,
|
|
|
|
'SOAP request' );
|
|
|
|
my $host = $1;
|
|
|
|
my $url = $2;
|
|
|
|
my $query = $3;
|
|
|
|
my $res;
|
|
|
|
my $client = ( $host eq 'idp' ? $issuer : $sp );
|
|
|
|
if ( $req->method eq 'POST' ) {
|
|
|
|
my $s = $req->content;
|
|
|
|
ok(
|
|
|
|
$res = $client->_post(
|
|
|
|
$url, IO::String->new($s),
|
|
|
|
length => length($s),
|
|
|
|
query => $query,
|
|
|
|
type => 'application/xml',
|
|
|
|
),
|
|
|
|
"Execute POST request to $url"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ok(
|
|
|
|
$res = $client->_get(
|
|
|
|
$url,
|
|
|
|
type => 'application/xml',
|
|
|
|
query => $query,
|
|
|
|
),
|
|
|
|
"Execute request to $url"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
expectOK($res);
|
|
|
|
ok( getHeader( $res, 'Content-Type' ) =~ m#xml#, 'Content is XML' )
|
|
|
|
or explain( $res->[1], 'Content-Type => application/xml' );
|
|
|
|
count(3);
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
);
|
2016-12-21 19:06:23 +01:00
|
|
|
|
2020-02-05 17:12:07 +01:00
|
|
|
$issuer = register ( 'issuer', \&issuer );
|
|
|
|
$sp = register ('sp', \&sp );
|
2017-03-29 21:50:43 +02:00
|
|
|
|
|
|
|
# Simple SP access
|
|
|
|
ok(
|
|
|
|
$res = $sp->_get(
|
|
|
|
'/', accept => 'text/html',
|
|
|
|
),
|
|
|
|
'Unauth SP request'
|
|
|
|
);
|
|
|
|
count(1);
|
2017-04-16 11:47:32 +02:00
|
|
|
ok( expectCookie( $res, 'llngcasserver' ) eq 'idp', 'Get CAS server cookie' );
|
|
|
|
count(1);
|
2017-03-29 21:50:43 +02:00
|
|
|
expectRedirection( $res,
|
|
|
|
'http://auth.idp.com/cas/login?service=http%3A%2F%2Fauth.sp.com%2F' );
|
|
|
|
|
|
|
|
# Query IdP
|
|
|
|
switch ('issuer');
|
|
|
|
ok(
|
|
|
|
$res = $issuer->_get(
|
|
|
|
'/cas/login',
|
|
|
|
query => 'service=http://auth.sp.com/',
|
|
|
|
accept => 'text/html'
|
|
|
|
),
|
|
|
|
'Query CAS server'
|
|
|
|
);
|
|
|
|
count(1);
|
|
|
|
expectOK($res);
|
2018-07-05 23:00:40 +02:00
|
|
|
my $pdata = 'lemonldappdata=' . expectCookie( $res, 'lemonldappdata' );
|
2017-03-29 21:50:43 +02:00
|
|
|
|
|
|
|
# Try to authenticate to IdP
|
|
|
|
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;
|
|
|
|
my $s = join( '&', map { "$_=" . uri_escape( $fields{$_} ) } keys %fields );
|
|
|
|
ok(
|
|
|
|
$res = $issuer->_post(
|
|
|
|
'/cas/login',
|
|
|
|
IO::String->new($s),
|
2018-07-04 22:54:09 +02:00
|
|
|
cookie => $pdata,
|
2017-03-29 21:50:43 +02:00
|
|
|
accept => 'text/html',
|
|
|
|
length => length($s),
|
|
|
|
),
|
|
|
|
'Post authentication'
|
|
|
|
);
|
|
|
|
count(1);
|
2018-07-04 22:54:09 +02:00
|
|
|
my $idpId = expectCookie($res);
|
2019-04-03 19:15:56 +02:00
|
|
|
|
|
|
|
# Expect pdata to be cleared
|
|
|
|
$pdata = expectCookie( $res, 'lemonldappdata' );
|
|
|
|
ok( $pdata !~ 'issuerRequestsaml', 'SAML request cleared from pdata' );
|
|
|
|
count(1);
|
|
|
|
|
2017-03-29 21:50:43 +02:00
|
|
|
my ($query) =
|
|
|
|
expectRedirection( $res, qr#^http://auth.sp.com/\?(ticket=[^&]+)$# );
|
|
|
|
|
|
|
|
# Back to SP
|
|
|
|
switch ('sp');
|
2017-04-16 11:47:32 +02:00
|
|
|
ok(
|
|
|
|
$res = $sp->_get(
|
|
|
|
'/',
|
|
|
|
query => $query,
|
|
|
|
accept => 'text/html',
|
|
|
|
cookie => 'llngcasserver=idp',
|
|
|
|
),
|
|
|
|
'Query SP with ticket'
|
|
|
|
);
|
2017-03-29 21:50:43 +02:00
|
|
|
count(1);
|
|
|
|
my $spId = expectCookie($res);
|
|
|
|
|
|
|
|
# Test authentication
|
2017-04-16 11:47:32 +02:00
|
|
|
ok( $res = $sp->_get( '/', cookie => "lemonldap=$spId,llngcasserver=idp" ),
|
|
|
|
'Get / on SP' );
|
2017-03-29 21:50:43 +02:00
|
|
|
count(1);
|
|
|
|
expectOK($res);
|
|
|
|
expectAuthenticatedAs( $res, 'french' );
|
|
|
|
|
2017-04-13 21:36:25 +02:00
|
|
|
# Test attributes
|
|
|
|
ok( $res = $sp->_get("/sessions/global/$spId"), 'Get UTF-8' );
|
|
|
|
expectOK($res);
|
|
|
|
ok( $res = eval { JSON::from_json( $res->[2]->[0] ) }, ' GET JSON' )
|
2017-04-16 11:47:32 +02:00
|
|
|
or print STDERR $@;
|
2017-04-13 21:36:25 +02:00
|
|
|
ok( $res->{cn} eq 'Frédéric Accents', 'UTF-8 values' )
|
2017-04-16 11:47:32 +02:00
|
|
|
or explain( $res, 'cn => Frédéric Accents' );
|
2017-04-13 21:36:25 +02:00
|
|
|
count(3);
|
|
|
|
|
2017-03-29 21:50:43 +02:00
|
|
|
# Logout initiated by SP
|
|
|
|
ok(
|
|
|
|
$res = $sp->_get(
|
|
|
|
'/',
|
|
|
|
query => 'logout',
|
2017-04-16 11:47:32 +02:00
|
|
|
cookie => "lemonldap=$spId,llngcasserver=idp",
|
2017-03-29 21:50:43 +02:00
|
|
|
accept => 'text/html'
|
|
|
|
),
|
|
|
|
'Query SP for logout'
|
|
|
|
);
|
|
|
|
count(1);
|
|
|
|
expectOK($res);
|
|
|
|
ok(
|
|
|
|
$res->[2]->[0] =~ m#iframe src="http://auth.idp.com(/cas/logout)\?(.+?)"#s,
|
|
|
|
'Found iframe'
|
|
|
|
);
|
|
|
|
count(1);
|
|
|
|
|
|
|
|
# Query IdP with iframe src
|
|
|
|
my $url = $1;
|
|
|
|
$query = $2;
|
|
|
|
ok( getHeader( $res, 'Content-Security-Policy' ) =~ /child-src auth.idp.com/,
|
2019-09-18 20:58:51 +02:00
|
|
|
'Frame is authorized' )
|
2017-03-29 21:50:43 +02:00
|
|
|
or
|
|
|
|
explain( $res->[1], 'Content-Security-Policy => ...child-src auth.idp.com' );
|
|
|
|
count(1);
|
|
|
|
|
|
|
|
switch ('issuer');
|
|
|
|
ok(
|
|
|
|
$res = $issuer->_get(
|
|
|
|
$url,
|
|
|
|
query => $query,
|
|
|
|
accept => 'text/html',
|
|
|
|
cookie => "lemonldap=$idpId"
|
|
|
|
),
|
|
|
|
'Get iframe from IdP'
|
|
|
|
);
|
|
|
|
count(1);
|
2017-09-11 17:26:44 +02:00
|
|
|
expectRedirection( $res, 'http://auth.sp.com/?logout' );
|
2018-04-09 22:56:14 +02:00
|
|
|
my $h = getHeader( $res, 'Content-Security-Policy' );
|
|
|
|
ok( ( not $h or $h !~ /frame-ancestors/ ), ' Frame can be embedded' )
|
2017-03-29 21:50:43 +02:00
|
|
|
or explain( $res->[1],
|
|
|
|
'Content-Security-Policy does not contain a frame-ancestors' );
|
|
|
|
count(1);
|
|
|
|
|
|
|
|
# Verify that user has been disconnected
|
|
|
|
ok( $res = $issuer->_get( '/', cookie => "lemonldap=$idpId" ), 'Query IdP' );
|
|
|
|
count(1);
|
|
|
|
expectReject($res);
|
|
|
|
|
2017-03-29 21:54:54 +02:00
|
|
|
switch ('sp');
|
|
|
|
ok(
|
2017-04-16 11:47:32 +02:00
|
|
|
$res = $sp->_get(
|
|
|
|
'/',
|
|
|
|
accept => 'text/html',
|
|
|
|
cookie => "lemonldap=$idpId,llngcasserver=idp"
|
|
|
|
),
|
2017-03-29 21:54:54 +02:00
|
|
|
'Query IdP'
|
|
|
|
);
|
|
|
|
count(1);
|
|
|
|
expectRedirection( $res,
|
|
|
|
'http://auth.idp.com/cas/login?service=http%3A%2F%2Fauth.sp.com%2F' );
|
2016-12-20 12:53:33 +01:00
|
|
|
|
|
|
|
clean_sessions();
|
|
|
|
done_testing( count() );
|
|
|
|
|
|
|
|
sub issuer {
|
2019-02-07 09:27:56 +01:00
|
|
|
return LLNG::Manager::Test->new( {
|
2016-12-20 12:53:33 +01:00
|
|
|
ini => {
|
|
|
|
logLevel => $debug,
|
|
|
|
domain => 'idp.com',
|
|
|
|
portal => 'http://auth.idp.com',
|
|
|
|
authentication => 'Demo',
|
2017-02-05 10:13:20 +01:00
|
|
|
userDB => 'Same',
|
2016-12-20 12:53:33 +01:00
|
|
|
issuerDBCASActivation => 1,
|
|
|
|
casAttr => 'uid',
|
|
|
|
casAttributes => { cn => 'cn', uid => 'uid', },
|
|
|
|
casAccessControlPolicy => 'none',
|
2016-12-22 06:57:44 +01:00
|
|
|
multiValuesSeparator => ';',
|
2016-12-20 12:53:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub sp {
|
2019-02-07 09:27:56 +01:00
|
|
|
return LLNG::Manager::Test->new( {
|
2016-12-20 12:53:33 +01:00
|
|
|
ini => {
|
2017-06-23 11:57:07 +02:00
|
|
|
logLevel => $debug,
|
|
|
|
domain => 'sp.com',
|
|
|
|
portal => 'http://auth.sp.com',
|
|
|
|
authentication => 'CAS',
|
|
|
|
userDB => 'CAS',
|
|
|
|
restSessionServer => 1,
|
|
|
|
issuerDBCASActivation => 0,
|
|
|
|
multiValuesSeparator => ';',
|
2017-04-13 09:28:15 +02:00
|
|
|
casSrvMetaDataExportedVars => {
|
|
|
|
idp => {
|
|
|
|
cn => 'cn',
|
|
|
|
mail => 'mail',
|
|
|
|
uid => 'uid',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
casSrvMetaDataOptions => {
|
|
|
|
idp => {
|
|
|
|
casSrvMetaDataOptionsUrl => 'http://auth.idp.com/cas',
|
|
|
|
casSrvMetaDataOptionsGateway => 0,
|
|
|
|
}
|
|
|
|
},
|
2016-12-20 12:53:33 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|