2019-08-09 13:53:58 +02:00
|
|
|
use lib 'inc';
|
|
|
|
use Test::More;
|
|
|
|
use strict;
|
|
|
|
use IO::String;
|
|
|
|
use LWP::UserAgent;
|
|
|
|
use LWP::Protocol::PSGI;
|
|
|
|
use MIME::Base64;
|
2019-08-20 11:16:27 +02:00
|
|
|
use JSON;
|
2019-08-09 13:53:58 +02:00
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
require 't/test-lib.pm';
|
2020-02-05 22:44:14 +01:00
|
|
|
require 't/oidc-lib.pm';
|
2019-08-09 13:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
my $debug = 'error';
|
|
|
|
|
|
|
|
# Initialization
|
|
|
|
my $op = LLNG::Manager::Test->new( {
|
2019-09-05 08:53:05 +02:00
|
|
|
ini => {
|
|
|
|
logLevel => $debug,
|
|
|
|
domain => 'idp.com',
|
|
|
|
portal => 'http://auth.op.com',
|
|
|
|
authentication => 'Demo',
|
|
|
|
userDB => 'Same',
|
|
|
|
issuerDBOpenIDConnectActivation => 1,
|
|
|
|
issuerDBOpenIDConnectRule => '$uid eq "french"',
|
|
|
|
oidcRPMetaDataExportedVars => {
|
|
|
|
rp => {
|
|
|
|
email => "mail",
|
|
|
|
family_name => "cn",
|
|
|
|
name => "cn"
|
2019-08-09 13:53:58 +02:00
|
|
|
},
|
2019-09-05 08:53:05 +02:00
|
|
|
rp2 => {
|
|
|
|
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",
|
|
|
|
oidcRPMetaDataOptionsClientSecret => "rpsecret",
|
|
|
|
oidcRPMetaDataOptionsUserIDAttr => "",
|
2019-11-04 14:54:41 +01:00
|
|
|
oidcRPMetaDataOptionsAccessTokenExpiration => 3600,
|
2019-09-05 08:53:05 +02:00
|
|
|
oidcRPMetaDataOptionsBypassConsent => 1,
|
2019-08-09 13:53:58 +02:00
|
|
|
},
|
2019-09-05 08:53:05 +02:00
|
|
|
rp2 => {
|
|
|
|
oidcRPMetaDataOptionsDisplayName => "RP2",
|
|
|
|
oidcRPMetaDataOptionsIDTokenExpiration => 3600,
|
|
|
|
oidcRPMetaDataOptionsClientID => "rp2id",
|
|
|
|
oidcRPMetaDataOptionsIDTokenSignAlg => "HS512",
|
|
|
|
oidcRPMetaDataOptionsClientSecret => "rp2secret",
|
|
|
|
oidcRPMetaDataOptionsUserIDAttr => "",
|
2019-11-04 14:54:41 +01:00
|
|
|
oidcRPMetaDataOptionsAccessTokenExpiration => 3600,
|
2019-09-05 08:53:05 +02:00
|
|
|
oidcRPMetaDataOptionsBypassConsent => 1,
|
|
|
|
oidcRPMetaDataOptionsRule => '$uid eq "dwho"',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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,
|
2019-08-09 13:53:58 +02:00
|
|
|
}
|
2019-09-05 08:53:05 +02:00
|
|
|
}
|
|
|
|
);
|
2019-08-09 13:53:58 +02:00
|
|
|
my $res;
|
|
|
|
|
|
|
|
# Authenticate to LLNG
|
2019-09-05 08:53:05 +02:00
|
|
|
my $url = "/";
|
2019-08-09 13:53:58 +02:00
|
|
|
my $query = "user=french&password=french";
|
|
|
|
ok(
|
|
|
|
$res = $op->_post(
|
|
|
|
"/",
|
|
|
|
IO::String->new($query),
|
|
|
|
accept => 'text/html',
|
|
|
|
length => length($query),
|
|
|
|
),
|
|
|
|
"Post authentication"
|
|
|
|
);
|
|
|
|
count(1);
|
|
|
|
my $idpId = expectCookie($res);
|
|
|
|
|
|
|
|
# Get code for RP1
|
2019-09-05 08:53:05 +02:00
|
|
|
my $query =
|
|
|
|
"response_type=code&scope=openid%20profile%20email&client_id=rpid&state=af0ifjsldkj&redirect_uri=http%3A%2F%2Frp2.com%2F";
|
2019-08-09 13:53:58 +02:00
|
|
|
ok(
|
|
|
|
$res = $op->_get(
|
|
|
|
"/oauth2/authorize",
|
2019-09-05 08:53:05 +02:00
|
|
|
query => "$query",
|
2019-08-09 13:53:58 +02:00
|
|
|
accept => 'text/html',
|
|
|
|
cookie => "lemonldap=$idpId",
|
|
|
|
),
|
2019-10-02 11:37:52 +02:00
|
|
|
"Get authorization code for rp1"
|
2019-08-09 13:53:58 +02:00
|
|
|
);
|
|
|
|
count(1);
|
|
|
|
|
2019-09-05 08:53:05 +02:00
|
|
|
my ($code) = expectRedirection( $res, qr#http://rp2\.com/.*code=([^\&]*)# );
|
2019-08-09 13:53:58 +02:00
|
|
|
|
|
|
|
# Play code on RP2
|
2019-09-05 08:53:05 +02:00
|
|
|
$query =
|
|
|
|
"grant_type=authorization_code&code=$code&redirect_uri=http%3A%2F%2Frp2.com%2F";
|
2019-08-09 13:53:58 +02:00
|
|
|
|
|
|
|
ok(
|
|
|
|
$res = $op->_post(
|
|
|
|
"/oauth2/token",
|
|
|
|
IO::String->new($query),
|
|
|
|
accept => 'text/html',
|
|
|
|
length => length($query),
|
|
|
|
custom => {
|
2019-09-05 08:53:05 +02:00
|
|
|
HTTP_AUTHORIZATION => "Basic " . encode_base64("rp2id:rp2secret"),
|
2019-08-09 13:53:58 +02:00
|
|
|
},
|
|
|
|
),
|
2019-10-02 11:37:52 +02:00
|
|
|
"Post token on wrong RP"
|
2019-08-09 13:53:58 +02:00
|
|
|
);
|
|
|
|
count(1);
|
|
|
|
|
|
|
|
# Expect an invalid request
|
2019-10-02 11:37:52 +02:00
|
|
|
is( $res->[0], 400, "Got invalid request" );
|
2019-08-09 13:53:58 +02:00
|
|
|
count(1);
|
|
|
|
|
2019-10-02 11:37:52 +02:00
|
|
|
# Get new code for RP1
|
|
|
|
my $query =
|
|
|
|
"response_type=code&scope=openid%20profile%20email&client_id=rpid&state=af0ifjsldkj&redirect_uri=http%3A%2F%2Frp.com%2F";
|
|
|
|
ok(
|
|
|
|
$res = $op->_get(
|
|
|
|
"/oauth2/authorize",
|
|
|
|
query => "$query",
|
|
|
|
accept => 'text/html',
|
|
|
|
cookie => "lemonldap=$idpId",
|
|
|
|
),
|
|
|
|
"Get authorization code again"
|
|
|
|
);
|
|
|
|
count(1);
|
|
|
|
|
|
|
|
my ($code) = expectRedirection( $res, qr#http://rp\.com/.*code=([^\&]*)# );
|
|
|
|
|
2019-08-20 11:16:27 +02:00
|
|
|
# Play code on RP1
|
2019-09-05 08:53:05 +02:00
|
|
|
$query =
|
2019-10-02 11:37:52 +02:00
|
|
|
"grant_type=authorization_code&code=$code&redirect_uri=http%3A%2F%2Frp.com%2F";
|
2019-08-20 11:16:27 +02:00
|
|
|
|
|
|
|
ok(
|
|
|
|
$res = $op->_post(
|
|
|
|
"/oauth2/token",
|
|
|
|
IO::String->new($query),
|
|
|
|
accept => 'text/html',
|
|
|
|
length => length($query),
|
|
|
|
custom => {
|
2019-09-05 08:53:05 +02:00
|
|
|
HTTP_AUTHORIZATION => "Basic " . encode_base64("rpid:rpsecret"),
|
2019-08-20 11:16:27 +02:00
|
|
|
},
|
|
|
|
),
|
2019-10-02 11:37:52 +02:00
|
|
|
"Post auth code on correct RP"
|
2019-08-20 11:16:27 +02:00
|
|
|
);
|
|
|
|
count(1);
|
2019-10-25 16:41:34 +02:00
|
|
|
$res = expectJSON($res);
|
|
|
|
my $token = $res->{access_token};
|
2019-09-05 08:53:05 +02:00
|
|
|
ok( $token, 'Access token present' );
|
2019-08-20 11:16:27 +02:00
|
|
|
count(1);
|
2019-11-04 14:54:41 +01:00
|
|
|
Time::Fake->offset("+2h");
|
|
|
|
|
2019-08-20 11:16:27 +02:00
|
|
|
|
|
|
|
ok(
|
|
|
|
$res = $op->_post(
|
|
|
|
"/oauth2/userinfo",
|
|
|
|
IO::String->new(""),
|
|
|
|
accept => 'text/html',
|
|
|
|
length => 0,
|
|
|
|
custom => {
|
2019-09-05 08:53:05 +02:00
|
|
|
HTTP_AUTHORIZATION => "Bearer " . $token,
|
2019-08-20 11:16:27 +02:00
|
|
|
},
|
|
|
|
),
|
2019-10-02 11:37:52 +02:00
|
|
|
"post to userinfo with expired access token"
|
2019-08-20 11:16:27 +02:00
|
|
|
);
|
|
|
|
count(1);
|
2019-10-02 11:37:52 +02:00
|
|
|
ok( $res->[0] == 401, "Access denied with expired token" );
|
2019-08-20 11:16:27 +02:00
|
|
|
count(1);
|
|
|
|
|
2019-08-09 13:53:58 +02:00
|
|
|
clean_sessions();
|
|
|
|
done_testing( count() );
|
|
|
|
|