Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(config): Fix incorrect config handling #91

Merged
merged 1 commit into from
Jul 22, 2024
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
2 changes: 2 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
'routes' => [
['name' => 'config#oauthRedirect', 'url' => '/oauth-redirect', 'verb' => 'GET'],
['name' => 'config#setConfig', 'url' => '/config', 'verb' => 'PUT'],
['name' => 'config#setSensitiveConfig', 'url' => '/sensitive-config', 'verb' => 'PUT'],
['name' => 'config#setAdminConfig', 'url' => '/admin-config', 'verb' => 'PUT'],
['name' => 'config#setSensitiveAdminConfig', 'url' => '/sensitive-admin-config', 'verb' => 'PUT'],
['name' => 'config#popupSuccessPage', 'url' => '/popup-success', 'verb' => 'GET'],

['name' => 'gitlabAPI#getEvents', 'url' => '/events', 'verb' => 'GET'],
Expand Down
38 changes: 34 additions & 4 deletions lib/Controller/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use OCP\AppFramework\Services\IInitialState;
use OCP\IConfig;
use OCP\IL10N;

use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\PreConditionNotMetException;
Expand All @@ -46,11 +45,27 @@ public function __construct(string $appName,
* set config values
* @NoAdminRequired
*
* @param array $values
* @return DataResponse
* @throws PreConditionNotMetException
*/
public function setConfig(array $values): DataResponse {
foreach ($values as $key => $value) {
if ($key === 'url' || $key === 'token') {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}

$this->config->setUserValue($this->userId, Application::APP_ID, $key, $value);
}

return new DataResponse([]);
}

/**
* @PasswordConfirmationRequired
* @NoAdminRequired
*
* @throws PreConditionNotMetException
*/
public function setSensitiveConfig(array $values): DataResponse {
// revoke the oauth token if needed
if (isset($values['token']) && $values['token'] === '') {
$tokenType = $this->config->getUserValue($this->userId, Application::APP_ID, 'token_type');
Expand All @@ -62,6 +77,7 @@ public function setConfig(array $values): DataResponse {
foreach ($values as $key => $value) {
$this->config->setUserValue($this->userId, Application::APP_ID, $key, $value);
}

$result = [];

if (isset($values['token'])) {
Expand Down Expand Up @@ -100,6 +116,20 @@ public function setConfig(array $values): DataResponse {
* @return DataResponse
*/
public function setAdminConfig(array $values): DataResponse {
foreach ($values as $key => $value) {
if ($key === 'client_id' || $key === 'client_secret' || $key === 'oauth_instance_url') {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}

$this->config->setAppValue(Application::APP_ID, $key, $value);
}
return new DataResponse(1);
}

/**
* @PasswordConfirmationRequired
*/
public function setSensitiveAdminConfig(array $values): DataResponse {
foreach ($values as $key => $value) {
$this->config->setAppValue(Application::APP_ID, $key, $value);
}
Expand Down Expand Up @@ -153,7 +183,7 @@ public function oauthRedirect(string $code = '', string $state = ''): RedirectRe
$refreshToken = $result['refresh_token'] ?? '';
if (isset($result['expires_in'])) {
$nowTs = (new Datetime())->getTimestamp();
$expiresAt = $nowTs + (int) $result['expires_in'];
$expiresAt = $nowTs + (int)$result['expires_in'];
$this->config->setUserValue($this->userId, Application::APP_ID, 'token_expires_at', strval($expiresAt));
}
$this->config->setUserValue($this->userId, Application::APP_ID, 'url', $adminOauthUrl);
Expand Down
4 changes: 2 additions & 2 deletions lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IConfig;

use OCP\Settings\ISettings;

class Admin implements ISettings {
Expand All @@ -20,7 +19,8 @@ public function __construct(private IConfig $config,
*/
public function getForm(): TemplateResponse {
$clientID = $this->config->getAppValue(Application::APP_ID, 'client_id');
$clientSecret = $this->config->getAppValue(Application::APP_ID, 'client_secret');
// Do not expose the saved client secret to the user
$clientSecret = $this->config->getAppValue(Application::APP_ID, 'client_secret') !== '' ? 'dummyToken' : '';
$oauthUrl = $this->config->getAppValue(Application::APP_ID, 'oauth_instance_url');
$usePopup = $this->config->getAppValue(Application::APP_ID, 'use_popup', '0') === '1';
$adminLinkPreviewEnabled = $this->config->getAppValue(Application::APP_ID, 'link_preview_enabled', '1') === '1';
Expand Down
3 changes: 2 additions & 1 deletion lib/Settings/Personal.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public function __construct(private IConfig $config,
* @return TemplateResponse
*/
public function getForm(): TemplateResponse {
$token = $this->config->getUserValue($this->userId, Application::APP_ID, 'token');
// Do not expose the saved token to the user
$token = $this->config->getUserValue($this->userId, Application::APP_ID, 'token') !== '' ? 'dummyToken' : '';
$searchEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'search_enabled', '0') === '1';
$searchIssuesEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'search_issues_enabled', '0') === '1';
$searchMRsEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'search_mrs_enabled', '0') === '1';
Expand Down
Loading
Loading