Lemonldap-NG-Portal

 view release on metacpan or  search on metacpan

t/32-OIDC-Hooks.t  view on Meta::CPAN

ok(
    $json->{custom_metadata_hook} eq 'hooked',
    'oidcGenerateMetadata hook added custom field'
);
count(1);
ok(
    grep( { $_ eq 'urn:test:custom_grant' }
        @{ $json->{grant_types_supported} } ),
    'oidcGenerateMetadata hook added custom grant type'
);
count(1);

# --------------------------------------------------------------------------
# Test oidcGotRegistrationRequest and oidcRegisterClient hooks
# --------------------------------------------------------------------------
note "Testing oidcGotRegistrationRequest and oidcRegisterClient hooks";

# Test that registration is denied by hook for matching denied pattern
my $reg_metadata = to_json( {
        redirect_uris => ["https://denied.example.com/callback"],
        client_name   => "Test Client"
    }
);
ok(
    $res = $op->_post(
        "/oauth2/register",
        IO::String->new($reg_metadata),
        accept => 'application/json',
        length => length($reg_metadata),
        type   => 'application/json',
    ),
    "Registration attempt with denied redirect_uri"
);

is( $res->[0], 403, "Registration denied by hook returns 403" );

# Test that registration succeeds for allowed redirect_uri
$reg_metadata = to_json( {
        redirect_uris => ["https://allowed.example.com/callback"],
        client_name   => "Test Allowed Client"
    }
);
ok(
    $res = $op->_post(
        "/oauth2/register",
        IO::String->new($reg_metadata),
        accept => 'application/json',
        length => length($reg_metadata),
        type   => 'application/json',
    ),
    "Registration attempt with allowed redirect_uri"
);

# RFC 7591: Successful registration returns 201 Created
is( $res->[0], 201, "Registration returns 201 Created" );
$json = from_json( $res->[2]->[0] );
ok( $json->{client_id}, "Registration succeeded with client_id" );

# Verify that the oidcRegisterClient hook modified the saved RP options
my $savedConf = from_json(
    do { local ( @ARGV, $/ ) = "$main::tmpDir/lmConf-2.json"; <> }
);
my ($newRpKey) =
  grep {
    ( $savedConf->{oidcRPMetaDataOptions}->{$_}
          ->{oidcRPMetaDataOptionsClientID} || '' ) eq $json->{client_id}
  } keys %{ $savedConf->{oidcRPMetaDataOptions} };
ok( $newRpKey, "Newly registered RP found in saved configuration" );
is(
    $savedConf->{oidcRPMetaDataOptions}->{$newRpKey}
      ->{oidcRPMetaDataOptionsBypassConsent},
    1,
    "oidcRegisterClient hook set BypassConsent on the new RP"
);

clean_sessions();
done_testing();



( run in 0.720 second using v1.01-cache-2.11-cpan-007c89162af )