Skip to content

Commit

Permalink
refactor(token_auth)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvikan committed Jan 3, 2025
1 parent e1a8d0e commit 0c051b7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
16 changes: 10 additions & 6 deletions middlewares/TokenAuthenticationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ public function __invoke(Request $request, $next): Response
return $next($request);
}

// Add token as request attribute
$request = $request->withAttribute('token', $request->get('token'));
$token = $request->get('token');

if (! $request->getAttribute('token')) {
if (! $token) {
return new Response(render(__DIR__ . '/../templates/token.html.php', [
'message' => 'Missing token',
'message' => 'Missing token',
'token' => '',
]), 401);
}
if (! hash_equals(Configuration::getConfig('authentication', 'token'), $request->getAttribute('token'))) {

if (! hash_equals(Configuration::getConfig('authentication', 'token'), $token)) {
return new Response(render(__DIR__ . '/../templates/token.html.php', [
'message' => 'Invalid token',
'message' => 'Invalid token',
'token' => $token,
]), 401);
}

$request = $request->withAttribute('token', $token);

return $next($request);
}
}
4 changes: 2 additions & 2 deletions templates/token.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<?= e($message) ?>
</p>

<form action="" method="get">
<form action="" method="get" autocomplete="off">
<label for="token">Token:</label>
<input type="password" name="token" id="token" placeholder="token">
<input type="text" name="token" id="token" placeholder="token" value="<?= e($token) ?>">
<input type="submit" value="OK">
</form>

0 comments on commit 0c051b7

Please sign in to comment.