2018-09-05 22:24:23 +02:00
|
|
|
use lib 'inc';
|
2017-03-09 22:56:53 +01:00
|
|
|
use Test::More;
|
|
|
|
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;
|
2017-03-09 22:56:53 +01:00
|
|
|
use MIME::Base64;
|
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
require 't/test-lib.pm';
|
2020-02-05 22:44:14 +01:00
|
|
|
require 't/oidc-lib.pm';
|
2017-03-09 22:56:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my $debug = 'error';
|
|
|
|
my ( $op, $rp, $res );
|
|
|
|
|
2018-04-09 22:56:14 +02:00
|
|
|
LWP::Protocol::PSGI->register(
|
|
|
|
sub {
|
|
|
|
my $req = Plack::Request->new(@_);
|
|
|
|
ok( $req->uri =~ m#http://auth.((?:o|r)p).com(.*)#, ' REST request' );
|
|
|
|
my $host = $1;
|
|
|
|
my $url = $2;
|
|
|
|
my ( $res, $client );
|
|
|
|
count(1);
|
|
|
|
if ( $host eq 'op' ) {
|
|
|
|
pass(" Request from RP to OP, endpoint $url");
|
|
|
|
$client = $op;
|
|
|
|
}
|
|
|
|
elsif ( $host eq 'rp' ) {
|
|
|
|
pass(' Request from OP to RP');
|
|
|
|
$client = $rp;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fail(' Aborting REST request (external)');
|
|
|
|
return [ 500, [], [] ];
|
|
|
|
}
|
|
|
|
if ( $req->method =~ /^post$/i ) {
|
|
|
|
my $s = $req->content;
|
|
|
|
ok(
|
|
|
|
$res = $client->_post(
|
|
|
|
$url, IO::String->new($s),
|
|
|
|
length => length($s),
|
|
|
|
type => $req->header('Content-Type'),
|
|
|
|
),
|
|
|
|
' Execute request'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ok(
|
|
|
|
$res = $client->_get(
|
|
|
|
$url,
|
|
|
|
custom => {
|
|
|
|
HTTP_AUTHORIZATION => $req->header('Authorization'),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
' Execute request'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
ok( $res->[0] == 200, ' Response is 200' );
|
|
|
|
ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#,
|
|
|
|
' Content is JSON' )
|
|
|
|
or explain( $res->[1], 'Content-Type => application/json' );
|
|
|
|
count(4);
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-03-09 22:56:53 +01:00
|
|
|
# Initialization
|
|
|
|
ok( $op = op(), 'OP portal' );
|
|
|
|
|
|
|
|
ok( $res = $op->_get('/oauth2/jwks'), 'Get JWKS, endpoint /oauth2/jwks' );
|
|
|
|
expectOK($res);
|
|
|
|
my $jwks = $res->[2]->[0];
|
|
|
|
|
|
|
|
ok(
|
|
|
|
$res = $op->_get('/.well-known/openid-configuration'),
|
|
|
|
'Get metadata, endpoint /.well-known/openid-configuration'
|
|
|
|
);
|
|
|
|
expectOK($res);
|
|
|
|
my $metadata = $res->[2]->[0];
|
|
|
|
count(3);
|
|
|
|
|
|
|
|
switch ('rp');
|
2018-06-05 23:13:18 +02:00
|
|
|
&Lemonldap::NG::Handler::Main::cfgNum( 0, 0 );
|
2017-03-09 22:56:53 +01:00
|
|
|
ok( $rp = rp( $jwks, $metadata ), 'RP portal' );
|
|
|
|
count(1);
|
|
|
|
|
|
|
|
# Query RP for auth
|
|
|
|
ok( $res = $rp->_get( '/', accept => 'text/html' ), 'Unauth SP request' );
|
|
|
|
count(1);
|
|
|
|
my ( $url, $query ) =
|
|
|
|
expectRedirection( $res, qr#http://auth.op.com(/oauth2/authorize)\?(.*)$# );
|
|
|
|
|
|
|
|
# Push request to OP
|
|
|
|
switch ('op');
|
|
|
|
ok( $res = $op->_get( $url, query => $query, accept => 'text/html' ),
|
|
|
|
"Push request to OP, endpoint $url" );
|
|
|
|
count(1);
|
|
|
|
expectOK($res);
|
|
|
|
|
|
|
|
# Try to authenticate to IdP
|
|
|
|
$query = "user=french&password=french&$query";
|
|
|
|
ok(
|
|
|
|
$res = $op->_post(
|
|
|
|
$url,
|
|
|
|
IO::String->new($query),
|
|
|
|
accept => 'text/html',
|
|
|
|
length => length($query),
|
|
|
|
),
|
|
|
|
"Post authentication, endpoint $url"
|
|
|
|
);
|
|
|
|
count(1);
|
|
|
|
my $idpId = expectCookie($res);
|
|
|
|
my ( $host, $tmp );
|
|
|
|
( $host, $tmp, $query ) = expectForm( $res, '#', undef, 'confirm' );
|
|
|
|
|
|
|
|
ok(
|
|
|
|
$res = $op->_post(
|
|
|
|
$url,
|
|
|
|
IO::String->new($query),
|
|
|
|
accept => 'text/html',
|
|
|
|
cookie => "lemonldap=$idpId",
|
|
|
|
length => length($query),
|
|
|
|
),
|
|
|
|
"Post confirmation, endpoint $url"
|
|
|
|
);
|
|
|
|
count(1);
|
|
|
|
|
|
|
|
($query) = expectRedirection( $res, qr#^http://auth.rp.com/?\?(.*)$# );
|
|
|
|
|
|
|
|
# Push OP response to RP
|
|
|
|
switch ('rp');
|
|
|
|
|
|
|
|
ok( $res = $rp->_get( '/', query => $query, accept => 'text/html' ),
|
|
|
|
'Call openidconnectcallback on RP' );
|
|
|
|
count(1);
|
|
|
|
my $spId = expectCookie($res);
|
|
|
|
|
|
|
|
switch ('op');
|
|
|
|
ok(
|
|
|
|
$res = $op->_get( '/oauth2/checksession.html', accept => 'text.html' ),
|
|
|
|
'Check session, endpoint /oauth2/checksession.html'
|
|
|
|
);
|
|
|
|
count(1);
|
|
|
|
expectOK($res);
|
|
|
|
ok( getHeader( $res, 'Content-Security-Policy' ) !~ /frame-ancestors/,
|
|
|
|
' Frame can be embedded' )
|
|
|
|
or explain( $res->[1],
|
|
|
|
'Content-Security-Policy does not contain a frame-ancestors' );
|
|
|
|
count(1);
|
|
|
|
|
|
|
|
# Verify UTF-8
|
|
|
|
switch ('rp');
|
|
|
|
ok( $res = $rp->_get("/sessions/global/$spId"), 'Get UTF-8' );
|
2019-10-25 16:41:34 +02:00
|
|
|
$res = expectJSON($res);
|
2017-03-09 22:56:53 +01:00
|
|
|
ok( $res->{cn} eq 'Frédéric Accents', 'UTF-8 values' )
|
|
|
|
or explain( $res, 'cn => Frédéric Accents' );
|
2019-10-25 16:41:34 +02:00
|
|
|
count(2);
|
2017-03-09 22:56:53 +01:00
|
|
|
|
|
|
|
# Logout initiated by OP
|
|
|
|
switch ('op');
|
|
|
|
ok(
|
|
|
|
$res = $op->_get(
|
|
|
|
'/',
|
|
|
|
query => 'logout',
|
|
|
|
cookie => "lemonldap=$idpId",
|
|
|
|
accept => 'text/html'
|
|
|
|
),
|
|
|
|
'Query SP for logout'
|
|
|
|
);
|
|
|
|
count(1);
|
|
|
|
expectOK($res);
|
|
|
|
ok( $res->[2]->[0] =~ m#<iframe.*?src="http://auth.rp.com/oidc/logout"#,
|
|
|
|
'Found RP logout iframe' );
|
|
|
|
count(1);
|
|
|
|
|
|
|
|
# Test if logout is done
|
|
|
|
ok(
|
|
|
|
$res = $op->_get(
|
|
|
|
'/', cookie => "lemonldap=$idpId",
|
|
|
|
),
|
|
|
|
'Test if user is reject on IdP'
|
|
|
|
);
|
|
|
|
count(1);
|
|
|
|
expectReject($res);
|
|
|
|
|
|
|
|
clean_sessions();
|
|
|
|
done_testing( count() );
|
|
|
|
|
|
|
|
sub op {
|
2019-07-02 20:03:40 +02:00
|
|
|
return LLNG::Manager::Test->new( {
|
2017-03-09 22:56:53 +01:00
|
|
|
ini => {
|
|
|
|
logLevel => $debug,
|
|
|
|
domain => 'idp.com',
|
|
|
|
portal => 'http://auth.op.com',
|
|
|
|
authentication => 'Demo',
|
|
|
|
userDB => 'Same',
|
|
|
|
issuerDBOpenIDConnectActivation => "1",
|
|
|
|
oidcRPMetaDataExportedVars => {
|
|
|
|
rp => {
|
|
|
|
email => "mail",
|
|
|
|
family_name => "cn",
|
|
|
|
name => "cn"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
oidcServiceMetaDataAuthorizeURI => "authorize",
|
|
|
|
oidcServiceMetaDataCheckSessionURI => "checksession.html",
|
|
|
|
oidcServiceMetaDataJWKSURI => "jwks",
|
|
|
|
oidcServiceMetaDataEndSessionURI => "logout",
|
|
|
|
oidcServiceMetaDataRegistrationURI => "register",
|
|
|
|
oidcServiceMetaDataTokenURI => "token",
|
|
|
|
oidcServiceMetaDataUserInfoURI => "userinfo",
|
|
|
|
oidcServiceAllowHybridFlow => 1,
|
|
|
|
oidcServiceAllowImplicitFlow => 1,
|
|
|
|
oidcServiceAllowDynamicRegistration => 1,
|
|
|
|
oidcServiceAllowAuthorizationCodeFlow => 1,
|
|
|
|
oidcRPMetaDataOptions => {
|
|
|
|
rp => {
|
|
|
|
oidcRPMetaDataOptionsDisplayName => "RP",
|
|
|
|
oidcRPMetaDataOptionsIDTokenExpiration => 3600,
|
|
|
|
oidcRPMetaDataOptionsClientID => "rpid",
|
|
|
|
oidcRPMetaDataOptionsIDTokenSignAlg => "HS512",
|
|
|
|
oidcRPMetaDataOptionsBypassConsent => 0,
|
|
|
|
oidcRPMetaDataOptionsClientSecret => "rpsecret",
|
|
|
|
oidcRPMetaDataOptionsUserIDAttr => "",
|
|
|
|
oidcRPMetaDataOptionsAccessTokenExpiration => 3600,
|
|
|
|
oidcRPMetaDataOptionsLogoutUrl =>
|
|
|
|
'http://auth.rp.com/oidc/logout',
|
|
|
|
oidcRPMetaDataOptionsLogoutType => 'front',
|
|
|
|
oidcRPMetaDataOptionsLogoutSessionRequired => 0,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
oidcOPMetaDataOptions => {},
|
|
|
|
oidcOPMetaDataJSON => {},
|
|
|
|
oidcOPMetaDataJWKS => {},
|
|
|
|
oidcServiceMetaDataAuthnContext => {
|
|
|
|
'loa-4' => 4,
|
|
|
|
'loa-1' => 1,
|
|
|
|
'loa-5' => 5,
|
|
|
|
'loa-2' => 2,
|
|
|
|
'loa-3' => 3
|
|
|
|
},
|
2020-02-05 22:44:14 +01:00
|
|
|
oidcServicePrivateKeySig => oidc_key_op_private_sig,
|
|
|
|
oidcServicePublicKeySig => oidc_key_op_public_sig,
|
2017-03-09 22:56:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rp {
|
|
|
|
my ( $jwks, $metadata ) = @_;
|
2019-07-02 20:03:40 +02:00
|
|
|
return LLNG::Manager::Test->new( {
|
2017-03-09 22:56:53 +01:00
|
|
|
ini => {
|
|
|
|
logLevel => $debug,
|
|
|
|
domain => 'rp.com',
|
|
|
|
portal => 'http://auth.rp.com',
|
|
|
|
authentication => 'OpenIDConnect',
|
|
|
|
userDB => 'Same',
|
|
|
|
restSessionServer => 1,
|
|
|
|
oidcOPMetaDataExportedVars => {
|
|
|
|
op => {
|
|
|
|
cn => "name",
|
|
|
|
uid => "sub",
|
|
|
|
sn => "family_name",
|
|
|
|
mail => "email"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
oidcOPMetaDataOptions => {
|
|
|
|
op => {
|
2019-07-02 17:27:17 +02:00
|
|
|
oidcOPMetaDataOptionsCheckJWTSignature => 1,
|
|
|
|
oidcOPMetaDataOptionsJWKSTimeout => 0,
|
|
|
|
oidcOPMetaDataOptionsClientSecret => "rpsecret",
|
2017-03-09 22:56:53 +01:00
|
|
|
oidcOPMetaDataOptionsScope => "openid profile",
|
|
|
|
oidcOPMetaDataOptionsStoreIDToken => 0,
|
|
|
|
oidcOPMetaDataOptionsDisplay => "",
|
|
|
|
oidcOPMetaDataOptionsClientID => "rpid",
|
|
|
|
oidcOPMetaDataOptionsConfigurationURI =>
|
|
|
|
"https://auth.op.com/.well-known/openid-configuration"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
oidcOPMetaDataJWKS => {
|
|
|
|
op => $jwks,
|
|
|
|
},
|
|
|
|
oidcOPMetaDataJSON => {
|
|
|
|
op => $metadata,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|