Skip to content

Commit e508548

Browse files
author
jessevz
committed
Fixed copilot code suggestions
1 parent 50ae402 commit e508548

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/api/v2/index.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@
6868
AppFactory::setContainer($container);
6969

7070

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+
7182
/* Authentication middleware for token retrival */
7283

7384
class HashtopolisAuthenticator implements AuthenticatorInterface {
@@ -134,17 +145,6 @@ public function get($key): string {
134145
new Secret($PEPPER[0], 'HS256')
135146
);
136147

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-
148148
$options = new Options(
149149
isSecure: false,
150150
before: new JWTBeforeHandler,
@@ -267,10 +267,13 @@ public static function addCORSheaders(Request $request, $response) {
267267

268268
$msg = $exception->getMessage();
269269

270-
if ($exception instanceof AuthorizationException && !isset($msg)) {
270+
if ($exception instanceof AuthorizationException && empty($msg)) {
271271
//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+
}
274277
}
275278

276279

0 commit comments

Comments
 (0)