-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from verschoof/refresh-session
Add RefreshSessionMiddleware
- Loading branch information
Showing
9 changed files
with
256 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Bunq\Exception; | ||
|
||
final class SessionWasExpiredException extends \Exception | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('Session has expired should now be refreshed', 400); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Bunq\Middleware; | ||
|
||
use Bunq\Exception\SessionWasExpiredException; | ||
use Bunq\Service\InstallationService; | ||
use Bunq\Token\DefaultToken; | ||
use Bunq\Token\Storage\TokenStorage; | ||
use Bunq\Token\TokenType; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* bunq documentation: | ||
* A session expires after the same amount of time you have set for auto logout in your user account. If a request is | ||
* made 30 seconds before a session expires, it will automatically be extended. | ||
* | ||
* This middleware detects if the session has expired, if so, the middleware will renew the session and throws an | ||
* exception to let the client know it can be retried. | ||
*/ | ||
final class RefreshSessionMiddleware | ||
{ | ||
/** | ||
* @var InstallationService | ||
*/ | ||
private $installationService; | ||
|
||
/** | ||
* @var TokenStorage | ||
*/ | ||
private $tokenStorage; | ||
|
||
/** | ||
* @param InstallationService $installationService | ||
* @param TokenStorage $tokenStorage | ||
*/ | ||
public function __construct( | ||
InstallationService $installationService, | ||
TokenStorage $tokenStorage | ||
) { | ||
$this->installationService = $installationService; | ||
$this->tokenStorage = $tokenStorage; | ||
} | ||
|
||
/** | ||
* @param ResponseInterface $response | ||
* | ||
* @return ResponseInterface | ||
* @throws \Exception | ||
*/ | ||
public function __invoke(ResponseInterface $response) | ||
{ | ||
if ($response->getStatusCode() !== 401) { | ||
return $response; | ||
} | ||
|
||
$responseBody = \json_decode($response->getBody()->__toString(), true); | ||
|
||
if ($responseBody['Error'][0]['error_description'] !== 'Insufficient authorisation.') { | ||
return $response; | ||
} | ||
|
||
// make new session | ||
|
||
$installationToken = $currentInstalltionToken = $this->tokenStorage->load(TokenType::INSTALLATION_TOKEN()); | ||
$session = $this->installationService->createSession($installationToken); | ||
|
||
$sessionToken = DefaultToken::fromString($session); | ||
|
||
$this->tokenStorage->save($sessionToken, TokenType::SESSION_TOKEN()); | ||
|
||
throw new SessionWasExpiredException(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Bunq\Tests\Exception; | ||
|
||
use Bunq\Exception\SessionWasExpiredException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SessionWasExpiredExceptionTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function itRepresentsASessionWasExpiredException() | ||
{ | ||
$sessionWasExpiredException = new SessionWasExpiredException(); | ||
|
||
$this->assertInstanceOf(SessionWasExpiredException::class, $sessionWasExpiredException); | ||
$this->assertInstanceOf(\Exception::class, $sessionWasExpiredException); | ||
$this->assertSame('Session has expired should now be refreshed', $sessionWasExpiredException->getMessage()); | ||
$this->assertSame(400, $sessionWasExpiredException->getCode()); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Bunq\Tests\Middleware; | ||
|
||
use Bunq\Middleware\RefreshSessionMiddleware; | ||
use Bunq\Service\InstallationService; | ||
use Bunq\Token\DefaultToken; | ||
use Bunq\Token\Storage\TokenStorage; | ||
use Bunq\Token\TokenType; | ||
use GuzzleHttp\Psr7\Response; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
final class RefreshSessionMiddlewareTest extends TestCase | ||
{ | ||
/** | ||
* @var InstallationService|ObjectProphecy | ||
*/ | ||
private $installationService; | ||
|
||
/** | ||
* @var TokenStorage|ObjectProphecy | ||
*/ | ||
private $tokenStorage; | ||
|
||
/** | ||
* @var RefreshSessionMiddleware | ||
*/ | ||
private $middleware; | ||
|
||
public function setUp() | ||
{ | ||
$this->installationService = $this->prophesize(InstallationService::class); | ||
$this->tokenStorage = $this->prophesize(TokenStorage::class); | ||
|
||
$this->middleware = new RefreshSessionMiddleware( | ||
$this->installationService->reveal(), | ||
$this->tokenStorage->reveal() | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function itWontGetExecutedWhenResponseIsNot401() | ||
{ | ||
$response = new Response(200, [], '{"ok"}'); | ||
|
||
$this->tokenStorage->load(Argument::any())->shouldNotBeCalled(); | ||
|
||
$resultResponse = $this->middleware->__invoke($response); | ||
|
||
$this->assertSame($response, $resultResponse); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function itWontGetExecutedWhenResponseIsNotInsufficientAuthorisation() | ||
{ | ||
$body = '{"Error":[{"error_description": "An other error"}]}'; | ||
$response = new Response(401, [], $body); | ||
|
||
$this->tokenStorage->load(Argument::any())->shouldNotBeCalled(); | ||
|
||
$resultResponse = $this->middleware->__invoke($response); | ||
|
||
$this->assertEquals($response, $resultResponse); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \Bunq\Exception\SessionWasExpiredException | ||
*/ | ||
public function itWillRefreshSessionAndThrowsAnExceptionIfInsufficientAuthorisation() | ||
{ | ||
$body = '{"Error":[{"error_description": "Insufficient authorisation."}]}'; | ||
$response = new Response(401, [], $body); | ||
|
||
$installationToken = new DefaultToken('Installation Token'); | ||
$this->tokenStorage->load(TokenType::INSTALLATION_TOKEN())->willReturn($installationToken); | ||
$this->installationService->createSession($installationToken)->willReturn('someSessionId'); | ||
|
||
$sessionToken = DefaultToken::fromString('someSessionId'); | ||
|
||
$this->tokenStorage->save($sessionToken, TokenType::SESSION_TOKEN())->shouldBeCalled(); | ||
|
||
$this->middleware->__invoke($response); | ||
} | ||
} | ||
|