Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
141 righe
5.1 KiB
141 righe
5.1 KiB
<?php |
|
|
|
namespace MwgAuth\Controllers; |
|
|
|
use MwgAuth\Core\Controller; |
|
use MwgAuth\Models\User; |
|
|
|
class ControllerV1 extends Controller |
|
{ |
|
public static function login(): string |
|
{ |
|
self::requireMethod('POST'); |
|
$input = self::getPost(); |
|
if (null === $input) { |
|
return self::error(self::ERROR_INVALID_INPUT); |
|
} |
|
$userName = $input[self::PARAMETER_USERNAME] ?? null; |
|
if (null === $userName) { |
|
return self::error(self::ERROR_MISSING_PARAMETER, self::PARAMETER_USERNAME); |
|
} |
|
$password = $input[self::PARAMETER_PASSWORD] ?? null; |
|
if (null === $password) { |
|
return self::error(self::ERROR_MISSING_PARAMETER, self::PARAMETER_PASSWORD); |
|
} |
|
$user = User::load($userName, $password, $_SERVER['REMOTE_ADDR']); |
|
if (null === $user) { |
|
return self::error(self::ERROR_INVALID_PASSWORD); |
|
} |
|
return self::response([self::PARAMETER_AUTH => $user->getAuth()]); |
|
} |
|
|
|
public static function getToken(): string |
|
{ |
|
self::requireMethod('POST'); |
|
$input = self::getPost(); |
|
if (null === $input) { |
|
return self::error(self::ERROR_INVALID_INPUT); |
|
} |
|
$auth = $input[self::PARAMETER_AUTH] ?? null; |
|
if (null === $auth) { |
|
return self::error(self::ERROR_MISSING_PARAMETER, self::PARAMETER_AUTH); |
|
} |
|
$callback = $input[self::PARAMETER_CALLBACK_URL] ?? null; |
|
if (null === $callback) { |
|
return self::error(self::ERROR_MISSING_PARAMETER, self::PARAMETER_CALLBACK_URL); |
|
} |
|
$user = User::verifyAuth($auth); |
|
if (null === $user) { |
|
return self::error(self::ERROR_INVALID_AUTH); |
|
} |
|
$token = $user->getToken($callback); |
|
return self::response([self::PARAMETER_TOKEN => $token]); |
|
} |
|
|
|
public static function getUser(): string |
|
{ |
|
self::requireMethod('POST'); |
|
$input = self::getPost(); |
|
if (null === $input) { |
|
return self::error(self::ERROR_INVALID_INPUT); |
|
} |
|
$auth = $input[self::PARAMETER_AUTH] ?? null; |
|
if (null === $auth) { |
|
return self::error(self::ERROR_MISSING_PARAMETER, self::PARAMETER_AUTH); |
|
} |
|
$user = User::verifyAuth($auth); |
|
if (null === $user) { |
|
return self::error(self::ERROR_INVALID_AUTH); |
|
} |
|
$token = $input[self::PARAMETER_TOKEN] ?? null; |
|
if (null === $token) { |
|
return self::error(self::ERROR_MISSING_PARAMETER, self::PARAMETER_TOKEN); |
|
} |
|
if (!$user->verifyToken($token)) { |
|
return self::error(self::ERROR_INVALID_TOKEN); |
|
} |
|
$userData = $user->getLoginData($token); |
|
if (null === $userData) { |
|
return self::error(self::ERROR_INVALID_LOGIN); |
|
} |
|
return self::response($userData); |
|
} |
|
|
|
private static function json(array $values): string |
|
{ |
|
return json_encode($values, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
} |
|
|
|
private static function response(array $values): string |
|
{ |
|
$response = [ |
|
self::LABEL_ERROR_CODE => self::ERROR_OK, |
|
self::LABEL_STATUS => self::STATUS_OK, |
|
self::LABEL_DESCRIPTION => self::ERROR_DESCRIPTIONS[self::ERROR_OK], |
|
]; |
|
$response += $values; |
|
return self::json($response); |
|
} |
|
|
|
private static function error(int $errorCode, ...$parameters): string |
|
{ |
|
$status = $errorCode == self::ERROR_OK ? self::STATUS_OK : self::STATUS_ERROR; |
|
return self::json([ |
|
self::LABEL_ERROR_CODE => $errorCode, |
|
self::LABEL_STATUS => $status, |
|
self::LABEL_DESCRIPTION => vsprintf(self::ERROR_DESCRIPTIONS[$errorCode], $parameters), |
|
]); |
|
} |
|
|
|
protected const LABEL_ERROR_CODE = 'code'; |
|
protected const LABEL_DESCRIPTION = 'description'; |
|
protected const LABEL_STATUS = 'status'; |
|
|
|
protected const ERROR_OK = 0; |
|
protected const ERROR_INVALID_AUTH = 1; |
|
protected const ERROR_INVALID_LOGIN = 2; |
|
protected const ERROR_INVALID_TOKEN = 3; |
|
protected const ERROR_INVALID_INPUT = 4; |
|
protected const ERROR_MISSING_PARAMETER = 5; |
|
protected const ERROR_INVALID_PASSWORD = 6; |
|
|
|
protected const STATUS_OK = 'success'; |
|
protected const STATUS_ERROR = 'error'; |
|
|
|
protected const ERROR_DESCRIPTIONS = [ |
|
self::ERROR_OK => 'success', |
|
self::ERROR_INVALID_AUTH => 'invalid or expired authorization', |
|
self::ERROR_INVALID_LOGIN => 'no login associated with this token', |
|
self::ERROR_INVALID_TOKEN => 'invalid or expired token', |
|
self::ERROR_INVALID_INPUT => 'invalid input', |
|
self::ERROR_MISSING_PARAMETER => "parameter '%s' is missing", |
|
self::ERROR_INVALID_PASSWORD => 'invalid username or password', |
|
]; |
|
|
|
protected const PARAMETER_AUTH = 'auth'; |
|
protected const PARAMETER_CALLBACK_URL = 'callback'; |
|
protected const PARAMETER_REDIRECT = 'redirect'; |
|
protected const PARAMETER_TOKEN = 'token'; |
|
protected const PARAMETER_USERNAME = 'username'; |
|
protected const PARAMETER_PASSWORD = 'password'; |
|
}
|
|
|