refactor
- move elixForms API logic to specific folder - move everything else under Api folder
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
namespace Api\Auth;
|
||||
|
||||
use Core\Config;
|
||||
use Core\Request;
|
||||
|
||||
class ApiTokenAuthenticator
|
||||
{
|
||||
private Config $config;
|
||||
|
||||
public function __construct(Config $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function authenticate(Request $request): void
|
||||
{
|
||||
$authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] ?? '';
|
||||
if (!$authorization) {
|
||||
throw new \Exception('Missing Authorization header');
|
||||
}
|
||||
|
||||
if (!preg_match('/^Bearer\s+(.*)$/i', trim($authorization), $matches)) {
|
||||
throw new \Exception('Invalid Authorization header format');
|
||||
}
|
||||
|
||||
$token = $matches[1];
|
||||
$expected = $this->config->secret('api_access_token');
|
||||
|
||||
if (empty($expected) || !hash_equals((string) $expected, (string) $token)) {
|
||||
throw new \Exception('Invalid API access token');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
namespace Controllers;
|
||||
|
||||
use Core\Request;
|
||||
use Core\Response;
|
||||
use ElixForms\ElixFormsClient;
|
||||
|
||||
class ExampleController
|
||||
{
|
||||
private ElixFormsClient $elixFormsClient;
|
||||
|
||||
public function __construct(ElixFormsClient $elixFormsClient)
|
||||
{
|
||||
$this->elixFormsClient = $elixFormsClient;
|
||||
}
|
||||
|
||||
public function test(Request $req, Response $res)
|
||||
{
|
||||
$response = $this->elixFormsClient->request('GET', '/status');
|
||||
|
||||
return $res->json([
|
||||
'external_api_response' => $response['json'] ?? $response['body'],
|
||||
'status' => $response['status']
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
namespace Api\Controllers;
|
||||
|
||||
use Core\Request;
|
||||
use Core\Response;
|
||||
use ElixForms\ElixFormsClient;
|
||||
use ElixForms\Exceptions\ElixFormsException;
|
||||
|
||||
class ExternalElixFormsController
|
||||
{
|
||||
private ElixFormsClient $elixFormsClient;
|
||||
|
||||
public function __construct(ElixFormsClient $elixFormsClient)
|
||||
{
|
||||
$this->elixFormsClient = $elixFormsClient;
|
||||
}
|
||||
|
||||
public function status(Request $req, Response $res)
|
||||
{
|
||||
try {
|
||||
$response = $this->elixFormsClient->request('GET', '/status');
|
||||
return $res->json([
|
||||
'status' => $response['status'],
|
||||
'body' => $response['json'] ?? $response['body']
|
||||
]);
|
||||
} catch (ElixFormsException $e) {
|
||||
return $res->json(['error' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function login(Request $req, Response $res)
|
||||
{
|
||||
$data = $req->body();
|
||||
$username = $data['username'] ?? '';
|
||||
$password = $data['password'] ?? '';
|
||||
|
||||
try {
|
||||
$token = $this->elixFormsClient->auth()->login($username, $password);
|
||||
return $res->json(['authToken' => $token]);
|
||||
} catch (ElixFormsException $e) {
|
||||
return $res->json(['error' => $e->getMessage()], 401);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,4 +17,10 @@ class Request {
|
||||
public function query() {
|
||||
return $_GET;
|
||||
}
|
||||
|
||||
public function header(string $name): ?string
|
||||
{
|
||||
$key = 'HTTP_' . strtoupper(str_replace('-', '_', $name));
|
||||
return $_SERVER[$key] ?? null;
|
||||
}
|
||||
}
|
||||
@@ -17,8 +17,8 @@ class ExternalApiService
|
||||
|
||||
public function getStatus()
|
||||
{
|
||||
$url = rtrim($this->config->get('external_api_base_url'), '/') . '/status';
|
||||
$token = $this->config->secret('external_api_token');
|
||||
$url = rtrim($this->config->get('elixforms_api_base_url'), '/') . '/status';
|
||||
$token = $this->config->secret('elixforms_api_token');
|
||||
$headers = ['Authorization' => 'Bearer ' . $token];
|
||||
return $this->httpClient->get($url, $headers);
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
namespace Controllers;
|
||||
|
||||
use Core\Request;
|
||||
use Core\Response;
|
||||
use Core\Config;
|
||||
use Core\HttpClient;
|
||||
|
||||
class ExampleController
|
||||
{
|
||||
private $config;
|
||||
private $httpClient;
|
||||
|
||||
public function __construct(Config $config, HttpClient $httpClient)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->httpClient = $httpClient;
|
||||
}
|
||||
|
||||
public function test(Request $req, Response $res)
|
||||
{
|
||||
|
||||
$url = $this->config->get('external_api_base_url') . '/status';
|
||||
|
||||
$response = $this->httpClient->get($url, [
|
||||
'Authorization' => 'Bearer ' . $this->config->secret('external_api_token')
|
||||
]);
|
||||
|
||||
return $res->json([
|
||||
'external_api_response' => $response
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
namespace ElixForms\Auth;
|
||||
|
||||
use ElixForms\Exceptions\ElixFormsException;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
class ElixFormsAuthenticationClient {
|
||||
private $baseUrl;
|
||||
private $httpClient;
|
||||
|
||||
public function __construct(string $baseUrl, Client $httpClient = null) {
|
||||
$this->baseUrl = rtrim($baseUrl, '/');
|
||||
$this->httpClient = $httpClient ?? new Client(['timeout' => 10.0, 'http_errors' => false]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Esegue il login verso l'API di elixForms.
|
||||
*
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
* @return string Il token di autenticazione (authToken)
|
||||
* @throws ElixFormsException Se le credenziali sono errate o c'è un errore server
|
||||
*/
|
||||
public function login(string $username, string $password): string {
|
||||
$url = $this->baseUrl . '/eF/services/api/authentication/login/v1';
|
||||
|
||||
try {
|
||||
$response = $this->httpClient->post($url, [
|
||||
'form_params' => [
|
||||
'username' => $username,
|
||||
'password' => $password
|
||||
]
|
||||
]);
|
||||
|
||||
$status = $response->getStatusCode();
|
||||
if ($status !== 200) {
|
||||
throw new ElixFormsException("Errore durante il login elixForms. HTTP Status: " . $status);
|
||||
}
|
||||
|
||||
$body = $response->getBody()->getContents();
|
||||
$json = json_decode($body, true);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE || empty($json)) {
|
||||
throw new ElixFormsException("Risposta non valida dal server elixForms: atteso JSON.");
|
||||
}
|
||||
|
||||
if (isset($json['value']['authToken'])) {
|
||||
return $json['value']['authToken'];
|
||||
}
|
||||
|
||||
throw new ElixFormsException("authToken non trovato nella risposta del login elixForms.");
|
||||
} catch (GuzzleException $e) {
|
||||
throw new ElixFormsException("Errore di connessione al server elixForms: " . $e->getMessage(), 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Effettua il logout invalidando il token sul server elixForms.
|
||||
*
|
||||
* @param string $username
|
||||
* @param string $token
|
||||
* @return bool True se il logout ha successo
|
||||
* @throws ElixFormsException Se c'è un errore durante il logout
|
||||
*/
|
||||
public function logout(string $username, string $token): bool {
|
||||
$url = $this->baseUrl . '/eF/services/api/authentication/' . urlencode($username) . '/logout/v1';
|
||||
|
||||
try {
|
||||
$response = $this->httpClient->post($url, [
|
||||
'headers' => [
|
||||
'Authorization' => 'Bearer ' . $token,
|
||||
'Content-Type' => 'application/x-www-form-urlencoded'
|
||||
]
|
||||
]);
|
||||
|
||||
$status = $response->getStatusCode();
|
||||
if ($status !== 200) {
|
||||
throw new ElixFormsException("Errore durante il logout elixForms. HTTP Status: " . $status);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (GuzzleException $e) {
|
||||
throw new ElixFormsException("Errore di connessione al server elixForms: " . $e->getMessage(), 0, $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
namespace ElixForms;
|
||||
|
||||
use ElixForms<Auth\ElixFormsAuthenticationClient as AuthClient;
|
||||
use ElixForms\Exceptions\ElixFormsException;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
class ElixFormsClient
|
||||
{
|
||||
private string $baseUrl;
|
||||
private Client $httpClient;
|
||||
|
||||
public function __construct(string $baseUrl, Client $httpClient = null)
|
||||
{
|
||||
$this->baseUrl = rtrim($baseUrl, '/');
|
||||
$this->httpClient = $httpClient ?? new Client(['timeout' => 10.0, 'http_errors' => false]);
|
||||
}
|
||||
|
||||
public function auth(): AuthClient
|
||||
{
|
||||
return new AuthClient($this->baseUrl, $this->httpClient);
|
||||
}
|
||||
|
||||
public function request(string $method, string $path, ?array $body = null, array $headers = []): array
|
||||
{
|
||||
$url = $this->baseUrl . '/' . ltrim($path, '/');
|
||||
$options = ['headers' => $headers];
|
||||
|
||||
if ($body !== null) {
|
||||
$options['json'] = $body;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = $this->httpClient->request($method, $url, $options);
|
||||
$status = $response->getStatusCode();
|
||||
$bodyRaw = $response->getBody()->getContents();
|
||||
$contentType = $response->getHeaderLine('Content-Type');
|
||||
$isJson = stripos($contentType, 'application/json') !== false;
|
||||
|
||||
return [
|
||||
'status' => $status,
|
||||
'headers' => $response->getHeaders(),
|
||||
'body' => $bodyRaw,
|
||||
'is_json' => $isJson,
|
||||
'json' => $isJson ? json_decode($bodyRaw, true) : null,
|
||||
];
|
||||
} catch (GuzzleException $e) {
|
||||
throw new ElixFormsException('Errore di connessione al server elixForms: ' . $e->getMessage(), 0, $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ElixForms\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ElixFormsException extends Exception {
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
<?php
|
||||
namespace Services;
|
||||
|
||||
use Core\Config;
|
||||
use Core\HttpClient;
|
||||
use Core\ElixFormsException;
|
||||
|
||||
class ElixFormsAuthenticationClient {
|
||||
private $config;
|
||||
private $httpClient;
|
||||
|
||||
public function __construct(Config $config, HttpClient $httpClient) {
|
||||
$this->config = $config;
|
||||
$this->httpClient = $httpClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Esegue il login verso l'API di elixForms.
|
||||
*
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
* @return string Il token di autenticazione (authToken)
|
||||
* @throws ElixFormsException Se le credenziali sono errate o c'è un errore server
|
||||
*/
|
||||
public function login(string $username, string $password): string {
|
||||
$baseUrl = rtrim($this->config->get('elixforms_api_base_url'), '/');
|
||||
$url = $baseUrl . '/eF/services/api/authentication/login/v1';
|
||||
|
||||
$data = [
|
||||
'username' => $username,
|
||||
'password' => $password
|
||||
];
|
||||
|
||||
// L'API supporta application/json
|
||||
$response = $this->httpClient->post($url, $data);
|
||||
|
||||
if ($response['status'] !== 200) {
|
||||
throw new ElixFormsException("Errore durante il login elixForms. HTTP Status: " . $response['status']);
|
||||
}
|
||||
|
||||
if (!$response['is_json'] || empty($response['json'])) {
|
||||
throw new ElixFormsException("Risposta non valida dal server elixForms: atteso JSON.");
|
||||
}
|
||||
|
||||
$json = $response['json'];
|
||||
|
||||
// Estraiamo il token dalla struttura complessa
|
||||
if (isset($json['value']['authToken'])) {
|
||||
return $json['value']['authToken'];
|
||||
}
|
||||
|
||||
throw new ElixFormsException("authToken non trovato nella risposta del login elixForms.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Effettua il logout invalidando il token sul server elixForms.
|
||||
*
|
||||
* @param string $username
|
||||
* @param string $token
|
||||
* @return bool True se il logout ha successo
|
||||
* @throws ElixFormsException Se c'è un errore durante il logout
|
||||
*/
|
||||
public function logout(string $username, string $token): bool {
|
||||
$baseUrl = rtrim($this->config->get('elixforms_api_base_url'), '/');
|
||||
$url = $baseUrl . '/eF/services/api/authentication/' . urlencode($username) . '/logout/v1';
|
||||
|
||||
$headers = [
|
||||
'Authorization' => 'Bearer ' . $token,
|
||||
'Content-Type' => 'application/x-www-form-urlencoded'
|
||||
];
|
||||
|
||||
$response = $this->httpClient->post($url, null, $headers);
|
||||
|
||||
if ($response['status'] !== 200) {
|
||||
throw new ElixFormsException("Errore durante il logout elixForms. HTTP Status: " . $response['status']);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user