lemonldap-ng/lemonldap-ng-portal/t/33-Auth-and-issuer-OpenID2.t

178 lines
4.7 KiB
Perl
Raw Normal View History

2017-03-21 17:06:44 +01:00
use Test::More;
2017-01-05 16:19:57 +01:00
use strict;
use IO::String;
use MIME::Base64;
BEGIN {
require 't/test-lib.pm';
}
2017-01-06 07:22:31 +01:00
my $maintests = 9;
2017-03-21 17:06:44 +01:00
my $debug = 'error';
2017-01-05 16:19:57 +01:00
my ( $issuer, $sp, $res );
my %handlerOR = ( issuer => [], sp => [] );
SKIP: {
eval { require Net::OpenID::Consumer; require Net::OpenID::Server; };
if ($@) {
skip 'Net::OpenID::* notfound', $maintests;
}
ok( $issuer = issuer(), 'Issuer portal' );
2017-02-08 23:18:52 +01:00
$handlerOR{issuer} = \@Lemonldap::NG::Handler::Main::_onReload;
2017-01-05 16:19:57 +01:00
switch ('sp');
ok( $sp = sp(), 'SP portal' );
2017-02-08 23:18:52 +01:00
$handlerOR{sp} = \@Lemonldap::NG::Handler::Main::_onReload;
2017-01-05 16:19:57 +01:00
# Simple SP access
my $res;
ok(
$res = $sp->_get(
'/', accept => 'text/html',
),
'Unauth SP request'
);
2017-03-21 17:06:44 +01:00
my ( $host, $url, $query ) = expectForm( $res, '#', undef );
2017-01-05 22:45:34 +01:00
ok( $res->[2]->[0] =~ /name="openid_identifier"/,
' Ask for OpenID identity' );
2017-01-05 17:44:16 +01:00
2017-03-21 17:06:50 +01:00
$query .=
'&openid_identifier=http%3A%2F%2Fauth.idp.com%2Fopenidserver%2Fdwho';
2017-01-05 22:45:34 +01:00
ok(
$res = $sp->_post(
'/', IO::String->new($query),
length => length($query),
accept => 'text/html',
),
'Post OpenID identity'
);
my $uri;
( $uri, $query ) = expectRedirection( $res,
qr#http://auth.idp.com(/openidserver/?)\?(openid.*)$# );
# Follow redirection do IdP
switch ('issuer');
ok( $res = $issuer->_get( $uri, query => $query, accept => 'text/html' ),
'Follow redirection to IdP' );
expectOK($res);
2017-03-21 17:06:50 +01:00
my ($tmp);
( $host, $tmp, $query ) = expectForm( $res, '#', undef );
2017-01-05 22:45:34 +01:00
$query .= '&user=dwho&password=dwho';
# Try to authenticate
ok(
$res = $issuer->_post(
$uri, IO::String->new($query),
length => length($query),
accept => 'text/html'
),
'Try to authenticate'
);
2017-01-06 07:02:54 +01:00
my $idpId = expectCookie($res);
( $host, $tmp, $query ) = expectForm( $res, '#', undef, 'confirm' );
# Confirm
ok(
$res = $issuer->_post(
$uri, IO::String->new($query),
length => length($query),
2017-03-21 17:06:44 +01:00
cookie => "lemonldap=$idpId",
2017-01-06 07:02:54 +01:00
accept => 'text/html'
),
'Confirm choice'
);
2017-01-06 07:22:31 +01:00
($query) = expectRedirection( $res, qr#^http://auth.sp.com/?\?(.*)# );
2017-01-06 07:02:54 +01:00
# Push redirection to SP
switch ('sp');
ok( $res = $sp->_get( '/', query => $query, accept => 'text/html' ),
'Follow redirection to SP' );
2017-01-06 07:22:31 +01:00
my $spId = expectCookie($res);
2017-02-15 07:41:50 +01:00
expectRedirection( $res, qr#^http://auth.sp.com/?$# );
2017-01-05 22:45:34 +01:00
2017-01-06 07:02:54 +01:00
#print STDERR Dumper($res);
2017-01-05 16:19:57 +01:00
}
count($maintests);
clean_sessions();
done_testing( count() );
sub switch {
my $type = shift;
2017-02-08 23:18:52 +01:00
@Lemonldap::NG::Handler::Main::_onReload = @{
2017-01-05 16:19:57 +01:00
$handlerOR{$type};
};
}
2017-01-05 17:44:16 +01:00
no warnings 'redefine';
sub LWP::UserAgent::request {
my ( $self, $req ) = @_;
ok( $req->uri =~ m#http://auth.idp.com(.*)#, ' Request from SP to IdP' );
my $url = $1;
my ($res);
count(1);
if ( $req->method =~ /^post$/i ) {
my $s = $req->content;
ok(
$res = $issuer->_post(
$url, IO::String->new($s),
length => length($s),
type => $req->header('Content-Type'),
2017-01-05 22:45:34 +01:00
accept => 'text/plain',
2017-01-05 17:44:16 +01:00
),
' Execute request'
);
}
else {
2017-01-05 22:45:34 +01:00
ok( $res = $issuer->_get( $url, accept => 'text/plain', ),
' Execute post request' );
2017-01-05 17:44:16 +01:00
}
expectOK($res);
my $httpResp = HTTP::Response->new( $res->[0], 'OK' );
while ( my $name = shift @{ $res->[1] } ) {
$httpResp->header( $name, shift( @{ $res->[1] } ) );
}
2017-01-06 07:02:52 +01:00
$httpResp->request($req);
2017-01-05 17:44:16 +01:00
$httpResp->content( join( '', @{ $res->[2] } ) );
2017-01-05 22:45:34 +01:00
count(1);
2017-01-05 17:44:16 +01:00
return $httpResp;
}
2017-01-05 16:19:57 +01:00
sub issuer {
return LLNG::Manager::Test->new(
{
ini => {
2017-01-05 17:44:16 +01:00
logLevel => $debug,
templatesDir => 'site/htdocs/static',
domain => 'idp.com',
portal => 'http://auth.idp.com',
authentication => 'Demo',
userDB => 'Same',
2017-01-05 17:44:16 +01:00
issuerDBOpenIDActivation => 1,
2017-01-05 16:19:57 +01:00
}
}
);
}
sub sp {
return LLNG::Manager::Test->new(
{
ini => {
logLevel => $debug,
domain => 'sp.com',
portal => 'http://auth.sp.com',
authentication => 'OpenID',
userDB => 'Same',
2017-01-05 17:44:16 +01:00
openIdSecret => 'qwerty',
2017-01-05 22:45:34 +01:00
exportedVars => {
mail => 'email',
2017-01-06 07:22:31 +01:00
},
openIdIDPList => '0;',
2017-01-05 16:19:57 +01:00
},
}
);
}