Skip to content

Commit acf9f16

Browse files
committed
feat(events): rename TokenRefreshedEvent to UserObtainedTokenEvent, use this event when logging in and when refreshing
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
1 parent 4dc044f commit acf9f16

4 files changed

Lines changed: 100 additions & 52 deletions

File tree

lib/Controller/LoginController.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use OCA\UserOIDC\Db\ProviderMapper;
2020
use OCA\UserOIDC\Db\SessionMapper;
2121
use OCA\UserOIDC\Event\TokenObtainedEvent;
22+
use OCA\UserOIDC\Event\UserObtainedTokenEvent;
2223
use OCA\UserOIDC\Helper\HttpClientHelper;
2324
use OCA\UserOIDC\Service\DiscoveryService;
2425
use OCA\UserOIDC\Service\LdapService;
@@ -711,6 +712,16 @@ public function code(string $state = '', string $code = '', string $scope = '',
711712
$this->eventDispatcher->dispatchTyped(new UserLoggedInEvent($user, $userId, null, false));
712713
}
713714

715+
$this->eventDispatcher->dispatchTyped(
716+
new UserObtainedTokenEvent(
717+
$user->getUID(),
718+
null,
719+
$data,
720+
$provider,
721+
$discovery
722+
)
723+
);
724+
714725
$storeLoginTokenEnabled = $this->appConfig->getValueString(Application::APP_ID, 'store_login_token', '0', lazy: true) === '1';
715726
if ($storeLoginTokenEnabled) {
716727
// store all token information for potential token exchange requests

lib/Event/TokenRefreshedEvent.php

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/**
4+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OCA\UserOIDC\Event;
11+
12+
use OCA\UserOIDC\Db\Provider;
13+
use OCP\EventDispatcher\Event;
14+
15+
/**
16+
* This event is emitted with the raw token information when the login token is obtained
17+
* on a successful login and when it is refreshed
18+
*
19+
* It may be used by other apps to make use of the login token and stay informed when it gets refreshed
20+
*
21+
* The tokens are arrays with this shape:
22+
* [
23+
* 'id_token' => '...',
24+
* 'access_token' => '...',
25+
* 'refresh_token' => '...',
26+
* 'expires_in' => 3600,
27+
* 'refresh_expires_in' => 3600,
28+
* 'created_at' => 123456789,
29+
* ]
30+
*/
31+
class UserObtainedTokenEvent extends Event {
32+
33+
public function __construct(
34+
private string $userId,
35+
private ?array $oldToken,
36+
private array $newToken,
37+
private Provider $provider,
38+
private array $discovery,
39+
) {
40+
parent::__construct();
41+
}
42+
43+
/**
44+
* @return string The user ID of the user who obtained the token
45+
*/
46+
public function getUserId(): string {
47+
return $this->userId;
48+
}
49+
50+
/**
51+
* This old token is set only when the token is refreshed
52+
* @return array|null The old token that has been refreshed
53+
*/
54+
public function getOldToken(): ?array {
55+
return $this->oldToken;
56+
}
57+
58+
/**
59+
* @return array The freshly obtained token
60+
*/
61+
public function getNewToken(): array {
62+
return $this->newToken;
63+
}
64+
65+
/**
66+
* @return Provider The related Oidc provider
67+
*/
68+
public function getProvider(): Provider {
69+
return $this->provider;
70+
}
71+
72+
/**
73+
* The decoded discovery data from the discovery endpoint payload
74+
* @return array The discovery data
75+
*/
76+
public function getDiscovery(): array {
77+
return $this->discovery;
78+
}
79+
}

lib/Service/TokenService.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use OC\Authentication\Token\IProvider;
1414
use OCA\UserOIDC\AppInfo\Application;
1515
use OCA\UserOIDC\Db\ProviderMapper;
16-
use OCA\UserOIDC\Event\TokenRefreshedEvent;
16+
use OCA\UserOIDC\Event\UserObtainedTokenEvent;
1717
use OCA\UserOIDC\Exception\TokenExchangeFailedException;
1818
use OCA\UserOIDC\Helper\HttpClientHelper;
1919
use OCA\UserOIDC\Model\Token;
@@ -278,11 +278,15 @@ public function refresh(Token $token): Token {
278278
$bodyArray = json_decode(trim($body), true, 512, JSON_THROW_ON_ERROR);
279279
$this->logger->debug('[TokenService] ---- Refresh token success');
280280

281-
$this->eventDispatcher->dispatchTyped(
282-
new TokenRefreshedEvent(
283-
$token->jsonSerialize(), $bodyArray, $oidcProvider, $discovery
284-
)
285-
);
281+
$currentUser = $this->userSession->getUser();
282+
if ($currentUser !== null) {
283+
$currentUserId = $currentUser->getUID();
284+
$this->eventDispatcher->dispatchTyped(
285+
new UserObtainedTokenEvent(
286+
$currentUserId, $token->jsonSerialize(), $bodyArray, $oidcProvider, $discovery
287+
)
288+
);
289+
}
286290

287291
return $this->storeToken(
288292
array_merge($bodyArray, ['provider_id' => $token->getProviderId()])

0 commit comments

Comments
 (0)