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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Changed

- Update npm dependencies [#1123](https://github.com/nextcloud/integration_openproject/pull/1123)
- Make OAuth2 Client class usage compatible with older Nextcloud versions [#1148](https://github.com/nextcloud/integration_openproject/pull/1148)

### Removed

Expand Down
12 changes: 3 additions & 9 deletions lib/Controller/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
use GuzzleHttp\Exception\GuzzleException;
use InvalidArgumentException;
use OC\User\NoUserException;
use OCA\OAuth2\Controller\SettingsController;
use OCA\OAuth2\Exceptions\ClientNotFoundException;
use OCA\OAuth2\Service\ClientService;
use OCA\OpenProject\AppInfo\Application;
use OCA\OpenProject\Exception\OpenprojectErrorException;
use OCA\OpenProject\Exception\OpenprojectGroupfolderSetupConflictException;
Expand Down Expand Up @@ -71,11 +71,6 @@ class ConfigController extends Controller {
*/
private $oauthService;

/**
* @var SettingsController
*/
private $oauthSettingsController;

private SettingsService $settingsService;

public function __construct(
Expand All @@ -88,7 +83,7 @@ public function __construct(
OpenProjectAPIService $openprojectAPIService,
LoggerInterface $logger,
OauthService $oauthService,
SettingsController $oauthSettingsController,
private readonly ClientService $clientService,
SettingsService $settingsService,
?string $userId
) {
Expand All @@ -101,7 +96,6 @@ public function __construct(
$this->logger = $logger;
$this->userId = $userId;
$this->oauthService = $oauthService;
$this->oauthSettingsController = $oauthSettingsController;
$this->settingsService = $settingsService;
}

Expand Down Expand Up @@ -583,7 +577,7 @@ private function deleteOauthClient(): void {
if ($oauthClientInternalId !== '') {
$id = (int) $oauthClientInternalId;
try {
$this->oauthSettingsController->deleteClient($id);
$this->clientService->deleteClient($id);
} catch (ClientNotFoundException $e) {
}
$this->config->deleteAppValue(Application::APP_ID, 'nc_oauth_client_id');
Expand Down
76 changes: 60 additions & 16 deletions lib/Service/OauthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,24 @@ public function __construct(ClientMapper $clientMapper,
*/
public function createNcOauthClient(string $name, string $redirectUri): array {
$clientId = $this->secureRandom->generate(64, self::validChars);
$clientSecret = $this->secureRandom->generate(64, self::validChars);
$hashedClientSecret = bin2hex($this->crypto->calculateHMAC($clientSecret));
$redirectUri = sprintf($redirectUri, $clientId);

$client = new Client();
$client->setName($name);
$client->setRedirectUri(sprintf($redirectUri, $clientId));
$secret = $this->secureRandom->generate(64, self::validChars);
$client->setSecret(bin2hex($this->crypto->calculateHMAC($secret)));
$client->setClientIdentifier($clientId);
$this->setClientProperty($client, 'name', $name);
$this->setClientProperty($client, 'redirectUri', $redirectUri);
$this->setClientProperty($client, 'clientIdentifier', $clientId);
$this->setClientProperty($client, 'secret', $hashedClientSecret);

$client = $this->clientMapper->insert($client);

return [
'id' => $client->getId(),
'nextcloud_oauth_client_name' => $client->getName(),
'openproject_redirect_uri' => $client->getRedirectUri(),
'nextcloud_client_id' => $client->getClientIdentifier(),
'nextcloud_client_secret' => $secret,
'id' => $this->getClientProperty($client, 'id'),
'nextcloud_oauth_client_name' => $this->getClientProperty($client, 'name'),
'openproject_redirect_uri' => $this->getClientProperty($client, 'redirectUri'),
'nextcloud_client_id' => $this->getClientProperty($client, 'clientIdentifier'),
'nextcloud_client_secret' => $clientSecret,
];
}

Expand All @@ -74,10 +78,10 @@ public function getClientInfo(int $id): ?array {
try {
$client = $this->clientMapper->getByUid($id);
return [
'id' => $client->getId(),
'nextcloud_oauth_client_name' => $client->getName(),
'openproject_redirect_uri' => $client->getRedirectUri(),
'nextcloud_client_id' => $client->getClientIdentifier()
'id' => $this->getClientProperty($client, 'id'),
'nextcloud_oauth_client_name' => $this->getClientProperty($client, 'name'),
'openproject_redirect_uri' => $this->getClientProperty($client, 'redirectUri'),
'nextcloud_client_id' => $this->getClientProperty($client, 'clientIdentifier')
];
} catch (ClientNotFoundException $e) {
return null;
Expand All @@ -92,13 +96,53 @@ public function getClientInfo(int $id): ?array {
public function setClientRedirectUri(int $id, string $opUrl): bool {
try {
$client = $this->clientMapper->getByUid($id);
$clientId = $client->getClientIdentifier();
$clientId = $this->getClientProperty($client, 'clientIdentifier');

$redirectUri = rtrim($opUrl, '/') .'/oauth_clients/'.$clientId.'/callback';
$client->setRedirectUri($redirectUri);
$client = $this->setClientProperty($client, 'redirectUri', $redirectUri);

$this->clientMapper->update($client);
return true;
} catch (ClientNotFoundException $e) {
return false;
}
}

/**
* @param Client $client
* @param string $property
* @param string|int $value
*
* @return Client
*/
public function setClientProperty(Client $client, string $property, string|int $value): Client {
$fn = 'set' . ucfirst($property);
// In NC35, OAuth2 Client class changed to attribute-based entity.
// NOTE: we can remove setClientProperty and getClientProperty methods
// once we drop support for NC34 and below.
if (\method_exists(Client::class, 'addType')) {
$client->$fn($value);
} else {
$client->{$property} = $value;
}

return $client;
}

/**
* @param Client $client
* @param string $property
*
* @return string|int
*/
public function getClientProperty(Client $client, string $property): string|int {
$fn = 'get' . ucfirst($property);
// In NC35, OAuth2 Client class changed to attribute-based entity.
// NOTE: we can remove setClientProperty and getClientProperty methods
// once we drop support for NC34 and below.
if (\method_exists(Client::class, 'addType')) {
return $client->$fn();
}
return $client->{$property};
}
}
Loading
Loading