Skip to content

Commit fb5ad45

Browse files
committed
Merge pull request #28 from zf-fr/oauth
Trigger exception if invalid token
2 parents 66cbf21 + b597179 commit fb5ad45

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Please note that until I reach 1.0, I **WILL NOT** follow semantic version. This
4141
Installation is only officially supported using Composer:
4242

4343
```sh
44-
php composer.phar require zfr/zfr-oauth2-server:0.5.*
44+
php composer.phar require zfr/zfr-oauth2-server:0.6.*
4545
```
4646

4747
## Framework integration

src/ZfrOAuth2/Server/ResourceServer.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ public function __construct(TokenService $accessTokenService)
5252
* Get the access token
5353
*
5454
* Note that this method will only match tokens that are not expired and match the given scopes (if any).
55-
* Otherwise, null will be returned
55+
* If no token is pass, this method will return null, but if a token is given does not exist (ie. has been
56+
* deleted) or is not valid, then it will trigger an exception
5657
*
5758
* @link http://tools.ietf.org/html/rfc6750#page-5
5859
* @param HttpRequest $request
5960
* @param array $scopes
6061
* @return AccessToken|null
62+
* @throws Exception\InvalidAccessTokenException If given access token is invalid or expired
6163
*/
6264
public function getAccessToken(HttpRequest $request, $scopes = [])
6365
{
@@ -68,7 +70,7 @@ public function getAccessToken(HttpRequest $request, $scopes = [])
6870
$token = $this->accessTokenService->getToken($token);
6971

7072
if ($token === null || !$this->isTokenValid($token, $scopes)) {
71-
return null;
73+
throw new InvalidAccessTokenException('Access token has expired or has been deleted');
7274
}
7375

7476
return $token;
@@ -79,7 +81,6 @@ public function getAccessToken(HttpRequest $request, $scopes = [])
7981
*
8082
* @param HttpRequest $request
8183
* @return string|null
82-
* @throws Exception\InvalidAccessTokenException If access token is malformed in the Authorization header
8384
*/
8485
private function extractAccessToken(HttpRequest $request)
8586
{
@@ -89,16 +90,16 @@ private function extractAccessToken(HttpRequest $request)
8990
if ($headers->has('Authorization')) {
9091
// Header value is expected to be "Bearer xxx"
9192
$parts = explode(' ', $headers->get('Authorization')->getFieldValue());
92-
$token = end($parts); // Access token is the last value
9393

94-
if (count($parts) < 2 || empty($token)) {
95-
throw new InvalidAccessTokenException('No access token could be found in Authorization header');
94+
if (count($parts) < 2) {
95+
return null;
9696
}
97-
} else {
98-
$token = $request->getQuery('access_token');
97+
98+
return end($parts);
9999
}
100100

101-
return $token;
101+
// Default back to authorization in query param
102+
return $request->getQuery('access_token');
102103
}
103104

104105
/**

tests/ZfrOAuth2Test/Server/ResourceServerTest.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,25 @@ public function testCanExtractAccessTokenFromQueryString()
8080
$this->assertSame($token, $this->resourceServer->getAccessToken($request));
8181
}
8282

83-
public function testThrowExceptionIfNoAccessTokenIsInAuthorizationHeader()
83+
public function testReturnNullIfNoAccessTokenIsInAuthorizationHeader()
84+
{
85+
$request = new HttpRequest();
86+
$request->getHeaders()->addHeaderLine('Authorization', '');
87+
88+
$this->assertNull($this->resourceServer->getAccessToken($request));
89+
}
90+
91+
public function testThrowExceptionIfTokenDoesNotExistAnymore()
8492
{
8593
$this->setExpectedException('ZfrOAuth2\Server\Exception\InvalidAccessTokenException');
8694

8795
$request = new HttpRequest();
88-
$request->getHeaders()->addHeaderLine('Authorization', '');
96+
$request->getHeaders()->addHeaderLine('Authorization', 'Bearer foo');
97+
98+
$this->tokenService->expects($this->once())
99+
->method('getToken')
100+
->with('foo')
101+
->will($this->returnValue(null));
89102

90103
$this->resourceServer->getAccessToken($request);
91104
}
@@ -144,12 +157,11 @@ public function testCanValidateAccessToResource($expiredToken, $tokenScope, $des
144157
->with('token')
145158
->will($this->returnValue($accessToken));
146159

147-
$tokenResult = $this->resourceServer->getAccessToken($request, $desiredScope);
148-
149-
if ($match) {
150-
$this->assertInstanceOf('ZfrOAuth2\Server\Entity\AccessToken', $tokenResult);
151-
} else {
152-
$this->assertNull($tokenResult);
160+
if (!$match || $expiredToken) {
161+
$this->setExpectedException('ZfrOAuth2\Server\Exception\InvalidAccessTokenException');
153162
}
163+
164+
$tokenResult = $this->resourceServer->getAccessToken($request, $desiredScope);
165+
$this->assertInstanceOf('ZfrOAuth2\Server\Entity\AccessToken', $tokenResult);
154166
}
155167
}

0 commit comments

Comments
 (0)