Skip to content

Commit a2ff340

Browse files
committed
Fix client_id verification bug in token grant
Fix other tests which the above fix broke Add test to prove that invalid client_id isn't granted a token
1 parent 33773f3 commit a2ff340

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

lib/CatalystX/OAuth2/Store/DBIC.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ sub create_client_code {
5959
sub find_client_code {
6060
my ( $self, $code, $id ) = @_;
6161
return $id
62-
? $self->_code_rs->find($code)
63-
: $self->_code_rs($id)->find($code);
62+
? $self->_code_rs($id)->find($code)
63+
: $self->_code_rs->find($code);
6464
}
6565

6666
sub activate_client_code {

t/unit/300-actionrole-grant-auth.t

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ my $code =
124124
my $uri = URI->new('/grant');
125125
$uri->query_form(
126126
{ response_type => 'code',
127-
client_id => 1,
127+
client_id => $code->client_id,
128128
state => 'bar',
129129
redirect_uri => '/client/foo',
130130
code => $code->as_string,
@@ -157,13 +157,52 @@ my $code =
157157
is( $res->status, 302 );
158158
}
159159

160+
# try a grant with an incorrect client id
161+
# should redirect with access_denied
162+
{
163+
my $uri = URI->new('/grant');
164+
$uri->query_form(
165+
{ response_type => 'code',
166+
client_id => 9999999,
167+
state => 'bar',
168+
redirect_uri => '/client/foo',
169+
code => $code->as_string,
170+
approved => 0
171+
}
172+
);
173+
$code->discard_changes;
174+
ok(!$code->is_active);
175+
my $c = $mock->( GET $uri );
176+
$c->dispatch;
177+
is_deeply( $c->error, [], 'dispatches to request action cleanly' );
178+
is( $c->res->body, undef, q{doesn't produce warning} );
179+
ok( $c->req->can('oauth2'),
180+
"installs oauth2 accessors if request is valid" );
181+
ok( Moose::Util::does_role( $c->req, 'CatalystX::OAuth2::Request' ) );
182+
my $res = $c->res;
183+
isa_ok( my $oauth2 = $c->req->oauth2,
184+
'CatalystX::OAuth2::Request::GrantAuth' );
185+
my $redirect = $c->req->oauth2->next_action_uri( $c->controller, $c );
186+
is_deeply(
187+
{ $redirect->query_form },
188+
{ error => 'unauthorized_client',
189+
error_description =>
190+
'the client identified by 9999999 is not authorized to access this resource'
191+
},
192+
"deny access to incorrect clients"
193+
);
194+
is( $res->location, $redirect );
195+
is( $res->status, 302 );
196+
}
197+
198+
160199
# try a grant with a valid code and approval
161200
# should activate code and redirect
162201
{
163202
my $uri = URI->new('/grant');
164203
$uri->query_form(
165204
{ response_type => 'code',
166-
client_id => 1,
205+
client_id => $code->client_id,
167206
state => 'bar',
168207
redirect_uri => '/client/foo',
169208
code => $code->as_string,

0 commit comments

Comments
 (0)