implemented the GitHub calls for the repository
This commit is contained in:
80
nibiru-auth-api/src/AuthController.php
Normal file
80
nibiru-auth-api/src/AuthController.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace NibiruAuthApi;
|
||||
|
||||
use Firebase\JWT\JWT;
|
||||
use Firebase\JWT\Key;
|
||||
|
||||
class AuthController
|
||||
{
|
||||
private $secretKey;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->secretKey = getenv('JWT_SECRET_KEY'); // Use getenv to get the secret key
|
||||
}
|
||||
public function handle()
|
||||
{
|
||||
$action = $_GET['action'] ?? '';
|
||||
|
||||
if ($action === 'token') {
|
||||
$this->generateToken();
|
||||
} elseif ($action === 'validate') {
|
||||
$this->validateToken();
|
||||
} else {
|
||||
echo json_encode(['error' => 'Invalid action']);
|
||||
}
|
||||
}
|
||||
|
||||
private function generateToken()
|
||||
{
|
||||
$issuedAt = time();
|
||||
$expirationTime = $issuedAt + 3600;
|
||||
$payload = [
|
||||
'iss' => getenv('NIBIRU_AUTH_VIRTUAL_HOST'),
|
||||
'aud' => getenv('NIBIRU_AUTH_VIRTUAL_HOST'),
|
||||
'iat' => $issuedAt,
|
||||
'nbf' => $issuedAt,
|
||||
'exp' => $expirationTime,
|
||||
'data' => [
|
||||
'userId' => 123,
|
||||
'username' => 'example_user'
|
||||
]
|
||||
];
|
||||
|
||||
$jwt = $this->encodeJwt($payload, $this->secretKey);
|
||||
echo json_encode(['token' => $jwt]);
|
||||
}
|
||||
|
||||
public function validateToken()
|
||||
{
|
||||
$headers = getallheaders();
|
||||
$authHeader = $headers['Authorization'] ?? '';
|
||||
|
||||
if (preg_match('/Bearer\s(\S+)/', $authHeader, $matches))
|
||||
{
|
||||
$token = $matches[1];
|
||||
|
||||
try {
|
||||
$decoded = JWT::decode($token, new Key($this->secretKey, 'HS256'));
|
||||
echo json_encode(['valid' => true, 'data' => $decoded]);
|
||||
} catch (\Exception $e) {
|
||||
http_response_code(401);
|
||||
echo json_encode(['error' => 'Invalid token', 'message' => $e->getMessage()]);
|
||||
}
|
||||
} else {
|
||||
http_response_code(401);
|
||||
echo json_encode(['error' => 'Authorization header missing']);
|
||||
}
|
||||
}
|
||||
|
||||
private function encodeJwt($payload, $key)
|
||||
{
|
||||
return JWT::encode($payload, $key, 'HS256');
|
||||
}
|
||||
|
||||
private function decodeJwt($jwt, $key)
|
||||
{
|
||||
return JWT::decode($jwt, new Key($key, 'HS256'));
|
||||
}
|
||||
}
|
||||
1
nibiru-auth-api/src/ValidateToken.php
Normal file
1
nibiru-auth-api/src/ValidateToken.php
Normal file
@@ -0,0 +1 @@
|
||||
<?php
|
||||
27
nibiru-auth-api/src/composer.json
Normal file
27
nibiru-auth-api/src/composer.json
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "nibiru/auth-api",
|
||||
"description": "Authentication API for Nibiru Framework",
|
||||
"type": "project",
|
||||
"require": {
|
||||
"guzzlehttp/guzzle": "^7.0",
|
||||
"psr/http-message": "^1.0",
|
||||
"psr/http-client": "^1.0",
|
||||
"psr/http-factory": "^1.0",
|
||||
"symfony/deprecation-contracts": "^2.1",
|
||||
"firebase/php-jwt": "^6.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"NibiruAuthApi\\": ""
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
"optimize-autoloader": true,
|
||||
"sort-packages": true
|
||||
},
|
||||
"scripts": {
|
||||
"post-update-cmd": [
|
||||
"chmod -R 777 storage"
|
||||
]
|
||||
}
|
||||
}
|
||||
9
nibiru-auth-api/src/index.php
Normal file
9
nibiru-auth-api/src/index.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
use NibiruAuthApi\AuthController;
|
||||
|
||||
// Initialize and handle the request
|
||||
$controller = new AuthController();
|
||||
$controller->handle();
|
||||
Reference in New Issue
Block a user