|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bunq\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Bunq\Exception\SessionWasExpiredException; |
|
6
|
|
|
use Bunq\Service\InstallationService; |
|
7
|
|
|
use Bunq\Token\DefaultToken; |
|
8
|
|
|
use Bunq\Token\Storage\TokenStorage; |
|
9
|
|
|
use Bunq\Token\TokenType; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* bunq documentation: |
|
14
|
|
|
* A session expires after the same amount of time you have set for auto logout in your user account. If a request is |
|
15
|
|
|
* made 30 seconds before a session expires, it will automatically be extended. |
|
16
|
|
|
* |
|
17
|
|
|
* This middleware detects if the session has expired, if so, the middleware will renew the session and throws an |
|
18
|
|
|
* exception to let the client know it can be retried. |
|
19
|
|
|
*/ |
|
20
|
|
|
final class RefreshSessionMiddleware |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var InstallationService |
|
24
|
|
|
*/ |
|
25
|
|
|
private $installationService; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var TokenStorage |
|
29
|
|
|
*/ |
|
30
|
|
|
private $tokenStorage; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param InstallationService $installationService |
|
34
|
|
|
* @param TokenStorage $tokenStorage |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct( |
|
37
|
|
|
InstallationService $installationService, |
|
38
|
|
|
TokenStorage $tokenStorage |
|
39
|
|
|
) { |
|
40
|
|
|
$this->installationService = $installationService; |
|
41
|
|
|
$this->tokenStorage = $tokenStorage; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param ResponseInterface $response |
|
46
|
|
|
* |
|
47
|
|
|
* @return ResponseInterface |
|
48
|
|
|
* @throws \Exception |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __invoke(ResponseInterface $response) |
|
51
|
|
|
{ |
|
52
|
|
|
if ($response->getStatusCode() !== 401) { |
|
53
|
|
|
return $response; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$responseBody = \json_decode($response->getBody()->__toString(), true); |
|
57
|
|
|
|
|
58
|
|
|
if ($responseBody['Error'][0]['error_description'] !== 'Insufficient authorisation.') { |
|
59
|
|
|
return $response; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// make new session |
|
63
|
|
|
|
|
64
|
|
|
$installationToken = $currentInstalltionToken = $this->tokenStorage->load(TokenType::INSTALLATION_TOKEN()); |
|
|
|
|
|
|
65
|
|
|
$session = $this->installationService->createSession($installationToken); |
|
66
|
|
|
|
|
67
|
|
|
$sessionToken = DefaultToken::fromString($session); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
$this->tokenStorage->save($sessionToken, TokenType::SESSION_TOKEN()); |
|
70
|
|
|
|
|
71
|
|
|
throw new SessionWasExpiredException(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.