Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ Fix sipmple-cache requirements, thanks to @neclimdul
# 2.0.0

For PHP 8 only

# 2.1.0 ???

Possibility to set the property accessor to null in order to support tokens returned in plain text format.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,26 @@ $jwtManager = new JwtManager(
);
```

### Null property accessor

If the response is not in json format (i.e. the response body contains only the token in plain text format), you need
to set the property accessor to `null`:

```php
//Create the JwtManager
$jwtManager = new JwtManager(
$authClient,
$authStrategy,
$persistenceStrategy,
[
'token_url' => '/api/token',
]
);
//Set property accessor to `null`
$jwtManager->setPropertyAccessor(null);
```


## Authorization Header Type

Some endpoints use different Authorization header types (Bearer, JWT, etc...).
Expand Down
27 changes: 21 additions & 6 deletions src/Manager/JwtManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ public function __construct(
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
}

/**
* @param \Symfony\Component\PropertyAccess\PropertyAccessor|null $propertyAccessor
*/
public function setPropertyAccessor(?PropertyAccessor $propertyAccessor): void
{
$this->propertyAccessor = $propertyAccessor;
}

/**
* getToken.
*
Expand All @@ -120,14 +128,21 @@ public function getJwtToken()
);

$response = $this->client->request('POST', $url, $requestOptions);
$body = json_decode($response->getBody());

//Will be throw because it's mandatory
$tokenValue = $this->propertyAccessor->getValue($body, $this->options['token_key']);
if ($this->propertyAccessor !== null) {
$body = json_decode($response->getBody());

//Will be throw because it's mandatory
$tokenValue = $this->propertyAccessor->getValue($body, $this->options['token_key']);

try {
$expiresIn = $this->propertyAccessor->getValue($body, $this->options['expire_key']);
} catch (NoSuchPropertyException $e) {
try {
$expiresIn = $this->propertyAccessor->getValue($body, $this->options['expire_key']);
} catch (NoSuchPropertyException $e) {
$expiresIn = null;
}
} else {
// no property accessor means that token is in plain text format
$tokenValue = $response->getBody()->getContents();
$expiresIn = null;
}

Expand Down
41 changes: 41 additions & 0 deletions tests/Manager/JwtManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,47 @@ function (RequestInterface $request) {
$this->assertEquals('1453720507', $token->getToken());
}

public function testGetTokenWithNullPropertyAccessor()
{
$mockHandler = new MockHandler([
function (RequestInterface $request) {

$this->assertTrue($request->hasHeader('timeout'));
$this->assertEquals(
3,
$request->getHeaderLine('timeout')
);

return new Response(
200,
['Content-Type' => 'text/plain'],
'1453720507' // plain text !
);
},
]);

$handler = HandlerStack::create($mockHandler);

$authClient = new Client([
'handler' => $handler,
]);

$authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);

$jwtManager = new JwtManager(
$authClient,
$authStrategy,
null,
['token_url' => '/api/token', 'timeout' => 3] // no (useless) 'token_key' option
);
$jwtManager->setPropertyAccessor(null);

$token = $jwtManager->getJwtToken();

$this->assertInstanceOf(JwtToken::class, $token);
$this->assertEquals('1453720507', $token->getToken());
}

public function testGetTokenShouldGetNewTokenIfCachedTokenIsNotValid()
{
$mockHandler = new MockHandler(
Expand Down