[FIX] Fix some securiry issues
This commit is contained in:
@@ -1,14 +1,20 @@
|
||||
<?php
|
||||
|
||||
class Auth {
|
||||
private const JWT_SECRET = 'ohmj_secret_key_change_in_production';
|
||||
private const JWT_ALGO = 'HS256';
|
||||
private const JWT_EXPIRY = 3600; // 1 hour
|
||||
|
||||
private string $usersFile;
|
||||
private string $jwtSecret;
|
||||
|
||||
public function __construct(string $usersFile = null) {
|
||||
$this->usersFile = $usersFile ?? __DIR__ . '/../config/users.json';
|
||||
|
||||
// Load JWT secret from environment variable
|
||||
$this->jwtSecret = $_ENV['JWT_SECRET'] ?? getenv('JWT_SECRET');
|
||||
if (empty($this->jwtSecret)) {
|
||||
throw new Exception('JWT_SECRET environment variable is not configured');
|
||||
}
|
||||
}
|
||||
|
||||
public function login(string $username, string $password): array {
|
||||
@@ -45,7 +51,7 @@ class Auth {
|
||||
|
||||
// Verify signature
|
||||
$expectedSignature = base64_encode(
|
||||
hash_hmac('sha256', "$header.$payload", self::JWT_SECRET, true)
|
||||
hash_hmac('sha256', "$header.$payload", $this->jwtSecret, true)
|
||||
);
|
||||
|
||||
if (!hash_equals($expectedSignature, $signature)) {
|
||||
@@ -101,7 +107,7 @@ class Auth {
|
||||
]));
|
||||
|
||||
$signature = base64_encode(
|
||||
hash_hmac('sha256', "$header.$payload", self::JWT_SECRET, true)
|
||||
hash_hmac('sha256', "$header.$payload", $this->jwtSecret, true)
|
||||
);
|
||||
|
||||
return "$header.$payload.$signature";
|
||||
|
||||
Reference in New Issue
Block a user