Skip to content

Commit

Permalink
Merge pull request #25 from picqer/access-token-scope-casing
Browse files Browse the repository at this point in the history
Accept lower case token type and scopes when validating access tokens (version 5)
  • Loading branch information
kleiram committed Jan 26, 2022
2 parents 2774a9b + 357e003 commit 308fb9e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ protected function validateToken($token): void
throw new ResponseException('Missing expires_in');
}

if ($token['token_type'] != 'Bearer') {
if (strtolower($token['token_type']) !== 'bearer') {
throw new ResponseException(
sprintf('Unexpected token_type \'%s\', expected \'Bearer\'', $token['token_type'])
);
}

if ($token['scope'] != 'RETAILER') {
if (strtolower($token['scope']) !== 'retailer') {
throw new ResponseException(
sprintf('Unexpected token_type \'%s\', expected \'RETAILER\'', $token['scope'])
);
Expand Down
19 changes: 17 additions & 2 deletions tests/BaseClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ResponseInterface;

class BaseClientTest extends TestCase
{
Expand Down Expand Up @@ -54,9 +55,9 @@ public function testClientIsInitiallyNotAuthenticated()
$this->assertFalse($this->client->isAuthenticated());
}

protected function authenticate()
protected function authenticate(?ResponseInterface $response = null)
{
$response = Message::parseResponse(file_get_contents(__DIR__ . '/Fixtures/http/200-token'));
$response = $response ?? Message::parseResponse(file_get_contents(__DIR__ . '/Fixtures/http/200-token'));

$credentials = base64_encode('secret_id' . ':' . 'somesupersecretvaluethatshouldnotbeshared');
$this->httpProphecy->request('POST', 'https://login.bol.com/token', [
Expand All @@ -79,6 +80,20 @@ public function testClientIsAuthenticatedAfterSuccessfulAuthentication()
$this->assertTrue($this->client->isAuthenticated());
}

public function testClientAcceptsLowercaseScopeInAccessToken()
{
$this->authenticate(Message::parseResponse(file_get_contents(__DIR__ . '/Fixtures/http/200-token-lowercase-scope')));

$this->assertTrue($this->client->isAuthenticated());
}

public function testClientAcceptsLowercaseTokenTypeInAccessToken()
{
$this->authenticate(Message::parseResponse(file_get_contents(__DIR__ . '/Fixtures/http/200-token-lowercase-type')));

$this->assertTrue($this->client->isAuthenticated());
}

public function testAuthenticateThrowsUnauthorizedExceptionWhenAuthenticatingWithBadCredentials()
{
$response = Message::parseResponse(file_get_contents(__DIR__ . '/Fixtures/http/401-unauthorized'));
Expand Down
9 changes: 9 additions & 0 deletions tests/Fixtures/http/200-token-lowercase-scope
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8

{
"access_token": "sometoken",
"token_type": "Bearer",
"expires_in": 299,
"scope": "retailer"
}
9 changes: 9 additions & 0 deletions tests/Fixtures/http/200-token-lowercase-type
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8

{
"access_token": "sometoken",
"token_type": "bearer",
"expires_in": 299,
"scope": "RETAILER"
}

0 comments on commit 308fb9e

Please sign in to comment.