From 0848b6e70869b08960fdac623d2b163ab5b33bed Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 5 Sep 2024 20:10:04 +0000 Subject: [PATCH] Added API endpoint to fetch current user info. --- src/V1/V1Routes.php | 6 ++++-- src/V1/V1UsersRoutes.php | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/V1/V1UsersRoutes.php diff --git a/src/V1/V1Routes.php b/src/V1/V1Routes.php index 9207459..fa7f758 100644 --- a/src/V1/V1Routes.php +++ b/src/V1/V1Routes.php @@ -1,8 +1,6 @@ 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); } } diff --git a/src/V1/V1UsersRoutes.php b/src/V1/V1UsersRoutes.php new file mode 100644 index 0000000..3bab851 --- /dev/null +++ b/src/V1/V1UsersRoutes.php @@ -0,0 +1,38 @@ +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; + } +}