|
68 | 68 | AppFactory::setContainer($container); |
69 | 69 |
|
70 | 70 |
|
| 71 | +class JWTBeforeHandler implements BeforeHandlerInterface { |
| 72 | + /** |
| 73 | + * @param array{decoded: array<string, mixed>, token: string} $arguments |
| 74 | + */ |
| 75 | + public function __invoke(ServerRequestInterface $request, array $arguments): ServerRequestInterface |
| 76 | + { |
| 77 | + // adds the decoded userId and scope to the request attributes |
| 78 | + return $request->withAttribute("userId", $arguments["decoded"]["userId"])->withAttribute("scope", $arguments["decoded"]["scope"]); |
| 79 | + } |
| 80 | +} |
| 81 | + |
71 | 82 | /* Authentication middleware for token retrival */ |
72 | 83 |
|
73 | 84 | class HashtopolisAuthenticator implements AuthenticatorInterface { |
@@ -134,17 +145,6 @@ public function get($key): string { |
134 | 145 | new Secret($PEPPER[0], 'HS256') |
135 | 146 | ); |
136 | 147 |
|
137 | | - class JWTBeforeHandler implements BeforeHandlerInterface { |
138 | | - /** |
139 | | - * @param array{decoded: array<string, mixed>, token: string} $arguments |
140 | | - */ |
141 | | - public function __invoke(ServerRequestInterface $request, array $arguments): ServerRequestInterface |
142 | | - { |
143 | | - // adds the unparsed token to the request |
144 | | - return $request->withAttribute("userId", $arguments["decoded"]["userId"])->withAttribute("scope", $arguments["decoded"]["scope"]); |
145 | | - } |
146 | | - } |
147 | | - |
148 | 148 | $options = new Options( |
149 | 149 | isSecure: false, |
150 | 150 | before: new JWTBeforeHandler, |
@@ -267,10 +267,13 @@ public static function addCORSheaders(Request $request, $response) { |
267 | 267 |
|
268 | 268 | $msg = $exception->getMessage(); |
269 | 269 |
|
270 | | - if ($exception instanceof AuthorizationException && !isset($msg)) { |
| 270 | + if ($exception instanceof AuthorizationException && empty($msg)) { |
271 | 271 | //the JWT authorization exceptions are wrapped in an outer exception |
272 | | - $code = 400; |
273 | | - $msg = $exception->getPrevious()->getMessage(); |
| 272 | + $previous = $exception->getPrevious(); |
| 273 | + if ($previous !== null) { |
| 274 | + $code = 400; |
| 275 | + $msg = $previous->getMessage(); |
| 276 | + } |
274 | 277 | } |
275 | 278 |
|
276 | 279 |
|
|
0 commit comments