Skip to content

Commit 53d873f

Browse files
authored
Merge pull request #5 from youssef-deriv/fix_validate_token_bug
fix_validate_token_bug
2 parents 17b5b2a + c9282fb commit 53d873f

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

Changes

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{{$NEXT}}
2+
- Bug fix in `validate_token` and `validate_id_token` methods.
23

34
0.001 2024-09-30 03:56:03+00:00 UTC
45
- Initial release

lib/WebService/Hydra/Client.pm

+3-3
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ method validate_id_token ($id_token) {
354354
try {
355355
my $payload = decode_jwt(
356356
token => $id_token,
357-
kid_keys => $jwks
357+
kid_keys => $self->jwks
358358
);
359359
return $payload;
360360
} catch ($e) {
@@ -385,8 +385,8 @@ method validate_token ($token) {
385385
token => $token,
386386
verify_iat => 1,
387387
verify_exp => 1,
388-
verify_iss => $oidc_config->{issuer},
389-
kid_keys => $jwks
388+
verify_iss => $self->oidc_config->{issuer},
389+
kid_keys => $self->jwks
390390
);
391391
return $payload;
392392
}

t/unit/hydra_client.t

+57-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ subtest 'revoke_login_sessions' => sub {
416416
is_deeply $got , $mock_api_response->{data}, 'api_call response correctly parsed';
417417

418418
@params = ();
419-
$got = $client->revoke_login_sessions(sid => '1234');
419+
$got = $client->revoke_login_sessions(sid => '1234');
420420

421421
is $params[1], 'DELETE', 'DELETE request method';
422422
is $params[2], 'http://dummyhydra.com/admin/admin/oauth2/auth/sessions/login?sid=1234', 'Request URL built with correct parameters';
@@ -539,6 +539,62 @@ subtest 'oidc_config' => sub {
539539

540540
};
541541

542+
subtest 'validate_token' => sub {
543+
my $mock_hydra = Test::MockModule->new('WebService::Hydra::Client');
544+
my $mock_token = 'mock.jwt.token';
545+
my $mock_oidc_config = {issuer => 'https://example.com'};
546+
my $mock_jwks = {keys => [{kid => 'key1', kty => 'RSA', n => '...', e => '...'}]};
547+
my $mock_payload = {
548+
sub => '1234567890',
549+
name => 'John Doe',
550+
admin => 'true'
551+
};
552+
553+
$mock_hydra->redefine(
554+
'decode_jwt',
555+
sub {
556+
my %args = @_;
557+
if ($args{token} eq $mock_token) {
558+
return $mock_payload;
559+
} else {
560+
die "Invalid token";
561+
}
562+
});
563+
564+
$mock_hydra->redefine(
565+
'fetch_openid_configuration',
566+
sub {
567+
return $mock_oidc_config;
568+
});
569+
570+
$mock_hydra->redefine(
571+
'fetch_jwks',
572+
sub {
573+
return $mock_jwks;
574+
});
575+
576+
my $client = WebService::Hydra::Client->new(
577+
admin_endpoint => 'http://dummyhydra.com/admin',
578+
public_endpoint => 'http://dummyhydra.com'
579+
);
580+
581+
subtest 'validate_token' => sub {
582+
my $decoded_payload;
583+
584+
lives_ok {
585+
$decoded_payload = $client->validate_token($mock_token);
586+
}
587+
'Token validation should succeed';
588+
589+
is_deeply($decoded_payload, $mock_payload, 'Decoded payload should match expected payload');
590+
591+
throws_ok {
592+
$client->validate_token('invalid.token');
593+
}
594+
qr/Invalid token/, 'Invalid token should throw an exception';
595+
};
596+
};
597+
542598
done_testing();
543599

544600
1;

0 commit comments

Comments
 (0)