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

213 lines
5.9 KiB
Perl
Raw Normal View History

2018-09-05 22:24:23 +02:00
use lib 'inc';
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 LWP::UserAgent;
2018-09-05 22:24:23 +02:00
use LWP::Protocol::PSGI;
2017-01-05 16:19:57 +01:00
use MIME::Base64;
BEGIN {
require 't/test-lib.pm';
}
2020-02-05 17:12:07 +01:00
my $maintests = 12;
2017-03-21 17:06:44 +01:00
my $debug = 'error';
2017-01-05 16:19:57 +01:00
my ( $issuer, $sp, $res );
LWP::Protocol::PSGI->register(
sub {
my $req = Plack::Request->new(@_);
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'),
accept => 'text/plain',
),
' Execute request'
);
}
else {
ok( $res = $issuer->_get( $url, accept => 'text/plain', ),
' Execute post request' );
}
expectOK($res);
count(1);
return $res;
}
);
2017-01-05 16:19:57 +01:00
SKIP: {
eval { require Net::OpenID::Consumer; require Net::OpenID::Server; };
if ($@) {
skip 'Net::OpenID::* notfound', $maintests;
}
2020-02-20 23:34:02 +01:00
$issuer = register( 'issuer', \&issuer );
2020-02-05 17:12:07 +01:00
2020-02-20 23:34:02 +01:00
$sp = register( 'sp', \&sp );
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 .=
2019-02-10 22:29:50 +01:00
'&openid_identifier=http%3A%2F%2Fauth.idp.com%2Fopenidserver%2Ffrench';
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);
2018-07-05 23:00:40 +02:00
my $pdata = 'lemonldappdata=' . expectCookie( $res, 'lemonldappdata' );
( $host, $tmp, $query ) = expectForm( $res, '#', undef );
2019-02-10 22:29:50 +01:00
$query .= '&user=french&password=french';
# Try to authenticate with an unauthorized user
ok(
$res = $issuer->_post(
$uri, IO::String->new($query),
length => length($query),
accept => 'text/html',
cookie => $pdata,
),
'Try to authenticate'
);
ok( $res->[2]->[0] =~ /trmsg="91"/, 'Reject reason is 91' )
2019-03-07 18:22:16 +01:00
or print STDERR Dumper( $res->[2]->[0] );
2019-02-10 22:29:50 +01:00
count(1);
# Simple SP access
ok(
$res = $sp->_get(
'/', accept => 'text/html',
),
'Unauth SP request'
);
( $host, $url, $query ) = expectForm( $res, '#', undef );
ok( $res->[2]->[0] =~ /name="openid_identifier"/,
' Ask for OpenID identity' );
$query .=
'&openid_identifier=http%3A%2F%2Fauth.idp.com%2Fopenidserver%2Fdwho';
ok(
$res = $sp->_post(
'/', IO::String->new($query),
length => length($query),
accept => 'text/html',
),
'Post OpenID identity'
);
( $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);
$pdata = 'lemonldappdata=' . expectCookie( $res, 'lemonldappdata' );
( $host, $tmp, $query ) = expectForm( $res, '#', undef );
2017-01-05 22:45:34 +01:00
$query .= '&user=dwho&password=dwho';
2019-02-10 22:29:50 +01:00
# Try to authenticate with an authorized user
2017-01-05 22:45:34 +01:00
ok(
$res = $issuer->_post(
$uri, IO::String->new($query),
length => length($query),
accept => 'text/html',
cookie => $pdata,
2017-01-05 22:45:34 +01:00
),
'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 issuer {
2019-02-07 09:27:56 +01:00
return LLNG::Manager::Test->new( {
2017-01-05 16:19:57 +01:00
ini => {
2017-01-05 17:44:16 +01:00
logLevel => $debug,
domain => 'idp.com',
portal => 'http://auth.idp.com',
authentication => 'Demo',
userDB => 'Same',
2017-01-05 17:44:16 +01:00
issuerDBOpenIDActivation => 1,
issuerDBOpenIDRule => '$uid eq "dwho"',
2017-01-05 16:19:57 +01:00
}
}
);
}
sub sp {
2019-02-07 09:27:56 +01:00
return LLNG::Manager::Test->new( {
2017-01-05 16:19:57 +01:00
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
},
}
);
}