Commit 8cc8668a authored by Roman Ondráček's avatar Roman Ondráček

API: fix authenticator

Signed-off-by: Roman Ondráček's avatarRoman Ondráček <ondracek.roman@centrum.cz>
parent 892006c1
Pipeline #2202 failed with stages
in 59 seconds
......@@ -55,6 +55,8 @@ class OpenApiController extends BaseController {
* @Method("GET")
* @OpenApi("
* summary: Returns OpenAPI schema
* security:
* - []
* ")
* @Responses({
* @Response(code="200", description="Success")
......
......@@ -30,6 +30,7 @@ use Apitte\Core\Annotation\Controller\Responses;
use Apitte\Core\Annotation\Controller\Tag;
use Apitte\Core\Http\ApiRequest;
use Apitte\Core\Http\ApiResponse;
use App\ApiModule\Version0\RequestAttributes;
use Nette\Security\AuthenticationException;
use Nette\Security\Identity;
use Nette\Security\User;
......@@ -65,13 +66,8 @@ class UserController extends BaseController {
* @return ApiResponse API response
*/
public function get(ApiRequest $request, ApiResponse $response): ApiResponse {
if (!$this->user->isLoggedIn()) {
return $response->withStatus(401);
}
/**
* @var Identity Identity
*/
$identity = $this->user->getIdentity();
/** @var Identity $identity */
$identity = $request->getAttribute(RequestAttributes::APP_LOGGED_USER);
$data = $identity->getData();
return $response->writeJsonBody([
'id' => $this->user->getId(),
......@@ -86,6 +82,8 @@ class UserController extends BaseController {
* @Method("POST")
* @OpenApi("
* summary: Signs in the user
* security:
* - []
* ")
* @Responses({
* @Response(code="200", description="Success"),
......
......@@ -61,7 +61,7 @@ class AuthenticationMiddleware implements IMiddleware {
}
$user = $this->authenticator->authenticate($request);
// If we have a identity, then go to next middleware, otherwise stop and return current response
if ($user !== null) {
if ($user === null) {
$response->getBody()->write(Json::encode([
'error' => 'Client authentication failed',
]));
......
......@@ -20,45 +20,37 @@ declare(strict_types = 1);
namespace App\ApiModule\Version0\Models;
use App\CoreModule\Models\AppAuthenticator;
use Contributte\Middlewares\Security\IAuthenticator;
use Nette\Database\Context;
use Nette\Security\Identity;
use Nette\Security\IIdentity;
use Psr\Http\Message\ServerRequestInterface;
use Throwable;
class BasicAuthenticator implements IAuthenticator {
/**
* @var Context Database context
* @var AppAuthenticator Application authenticator
*/
private $context;
private $authenticator;
/**
* Constructor
* @param Context $context Database context
* @param AppAuthenticator $authenticator Application authenticator
*/
public function __construct(Context $context) {
$this->context = $context;
public function __construct(AppAuthenticator $authenticator) {
$this->authenticator = $authenticator;
}
/**
* @inheritDoc
*/
public function authenticate(ServerRequestInterface $request): ?IIdentity {
$table = $this->context->table('users');
$credentials = $this->parseAuthorizationHeader($request->getHeader('Authorization')[0] ?? '');
if ($credentials === null) {
try {
return $this->authenticator->authenticate($credentials);
} catch (Throwable $e) {
return null;
}
$row = $table->where('username', $credentials['username'])->fetch();
if ($row === null) {
return null;
}
if (!password_verify($credentials['password'], $row['password'])) {
return null;
}
$data = ['username' => $row['username'], 'language' => $row['language']];
return new Identity($row['id'], $row['role'], $data);
}
/**
......@@ -71,10 +63,7 @@ class BasicAuthenticator implements IAuthenticator {
return null;
}
$header = explode(':', (string) base64_decode(substr($header, 6), true), 2);
return [
'username' => $header[0],
'password' => $header[1] ?? '',
];
return [$header[0],$header[1] ?? ''];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment