microserviciile de register, login finalizate

This commit is contained in:
Cerbu Andrei Mihnea
2023-06-15 22:21:53 +03:00
parent a739a66ae4
commit 4e585ba278
159 changed files with 4775 additions and 1747 deletions
+96
View File
@@ -0,0 +1,96 @@
<?php
namespace app\core;
Class DVC
{
public static string $nameOfResponsePackage = "response_package";
public static string $successName = "Success";
public static string $genericExceptionName = "GenericErrorException";
public static string $invalidTokenName = "InvalidTokenException";
public static string $invalidRequestName = "InvalidRequestException";
public static string $invalidCurlStructureName = "InvalidCurlStructureException";
public static string $invalidBodyName = "InvalidBodyException";
public static string $alreadyExistsName = "AlreadyExistsException";
public static string $userNotExistingName = "userNotExistingException";
public static string $fieldNotExistingName = "FieldNotExistingException";
public static string $pageNotFoundName = "pageNotfoundException";
public static string $forbiddenAccessName = "forbiddenAccessException";
public static string $serviceNotFoundName = "FieldNotFoundException";
public static string $returnCode = "returnCode";
public static string $returnMessage = "returnMessage";
public static int $successReturnCode = 460;
public static function getArrayResponse(string $name): array
{
return match ($name){
DVC::$successName => [
DVC::$returnCode => 460,
DVC::$returnMessage => "Operation done successfully"
],
DVC::$genericExceptionName => [
DVC::$returnCode => 461,
DVC::$returnMessage => "Internal Server Error"
],
DVC::$invalidTokenName => [
DVC::$returnCode => 462,
DVC::$returnMessage => "Unauthorized access"
],
DVC::$invalidRequestName => [
DVC::$returnCode => 463,
DVC::$returnMessage => "Service doesn't provide this type of request"
],
DVC::$invalidBodyName => [
DVC::$returnCode => 464,
DVC::$returnMessage => "Not valid parameters sent"
],
DVC::$alreadyExistsName => [
DVC::$returnCode => 465,
DVC::$returnMessage => "Field already existing"
],
DVC::$fieldNotExistingName => [
DVC::$returnCode => 466,
DVC::$returnMessage => "Field not existing anymore"
],
DVC::$invalidCurlStructureName => [
DVC::$returnCode => 467,
DVC::$returnMessage => "cURL class needs URL, request method and headers to run"
],
DVC::$userNotExistingName => [
DVC::$returnCode => 468,
DVC::$returnMessage => "User account doesn't exists"
],
DVC::$pageNotFoundName => [
DVC::$returnCode => 469,
DVC::$returnMessage => "Page not found"
],
DVC::$forbiddenAccessName => [
DVC::$returnCode => 470,
DVC::$returnMessage => "You are not authorised to access this page"
],
DVC::$serviceNotFoundName => [
DVC::$returnCode => 471,
DVC::$returnMessage => "Service not existing"
]
};
}
public static function getHttpCodeWithCode(int $code): int
{
return match ($code){
460 => 200,
461 => 500,
462 => 401,
463 => 405,
464 => 400,
465, 466 => 204,
467 => 409,
468, 469, 471 => 404,
470 => 403,
default => 100
};
}
}
+43
View File
@@ -0,0 +1,43 @@
<?php
namespace app\core;
class Request{
public function getPath(): string
{
return $_SERVER['REQUEST_URI'];
}
public function method(): string
{
return $_SERVER['REQUEST_METHOD'];
}
public function getToken(): ?string
{
return array_key_exists("Authorization",apache_request_headers()) ?
apache_request_headers()["Authorization"] : NULL;
}
public function getAuthRequest(): string
{
return array_key_exists("Auth-Request", apache_request_headers()) ?
apache_request_headers()["Auth-Request"] : "default";
}
public function getBody(): ?array
{
if(!array_key_exists("Content-Length", apache_request_headers())) return NULL;
$bytesToRead = intval(apache_request_headers()["Content-Length"]);
if($bytesToRead == 0) return NULL;
$inputStream = fopen('php://input', 'r');
$jsonData = fread($inputStream, $bytesToRead);
fclose($inputStream);
$jsonData = json_decode($jsonData, true);
return empty($jsonData) ? [] : $jsonData;
}
}
+105
View File
@@ -0,0 +1,105 @@
<?php
namespace app\core;
use app\exceptions\InvalidTokenException;
use app\exceptions\ServiceNotFoundException;
use app\external_requests\CurlRequestBuilder;
use Exception;
class Router
{
private array $routes = [];
private array $accessTokens = [];
private Request $request;
private ?string $token;
public function __construct(Request $request, ?string $token)
{
$this->request = $request;
$this->token = $token;
}
public function set($path, $callback): void
{
$this->routes[$path] = $callback;
}
public function getHeaders(string $path): array
{
$array = $_SESSION['headers_configuration'][$path];
if($path === '/authentication'){
$array = $array[$this->request->getAuthRequest()];
}
$array["Content-Length"] = empty($this->request->getBody()) ? 0 : strlen(json_encode($this->request->getBody()));
$toReturn = array();
foreach ($array as $key => $value){
$value = $key . ": " . $value;
$toReturn[] = $value;
}
return $toReturn;
}
public function resolve(): void
{
try {
if (!$this->isTokenValid()) throw new InvalidTokenException();
$path = $this->request->getPath();
$method = $this->request->method();
$body = $this->request->getBody();
$callback = $this->routes[$path] ?? false;
if (!$callback) throw new ServiceNotFoundException();
$headers = $this->getHeaders($path);
$requestBuilder = new CurlRequestBuilder();
$requestBuilder->setRequest($method);
$requestBuilder->setUrl($callback);
$requestBuilder->setHeaders($headers);
$requestBuilder->setBody($body);
$requestBuilder->setIsReturnable(true);
$request = $requestBuilder->getCurlRequestObject();
$response = $request->makeRequest();
$this->writeResponse(
DVC::getHttpCodeWithCode(DVC::$successReturnCode),
DVC::$successReturnCode, $response
);
} catch (Exception $e){
$this->writeResponse(DVC::getHttpCodeWithCode($e->getCode()),
$e->getCode(), $e->getMessage()
);
}
}
private function isTokenValid(): bool
{
$validToken = hash('md5', 'api.knowyourfood');
return $validToken == $this->token;
}
private function writeResponse(int $httpCode, int $returnCode, string $returnMessage): void
{
header("Content-Disposition: ".DVC::$nameOfResponsePackage);
header("Content-Type: application/json");
http_response_code($httpCode);
if($httpCode != 200) {
echo json_encode([
"code" => $returnCode,
"message" => $returnMessage
]);
}else{
echo $returnMessage;
}
}
}