Skip to content

Commit

Permalink
Add (optional) logger to log failed token fetch requests (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankverhoeven authored and Feijs committed Feb 1, 2019
1 parent cea24be commit 2bc2e29
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
"psr/cache": "^1.0",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0",
"psr/log": "^1.1",
"zendframework/zend-diactoros": "^1.8"
},
"require-dev": {
"escapestudios/symfony2-coding-standard": "^3.4",
"phpunit/phpunit": "^6.5",
"squizlabs/php_codesniffer": "^3.3"
"myonlinestore/coding-standard": "^1.0",
"phpunit/phpunit": "^6.5"
},
"config": {
"vendor-dir": "vendor",
Expand Down
33 changes: 12 additions & 21 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
<?xml version="1.0"?>
<ruleset name="MyOnlineStore">
<description>MyOnlineStore coding standard</description>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"
>
<arg value="sp"/>
<arg name="colors"/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>

<file>src</file>
<file>./src/</file>
<file>./tests/</file>

<exclude-pattern>tests/*</exclude-pattern>

<!-- Include the whole Symfony2 standard -->
<rule ref="vendor/escapestudios/symfony2-coding-standard/Symfony/ruleset.xml"/>

<rule ref="Symfony">
<exclude name="Symfony.Commenting.ClassComment.Missing"/>
<exclude name="Symfony.Commenting.FunctionComment.Missing"/>
<exclude name="Symfony.Commenting.License.Warning"/>
<exclude name="Symfony.Functions.Arguments.Invalid"/>
</rule>

<!-- The soft limit on line length MUST be 120 characters; automated style checkers MUST warn but MUST NOT error at the soft limit. -->
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="120"/>
<property name="absoluteLineLimit" value="0"/>
</properties>
<rule ref="vendor/myonlinestore/coding-standard/MyOnlineStore/ruleset.xml">
<exclude name="Symfony.NamingConventions.ValidClassName.InvalidInterfaceName"/>
<exclude name="Symfony.NamingConventions.ValidClassName.InvalidExceptionName"/>
</rule>
</ruleset>
21 changes: 18 additions & 3 deletions src/TokenManager/Jwt.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Lcobucci\JWT\Parser;
use MyOnlineStore\GuzzleAuthorizationMiddleware\Token;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

final class Jwt implements TokenManagerInterface
{
Expand All @@ -21,6 +23,11 @@ final class Jwt implements TokenManagerInterface
*/
private $jwtParser;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var RequestFactoryInterface
*/
Expand All @@ -35,17 +42,17 @@ public function __construct(
ClientInterface $httpClient,
Parser $jwtParser,
RequestFactoryInterface $requestFactory,
UriProviderInterface $uriProvider
UriProviderInterface $uriProvider,
LoggerInterface $logger = null
) {
$this->httpClient = $httpClient;
$this->requestFactory = $requestFactory;
$this->uriProvider = $uriProvider;
$this->jwtParser = $jwtParser;
$this->logger = $logger ?? new NullLogger();
}

/**
* @return Token
*
* @throws \InvalidArgumentException If the token is not a valid JWT
* @throws \OutOfBoundsException If the token does not have an exp claim
*/
Expand All @@ -63,6 +70,14 @@ public function getToken(): Token

$token = \json_decode($response->getBody()->getContents(), true)['accessToken'] ?? '';
} catch (GuzzleException $exception) {
$this->logger->critical(
'Unable to fetch JWT token',
[
'message' => $exception->getMessage(),
'previous' => $exception->getPrevious(),
'trace' => $exception->getTraceAsString(),
]
);
}

return new Token(
Expand Down
13 changes: 12 additions & 1 deletion tests/TokenManager/JwtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;

final class JwtTest extends TestCase
{
Expand All @@ -34,6 +35,11 @@ final class JwtTest extends TestCase
*/
private $jwtParser;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var RequestFactoryInterface
*/
Expand All @@ -50,7 +56,8 @@ protected function setUp()
$this->httpClient = $this->createMock(ClientInterface::class),
$this->jwtParser = $this->createMock(Parser::class),
$this->requestFactory = $this->createMock(RequestFactoryInterface::class),
$this->uriProvider = $this->createMock(UriProviderInterface::class)
$this->uriProvider = $this->createMock(UriProviderInterface::class),
$this->logger = $this->createMock(LoggerInterface::class)
);
}

Expand Down Expand Up @@ -152,6 +159,10 @@ public function testCatchesGuzzleException()
->with($request)
->willThrowException(new TransferException());

$this->logger->expects(self::once())
->method('critical')
->with('Unable to fetch JWT token', self::isType('array'));

$this->jwtParser->expects(self::once())
->method('parse')
->with('')
Expand Down

0 comments on commit 2bc2e29

Please sign in to comment.