@@ -41,8 +41,8 @@ discard block |
||
| 41 | 41 | public function getTransports(): array |
| 42 | 42 | { |
| 43 | 43 | $transports = []; |
| 44 | - foreach ($this->config['transports'] as $name => $transport) { |
|
| 45 | - if (is_object($transport) && !$transport instanceof Autowire) { |
|
| 44 | + foreach ($this->config['transports'] as $name => $transport){ |
|
| 45 | + if (is_object($transport) && !$transport instanceof Autowire){ |
|
| 46 | 46 | $transports[$name] = $transport; |
| 47 | 47 | continue; |
| 48 | 48 | } |
@@ -61,15 +61,15 @@ discard block |
||
| 61 | 61 | */ |
| 62 | 62 | private function wire($item): Autowire |
| 63 | 63 | { |
| 64 | - if ($item instanceof Autowire) { |
|
| 64 | + if ($item instanceof Autowire){ |
|
| 65 | 65 | return $item; |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | - if (is_string($item)) { |
|
| 68 | + if (is_string($item)){ |
|
| 69 | 69 | return new Autowire($item); |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | - if (is_array($item) && isset($item['class'])) { |
|
| 72 | + if (is_array($item) && isset($item['class'])){ |
|
| 73 | 73 | return new Autowire($item['class'], $item['options'] ?? []); |
| 74 | 74 | } |
| 75 | 75 | |
@@ -41,8 +41,10 @@ discard block |
||
| 41 | 41 | public function getTransports(): array |
| 42 | 42 | { |
| 43 | 43 | $transports = []; |
| 44 | - foreach ($this->config['transports'] as $name => $transport) { |
|
| 45 | - if (is_object($transport) && !$transport instanceof Autowire) { |
|
| 44 | + foreach ($this->config['transports'] as $name => $transport) |
|
| 45 | + { |
|
| 46 | + if (is_object($transport) && !$transport instanceof Autowire) |
|
| 47 | + { |
|
| 46 | 48 | $transports[$name] = $transport; |
| 47 | 49 | continue; |
| 48 | 50 | } |
@@ -61,15 +63,18 @@ discard block |
||
| 61 | 63 | */ |
| 62 | 64 | private function wire($item): Autowire |
| 63 | 65 | { |
| 64 | - if ($item instanceof Autowire) { |
|
| 66 | + if ($item instanceof Autowire) |
|
| 67 | + { |
|
| 65 | 68 | return $item; |
| 66 | 69 | } |
| 67 | 70 | |
| 68 | - if (is_string($item)) { |
|
| 71 | + if (is_string($item)) |
|
| 72 | + { |
|
| 69 | 73 | return new Autowire($item); |
| 70 | 74 | } |
| 71 | 75 | |
| 72 | - if (is_array($item) && isset($item['class'])) { |
|
| 76 | + if (is_array($item) && isset($item['class'])) |
|
| 77 | + { |
|
| 73 | 78 | return new Autowire($item['class'], $item['options'] ?? []); |
| 74 | 79 | } |
| 75 | 80 | |
@@ -100,7 +100,7 @@ |
||
| 100 | 100 | public static function unpack(array $data): Token |
| 101 | 101 | { |
| 102 | 102 | $expiresAt = null; |
| 103 | - if ($data['expiresAt'] != null) { |
|
| 103 | + if ($data['expiresAt'] != null){ |
|
| 104 | 104 | $expiresAt = (new \DateTimeImmutable())->setTimestamp($data['expiresAt']); |
| 105 | 105 | } |
| 106 | 106 | |
@@ -100,7 +100,8 @@ |
||
| 100 | 100 | public static function unpack(array $data): Token |
| 101 | 101 | { |
| 102 | 102 | $expiresAt = null; |
| 103 | - if ($data['expiresAt'] != null) { |
|
| 103 | + if ($data['expiresAt'] != null) |
|
| 104 | + { |
|
| 104 | 105 | $expiresAt = (new \DateTimeImmutable())->setTimestamp($data['expiresAt']); |
| 105 | 106 | } |
| 106 | 107 | |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | CookiesConfig $cookiesConfig, |
| 61 | 61 | SessionFactory $factory, |
| 62 | 62 | ScopeInterface $scope |
| 63 | - ) { |
|
| 63 | + ){ |
|
| 64 | 64 | $this->config = $config; |
| 65 | 65 | $this->httpConfig = $httpConfig; |
| 66 | 66 | $this->cookiesConfig = $cookiesConfig; |
@@ -79,14 +79,14 @@ discard block |
||
| 79 | 79 | $this->fetchID($request) |
| 80 | 80 | ); |
| 81 | 81 | |
| 82 | - try { |
|
| 82 | + try{ |
|
| 83 | 83 | $response = $this->scope->runScope( |
| 84 | 84 | [SessionInterface::class => $session], |
| 85 | 85 | function () use ($handler, $request, $session) { |
| 86 | 86 | return $handler->handle($request->withAttribute(static::ATTRIBUTE, $session)); |
| 87 | 87 | } |
| 88 | 88 | ); |
| 89 | - } catch (\Throwable $e) { |
|
| 89 | + }catch (\Throwable $e){ |
|
| 90 | 90 | $session->abort(); |
| 91 | 91 | throw $e; |
| 92 | 92 | } |
@@ -105,14 +105,14 @@ discard block |
||
| 105 | 105 | Request $request, |
| 106 | 106 | Response $response |
| 107 | 107 | ): Response { |
| 108 | - if (!$session->isStarted()) { |
|
| 108 | + if (!$session->isStarted()){ |
|
| 109 | 109 | return $response; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | $session->commit(); |
| 113 | 113 | |
| 114 | 114 | //SID changed |
| 115 | - if ($this->fetchID($request) != $session->getID()) { |
|
| 115 | + if ($this->fetchID($request) != $session->getID()){ |
|
| 116 | 116 | return $this->withCookie($request, $response, $session->getID()); |
| 117 | 117 | } |
| 118 | 118 | |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | protected function fetchID(Request $request): ?string |
| 130 | 130 | { |
| 131 | 131 | $cookies = $request->getCookieParams(); |
| 132 | - if (empty($cookies[$this->config->getCookie()])) { |
|
| 132 | + if (empty($cookies[$this->config->getCookie()])){ |
|
| 133 | 133 | return null; |
| 134 | 134 | } |
| 135 | 135 | |
@@ -162,8 +162,8 @@ discard block |
||
| 162 | 162 | protected function clientSignature(Request $request): string |
| 163 | 163 | { |
| 164 | 164 | $signature = ''; |
| 165 | - foreach (static::SIGNATURE_HEADERS as $header) { |
|
| 166 | - $signature .= $request->getHeaderLine($header) . ';'; |
|
| 165 | + foreach (static::SIGNATURE_HEADERS as $header){ |
|
| 166 | + $signature .= $request->getHeaderLine($header).';'; |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | return hash('sha256', $signature); |
@@ -79,14 +79,18 @@ discard block |
||
| 79 | 79 | $this->fetchID($request) |
| 80 | 80 | ); |
| 81 | 81 | |
| 82 | - try { |
|
| 82 | + try |
|
| 83 | + { |
|
| 83 | 84 | $response = $this->scope->runScope( |
| 84 | 85 | [SessionInterface::class => $session], |
| 85 | - function () use ($handler, $request, $session) { |
|
| 86 | + function () use ($handler, $request, $session) |
|
| 87 | + { |
|
| 86 | 88 | return $handler->handle($request->withAttribute(static::ATTRIBUTE, $session)); |
| 87 | 89 | } |
| 88 | 90 | ); |
| 89 | - } catch (\Throwable $e) { |
|
| 91 | + } |
|
| 92 | + catch (\Throwable $e) |
|
| 93 | + { |
|
| 90 | 94 | $session->abort(); |
| 91 | 95 | throw $e; |
| 92 | 96 | } |
@@ -105,14 +109,16 @@ discard block |
||
| 105 | 109 | Request $request, |
| 106 | 110 | Response $response |
| 107 | 111 | ): Response { |
| 108 | - if (!$session->isStarted()) { |
|
| 112 | + if (!$session->isStarted()) |
|
| 113 | + { |
|
| 109 | 114 | return $response; |
| 110 | 115 | } |
| 111 | 116 | |
| 112 | 117 | $session->commit(); |
| 113 | 118 | |
| 114 | 119 | //SID changed |
| 115 | - if ($this->fetchID($request) != $session->getID()) { |
|
| 120 | + if ($this->fetchID($request) != $session->getID()) |
|
| 121 | + { |
|
| 116 | 122 | return $this->withCookie($request, $response, $session->getID()); |
| 117 | 123 | } |
| 118 | 124 | |
@@ -129,7 +135,8 @@ discard block |
||
| 129 | 135 | protected function fetchID(Request $request): ?string |
| 130 | 136 | { |
| 131 | 137 | $cookies = $request->getCookieParams(); |
| 132 | - if (empty($cookies[$this->config->getCookie()])) { |
|
| 138 | + if (empty($cookies[$this->config->getCookie()])) |
|
| 139 | + { |
|
| 133 | 140 | return null; |
| 134 | 141 | } |
| 135 | 142 | |
@@ -162,7 +169,8 @@ discard block |
||
| 162 | 169 | protected function clientSignature(Request $request): string |
| 163 | 170 | { |
| 164 | 171 | $signature = ''; |
| 165 | - foreach (static::SIGNATURE_HEADERS as $header) { |
|
| 172 | + foreach (static::SIGNATURE_HEADERS as $header) |
|
| 173 | + { |
|
| 166 | 174 | $signature .= $request->getHeaderLine($header) . ';'; |
| 167 | 175 | } |
| 168 | 176 | |
@@ -49,7 +49,7 @@ |
||
| 49 | 49 | new Route('/auth/<action>', new Controller(AuthController::class)) |
| 50 | 50 | ); |
| 51 | 51 | |
| 52 | - $views->addDirectory('custom', __DIR__ . '/../../views/custom/'); |
|
| 52 | + $views->addDirectory('custom', __DIR__.'/../../views/custom/'); |
|
| 53 | 53 | $views->addEngine(TestEngine::class); |
| 54 | 54 | |
| 55 | 55 | $validation->addAlias('aliased', 'notEmpty'); |
@@ -21,7 +21,7 @@ |
||
| 21 | 21 | { |
| 22 | 22 | public function boot(EnvironmentInterface $env, BootloadManager $bootloadManager): void |
| 23 | 23 | { |
| 24 | - if ($env->get('CYCLE_AUTH')) { |
|
| 24 | + if ($env->get('CYCLE_AUTH')){ |
|
| 25 | 25 | $bootloadManager->bootload([CycleTokensBootloader::class]); |
| 26 | 26 | return; |
| 27 | 27 | } |
@@ -21,7 +21,8 @@ |
||
| 21 | 21 | { |
| 22 | 22 | public function boot(EnvironmentInterface $env, BootloadManager $bootloadManager): void |
| 23 | 23 | { |
| 24 | - if ($env->get('CYCLE_AUTH')) { |
|
| 24 | + if ($env->get('CYCLE_AUTH')) |
|
| 25 | + { |
|
| 25 | 26 | $bootloadManager->bootload([CycleTokensBootloader::class]); |
| 26 | 27 | return; |
| 27 | 28 | } |
@@ -28,7 +28,7 @@ |
||
| 28 | 28 | |
| 29 | 29 | public function tokenAction(AuthContextInterface $authContext) |
| 30 | 30 | { |
| 31 | - if ($authContext->getToken() !== null) { |
|
| 31 | + if ($authContext->getToken() !== null){ |
|
| 32 | 32 | return $authContext->getToken()->getID(); |
| 33 | 33 | } |
| 34 | 34 | |
@@ -28,7 +28,8 @@ |
||
| 28 | 28 | |
| 29 | 29 | public function tokenAction(AuthContextInterface $authContext) |
| 30 | 30 | { |
| 31 | - if ($authContext->getToken() !== null) { |
|
| 31 | + if ($authContext->getToken() !== null) |
|
| 32 | + { |
|
| 32 | 33 | return $authContext->getToken()->getID(); |
| 33 | 34 | } |
| 34 | 35 | |
@@ -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 | } |
@@ -50,7 +50,7 @@ |
||
| 50 | 50 | array $payload, |
| 51 | 51 | \DateTimeImmutable $createdAt, |
| 52 | 52 | \DateTimeInterface $expiresAt = null |
| 53 | - ) { |
|
| 53 | + ){ |
|
| 54 | 54 | $this->id = $id; |
| 55 | 55 | |
| 56 | 56 | $this->secretValue = $secretValue; |
@@ -38,7 +38,7 @@ 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 | |
@@ -47,14 +47,14 @@ discard block |
||
| 47 | 47 | /** @var Token $token */ |
| 48 | 48 | $token = $this->orm->getRepository(Token::class)->findByPK($pk); |
| 49 | 49 | |
| 50 | - if ($token === null || !hash_equals($token->getHashedValue(), hash('sha512', $hash))) { |
|
| 50 | + if ($token === null || !hash_equals($token->getHashedValue(), hash('sha512', $hash))){ |
|
| 51 | 51 | // hijacked or deleted |
| 52 | 52 | return null; |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | $token->setSecretValue($hash); |
| 56 | 56 | |
| 57 | - if ($token->getExpiresAt() !== null && $token->getExpiresAt() < new \DateTime()) { |
|
| 57 | + if ($token->getExpiresAt() !== null && $token->getExpiresAt() < new \DateTime()){ |
|
| 58 | 58 | $this->delete($token); |
| 59 | 59 | return null; |
| 60 | 60 | } |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | */ |
| 68 | 68 | public function create(array $payload, \DateTimeInterface $expiresAt = null): TokenInterface |
| 69 | 69 | { |
| 70 | - try { |
|
| 70 | + try{ |
|
| 71 | 71 | $token = new Token( |
| 72 | 72 | $this->issueID(), |
| 73 | 73 | $this->randomHash(128), |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | (new Transaction($this->orm))->persist($token)->run(); |
| 80 | 80 | |
| 81 | 81 | return $token; |
| 82 | - } catch (\Throwable $e) { |
|
| 82 | + }catch (\Throwable $e){ |
|
| 83 | 83 | throw new TokenStorageException('Unable to create token', $e->getCode(), $e); |
| 84 | 84 | } |
| 85 | 85 | } |
@@ -89,9 +89,9 @@ discard block |
||
| 89 | 89 | */ |
| 90 | 90 | public function delete(TokenInterface $token): void |
| 91 | 91 | { |
| 92 | - try { |
|
| 92 | + try{ |
|
| 93 | 93 | (new Transaction($this->orm))->delete($token)->run(); |
| 94 | - } catch (\Throwable $e) { |
|
| 94 | + }catch (\Throwable $e){ |
|
| 95 | 95 | throw new TokenStorageException('Unable to delete token', $e->getCode(), $e); |
| 96 | 96 | } |
| 97 | 97 | } |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | $this->orm->getSource(Token::class)->getTable() |
| 111 | 111 | ); |
| 112 | 112 | |
| 113 | - while ((clone $query)->where('id', $id)->count('id') !== 0) { |
|
| 113 | + while ((clone $query)->where('id', $id)->count('id') !== 0){ |
|
| 114 | 114 | $id = $this->randomHash(64); |
| 115 | 115 | } |
| 116 | 116 | |
@@ -38,7 +38,8 @@ 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 | |
@@ -47,14 +48,16 @@ discard block |
||
| 47 | 48 | /** @var Token $token */ |
| 48 | 49 | $token = $this->orm->getRepository(Token::class)->findByPK($pk); |
| 49 | 50 | |
| 50 | - if ($token === null || !hash_equals($token->getHashedValue(), hash('sha512', $hash))) { |
|
| 51 | + if ($token === null || !hash_equals($token->getHashedValue(), hash('sha512', $hash))) |
|
| 52 | + { |
|
| 51 | 53 | // hijacked or deleted |
| 52 | 54 | return null; |
| 53 | 55 | } |
| 54 | 56 | |
| 55 | 57 | $token->setSecretValue($hash); |
| 56 | 58 | |
| 57 | - if ($token->getExpiresAt() !== null && $token->getExpiresAt() < new \DateTime()) { |
|
| 59 | + if ($token->getExpiresAt() !== null && $token->getExpiresAt() < new \DateTime()) |
|
| 60 | + { |
|
| 58 | 61 | $this->delete($token); |
| 59 | 62 | return null; |
| 60 | 63 | } |
@@ -67,7 +70,8 @@ discard block |
||
| 67 | 70 | */ |
| 68 | 71 | public function create(array $payload, \DateTimeInterface $expiresAt = null): TokenInterface |
| 69 | 72 | { |
| 70 | - try { |
|
| 73 | + try |
|
| 74 | + { |
|
| 71 | 75 | $token = new Token( |
| 72 | 76 | $this->issueID(), |
| 73 | 77 | $this->randomHash(128), |
@@ -79,7 +83,9 @@ discard block |
||
| 79 | 83 | (new Transaction($this->orm))->persist($token)->run(); |
| 80 | 84 | |
| 81 | 85 | return $token; |
| 82 | - } catch (\Throwable $e) { |
|
| 86 | + } |
|
| 87 | + catch (\Throwable $e) |
|
| 88 | + { |
|
| 83 | 89 | throw new TokenStorageException('Unable to create token', $e->getCode(), $e); |
| 84 | 90 | } |
| 85 | 91 | } |
@@ -89,9 +95,12 @@ discard block |
||
| 89 | 95 | */ |
| 90 | 96 | public function delete(TokenInterface $token): void |
| 91 | 97 | { |
| 92 | - try { |
|
| 98 | + try |
|
| 99 | + { |
|
| 93 | 100 | (new Transaction($this->orm))->delete($token)->run(); |
| 94 | - } catch (\Throwable $e) { |
|
| 101 | + } |
|
| 102 | + catch (\Throwable $e) |
|
| 103 | + { |
|
| 95 | 104 | throw new TokenStorageException('Unable to delete token', $e->getCode(), $e); |
| 96 | 105 | } |
| 97 | 106 | } |
@@ -110,7 +119,8 @@ discard block |
||
| 110 | 119 | $this->orm->getSource(Token::class)->getTable() |
| 111 | 120 | ); |
| 112 | 121 | |
| 113 | - while ((clone $query)->where('id', $id)->count('id') !== 0) { |
|
| 122 | + while ((clone $query)->where('id', $id)->count('id') !== 0) |
|
| 123 | + { |
|
| 114 | 124 | $id = $this->randomHash(64); |
| 115 | 125 | } |
| 116 | 126 | |