@@ -123,9 +123,12 @@ |
||
| 123 | 123 | */ |
| 124 | 124 | public function getActiveSession(): SessionInterface |
| 125 | 125 | { |
| 126 | - try { |
|
| 126 | + try |
|
| 127 | + { |
|
| 127 | 128 | return $this->container->get(SessionInterface::class); |
| 128 | - } catch (NotFoundExceptionInterface $e) { |
|
| 129 | + } |
|
| 130 | + catch (NotFoundExceptionInterface $e) |
|
| 131 | + { |
|
| 129 | 132 | throw new ScopeException('Unable to receive active session, invalid request scope', $e->getCode(), $e); |
| 130 | 133 | } |
| 131 | 134 | } |
@@ -169,9 +169,12 @@ discard block |
||
| 169 | 169 | */ |
| 170 | 170 | private function getRequest(): ServerRequestInterface |
| 171 | 171 | { |
| 172 | - try { |
|
| 172 | + try |
|
| 173 | + { |
|
| 173 | 174 | return $this->container->get(ServerRequestInterface::class); |
| 174 | - } catch (NotFoundExceptionInterface $e) { |
|
| 175 | + } |
|
| 176 | + catch (NotFoundExceptionInterface $e) |
|
| 177 | + { |
|
| 175 | 178 | throw new ScopeException('Unable to receive active request', $e->getCode(), $e); |
| 176 | 179 | } |
| 177 | 180 | } |
@@ -183,9 +186,12 @@ discard block |
||
| 183 | 186 | */ |
| 184 | 187 | private function getCookieQueue(): CookieQueue |
| 185 | 188 | { |
| 186 | - try { |
|
| 189 | + try |
|
| 190 | + { |
|
| 187 | 191 | return $this->container->get(CookieQueue::class); |
| 188 | - } catch (NotFoundExceptionInterface $e) { |
|
| 192 | + } |
|
| 193 | + catch (NotFoundExceptionInterface $e) |
|
| 194 | + { |
|
| 189 | 195 | throw new ScopeException('Unable to receive cookie queue, invalid request scope', $e->getCode(), $e); |
| 190 | 196 | } |
| 191 | 197 | } |
@@ -98,7 +98,8 @@ |
||
| 98 | 98 | public static function unpack(array $data): Token |
| 99 | 99 | { |
| 100 | 100 | $expiresAt = null; |
| 101 | - if ($data['expiredAt'] != null) { |
|
| 101 | + if ($data['expiredAt'] != null) |
|
| 102 | + { |
|
| 102 | 103 | $expiresAt = (new \DateTimeImmutable())->setTimestamp($data['expiredAt']); |
| 103 | 104 | } |
| 104 | 105 | |
@@ -94,9 +94,12 @@ |
||
| 94 | 94 | */ |
| 95 | 95 | private function getAuthContext(): AuthContextInterface |
| 96 | 96 | { |
| 97 | - try { |
|
| 97 | + try |
|
| 98 | + { |
|
| 98 | 99 | return $this->container->get(AuthContextInterface::class); |
| 99 | - } catch (NotFoundExceptionInterface $e) { |
|
| 100 | + } |
|
| 101 | + catch (NotFoundExceptionInterface $e) |
|
| 102 | + { |
|
| 100 | 103 | throw new ScopeException('Unable to resolve auth context, invalid scope', $e->getCode(), $e); |
| 101 | 104 | } |
| 102 | 105 | } |
@@ -55,8 +55,10 @@ discard block |
||
| 55 | 55 | public function allows(string $permission, array $context = []): bool |
| 56 | 56 | { |
| 57 | 57 | $allows = false; |
| 58 | - foreach ($this->getRoles() as $role) { |
|
| 59 | - if (!$this->permissions->hasRole($role)) { |
|
| 58 | + foreach ($this->getRoles() as $role) |
|
| 59 | + { |
|
| 60 | + if (!$this->permissions->hasRole($role)) |
|
| 61 | + { |
|
| 60 | 62 | continue; |
| 61 | 63 | } |
| 62 | 64 | |
@@ -103,13 +105,17 @@ discard block |
||
| 103 | 105 | */ |
| 104 | 106 | public function getActor(): ActorInterface |
| 105 | 107 | { |
| 106 | - if (!is_null($this->actor)) { |
|
| 108 | + if (!is_null($this->actor)) |
|
| 109 | + { |
|
| 107 | 110 | return $this->actor; |
| 108 | 111 | } |
| 109 | 112 | |
| 110 | - try { |
|
| 113 | + try |
|
| 114 | + { |
|
| 111 | 115 | return $this->container->get(ActorInterface::class); |
| 112 | - } catch (NotFoundExceptionInterface $e) { |
|
| 116 | + } |
|
| 117 | + catch (NotFoundExceptionInterface $e) |
|
| 118 | + { |
|
| 113 | 119 | throw new ScopeException('Unable to resolve Actor from the scope', $e->getCode(), $e); |
| 114 | 120 | } |
| 115 | 121 | } |
@@ -41,25 +41,29 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | public function load(string $id): ?TokenInterface |
| 43 | 43 | { |
| 44 | - if (strpos($id, ':') === false) { |
|
| 44 | + if (strpos($id, ':') === false) |
|
| 45 | + { |
|
| 45 | 46 | return null; |
| 46 | 47 | } |
| 47 | 48 | |
| 48 | 49 | list($pk, $hash) = explode(':', $id, 2); |
| 49 | 50 | |
| 50 | - if (!is_numeric($id)) { |
|
| 51 | + if (!is_numeric($id)) |
|
| 52 | + { |
|
| 51 | 53 | return null; |
| 52 | 54 | } |
| 53 | 55 | |
| 54 | 56 | /** @var TokenInterface $token */ |
| 55 | 57 | $token = $this->findByPK((int)$pk); |
| 56 | 58 | |
| 57 | - if ($token === null || $token->getID() !== $id) { |
|
| 59 | + if ($token === null || $token->getID() !== $id) |
|
| 60 | + { |
|
| 58 | 61 | // hijacked or deleted |
| 59 | 62 | return null; |
| 60 | 63 | } |
| 61 | 64 | |
| 62 | - if ($token->getExpiresAt() !== null && $token->getExpiresAt() < new \DateTime()) { |
|
| 65 | + if ($token->getExpiresAt() !== null && $token->getExpiresAt() < new \DateTime()) |
|
| 66 | + { |
|
| 63 | 67 | $this->delete($token); |
| 64 | 68 | return null; |
| 65 | 69 | } |
@@ -72,13 +76,16 @@ discard block |
||
| 72 | 76 | */ |
| 73 | 77 | public function create(array $payload, \DateTimeInterface $expiresAt = null): TokenInterface |
| 74 | 78 | { |
| 75 | - try { |
|
| 79 | + try |
|
| 80 | + { |
|
| 76 | 81 | $token = new Token($this->randomHash(128), $payload, new \DateTimeImmutable(), $expiresAt); |
| 77 | 82 | |
| 78 | 83 | (new Transaction($this->orm))->persist($token)->run(); |
| 79 | 84 | |
| 80 | 85 | return $token; |
| 81 | - } catch (\Throwable $e) { |
|
| 86 | + } |
|
| 87 | + catch (\Throwable $e) |
|
| 88 | + { |
|
| 82 | 89 | throw new TokenStorageException('Unable to create token', $e->getCode(), $e); |
| 83 | 90 | } |
| 84 | 91 | } |
@@ -88,9 +95,12 @@ discard block |
||
| 88 | 95 | */ |
| 89 | 96 | public function delete(TokenInterface $token): void |
| 90 | 97 | { |
| 91 | - try { |
|
| 98 | + try |
|
| 99 | + { |
|
| 92 | 100 | (new Transaction($this->orm))->delete($token)->run(); |
| 93 | - } catch (\Throwable $e) { |
|
| 101 | + } |
|
| 102 | + catch (\Throwable $e) |
|
| 103 | + { |
|
| 94 | 104 | throw new TokenStorageException('Unable to delete token', $e->getCode(), $e); |
| 95 | 105 | } |
| 96 | 106 | } |
@@ -40,8 +40,10 @@ discard block |
||
| 40 | 40 | public function getTransports(): array |
| 41 | 41 | { |
| 42 | 42 | $transports = []; |
| 43 | - foreach ($this->config['transports'] as $transport) { |
|
| 44 | - if (is_object($transport) && !$transport instanceof Autowire) { |
|
| 43 | + foreach ($this->config['transports'] as $transport) |
|
| 44 | + { |
|
| 45 | + if (is_object($transport) && !$transport instanceof Autowire) |
|
| 46 | + { |
|
| 45 | 47 | $transports[] = $transport; |
| 46 | 48 | continue; |
| 47 | 49 | } |
@@ -60,15 +62,18 @@ discard block |
||
| 60 | 62 | */ |
| 61 | 63 | private function wire($item): Autowire |
| 62 | 64 | { |
| 63 | - if ($item instanceof Autowire) { |
|
| 65 | + if ($item instanceof Autowire) |
|
| 66 | + { |
|
| 64 | 67 | return $item; |
| 65 | 68 | } |
| 66 | 69 | |
| 67 | - if (is_string($item)) { |
|
| 70 | + if (is_string($item)) |
|
| 71 | + { |
|
| 68 | 72 | return new Autowire($item); |
| 69 | 73 | } |
| 70 | 74 | |
| 71 | - if (is_array($item) && isset($item['class'])) { |
|
| 75 | + if (is_array($item) && isset($item['class'])) |
|
| 76 | + { |
|
| 72 | 77 | return new Autowire($item['class'], $item['options'] ?? []); |
| 73 | 78 | } |
| 74 | 79 | |
@@ -43,18 +43,23 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function load(string $id): ?TokenInterface |
| 45 | 45 | { |
| 46 | - try { |
|
| 46 | + try |
|
| 47 | + { |
|
| 47 | 48 | $tokenData = $this->getAuthSection()->get('token'); |
| 48 | 49 | $token = Token::unpack($tokenData); |
| 49 | - } catch (\Throwable $e) { |
|
| 50 | + } |
|
| 51 | + catch (\Throwable $e) |
|
| 52 | + { |
|
| 50 | 53 | throw new TokenStorageException('Unable to load session token', $e->getCode(), $e); |
| 51 | 54 | } |
| 52 | 55 | |
| 53 | - if ($token->getID() !== $id) { |
|
| 56 | + if ($token->getID() !== $id) |
|
| 57 | + { |
|
| 54 | 58 | return null; |
| 55 | 59 | } |
| 56 | 60 | |
| 57 | - if ($token->getExpiresAt() !== null && $token->getExpiresAt() > new \DateTime()) { |
|
| 61 | + if ($token->getExpiresAt() !== null && $token->getExpiresAt() > new \DateTime()) |
|
| 62 | + { |
|
| 58 | 63 | $this->delete($token); |
| 59 | 64 | return null; |
| 60 | 65 | } |
@@ -67,12 +72,15 @@ discard block |
||
| 67 | 72 | */ |
| 68 | 73 | public function create(array $payload, \DateTimeInterface $expiresAt = null): TokenInterface |
| 69 | 74 | { |
| 70 | - try { |
|
| 75 | + try |
|
| 76 | + { |
|
| 71 | 77 | $token = new Token($this->randomHash(123), $payload, $expiresAt); |
| 72 | 78 | $this->getAuthSection()->set('token', $token->pack()); |
| 73 | 79 | |
| 74 | 80 | return $token; |
| 75 | - } catch (\Throwable $e) { |
|
| 81 | + } |
|
| 82 | + catch (\Throwable $e) |
|
| 83 | + { |
|
| 76 | 84 | throw new TokenStorageException('Unable to create session token', $e->getCode(), $e); |
| 77 | 85 | } |
| 78 | 86 | } |
@@ -90,12 +98,15 @@ discard block |
||
| 90 | 98 | */ |
| 91 | 99 | private function getAuthSection(): SessionSectionInterface |
| 92 | 100 | { |
| 93 | - try { |
|
| 101 | + try |
|
| 102 | + { |
|
| 94 | 103 | $session = $this->container->get(SessionInterface::class); |
| 95 | 104 | |
| 96 | 105 | /** @var SessionInterface $session */ |
| 97 | 106 | return $session->getSection(self::SESSION_SECTION); |
| 98 | - } catch (ContainerExceptionInterface $e) { |
|
| 107 | + } |
|
| 108 | + catch (ContainerExceptionInterface $e) |
|
| 109 | + { |
|
| 99 | 110 | throw new ScopeException('Unable to find auth token, invalid session scope', $e->getCode(), $e); |
| 100 | 111 | } |
| 101 | 112 | } |
@@ -38,7 +38,8 @@ |
||
| 38 | 38 | private function actor(AuthContextInterface $context): ActorInterface |
| 39 | 39 | { |
| 40 | 40 | $actor = $context->getActor(); |
| 41 | - if ($actor instanceof ActorInterface) { |
|
| 41 | + if ($actor instanceof ActorInterface) |
|
| 42 | + { |
|
| 42 | 43 | return $actor; |
| 43 | 44 | } |
| 44 | 45 | |