Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use OCA\UserOIDC\Db\ProviderMapper;
use OCA\UserOIDC\Db\SessionMapper;
use OCA\UserOIDC\Event\TokenObtainedEvent;
use OCA\UserOIDC\Event\UserObtainedTokenEvent;
use OCA\UserOIDC\Helper\HttpClientHelper;
use OCA\UserOIDC\Service\DiscoveryService;
use OCA\UserOIDC\Service\LdapService;
Expand Down Expand Up @@ -711,6 +712,16 @@ public function code(string $state = '', string $code = '', string $scope = '',
$this->eventDispatcher->dispatchTyped(new UserLoggedInEvent($user, $userId, null, false));
}

$this->eventDispatcher->dispatchTyped(
new UserObtainedTokenEvent(
$user->getUID(),
null,
$data,
$provider,
$discovery
)
);

$storeLoginTokenEnabled = $this->appConfig->getValueString(Application::APP_ID, 'store_login_token', '0', lazy: true) === '1';
if ($storeLoginTokenEnabled) {
// store all token information for potential token exchange requests
Expand Down
46 changes: 0 additions & 46 deletions lib/Event/TokenRefreshedEvent.php

This file was deleted.

79 changes: 79 additions & 0 deletions lib/Event/UserObtainedTokenEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

declare(strict_types=1);

namespace OCA\UserOIDC\Event;

use OCA\UserOIDC\Db\Provider;
use OCP\EventDispatcher\Event;

/**
* This event is emitted with the raw token information when the login token is obtained
* on a successful login and when it is refreshed
*
* It may be used by other apps to make use of the login token and stay informed when it gets refreshed
*
* The tokens are arrays with this shape:
* [
* 'id_token' => '...',
* 'access_token' => '...',
* 'refresh_token' => '...',
* 'expires_in' => 3600,
* 'refresh_expires_in' => 3600,
* 'created_at' => 123456789,
* ]
*/
class UserObtainedTokenEvent extends Event {

public function __construct(
private string $userId,
private ?array $oldToken,
private array $newToken,
private Provider $provider,
private array $discovery,
) {
parent::__construct();
}

/**
* @return string The user ID of the user who obtained the token
*/
public function getUserId(): string {
return $this->userId;
}

/**
* This old token is set only when the token is refreshed
* @return array|null The old token that has been refreshed
*/
public function getOldToken(): ?array {
return $this->oldToken;
}

/**
* @return array The freshly obtained token
*/
public function getNewToken(): array {
return $this->newToken;
}

/**
* @return Provider The related Oidc provider
*/
public function getProvider(): Provider {
return $this->provider;
}

/**
* The decoded discovery data from the discovery endpoint payload
* @return array The discovery data
*/
public function getDiscovery(): array {
return $this->discovery;
}
}
16 changes: 10 additions & 6 deletions lib/Service/TokenService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use OC\Authentication\Token\IProvider;
use OCA\UserOIDC\AppInfo\Application;
use OCA\UserOIDC\Db\ProviderMapper;
use OCA\UserOIDC\Event\TokenRefreshedEvent;
use OCA\UserOIDC\Event\UserObtainedTokenEvent;
use OCA\UserOIDC\Exception\TokenExchangeFailedException;
use OCA\UserOIDC\Helper\HttpClientHelper;
use OCA\UserOIDC\Model\Token;
Expand Down Expand Up @@ -278,11 +278,15 @@ public function refresh(Token $token): Token {
$bodyArray = json_decode(trim($body), true, 512, JSON_THROW_ON_ERROR);
$this->logger->debug('[TokenService] ---- Refresh token success');

$this->eventDispatcher->dispatchTyped(
new TokenRefreshedEvent(
$token->jsonSerialize(), $bodyArray, $oidcProvider, $discovery
)
);
$currentUser = $this->userSession->getUser();
if ($currentUser !== null) {
$currentUserId = $currentUser->getUID();
$this->eventDispatcher->dispatchTyped(
new UserObtainedTokenEvent(
$currentUserId, $token->jsonSerialize(), $bodyArray, $oidcProvider, $discovery
)
);
}

return $this->storeToken(
array_merge($bodyArray, ['provider_id' => $token->getProviderId()])
Expand Down
Loading