Skip to content

Commit 4ce1bcc

Browse files
artongenickvergessen
authored andcommitted
feat(EphemeralSessions): Introduce lax period
Signed-off-by: Louis Chmn <[email protected]>
1 parent 88b7e75 commit 4ce1bcc

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

lib/private/AppFramework/Middleware/FlowV2EphemeralSessionsMiddleware.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,47 @@
1111
use OC\Core\Controller\ClientFlowLoginV2Controller;
1212
use OC\Core\Controller\TwoFactorChallengeController;
1313
use OCP\AppFramework\Middleware;
14+
use OCP\AppFramework\Utility\ITimeFactory;
1415
use OCP\Authentication\TwoFactorAuth\ALoginSetupController;
1516
use OCP\ISession;
1617
use OCP\IUserSession;
1718

1819
// Will close the session if the user session is ephemeral.
1920
// Happens when the user logs in via the login flow v2.
2021
class FlowV2EphemeralSessionsMiddleware extends Middleware {
22+
private const EPHEMERAL_SESSION_TTL = 5 * 60; // 5 minutes
23+
2124
private ISession $session;
2225
private IUserSession $userSession;
2326
private ControllerMethodReflector $reflector;
27+
private ITimeFactory $timeFactory;
2428

2529
public function __construct(
2630
ISession $session,
2731
IUserSession $userSession,
28-
ControllerMethodReflector $reflector
32+
ControllerMethodReflector $reflector,
33+
ITimeFactory $timeFactory
2934
) {
3035
$this->session = $session;
3136
$this->userSession = $userSession;
3237
$this->reflector = $reflector;
38+
$this->timeFactory = $timeFactory;
3339
}
3440

3541
public function beforeController($controller, $methodName) {
36-
if (!$this->session->get(ClientFlowLoginV2Controller::EPHEMERAL_NAME)) {
42+
$sessionCreationTime = $this->session->get(ClientFlowLoginV2Controller::EPHEMERAL_NAME);
43+
44+
// Not an ephemeral session.
45+
if ($sessionCreationTime === null) {
46+
return;
47+
}
48+
49+
// Lax enforcement until TTL is reached.
50+
if ($this->timeFactory->getTime() < $sessionCreationTime + self::EPHEMERAL_SESSION_TTL) {
3751
return;
3852
}
3953

54+
// Allow certain controllers/methods to proceed without logging out.
4055
if (
4156
$controller instanceof ClientFlowLoginV2Controller &&
4257
($methodName === 'grantPage' || $methodName === 'generateAppPassword')

lib/private/Authentication/Login/FlowV2EphemeralSessionsCommand.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,29 @@
99
namespace OC\Authentication\Login;
1010

1111
use OC\Core\Controller\ClientFlowLoginV2Controller;
12+
use OCP\AppFramework\Utility\ITimeFactory;
1213
use OCP\ISession;
1314
use OCP\IURLGenerator;
1415

1516
class FlowV2EphemeralSessionsCommand extends ALoginCommand {
1617
private ISession $session;
1718
private IURLGenerator $urlGenerator;
19+
private ITimeFactory $timeFactory;
1820

1921
public function __construct(
2022
ISession $session,
21-
IURLGenerator $urlGenerator
23+
IURLGenerator $urlGenerator,
24+
ITimeFactory $timeFactory
2225
) {
2326
$this->session = $session;
2427
$this->urlGenerator = $urlGenerator;
28+
$this->timeFactory = $timeFactory;
2529
}
2630

2731
public function process(LoginData $loginData): LoginResult {
2832
$loginV2GrantRoute = $this->urlGenerator->linkToRoute('core.ClientFlowLoginV2.grantPage');
2933
if (str_starts_with($loginData->getRedirectUrl() ?? '', $loginV2GrantRoute)) {
30-
$this->session->set(ClientFlowLoginV2Controller::EPHEMERAL_NAME, true);
34+
$this->session->set(ClientFlowLoginV2Controller::EPHEMERAL_NAME, $this->timeFactory->getTime());
3135
}
3236

3337
return $this->processNextOrFinishSuccessfully($loginData);

0 commit comments

Comments
 (0)