eeprom/src/Auth/AuthRoutes.php

38 lines
1.1 KiB
PHP

<?php
namespace EEPROM\Auth;
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] ?? '');
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));
}
}
}
}