Skip to content

Commit 47fc509

Browse files
committed
Authorizer throws exceptions accessing non-existent access tokens
feedback adddressed
1 parent 3f85ba7 commit 47fc509

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

src/Authorizer.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,26 @@ public function getChecker()
8686
return $this->checker;
8787
}
8888

89+
/**
90+
* Get the current access token for the session.
91+
*
92+
* If the session does not have an active access token, an exception will be thrown.
93+
*
94+
* @throws \LucaDegasperi\OAuth2Server\NoActiveAccessTokenException
95+
*
96+
* @return \League\OAuth2\Server\Entity\AccessTokenEntity
97+
*/
98+
public function getAccessToken()
99+
{
100+
$accessToken = $this->getChecker()->getAccessToken();
101+
102+
if (is_null($accessToken)) {
103+
throw new NoActiveAccessTokenException('Tried to access session data without an active access token');
104+
}
105+
106+
return $accessToken;
107+
}
108+
89109
/**
90110
* Issue an access token if the request parameters are valid.
91111
*
@@ -209,7 +229,7 @@ public function validateAccessToken($httpHeadersOnly = false, $accessToken = nul
209229
*/
210230
public function getScopes()
211231
{
212-
return $this->checker->getAccessToken()->getScopes();
232+
return $this->getAccessToken()->getScopes();
213233
}
214234

215235
/**
@@ -231,7 +251,7 @@ public function hasScope($scope)
231251
return true;
232252
}
233253

234-
return $this->checker->getAccessToken()->hasScope($scope);
254+
return $this->getAccessToken()->hasScope($scope);
235255
}
236256

237257
/**
@@ -241,7 +261,7 @@ public function hasScope($scope)
241261
*/
242262
public function getResourceOwnerId()
243263
{
244-
return $this->checker->getAccessToken()->getSession()->getOwnerId();
264+
return $this->getAccessToken()->getSession()->getOwnerId();
245265
}
246266

247267
/**
@@ -251,7 +271,7 @@ public function getResourceOwnerId()
251271
*/
252272
public function getResourceOwnerType()
253273
{
254-
return $this->checker->getAccessToken()->getSession()->getOwnerType();
274+
return $this->getAccessToken()->getSession()->getOwnerType();
255275
}
256276

257277
/**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of OAuth 2.0 Laravel.
5+
*
6+
* (c) Luca Degasperi <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace LucaDegasperi\OAuth2Server;
13+
14+
use Exception;
15+
16+
/**
17+
* This is the no active access token exception class.
18+
*
19+
* @author Troy Pavlek <[email protected]>
20+
*/
21+
class NoActiveAccessTokenException extends Exception
22+
{
23+
}

tests/unit/LucaDegasperi/OAuth2Server/AuthorizerSpec.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use League\OAuth2\Server\Grant\AuthCodeGrant;
1919
use League\OAuth2\Server\ResourceServer;
2020
use League\OAuth2\Server\Util\RedirectUri;
21+
use LucaDegasperi\OAuth2Server\NoActiveAccessTokenException;
2122
use PhpSpec\ObjectBehavior;
2223
use Symfony\Component\HttpFoundation\Request;
2324

@@ -65,6 +66,12 @@ public function it_returns_the_current_scopes(ResourceServer $checker, AccessTok
6566
$this->getScopes()->shouldReturn(['foo', 'bar']);
6667
}
6768

69+
public function it_throws_exception_if_current_scopes_accessed_without_active_access_token(ResourceServer $checker)
70+
{
71+
$checker->getAccessToken()->willReturn(null);
72+
$this->shouldThrow(NoActiveAccessTokenException::class)->during('getScopes');
73+
}
74+
6875
public function it_checks_if_a_scope_is_included_into_the_current_ones(ResourceServer $checker, AccessTokenEntity $accessTokenEntity)
6976
{
7077
$accessTokenEntity->hasScope('foo')->willReturn(true)->shouldBeCalled();
@@ -100,6 +107,12 @@ public function it_checks_if_multiple_valid_scopes_are_included_into_the_current
100107
$this->hasScope(['foo', 'bar'])->shouldReturn(true);
101108
}
102109

110+
public function it_throws_if_scopes_are_checked_without_active_access_token(ResourceServer $checker)
111+
{
112+
$checker->getAccessToken()->willReturn(null);
113+
$this->shouldThrow(NoActiveAccessTokenException::class)->during('hasScope', ['foo']);
114+
}
115+
103116
public function it_returns_the_resource_owner_id(ResourceServer $checker, AccessTokenEntity $accessTokenEntity, SessionEntity $sessionEntity)
104117
{
105118
$sessionEntity->getOwnerId()->willReturn('1')->shouldBeCalled();
@@ -108,6 +121,12 @@ public function it_returns_the_resource_owner_id(ResourceServer $checker, Access
108121
$this->getResourceOwnerId()->shouldReturn('1');
109122
}
110123

124+
public function it_throws_exception_if_resource_owner_id_accessed_without_active_session(ResourceServer $checker)
125+
{
126+
$checker->getAccessToken()->willReturn(null);
127+
$this->shouldThrow(NoActiveAccessTokenException::class)->during('getResourceOwnerId');
128+
}
129+
111130
public function it_returns_the_resource_owner_type(ResourceServer $checker, AccessTokenEntity $accessTokenEntity, SessionEntity $sessionEntity)
112131
{
113132
$sessionEntity->getOwnerType()->willReturn('user')->shouldBeCalled();
@@ -116,6 +135,12 @@ public function it_returns_the_resource_owner_type(ResourceServer $checker, Acce
116135
$this->getResourceOwnerType()->shouldReturn('user');
117136
}
118137

138+
public function test_it_throws_exception_if_resource_owner_type_accessed_without_active_session(ResourceServer $checker)
139+
{
140+
$checker->getAccessToken()->willReturn(null);
141+
$this->shouldThrow(NoActiveAccessTokenException::class)->during('getResourceOwnerType');
142+
}
143+
119144
public function it_returns_the_client_id(ResourceServer $checker, AccessTokenEntity $accessTokenEntity, SessionEntity $sessionEntity, ClientEntity $clientEntity)
120145
{
121146
$clientEntity->getId()->willReturn('1')->shouldBeCalled();

0 commit comments

Comments
 (0)