forked from flashii/eeprom
41 lines
1.1 KiB
PHP
41 lines
1.1 KiB
PHP
<?php
|
|
namespace EEPROM\Auth;
|
|
|
|
use stdClass;
|
|
use Index\Routing\Route;
|
|
use Index\Routing\RouteHandler;
|
|
use Syokuhou\IConfig;
|
|
use EEPROM\Users\UsersContext;
|
|
|
|
class AuthRoutes extends RouteHandler {
|
|
public function __construct(
|
|
private IConfig $config,
|
|
private AuthInfo $authInfo,
|
|
private UsersContext $usersCtx
|
|
) {}
|
|
|
|
#[Route('/')]
|
|
public function getIndex($response, $request) {
|
|
$auth = $request->getHeaderLine('Authorization');
|
|
|
|
if(!empty($auth)) {
|
|
$authParts = explode(' ', $auth, 2);
|
|
$authMethod = strval($authParts[0] ?? '');
|
|
$authToken = strval($authParts[1] ?? '');
|
|
|
|
$authClients = $this->config->getArray('clients');
|
|
|
|
foreach($authClients as $client) {
|
|
$client = new $client;
|
|
if($client->getName() !== $authMethod)
|
|
continue;
|
|
$authUserId = $client->verifyToken($authToken);
|
|
break;
|
|
}
|
|
|
|
if(isset($authUserId) && $authUserId > 0)
|
|
$this->authInfo->setInfo($this->usersCtx->getUser($authUserId));
|
|
}
|
|
}
|
|
}
|