lemonldap-ng/lemonldap-ng-portal/t/31-Auth-and-issuer-CAS.t

213 lines
5.9 KiB
Perl
Raw Normal View History

2016-12-20 12:53:33 +01:00
use Test::More;
use strict;
use IO::String;
use MIME::Base64;
BEGIN {
require 't/test-lib.pm';
}
2017-01-20 07:19:54 +01:00
my $maintests = 14;
2016-12-22 23:06:13 +01:00
my $debug = 'error';
2016-12-20 12:53:33 +01:00
my ( $issuer, $sp, $res );
my %handlerOR = ( issuer => [], sp => [] );
SKIP: {
2016-12-21 23:39:12 +01:00
no warnings 'redefine';
eval q#use AuthCAS#;
2016-12-20 12:53:33 +01:00
if ($@) {
skip 'AuthCAS not found', $maintests;
}
2016-12-22 23:06:13 +01:00
no warnings 'once';
2016-12-21 23:39:12 +01:00
*AuthCAS::get_https2 = *mygethttps2;
2016-12-21 06:32:38 +01:00
ok( $issuer = issuer(), 'Issuer portal' );
$handlerOR{issuer} = \@Lemonldap::NG::Handler::Main::Reload::_onReload;
2016-12-20 12:53:33 +01:00
switch ('sp');
ok( $sp = sp(), 'SP portal' );
$handlerOR{sp} = \@Lemonldap::NG::Handler::Main::Reload::_onReload;
2016-12-21 07:19:41 +01:00
# Simple SP access
my $res;
ok(
$res = $sp->_get(
'/', accept => 'text/html',
),
'Unauth SP request'
);
2016-12-26 10:23:35 +01:00
expectRedirection( $res,
'http://auth.idp.com/cas/login?service=http://auth.sp.com/' );
2016-12-21 19:06:23 +01:00
# Query IdP
switch ('issuer');
ok(
$res = $issuer->_get(
'/cas/login',
query => 'service=http://auth.sp.com/',
accept => 'text/html'
),
'Query CAS server'
);
2016-12-23 11:02:21 +01:00
expectOK($res);
2016-12-21 19:06:23 +01: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} = 'dwho';
use URI::Escape;
my $s = join( '&', map { "$_=" . uri_escape( $fields{$_} ) } keys %fields );
ok(
$res = $issuer->_post(
'/cas/login',
IO::String->new($s),
accept => 'text/html',
length => length($s),
),
'Post authentication'
);
2016-12-26 10:23:35 +01:00
my ($query) =
expectRedirection( $res, qr#^http://auth.sp.com/\?(ticket=[^&]+)$# );
2016-12-23 11:02:21 +01:00
my $idpId = expectCookie($res);
2016-12-21 19:06:23 +01:00
# Back to SP
switch ('sp');
2016-12-21 23:39:12 +01:00
ok( $res = $sp->_get( '/', query => $query, accept => 'text/html' ),
2016-12-21 19:06:23 +01:00
'Query SP with ticket' );
2016-12-23 11:02:21 +01:00
my $spId = expectCookie($res);
2016-12-22 09:40:50 +01:00
# Test authentication
ok( $res = $sp->_get( '/', cookie => "lemonldap=$spId" ), 'Get / on SP' );
2016-12-23 11:02:21 +01:00
expectOK($res);
2016-12-26 10:23:35 +01:00
expectAuthenticatedAs( $res, 'dwho' );
2016-12-21 19:06:23 +01:00
2016-12-22 09:40:50 +01:00
# Logout initiated by SP
ok(
$res = $sp->_get(
'/',
query => 'logout',
cookie => "lemonldap=$spId",
accept => 'text/html'
),
'Query SP for logout'
);
2016-12-23 11:02:21 +01:00
expectOK($res);
ok(
$res->[2]->[0] =~
m#iframe src="http://auth.idp.com(/cas/logout)\?(.+?)"#s,
'Found iframe'
);
# Query IdP with iframe src
2016-12-26 10:23:35 +01:00
my $url = $1;
$query = $2;
2017-01-19 23:23:13 +01:00
ok(
getHeader( $res, 'Content-Security-Policy' ) =~
2017-01-21 10:17:24 +01:00
/child-src auth.idp.com/,
2017-01-19 23:23:13 +01:00
'Frame is authorizated'
)
or explain( $res->[1],
2017-01-21 10:17:24 +01:00
'Content-Security-Policy => ...child-src auth.idp.com' );
2017-01-19 23:23:13 +01:00
switch ('issuer');
ok(
$res = $issuer->_get(
2017-01-19 23:23:13 +01:00
$url,
query => $query,
accept => 'text/html',
cookie => "lemonldap=$idpId"
),
'Get iframe from IdP'
);
2016-12-23 11:02:21 +01:00
expectOK($res);
2017-01-21 10:50:59 +01:00
ok( getHeader( $res, 'Content-Security-Policy' ) !~ /frame-ancestors/,
2017-01-21 10:17:24 +01:00
' Frame can be embedded' )
or explain( $res->[1],
2017-01-21 10:50:59 +01:00
'Content-Security-Policy does not contain a frame-ancestors' );
2016-12-22 21:18:59 +01:00
# Verify that user has been disconnected
ok( $res = $issuer->_get( '/', cookie => "lemonldap=$idpId" ),
'Query IdP' );
2016-12-23 11:02:21 +01:00
expectReject($res);
2016-12-22 21:18:59 +01:00
#switch ('sp');
#ok( $res = $sp->_get( '/', cookie => "lemonldap=$idpId" ), 'Query IdP' );
#ok( $res->[0] == 302, 'Return code is 302' ) or explain( $res->[0], 302 );
#ok(
# $sp->getRedirection($res) eq
# 'http://auth.idp.com/cas/login?service=http://auth.sp.com/',
# 'Redirection points to IdP'
# )
# or explain(
# $res->[1],
# 'location => http://auth.idp.com/cas/login?service=http://auth.sp.com/'
# );
2016-12-20 12:53:33 +01:00
}
count($maintests);
clean_sessions();
done_testing( count() );
# Redefine LWP methods for tests
2016-12-21 23:39:12 +01:00
sub mygethttps2 {
my ( $host, $port, $path, $ssl_data ) = @_;
2016-12-22 06:57:44 +01:00
ok( $path =~ m#^(/[^\?]+)(?:\?(.*))?$#, "Path to push to IdP: $path" );
2016-12-21 23:39:12 +01:00
$path = $1;
my $query = $2;
ok( $res = $issuer->_get( $path, query => $query ), 'Execute request' );
2016-12-20 12:53:33 +01:00
ok( ( $res->[0] == 200 or $res->[0] == 400 ), 'Response is 200 or 400' )
or explain( $res->[0], "200 or 400" );
2016-12-21 23:39:12 +01:00
count(2);
my @res = map { "$_\n" } split /\r?\n/, join( '', @{ $res->[2] } );
return ( "200\n", "\n", @res );
2016-12-20 12:53:33 +01:00
}
sub switch {
my $type = shift;
@Lemonldap::NG::Handler::Main::Reload::_onReload = @{
$handlerOR{$type};
};
}
sub issuer {
return LLNG::Manager::Test->new(
{
ini => {
logLevel => $debug,
templatesDir => 'site/htdocs/static',
domain => 'idp.com',
portal => 'http://auth.idp.com',
authentication => 'Demo',
userDB => 'Demo',
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 {
return LLNG::Manager::Test->new(
{
ini => {
logLevel => $debug,
domain => 'sp.com',
portal => 'http://auth.sp.com',
authentication => 'CAS',
userDB => 'Null',
issuerDBCASActivation => 0,
2016-12-29 07:25:07 +01:00
CAS_url => 'http://auth.idp.com/cas',
CAS_CAFile => 't/caFile.pem',
2016-12-22 06:57:44 +01:00
multiValuesSeparator => ';',
2016-12-20 12:53:33 +01:00
},
}
);
}