Skip to content

Commit f40103a

Browse files
chore: authorization with refresh token
1 parent 36bad0b commit f40103a

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

.env-sample

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ CORS_ORIGIN = *
99
SHOW_ERROR = 1 or 0
1010

1111
# Access Token Variables
12-
ACCESS_TOKEN_SECRET =
13-
ACCESS_TOKEN_EXPIRY =
12+
ACCESS_TOKEN_SECRET =
13+
ACCESS_TOKEN_EXPIRY = In Minutes
14+
15+
# Refresh Token Variables
16+
REFRESH_TOKEN_SECRET =
17+
REFRESH_TOKEN_EXPIRY = In Days

src/Controller/AbstractAuthController.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Slim\Exception\HttpBadRequestException;
99
use Slim\Exception\HttpUnauthorizedException;
1010

11-
/** Abstract Authorization Controller Functions */
11+
/** Abstract Authentication Controller Functions */
1212
class AbstractAuthController extends AbstractController {
1313

1414
/** Login Function */
@@ -44,8 +44,12 @@ public function login($req, $res) {
4444
/** Generated Access Token */
4545
$accessToken = $user->generateAccessToken();
4646

47+
/** Generated Refresh Token */
48+
$refreshToken = $user->generateRefreshToken();
49+
4750
// Add Authorization Cookies
48-
setcookie('SSID', $accessToken, time() + 84600 * intval($_ENV['ACCESS_TOKEN_EXPIRY']), path: '/', secure: true, httponly: true);
51+
setcookie('SSID', $accessToken, time() + 60 * (int) $_ENV['ACCESS_TOKEN_EXPIRY'], path: '/', secure: true, httponly: true);
52+
setcookie('RTID', $refreshToken, time() + 86400 * (int) $_ENV['REFRESH_TOKEN_EXPIRY'], path: '/api/', secure: true, httponly: true);
4953

5054
return response($req, $res, new Response(message: "User logged in successfully.", data: ['accessToken' => $accessToken]));
5155
}
@@ -55,6 +59,7 @@ public function logout($req, $res) {
5559

5660
// Remove Authorization Cookies
5761
setcookie('SSID', '', time() - 100, path: '/', secure: true, httponly: true);
62+
setcookie('RTID', '', time() - 100, path: '/api/', secure: true, httponly: true);
5863

5964
return response($req, $res, new Response(message: "User logged out successfully."));
6065
}

src/Entity/AbstractAuthEntity.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Doctrine\ORM\Mapping as ORM;
77
use Doctrine\ORM\Event as Event;
88

9-
/** Abstract Authorization Entity Functions */
9+
/** Abstract Authentication Entity Functions */
1010
class AbstractAuthEntity extends AbstractEntity {
1111

1212
#[ORM\PrePersist]
@@ -31,7 +31,19 @@ public function verifyPassword($password) {
3131
public function generateAccessToken() {
3232
return JWT::encode([
3333
'id' => $this->id,
34-
'username' => $this->username
34+
'iat' => time(),
35+
'exp' => time() + 60 * (int) $_ENV['ACCESS_TOKEN_EXPIRY'],
3536
], $_ENV['ACCESS_TOKEN_SECRET'], 'HS256');
3637
}
38+
39+
/** Generate Refresh Token Function */
40+
public function generateRefreshToken() {
41+
return JWT::encode([
42+
'id' => $this->id,
43+
'username' => $this->username,
44+
'name' => $this->name,
45+
'iat' => time(),
46+
'exp' => time() + 86400 * (int) $_ENV['REFRESH_TOKEN_EXPIRY'],
47+
], $_ENV['REFRESH_TOKEN_SECRET'], 'HS256');
48+
}
3749
}

src/Middleware/AbstractAuthMiddleware.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ public function process(Request $req, RequestHandler $handler): ResponseInterfac
2525
/** User Access Token */
2626
$token = $_COOKIE['SSID'] ?? str_replace('Bearer ', '', $req->getHeader('Authorization'))[0] ?? $req->getQueryParams()['accessToken'] ?? null;
2727

28+
/** Server Access Token */
29+
$refreshToken = $_COOKIE['RTID'] ?? null;
30+
31+
// Genrate Access Token to Refresh Token
32+
if ($refreshToken && !$token):
33+
34+
/** Decode Json Web Token */
35+
$decodedToken = (array) JWT::decode($refreshToken, new Key($_ENV['REFRESH_TOKEN_SECRET'], 'HS256'));
36+
37+
/** Check User Entity */
38+
$user = $this->_user->findById($decodedToken['id']);
39+
40+
/** Generated Access Token */
41+
$accessToken = $user->generateAccessToken();
42+
43+
// Add Authorization Cookies
44+
setcookie('SSID', $accessToken, time() + 60 * (int) $_ENV['ACCESS_TOKEN_EXPIRY'], path: '/', secure: true, httponly: true);
45+
$token = $accessToken;
46+
endif;
47+
2848
if (!$token)
2949
throw new HttpUnauthorizedException($req, 'Unauthorized request');
3050

0 commit comments

Comments
 (0)