Skip to content

Commit 2ba7e40

Browse files
perf: authorization middleware convert abstract
1 parent 2471226 commit 2ba7e40

File tree

2 files changed

+56
-38
lines changed

2 files changed

+56
-38
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace RestJS\Middleware;
4+
5+
use Firebase\JWT\JWT;
6+
use Firebase\JWT\Key;
7+
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Message\ServerRequestInterface as Request;
9+
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
10+
use Psr\Http\Server\MiddlewareInterface;
11+
use Slim\Exception\HttpUnauthorizedException;
12+
13+
/** Abstract Authorization Middleware */
14+
abstract class AbstractAuthMiddleware implements MiddlewareInterface {
15+
16+
/** User Class Object */
17+
private $user;
18+
19+
public function __construct($user) {
20+
$this->user = $user;
21+
}
22+
23+
public function process(Request $req, RequestHandler $handler): ResponseInterface {
24+
25+
/** User Access Token */
26+
$token = $_COOKIE['SSID'] ?? str_replace('Bearer ', '', $req->getHeader('Authorization'))[0] ?? $req->getQueryParams()['accessToken'] ?? null;
27+
28+
if (!$token)
29+
throw new HttpUnauthorizedException($req, 'Unauthorized request');
30+
31+
try {
32+
/** Decode Json Web Token */
33+
$decodedToken = (array) JWT::decode($token, new Key($_ENV['ACCESS_TOKEN_SECRET'], 'HS256'));
34+
}
35+
catch (\Exception $e) {
36+
$decodedToken = null;
37+
}
38+
39+
if (!$decodedToken)
40+
throw new HttpUnauthorizedException($req, "Invalid access token");
41+
42+
/** Check User Entity */
43+
$user = $this->user->findById($decodedToken['id']);
44+
45+
if (!$user)
46+
throw new HttpUnauthorizedException($req, "Invalid access token");
47+
48+
$req->user = $user;
49+
50+
return $handler->handle($req);
51+
}
52+
}

src/middleware/authorization.php

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,13 @@
22
declare(strict_types=1);
33
namespace RestJS\Middleware;
44

5-
use Firebase\JWT\JWT;
6-
use Firebase\JWT\Key;
7-
use Psr\Http\Message\ResponseInterface;
8-
use Psr\Http\Message\ServerRequestInterface as Request;
9-
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
10-
use Psr\Http\Server\MiddlewareInterface;
115
use RestJS\Api\User\Model as User;
12-
use Slim\Exception\HttpUnauthorizedException;
6+
use RestJS\Middleware\AbstractAuthMiddleware;
137

148
/** Authorization Middleware */
15-
class Authorization implements MiddlewareInterface {
9+
class Authorization extends AbstractAuthMiddleware {
1610

17-
public function __construct(private User $user) {}
18-
19-
public function process(Request $req, RequestHandler $handler): ResponseInterface {
20-
21-
/** User Access Token */
22-
$token = $_COOKIE['SSID'] ?? str_replace('Bearer ', '', $req->getHeader('Authorization'))[0] ?? $req->getQueryParams()['accessToken'] ?? null;
23-
24-
if (!$token)
25-
throw new HttpUnauthorizedException($req, 'Unauthorized request');
26-
27-
try {
28-
/** Decode Json Web Token */
29-
$decodedToken = (array) JWT::decode($token, new Key($_ENV['ACCESS_TOKEN_SECRET'], 'HS256'));
30-
}
31-
catch (\Exception $e) {
32-
$decodedToken = null;
33-
}
34-
35-
if (!$decodedToken)
36-
throw new HttpUnauthorizedException($req, "Invalid access token");
37-
38-
/** Check User Entity */
39-
$user = $this->user->findById($decodedToken['id']);
40-
41-
if (!$user)
42-
throw new HttpUnauthorizedException($req, "Invalid access token");
43-
44-
$req->user = $user;
45-
46-
return $handler->handle($req);
11+
public function __construct(private User $user) {
12+
parent::__construct($user);
4713
}
4814
}

0 commit comments

Comments
 (0)