Added API endpoint to fetch current user info.

This commit is contained in:
flash 2024-09-05 20:10:04 +00:00
parent b127da74f0
commit 0848b6e708
2 changed files with 42 additions and 2 deletions

View file

@ -1,8 +1,6 @@
<?php
namespace Aleister\V1;
use Aleister\RpcModels\Hanyuu\OAuth2AuthInfo;
use Aiwass\Client\IRpcClient;
use Index\Http\Routing\{IRouter,IRouteHandler};
class V1Routes implements IRouteHandler {
@ -16,5 +14,9 @@ class V1Routes implements IRouteHandler {
$router->use('/', $authz->misuzuTokenAuthMiddleware(...));
$router->get('/', fn() => ['status' => 'operational']);
$usersRoutes = new V1UsersRoutes($this->ctx, $this->ctx->getRpcClient()->scopeTo('misuzu:users:'));
$router->get('/me', $usersRoutes->getMe(...));
$router->scopeTo('/users')->register($usersRoutes);
}
}

38
src/V1/V1UsersRoutes.php Normal file
View file

@ -0,0 +1,38 @@
<?php
namespace Aleister\V1;
use Aiwass\Client\IRpcClient;
use Index\Http\Routing\RouteHandler;
class V1UsersRoutes extends RouteHandler {
public function __construct(
private V1Context $ctx,
private IRpcClient $rpc
) {}
public function getMe() {
$authz = $this->ctx->getAuthzContext();
if(!$authz->hasScope('identify'))
return 403;
if($authz->isAppUser()) {
// TODO: what should app users even look like?
return 501;
}
if($authz->isRealUser()) {
$userInfo = $this->rpc->query('getUser', ['userId' => $authz->getUserId()]);
if(!is_array($userInfo))
return 500;
if(array_key_exists('error', $userInfo))
return match($userInfo['error']) {
'notfound' => 404,
default => 400,
};
return $userInfo;
}
return 401;
}
}