api secure auth

This commit is contained in:
Билай Станислав 2024-10-18 13:45:18 +03:00
parent bda9b03a9f
commit 68615d1f8d
4 changed files with 116 additions and 31 deletions

View File

@ -15,7 +15,8 @@
"samejack/php-argv": "dev-master", "samejack/php-argv": "dev-master",
"itguild/eloquent-table": "^0.4.1", "itguild/eloquent-table": "^0.4.1",
"ext-zip": "*", "ext-zip": "*",
"josantonius/session": "^2.0" "josantonius/session": "^2.0",
"firebase/php-jwt": "^6.10"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

101
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "e4f9dc9c939855869a503f576ef50d53", "content-hash": "c51d9ca5b40e143a4d89e80120b7cba8",
"packages": [ "packages": [
{ {
"name": "brick/math", "name": "brick/math",
@ -273,6 +273,69 @@
], ],
"time": "2024-02-18T20:23:39+00:00" "time": "2024-02-18T20:23:39+00:00"
}, },
{
"name": "firebase/php-jwt",
"version": "v6.10.1",
"source": {
"type": "git",
"url": "https://github.com/firebase/php-jwt.git",
"reference": "500501c2ce893c824c801da135d02661199f60c5"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/firebase/php-jwt/zipball/500501c2ce893c824c801da135d02661199f60c5",
"reference": "500501c2ce893c824c801da135d02661199f60c5",
"shasum": ""
},
"require": {
"php": "^8.0"
},
"require-dev": {
"guzzlehttp/guzzle": "^7.4",
"phpspec/prophecy-phpunit": "^2.0",
"phpunit/phpunit": "^9.5",
"psr/cache": "^2.0||^3.0",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0"
},
"suggest": {
"ext-sodium": "Support EdDSA (Ed25519) signatures",
"paragonie/sodium_compat": "Support EdDSA (Ed25519) signatures when libsodium is not present"
},
"type": "library",
"autoload": {
"psr-4": {
"Firebase\\JWT\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Neuman Vong",
"email": "neuman+pear@twilio.com",
"role": "Developer"
},
{
"name": "Anant Narayanan",
"email": "anant@php.net",
"role": "Developer"
}
],
"description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.",
"homepage": "https://github.com/firebase/php-jwt",
"keywords": [
"jwt",
"php"
],
"support": {
"issues": "https://github.com/firebase/php-jwt/issues",
"source": "https://github.com/firebase/php-jwt/tree/v6.10.1"
},
"time": "2024-05-18T18:05:11+00:00"
},
{ {
"name": "graham-campbell/result-type", "name": "graham-campbell/result-type",
"version": "v1.1.3", "version": "v1.1.3",
@ -337,16 +400,16 @@
}, },
{ {
"name": "illuminate/collections", "name": "illuminate/collections",
"version": "v11.27.2", "version": "v11.28.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/collections.git", "url": "https://github.com/illuminate/collections.git",
"reference": "4d333ea19a27230b424b9af56f34cd658b5bbce2" "reference": "2d99ccbb19e34450508ff3ab2f62ba90aa2e9793"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/illuminate/collections/zipball/4d333ea19a27230b424b9af56f34cd658b5bbce2", "url": "https://api.github.com/repos/illuminate/collections/zipball/2d99ccbb19e34450508ff3ab2f62ba90aa2e9793",
"reference": "4d333ea19a27230b424b9af56f34cd658b5bbce2", "reference": "2d99ccbb19e34450508ff3ab2f62ba90aa2e9793",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -388,11 +451,11 @@
"issues": "https://github.com/laravel/framework/issues", "issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework" "source": "https://github.com/laravel/framework"
}, },
"time": "2024-09-27T14:54:48+00:00" "time": "2024-10-10T19:23:07+00:00"
}, },
{ {
"name": "illuminate/conditionable", "name": "illuminate/conditionable",
"version": "v11.27.2", "version": "v11.28.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/conditionable.git", "url": "https://github.com/illuminate/conditionable.git",
@ -438,16 +501,16 @@
}, },
{ {
"name": "illuminate/container", "name": "illuminate/container",
"version": "v11.27.2", "version": "v11.28.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/container.git", "url": "https://github.com/illuminate/container.git",
"reference": "bc49d144a20b0d432e1ac812c9e056594b6c6480" "reference": "06dfc614aff58384b28ba5ad191f6a02d6b192cb"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/illuminate/container/zipball/bc49d144a20b0d432e1ac812c9e056594b6c6480", "url": "https://api.github.com/repos/illuminate/container/zipball/06dfc614aff58384b28ba5ad191f6a02d6b192cb",
"reference": "bc49d144a20b0d432e1ac812c9e056594b6c6480", "reference": "06dfc614aff58384b28ba5ad191f6a02d6b192cb",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -485,11 +548,11 @@
"issues": "https://github.com/laravel/framework/issues", "issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework" "source": "https://github.com/laravel/framework"
}, },
"time": "2024-10-08T13:34:53+00:00" "time": "2024-10-11T15:30:11+00:00"
}, },
{ {
"name": "illuminate/contracts", "name": "illuminate/contracts",
"version": "v11.27.2", "version": "v11.28.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/contracts.git", "url": "https://github.com/illuminate/contracts.git",
@ -672,7 +735,7 @@
}, },
{ {
"name": "illuminate/macroable", "name": "illuminate/macroable",
"version": "v11.27.2", "version": "v11.28.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/macroable.git", "url": "https://github.com/illuminate/macroable.git",
@ -718,16 +781,16 @@
}, },
{ {
"name": "illuminate/support", "name": "illuminate/support",
"version": "v11.27.2", "version": "v11.28.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/illuminate/support.git", "url": "https://github.com/illuminate/support.git",
"reference": "a567431e4820363d0bc28bdf14914ab16a2e63ef" "reference": "bd0983d4a22eccde1773809d4897bb533c3f26fd"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/illuminate/support/zipball/a567431e4820363d0bc28bdf14914ab16a2e63ef", "url": "https://api.github.com/repos/illuminate/support/zipball/bd0983d4a22eccde1773809d4897bb533c3f26fd",
"reference": "a567431e4820363d0bc28bdf14914ab16a2e63ef", "reference": "bd0983d4a22eccde1773809d4897bb533c3f26fd",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -790,7 +853,7 @@
"issues": "https://github.com/laravel/framework/issues", "issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework" "source": "https://github.com/laravel/framework"
}, },
"time": "2024-10-08T18:54:07+00:00" "time": "2024-10-16T16:25:45+00:00"
}, },
{ {
"name": "itguild/eloquent-table", "name": "itguild/eloquent-table",

View File

@ -2,6 +2,8 @@
namespace kernel\modules\secure\controllers; namespace kernel\modules\secure\controllers;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use JetBrains\PhpStorm\NoReturn; use JetBrains\PhpStorm\NoReturn;
use kernel\helpers\Debug; use kernel\helpers\Debug;
use kernel\modules\user\models\User; use kernel\modules\user\models\User;
@ -19,19 +21,37 @@ class SecureRestController extends RestController
{ {
$request = new Request(); $request = new Request();
$data = $request->post(); $data = $request->post();
$model = $this->model->where(['username', $data['username']])->first(); $model = $this->model->where('username', $data['username'])->first();
$res = [];
if ($model) {
if (password_verify($data["password"], $model->password_hash)) {
$baseUrl = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
$baseUrl .= $_SERVER['HTTP_HOST'];
// $baseUrl .= $_SERVER['REQUEST_URI'];;
$jwt = JWT::encode(
payload: [
"iss" => $baseUrl,
"aud" => $baseUrl,
"iat" => time(),
"exp" => date("Y-m-d H:i:s", strtotime("+30 days"))
],
key: $model->{'password_hash'},
alg: 'HS256'
);
$access_token = 123124312313; $model->{'access_token'} = $jwt;
$access_token_expires_at = time() + 3600; $model->{'access_token_expires_at'} =
JWT::decode($jwt, new Key($model->{'password_hash'}, 'HS256'))->exp;
$model->{'access_token'} = $access_token; $res = [
$model->{'access_token_expires_at'} = $access_token_expires_at; "access_token" => $model->{'access_token'},
foreach ($model->getFillable() as $item){ "access_token_expires_at" => $model->{'access_token_expires_at'},
$model->{$item} = $data[$item] ?? null; ];
}
$model->save();
} }
$model->save(); $this->renderApi($res);
$this->renderApi($this->model->toArray());
} }
} }

View File

@ -16,7 +16,8 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
}); });
App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){ App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){
$router->rest("secure", [\kernel\modules\secure\controllers\SecureRestController::class]); App::$collector->group(["prefix" => "secure"], function (CgRouteCollector $router) {
App::$collector->post('/auth', [\kernel\modules\secure\controllers\SecureRestController::class, 'actionAuth']); App::$collector->post('/auth', [\kernel\modules\secure\controllers\SecureRestController::class, 'actionAuth']);
});
}); });