Skip to content
Draft
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
4 changes: 2 additions & 2 deletions lib/Controller/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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'
Expand Down
39 changes: 27 additions & 12 deletions lib/Service/OpenProjectAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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 '';
Expand All @@ -1796,8 +1799,20 @@ public function getAccessToken(?string $userId): string {
* @return array<mixed>
* @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']);
Expand Down
Loading