Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject login request #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{{$NEXT}}

- Added reject_login_request method

0.004 2024-12-03 10:30:12+00:00 UTC

- Added timeout to `HTTP::Tiny`
Expand Down
35 changes: 35 additions & 0 deletions lib/WebService/Hydra/Client.pm
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,41 @@ method accept_login_request ($login_challenge, $accept_payload) {
return $result->{data};
}

=head2 reject_login_request

Rejects the login request and returns the response from hydra.

Arguments:

=over 1

=item C<$login_challenge>

Authentication challenge string that is used to identify the login request.

=item C<$reject_payload>

Payload to be sent to the Hydra service to reject the login request.

=back

=cut

method reject_login_request ($login_challenge, $reject_payload) {
my $method = "PUT";
my $path = "$admin_endpoint/admin/oauth2/auth/requests/login/reject?challenge=$login_challenge";

my $result = $self->api_call($method, $path, $reject_payload);
if ($result->{code} != OK_STATUS_CODE) {
WebService::Hydra::Exception::InvalidLoginRequest->new(
message => "Failed to reject login request",
category => "client",
details => $result
)->throw;
}
return $result->{data};
}

=head2 get_logout_request

Get the logout request and return the response from Hydra.
Expand Down
69 changes: 69 additions & 0 deletions t/unit/hydra_client.t
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,75 @@ subtest 'oidc_config' => sub {

};

subtest 'reject_login_request' => sub {
my $mock_hydra = Test::MockModule->new('WebService::Hydra::Client');
my $mock_api_response;
my @params;
$mock_hydra->redefine(
'api_call',
sub {
(@params) = @_;
return $mock_api_response;
});

my $client = WebService::Hydra::Client->new(
admin_endpoint => 'http://dummyhydra.com/admin',
public_endpoint => 'http://dummyhydra.com'
);

my $reject_payload = {
error => 'access_denied',
error_debug => 'User authentication failed',
error_description => 'Invalid credentials provided',
error_hint => 'Check your username and password',
status_code => 401
};

# Test for 200 OK status code
$mock_api_response = {
code => 200,
data => {redirect_to => 'http://dummyhydra.com/error'}
};

my $got = $client->reject_login_request("VALID_CHALLENGE", $reject_payload);
is $params[1], 'PUT', 'PUT request method';
is $params[2], 'http://dummyhydra.com/admin/admin/oauth2/auth/requests/login/reject?challenge=VALID_CHALLENGE',
'Request URL built with correct parameters';
is_deeply $params[3], $reject_payload, 'Request payload is correct';
is_deeply $got, $mock_api_response->{data}, 'api_call response correctly parsed';

# Test for non-200 status codes
$mock_api_response = {
code => 400,
data => {
error => "string",
error_description => "string",
status_code => 400
}
};

dies_ok { $client->reject_login_request("INVALID_CHALLENGE", $reject_payload) }
'Dies if non-200 status code is received from api_call';

my $exception = $@;
my $expected_exception = WebService::Hydra::Exception::InvalidLoginRequest->new(
message => 'Failed to reject login request',
category => 'client',
details => $mock_api_response
);
is_deeply $exception, $expected_exception, 'Return api_call response for Non 200 status code';

# Test network failure
$mock_hydra->redefine(
'api_call',
sub {
die "Request to http://dummyhydra.com/admin/oauth2/auth/requests/login/reject?challenge=VALID_CHALLENGE failed - Network issue";
});

dies_ok { $client->reject_login_request("VALID_CHALLENGE", $reject_payload) }
'Dies if http request fails for some reason';
};

subtest 'validate_token' => sub {
my $mock_hydra = Test::MockModule->new('WebService::Hydra::Client');
my $mock_token = 'mock.jwt.token';
Expand Down
Loading