Skip to content

Commit

Permalink
expire tokens wip
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed May 25, 2024
1 parent fc468df commit 3f31ba7
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 45 deletions.
34 changes: 18 additions & 16 deletions routes/api/v1/public/auth.route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);
59 changes: 30 additions & 29 deletions src/Module/Api/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,7 @@

use Windwalker\SRP\Step\ProofResult;

use function Windwalker\chronos;
use function Windwalker\Query\uuid2bin;
use function Windwalker\uid;

Expand Down Expand Up @@ -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']);
}

/**
Expand Down
89 changes: 89 additions & 0 deletions src/Module/Api/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace App\Module\Api;

use App\Entity\User;
use Psr\Container\ContainerExceptionInterface;
use Windwalker\Core\Application\AppContext;
use Windwalker\Core\Attributes\Controller;
use Windwalker\Core\Http\RequestAssert;
use Windwalker\ORM\ORM;

use function Windwalker\chronos;
use function Windwalker\Query\uuid2bin;

#[Controller]
class UserController
{
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
* @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;
}
}

0 comments on commit 3f31ba7

Please sign in to comment.