@@ -40,18 +40,18 @@ discard block |
||
| 40 | 40 | */ |
| 41 | 41 | public function load(string $id): ?TokenInterface |
| 42 | 42 | { |
| 43 | - try { |
|
| 43 | + try{ |
|
| 44 | 44 | $tokenData = $this->session->getSection(self::SESSION_SECTION)->get('token'); |
| 45 | 45 | $token = Token::unpack($tokenData); |
| 46 | - } catch (\Throwable $e) { |
|
| 46 | + }catch (\Throwable $e){ |
|
| 47 | 47 | throw new TokenStorageException('Unable to load session token', $e->getCode(), $e); |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - if (!hash_equals($token->getID(), $id)) { |
|
| 50 | + if (!hash_equals($token->getID(), $id)){ |
|
| 51 | 51 | return null; |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | - if ($token->getExpiresAt() !== null && $token->getExpiresAt() > new \DateTime()) { |
|
| 54 | + if ($token->getExpiresAt() !== null && $token->getExpiresAt() > new \DateTime()){ |
|
| 55 | 55 | $this->delete($token); |
| 56 | 56 | return null; |
| 57 | 57 | } |
@@ -64,12 +64,12 @@ discard block |
||
| 64 | 64 | */ |
| 65 | 65 | public function create(array $payload, \DateTimeInterface $expiresAt = null): TokenInterface |
| 66 | 66 | { |
| 67 | - try { |
|
| 67 | + try{ |
|
| 68 | 68 | $token = new Token($this->randomHash(128), $payload, $expiresAt); |
| 69 | 69 | $this->session->getSection(self::SESSION_SECTION)->set('token', $token->pack()); |
| 70 | 70 | |
| 71 | 71 | return $token; |
| 72 | - } catch (\Throwable $e) { |
|
| 72 | + }catch (\Throwable $e){ |
|
| 73 | 73 | throw new TokenStorageException('Unable to create session token', $e->getCode(), $e); |
| 74 | 74 | } |
| 75 | 75 | } |
@@ -40,18 +40,23 @@ discard block |
||
| 40 | 40 | */ |
| 41 | 41 | public function load(string $id): ?TokenInterface |
| 42 | 42 | { |
| 43 | - try { |
|
| 43 | + try |
|
| 44 | + { |
|
| 44 | 45 | $tokenData = $this->session->getSection(self::SESSION_SECTION)->get('token'); |
| 45 | 46 | $token = Token::unpack($tokenData); |
| 46 | - } catch (\Throwable $e) { |
|
| 47 | + } |
|
| 48 | + catch (\Throwable $e) |
|
| 49 | + { |
|
| 47 | 50 | throw new TokenStorageException('Unable to load session token', $e->getCode(), $e); |
| 48 | 51 | } |
| 49 | 52 | |
| 50 | - if (!hash_equals($token->getID(), $id)) { |
|
| 53 | + if (!hash_equals($token->getID(), $id)) |
|
| 54 | + { |
|
| 51 | 55 | return null; |
| 52 | 56 | } |
| 53 | 57 | |
| 54 | - if ($token->getExpiresAt() !== null && $token->getExpiresAt() > new \DateTime()) { |
|
| 58 | + if ($token->getExpiresAt() !== null && $token->getExpiresAt() > new \DateTime()) |
|
| 59 | + { |
|
| 55 | 60 | $this->delete($token); |
| 56 | 61 | return null; |
| 57 | 62 | } |
@@ -64,12 +69,15 @@ discard block |
||
| 64 | 69 | */ |
| 65 | 70 | public function create(array $payload, \DateTimeInterface $expiresAt = null): TokenInterface |
| 66 | 71 | { |
| 67 | - try { |
|
| 72 | + try |
|
| 73 | + { |
|
| 68 | 74 | $token = new Token($this->randomHash(128), $payload, $expiresAt); |
| 69 | 75 | $this->session->getSection(self::SESSION_SECTION)->set('token', $token->pack()); |
| 70 | 76 | |
| 71 | 77 | return $token; |
| 72 | - } catch (\Throwable $e) { |
|
| 78 | + } |
|
| 79 | + catch (\Throwable $e) |
|
| 80 | + { |
|
| 73 | 81 | throw new TokenStorageException('Unable to create session token', $e->getCode(), $e); |
| 74 | 82 | } |
| 75 | 83 | } |
@@ -38,25 +38,25 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | public function load(string $id): ?TokenInterface |
| 40 | 40 | { |
| 41 | - if (strpos($id, ':') === false) { |
|
| 41 | + if (strpos($id, ':') === false){ |
|
| 42 | 42 | return null; |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | [$pk, $hash] = explode(':', $id, 2); |
| 46 | 46 | |
| 47 | - if (!is_numeric($pk)) { |
|
| 47 | + if (!is_numeric($pk)){ |
|
| 48 | 48 | return null; |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | /** @var TokenInterface $token */ |
| 52 | 52 | $token = $this->orm->getRepository(Token::class)->findByPK((int)$pk); |
| 53 | 53 | |
| 54 | - if ($token === null || !hash_equals($token->getID(), $id)) { |
|
| 54 | + if ($token === null || !hash_equals($token->getID(), $id)){ |
|
| 55 | 55 | // hijacked or deleted |
| 56 | 56 | return null; |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | - if ($token->getExpiresAt() !== null && $token->getExpiresAt() < new \DateTime()) { |
|
| 59 | + if ($token->getExpiresAt() !== null && $token->getExpiresAt() < new \DateTime()){ |
|
| 60 | 60 | $this->delete($token); |
| 61 | 61 | return null; |
| 62 | 62 | } |
@@ -69,13 +69,13 @@ discard block |
||
| 69 | 69 | */ |
| 70 | 70 | public function create(array $payload, \DateTimeInterface $expiresAt = null): TokenInterface |
| 71 | 71 | { |
| 72 | - try { |
|
| 72 | + try{ |
|
| 73 | 73 | $token = new Token($this->randomHash(128), $payload, new \DateTimeImmutable(), $expiresAt); |
| 74 | 74 | |
| 75 | 75 | (new Transaction($this->orm))->persist($token)->run(); |
| 76 | 76 | |
| 77 | 77 | return $token; |
| 78 | - } catch (\Throwable $e) { |
|
| 78 | + }catch (\Throwable $e){ |
|
| 79 | 79 | throw new TokenStorageException('Unable to create token', $e->getCode(), $e); |
| 80 | 80 | } |
| 81 | 81 | } |
@@ -85,9 +85,9 @@ discard block |
||
| 85 | 85 | */ |
| 86 | 86 | public function delete(TokenInterface $token): void |
| 87 | 87 | { |
| 88 | - try { |
|
| 88 | + try{ |
|
| 89 | 89 | (new Transaction($this->orm))->delete($token)->run(); |
| 90 | - } catch (\Throwable $e) { |
|
| 90 | + }catch (\Throwable $e){ |
|
| 91 | 91 | throw new TokenStorageException('Unable to delete token', $e->getCode(), $e); |
| 92 | 92 | } |
| 93 | 93 | } |
@@ -38,25 +38,29 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | public function load(string $id): ?TokenInterface |
| 40 | 40 | { |
| 41 | - if (strpos($id, ':') === false) { |
|
| 41 | + if (strpos($id, ':') === false) |
|
| 42 | + { |
|
| 42 | 43 | return null; |
| 43 | 44 | } |
| 44 | 45 | |
| 45 | 46 | [$pk, $hash] = explode(':', $id, 2); |
| 46 | 47 | |
| 47 | - if (!is_numeric($pk)) { |
|
| 48 | + if (!is_numeric($pk)) |
|
| 49 | + { |
|
| 48 | 50 | return null; |
| 49 | 51 | } |
| 50 | 52 | |
| 51 | 53 | /** @var TokenInterface $token */ |
| 52 | 54 | $token = $this->orm->getRepository(Token::class)->findByPK((int)$pk); |
| 53 | 55 | |
| 54 | - if ($token === null || !hash_equals($token->getID(), $id)) { |
|
| 56 | + if ($token === null || !hash_equals($token->getID(), $id)) |
|
| 57 | + { |
|
| 55 | 58 | // hijacked or deleted |
| 56 | 59 | return null; |
| 57 | 60 | } |
| 58 | 61 | |
| 59 | - if ($token->getExpiresAt() !== null && $token->getExpiresAt() < new \DateTime()) { |
|
| 62 | + if ($token->getExpiresAt() !== null && $token->getExpiresAt() < new \DateTime()) |
|
| 63 | + { |
|
| 60 | 64 | $this->delete($token); |
| 61 | 65 | return null; |
| 62 | 66 | } |
@@ -69,13 +73,16 @@ discard block |
||
| 69 | 73 | */ |
| 70 | 74 | public function create(array $payload, \DateTimeInterface $expiresAt = null): TokenInterface |
| 71 | 75 | { |
| 72 | - try { |
|
| 76 | + try |
|
| 77 | + { |
|
| 73 | 78 | $token = new Token($this->randomHash(128), $payload, new \DateTimeImmutable(), $expiresAt); |
| 74 | 79 | |
| 75 | 80 | (new Transaction($this->orm))->persist($token)->run(); |
| 76 | 81 | |
| 77 | 82 | return $token; |
| 78 | - } catch (\Throwable $e) { |
|
| 83 | + } |
|
| 84 | + catch (\Throwable $e) |
|
| 85 | + { |
|
| 79 | 86 | throw new TokenStorageException('Unable to create token', $e->getCode(), $e); |
| 80 | 87 | } |
| 81 | 88 | } |
@@ -85,9 +92,12 @@ discard block |
||
| 85 | 92 | */ |
| 86 | 93 | public function delete(TokenInterface $token): void |
| 87 | 94 | { |
| 88 | - try { |
|
| 95 | + try |
|
| 96 | + { |
|
| 89 | 97 | (new Transaction($this->orm))->delete($token)->run(); |
| 90 | - } catch (\Throwable $e) { |
|
| 98 | + } |
|
| 99 | + catch (\Throwable $e) |
|
| 100 | + { |
|
| 91 | 101 | throw new TokenStorageException('Unable to delete token', $e->getCode(), $e); |
| 92 | 102 | } |
| 93 | 103 | } |