eeprom/src/Auth/AuthRoutes.php

42 lines
1.2 KiB
PHP

<?php
namespace EEPROM\Auth;
use Index\Http\Routing\{HttpMiddleware,RouteHandler};
use Syokuhou\IConfig;
use EEPROM\Users\UsersContext;
class AuthRoutes extends RouteHandler {
public function __construct(
private IConfig $config,
private AuthInfo $authInfo,
private UsersContext $usersCtx
) {}
#[HttpMiddleware('/')]
public function getIndex($response, $request) {
$auth = $request->getHeaderLine('Authorization');
if(empty($auth)) {
$cookie = (string)$request->getCookie('msz_auth');
if(!empty($cookie))
$auth = sprintf('Misuzu %s', $cookie);
}
if(!empty($auth)) {
$authParts = explode(' ', $auth, 2);
$authMethod = strval($authParts[0] ?? '');
$authToken = strval($authParts[1] ?? '');
if($authMethod === 'Misuzu') {
$authResult = ChatAuth::attempt(
$this->config->getString('endpoint'),
$this->config->getString('secret'),
$authToken
);
if(!empty($authResult->success))
$this->authInfo->setInfo($this->usersCtx->getUser($authResult->user_id));
}
}
}
}