diff --git a/lib/Controller/ConfigController.php b/lib/Controller/ConfigController.php index b4947ef61..8d924bdf3 100755 --- a/lib/Controller/ConfigController.php +++ b/lib/Controller/ConfigController.php @@ -155,7 +155,7 @@ public function setConfig(array $values): DataResponse { if (isset($values['token'])) { if ($values['token']) { - $result = $this->openprojectAPIService->initUserInfo($this->userId); + $result = $this->openprojectAPIService->initUserInfo($this->userId, $values['token']); } else { $this->clearUserInfo(); $result = [ @@ -536,7 +536,7 @@ public function oauthRedirect(string $code = '', string $state = ''): RedirectRe ); if (isset($result['access_token']) && isset($result['refresh_token'])) { // set user info - $userInfo = $this->openprojectAPIService->initUserInfo($this->userId); + $userInfo = $this->openprojectAPIService->initUserInfo($this->userId, $result['access_token']); if (isset($userInfo['user_name'])) { $this->config->setUserValue( $this->userId, Application::APP_ID, 'oauth_connection_result', 'success' diff --git a/lib/Service/OpenProjectAPIService.php b/lib/Service/OpenProjectAPIService.php index 8734faa4e..f29bde0d1 100644 --- a/lib/Service/OpenProjectAPIService.php +++ b/lib/Service/OpenProjectAPIService.php @@ -419,7 +419,7 @@ public function rawRequest( string $method = 'GET', array $options = [] ) { - $url = $openprojectUrl . '/api/v3/' . $endPoint; + $url = $openprojectUrl . '/api/v3/' . ltrim($endPoint, '/'); if (!isset($options['headers']['Authorization'])) { $options['headers']['Authorization'] = 'Bearer ' . $accessToken; } @@ -1716,13 +1716,6 @@ public function getOIDCToken(string $userId): string { $this->config->setUserValue($userId, Application::APP_ID, 'token', $token->getAccessToken()); $this->config->setUserValue($userId, Application::APP_ID, 'token_expires_at', $tokenExpiresAt); - $savedUserId = $this->config->getUserValue($userId, Application::APP_ID, 'user_id'); - $savedUsername = $this->config->getUserValue($userId, Application::APP_ID, 'user_name'); - if (!$savedUserId || !$savedUsername) { - // get user info - $this->initUserInfo($userId); - } - return $token->getAccessToken(); } @@ -1751,16 +1744,22 @@ public function getAccessToken(?string $userId): string { return ''; } $token = $this->config->getUserValue($userId, Application::APP_ID, 'token', ''); + $authMethod = $this->config->getAppValue(Application::APP_ID, 'authorization_method'); + if ($token && !$this->isAccessTokenExpired($userId)) { + if ($authMethod === SettingsService::AUTH_METHOD_OIDC) { + $this->initUserInfo($userId, $token); + } return $token; } if ($token) { $this->logger->debug('Token has expired.', ['app' => $this->appName]); $this->logger->debug('Refreshing access token.', ['app' => $this->appName]); + $this->config->deleteUserValue($userId, Application::APP_ID, 'user_name'); + $this->config->deleteUserValue($userId, Application::APP_ID, 'user_id'); } - $authMethod = $this->config->getAppValue(Application::APP_ID, 'authorization_method'); // For OAuth2 setup, only try to refresh the expired token. // Token exchange needs to be initiated from the UI. if ($authMethod === SettingsService::AUTH_METHOD_OAUTH && $token) { @@ -1784,7 +1783,11 @@ public function getAccessToken(?string $userId): string { } return $result['access_token']; } elseif ($authMethod === SettingsService::AUTH_METHOD_OIDC) { - return $this->getOIDCToken($userId); + $token = $this->getOIDCToken($userId); + if ($token) { + $this->initUserInfo($userId, $token); + } + return $token; } return ''; @@ -1796,8 +1799,20 @@ public function getAccessToken(?string $userId): string { * @return array * @throws PreConditionNotMetException */ - public function initUserInfo(string $userId): array { - $info = $this->request($userId, '/users/me'); + public function initUserInfo(string $userId, string $accessToken): array { + $savedUserId = $this->config->getUserValue($userId, Application::APP_ID, 'user_id'); + $savedUsername = $this->config->getUserValue($userId, Application::APP_ID, 'user_name'); + if ($savedUserId && $savedUsername) { + return ['user_name' => $savedUsername]; + } + try { + $openprojectUrl = $this->config->getAppValue(Application::APP_ID, 'openproject_instance_url'); + $response = $this->rawRequest($accessToken, $openprojectUrl, '/users/me'); + $info = json_decode($response->getBody(), true); + } catch (Exception $e) { + $this->logger->error('OpenProject error : ' . $e->getMessage(), ['app' => $this->appName]); + return ['error' => $e->getMessage()]; + } if (isset($info['lastName'], $info['firstName'], $info['id'])) { $fullName = $info['firstName'] . ' ' . $info['lastName']; $this->config->setUserValue($userId, Application::APP_ID, 'user_id', $info['id']);