diff --git a/routes/api/v1/public/auth.route.php b/routes/api/v1/public/auth.route.php index 028cfa4..968f151 100644 --- a/routes/api/v1/public/auth.route.php +++ b/routes/api/v1/public/auth.route.php @@ -13,24 +13,26 @@ $router->group('auth') ->prefix('auth') ->controller(AuthController::class) - ->register(function (RouteCreator $router) { - $router->post('/authenticate') - ->handler('authenticate'); + ->register( + function (RouteCreator $router) { + $router->post('/authenticate') + ->handler('authenticate'); - $router->post('/challenge') - ->handler('challenge'); + $router->post('/challenge') + ->handler('challenge'); - $router->post('/register') - ->handler('register'); + $router->post('/register') + ->handler('register'); - $router->any('/refreshToken') - ->handler('refreshToken'); + $router->any('/refreshToken') + ->handler('refreshToken'); - $router->any('/me') - ->handler('me') - ->middleware(ApiAuthMiddleware::class); + $router->any('/me') + ->handler('me') + ->middleware(ApiAuthMiddleware::class); - $router->any('/delete/me') - ->handler('deleteMe') - ->middleware(ApiAuthMiddleware::class); - }); + $router->any('/delete/me') + ->handler('deleteMe') + ->middleware(ApiAuthMiddleware::class); + } + ); diff --git a/src/Module/Api/AuthController.php b/src/Module/Api/AuthController.php index 72b0179..c0e0e48 100644 --- a/src/Module/Api/AuthController.php +++ b/src/Module/Api/AuthController.php @@ -18,6 +18,7 @@ use Firebase\JWT\Key; use Lyrasoft\Luna\Auth\SRP\SRPService; use Lyrasoft\Luna\User\UserService; +use Psr\Container\ContainerExceptionInterface; use Windwalker\Core\Application\AppContext; use Windwalker\Core\Attributes\Controller; use Windwalker\Core\Http\RequestAssert; @@ -29,6 +30,7 @@ use Windwalker\SRP\Step\ProofResult; +use function Windwalker\chronos; use function Windwalker\Query\uuid2bin; use function Windwalker\uid; @@ -258,43 +260,42 @@ public function refreshToken( return compact('accessToken', 'refreshToken'); } + public function refreshSessions(\CurrentUser $currentUser, ORM $orm): true + { + $orm->updateBatch( + User::class, + [ + 'sess_valid_from' => chronos() + ], + ['id' => $currentUser->getId()] + ); + + return true; + } + + /** + * @param \CurrentUser $currentUser + * + * @return \CurrentUser + * + * @deprecated Use user/me instead. + */ public function me(\CurrentUser $currentUser): \CurrentUser { return $currentUser; } + /** + * @param AppContext $app + * + * @return true + * + * @deprecated Use user/deleteMe instead. + */ public function deleteMe( AppContext $app, - ORM $orm, - \CurrentUser $user, ): true { - [ - $A, - $M1, - $sess, - ] = $app->input( - 'A', - 'M1', - 'sess', - )->values(); - - RequestAssert::assert($A, 'Invalid credentials'); - RequestAssert::assert($M1, 'Invalid credentials'); - - $app->call( - $this->srpValidate(...), - compact( - 'user', - 'A', - 'M1', - 'sess' - ) - ); - - // Delete User - $orm->deleteWhere(User::class, ['id' => uuid2bin($user->getId())]); - - return true; + return $app->dispatchController([UserController::class, 'deleteMe']); } /** diff --git a/src/Module/Api/UserController.php b/src/Module/Api/UserController.php new file mode 100644 index 0000000..e6d18c3 --- /dev/null +++ b/src/Module/Api/UserController.php @@ -0,0 +1,89 @@ +updateBatch( + User::class, + [ + 'sess_valid_from' => chronos() + ], + ['id' => $currentUser->getId()] + ); + + return true; + } + + /** + * @param \CurrentUser $currentUser + * + * @return \CurrentUser + * + * @deprecated Use user/me instead. + */ + public function me(\CurrentUser $currentUser): \CurrentUser + { + return $currentUser; + } + + /** + * @param AppContext $app + * @param ORM $orm + * @param \CurrentUser $user + * + * @return true + * + * @throws ContainerExceptionInterface + * @throws \ReflectionException + * @deprecated Use user/deleteMe instead. + */ + public function deleteMe( + AppContext $app, + ORM $orm, + \CurrentUser $user, + ): true { + [ + $A, + $M1, + $sess, + ] = $app->input( + 'A', + 'M1', + 'sess', + )->values(); + + RequestAssert::assert($A, 'Invalid credentials'); + RequestAssert::assert($M1, 'Invalid credentials'); + + $app->call( + $this->srpValidate(...), + compact( + 'user', + 'A', + 'M1', + 'sess' + ) + ); + + // Delete User + $orm->deleteWhere(User::class, ['id' => uuid2bin($user->getId())]); + + return true; + } +}