Skip to content

Commit a6f5f35

Browse files
committed
add auth
1 parent 4d72343 commit a6f5f35

File tree

13 files changed

+826
-98
lines changed

13 files changed

+826
-98
lines changed

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
DSN=mysql:host=localhost;dbname=exmaple
22
USERNAME=root
3-
PASSWORD=password
3+
PASSWORD=password
4+
APP_KEY=3sUFItpC7saODIigKrbjhCSpjorJCgO7qGmOM77nv0M=

app/Application.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ class Application
99
{
1010
public function __construct()
1111
{
12-
$this->request = Request::url();
13-
$this->route = new Route($this->request->method, $this->request->path);
12+
$this->route = new Route($_SERVER["REQUEST_METHOD"], $_SERVER["REQUEST_URI"]);
1413
}
1514

1615
public function run()

app/Http/HttpException.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace App\Http;
4+
5+
class HttpException
6+
{
7+
public static function HttpNotFoundException()
8+
{
9+
http_response_code(404);
10+
11+
return [
12+
"code" => 404,
13+
"message" => "404 Not Found",
14+
"description" => "The requested resource could not be found."
15+
];
16+
}
17+
18+
public static function HttpMethodNotAllowedException()
19+
{
20+
http_response_code(405);
21+
22+
return [
23+
"code" => 405,
24+
"message" => "Method not allowed.",
25+
"description" => "The request method is not supported for the requested resource."
26+
];
27+
}
28+
29+
30+
public static function HttpUnauthorizedException()
31+
{
32+
http_response_code(401);
33+
34+
return [
35+
"code" => 401,
36+
"message" => "Unauthorized.",
37+
"description" => "The request requires valid user authentication."
38+
];
39+
}
40+
41+
public static function HttpForbiddenException()
42+
{
43+
http_response_code(403);
44+
45+
return [
46+
"code" => 403,
47+
"message" => "Forbidden.",
48+
"description" => "You are not permitted to perform the requested operation."
49+
];
50+
}
51+
}

app/Http/Middleware/JWTAuth.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use App\Http\Response;
6+
use Firebase\JWT\JWT;
7+
8+
class JWTAuth
9+
{
10+
11+
public static function create($body)
12+
{
13+
$iat = time();
14+
15+
$payload = array(
16+
"iat" => $iat,
17+
"exp" => $iat + 6000000,
18+
"data" => $body
19+
);
20+
21+
return (object)["type" => "Bearer", "token" => JWT::encode($payload, $_ENV['APP_KEY'])];
22+
}
23+
24+
25+
public static function verify(?string $jwt = null, $bearer = false)
26+
{
27+
if ($bearer === true) {
28+
}
29+
// if (!preg_match('/Bearer\s/', $_SERVER['HTTP_AUTHORIZATION'], $matches)) {
30+
// header('HTTP/1.0 400 Bad Request');
31+
// echo 'Token not found in request';
32+
// exit;
33+
// }
34+
// Response::json($matches);
35+
try {
36+
$decoded = JWT::decode($jwt, $_ENV['APP_KEY'], array('HS256'));
37+
return $decoded;
38+
} catch (\Throwable $th) {
39+
return null;
40+
}
41+
}
42+
}

app/Http/Request.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,20 @@ public function __construct($args)
1414
{
1515
$this->params = $args->params;
1616
$this->query = $args->query;
17-
$this->contentType = $_SERVER["CONTENT_TYPE"] ?? '';
18-
$this->method = $_SERVER["REQUEST_METHOD"];
19-
$this->path = $_SERVER["REQUEST_URI"];
20-
}
21-
22-
public static function url()
23-
{
24-
return (object)["method" => $_SERVER["REQUEST_METHOD"], "path" => $_SERVER["REQUEST_URI"]];
17+
$this->contentType = $_SERVER["CONTENT_TYPE"] ?? null;
18+
$this->method = $_SERVER["REQUEST_METHOD"] ?? null;
19+
$this->path = $_SERVER["REQUEST_URI"] ?? null;
20+
$this->authorization = $_SERVER["HTTP_AUTHORIZATION"] ?? null;
2521
}
2622

2723
public function json()
2824
{
2925
if ($this->method !== "POST" || $this->contentType !== "application/json") {
3026
return [];
3127
}
32-
return json_decode(trim(file_get_contents("php://input")));
28+
$json = json_decode(trim(file_get_contents("php://input")));
29+
30+
return json_last_error() === JSON_ERROR_NONE ? $json : [];
3331
}
3432

3533
public function input()

app/Http/Response.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,4 @@ public static function make($code, $type, $message)
1818
"message" => $message
1919
];
2020
}
21-
22-
23-
public static function _404()
24-
{
25-
http_response_code(404);
26-
$resposne = [
27-
"code" => 404,
28-
"type" => "404 Not Found",
29-
"message" => "The requested resource could not be found but may be available again in the future."
30-
];
31-
return self::json($resposne);
32-
}
3321
}

app/Models/Model.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Models;
44

5+
use App\config\Database;
6+
57
class Model
68
{
79

app/Models/Users.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
class Users extends Model
66
{
7-
public function __construct()
8-
{
9-
//
10-
}
7+
/**
8+
* The attributes that are mass assignable.
9+
*
10+
* @var array
11+
*/
12+
protected $fillable = [];
1113
}

0 commit comments

Comments
 (0)