Skip to content
Open
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
13 changes: 10 additions & 3 deletions lib/Command/UpsertProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function configure() {
->addOption('mapping-quota', null, InputOption::VALUE_OPTIONAL, 'Attribute mapping of the quota')
->addOption('mapping-uid', null, InputOption::VALUE_OPTIONAL, 'Attribute mapping of the user id')
->addOption('extra-claims', null, InputOption::VALUE_OPTIONAL, 'Extra claims to request when getting tokens')

->addOption('bearersecret', 'bs', InputOption::VALUE_OPTIONAL, 'Telekom bearer token requires a different client secret for bearer tokens')
->addOption(
'output',
null,
Expand All @@ -100,11 +100,18 @@ protected function execute(InputInterface $input, OutputInterface $output) {
return $this->listProviders($input, $output);
}

// bearersecret is usually base64 encoded, but SAM delivers it non-encoded
// by default; so always encode/decode for this field
$bearersecret = $input->getOption('bearersecret');
if ($bearersecret !== null) {
$bearersecret = $this->crypto->encrypt(\Base64Url\Base64Url::encode($bearersecret));
}

// check if any option for updating is provided
$updateOptions = array_filter($input->getOptions(), static function ($value, $option) {
return in_array($option, [
'identifier', 'clientid', 'clientsecret', 'discoveryuri',
'scope', 'unique-uid', 'check-bearer',
'scope', 'unique-uid', 'check-bearer', 'bearersecret',
'mapping-uid', 'mapping-display-name', 'mapping-email', 'mapping-quota',
'extra-claims'
]) && $value !== null;
Expand Down Expand Up @@ -146,7 +153,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$scope = $scope ?? 'openid email profile';
}
try {
$provider = $this->providerMapper->createOrUpdateProvider($identifier, $clientid, $clientsecret, $discoveryuri, $scope);
$provider = $this->providerMapper->createOrUpdateProvider($identifier, $clientid, $clientsecret, $discoveryuri, $scope, $bearersecret);
// invalidate JWKS cache (even if it was just created)
$this->providerService->setSetting($provider->getId(), ProviderService::SETTING_JWKS_CACHE, '');
$this->providerService->setSetting($provider->getId(), ProviderService::SETTING_JWKS_CACHE_TIMESTAMP, '');
Expand Down
9 changes: 7 additions & 2 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __construct(
}

public function createProvider(string $identifier, string $clientId, string $clientSecret, string $discoveryEndpoint,
array $settings = [], string $scope = 'openid email profile'): JSONResponse {
string $bearerSecret, array $settings = [], string $scope = 'openid email profile'): JSONResponse {
if ($this->providerService->getProviderByIdentifier($identifier) !== null) {
return new JSONResponse(['message' => 'Provider with the given identifier already exists'], Http::STATUS_CONFLICT);
}
Expand All @@ -76,6 +76,8 @@ public function createProvider(string $identifier, string $clientId, string $cli
$provider->setClientSecret($encryptedClientSecret);
$provider->setDiscoveryEndpoint($discoveryEndpoint);
$provider->setScope($scope);
$encryptedBearerSecret = $this->crypto->encrypt(\Base64Url\Base64Url::encode($bearerSecret));
$provider->setBearerSecret($encryptedBearerSecret);
$provider = $this->providerMapper->insert($provider);

$providerSettings = $this->providerService->setSettings($provider->getId(), $settings);
Expand All @@ -84,7 +86,7 @@ public function createProvider(string $identifier, string $clientId, string $cli
}

public function updateProvider(int $providerId, string $identifier, string $clientId, string $discoveryEndpoint, string $clientSecret = null,
array $settings = [], string $scope = 'openid email profile'): JSONResponse {
string $bearerSecret = null, array $settings = [], string $scope = 'openid email profile'): JSONResponse {
$provider = $this->providerMapper->getProvider($providerId);

if ($this->providerService->getProviderByIdentifier($identifier) === null) {
Expand All @@ -97,6 +99,9 @@ public function updateProvider(int $providerId, string $identifier, string $clie
$encryptedClientSecret = $this->crypto->encrypt($clientSecret);
$provider->setClientSecret($encryptedClientSecret);
}
if ($bearerSecret) {
$provider->setBearerSecret(\Base64Url\Base64Url::encode($bearerSecret));
}
$provider->setDiscoveryEndpoint($discoveryEndpoint);
$provider->setScope($scope);
$provider = $this->providerMapper->update($provider);
Expand Down
6 changes: 6 additions & 0 deletions lib/Db/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@
* @method void setClientId(string $clientId)
* @method string getClientSecret()
* @method void setClientSecret(string $clientSecret)
* @method string getBearerSecret()
* @method void setBearerSecret(string $bearerSecret)
* @method string getDiscoveryEndpoint()
* @method void setDiscoveryEndpoint(string $discoveryEndpoint)
* @method string getScope()
* @method void setScope(string $scope)
*/
class Provider extends Entity implements \JsonSerializable {
Expand All @@ -55,6 +58,9 @@ class Provider extends Entity implements \JsonSerializable {
/** @var string */
protected $scope;

/** @var string */
protected $bearerSecret;

/**
* @return string
*/
Expand Down
7 changes: 6 additions & 1 deletion lib/Db/ProviderMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ public function getProviders() {
* @param string|null $clientsecret
* @param string|null $discoveryuri
* @param string scope
* @param string|null $bearersecret
* @throws \OCP\AppFramework\Db\DoesNotExistException
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
*/
public function createOrUpdateProvider(string $identifier, string $clientid = null,
string $clientsecret = null, string $discoveryuri = null,
string $scope = 'openid email profile') {
string $scope = 'openid email profile', string $bearersecret = null) {
try {
$provider = $this->findProviderByIdentifier($identifier);
} catch (DoesNotExistException $eNotExist) {
Expand All @@ -115,6 +116,7 @@ public function createOrUpdateProvider(string $identifier, string $clientid = nu
$provider->setClientSecret($clientsecret);
$provider->setDiscoveryEndpoint($discoveryuri);
$provider->setScope($scope);
$provider->setBearerSecret($bearersecret ?? '');
return $this->insert($provider);
} else {
if ($clientid !== null) {
Expand All @@ -126,6 +128,9 @@ public function createOrUpdateProvider(string $identifier, string $clientid = nu
if ($discoveryuri !== null) {
$provider->setDiscoveryEndpoint($discoveryuri);
}
if ($bearersecret !== null) {
$provider->setBearerSecret($bearersecret);
}
$provider->setScope($scope);
return $this->update($provider);
}
Expand Down
25 changes: 25 additions & 0 deletions lib/Migration/Version00008Date20211114183344.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace OCA\UserOIDC\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version00008Date20211114183344 extends SimpleMigrationStep {
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

$table = $schema->getTable('user_oidc_providers');
$table->addColumn('bearer_secret', 'string', [
'notnull' => true,
'length' => 64,
]);

return $schema;
}
}
97 changes: 97 additions & 0 deletions lib/Migration/Version010304Date20230902125945.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright 2023, Julien Veyssier <julien-nc@posteo.net>
*
* @author B. Rederlechner <bernd.rederlechner@t-systems.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\UserOIDC\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
use OCP\Security\ICrypto;

class Version010304Date20230902125945 extends SimpleMigrationStep {

/**
* @var IDBConnection
*/
private $connection;
/**
* @var ICrypto
*/
private $crypto;

public function __construct(
IDBConnection $connection,
ICrypto $crypto
) {
$this->connection = $connection;
$this->crypto = $crypto;
}

public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
$tableName = 'user_oidc_providers';

if ($schema->hasTable($tableName)) {
$table = $schema->getTable($tableName);
if ($table->hasColumn('bearer_secret')) {
$column = $table->getColumn('bearer_secret');
$column->setLength(512);
return $schema;
}
}

return null;
}

public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
$tableName = 'user_oidc_providers';

// update secrets in user_oidc_providers and user_oidc_id4me
$qbUpdate = $this->connection->getQueryBuilder();
$qbUpdate->update($tableName)
->set('bearer_secret', $qbUpdate->createParameter('updateSecret'))
->where(
$qbUpdate->expr()->eq('id', $qbUpdate->createParameter('updateId'))
);

$qbSelect = $this->connection->getQueryBuilder();
$qbSelect->select('id', 'bearer_secret')
->from($tableName);
$req = $qbSelect->executeQuery();
while ($row = $req->fetch()) {
$id = $row['id'];
$secret = $row['bearer_secret'];
$encryptedSecret = $this->crypto->encrypt($secret);
$qbUpdate->setParameter('updateSecret', $encryptedSecret, IQueryBuilder::PARAM_STR);
$qbUpdate->setParameter('updateId', $id, IQueryBuilder::PARAM_INT);
$qbUpdate->executeStatement();
}
$req->closeCursor();
}
}
9 changes: 9 additions & 0 deletions src/components/SettingsForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@
:required="!update"
autocomplete="off">
</p>
<p>
<label for="oidc-bearer-secret">{{ t('user_oidc', 'Bearer shared secret') }}</label>
<input id="oidc-bearer-secret"
v-model="localProvider.bearerSecret"
:placeholder="update ? t('user_oidc', 'Leave empty to keep existing') : null"
type="text"
:required="!update"
autocomplete="off">
</p>
<p class="settings-hint">
<AlertOutlineIcon :size="20" class="icon" />
{{ t('user_oidc', 'Warning, if the protocol of the URLs in the discovery content is HTTP, the ID token will be delivered through an insecure connection.') }}
Expand Down
Loading