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
16 changes: 14 additions & 2 deletions lib/Command/UpsertProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ protected function configure() {
->addOption('clientid', 'c', InputOption::VALUE_REQUIRED, 'OpenID client identifier')
->addOption('clientsecret', 's', InputOption::VALUE_REQUIRED, 'OpenID client secret')
->addOption('discoveryuri', 'd', InputOption::VALUE_REQUIRED, 'OpenID discovery endpoint uri')
->addOption('bearersecret', 'bs', InputOption::VALUE_OPTIONAL, 'Telekom bearer token requires a different client secret for bearer tokens')
->addOption('endsessionendpointuri', 'e', InputOption::VALUE_REQUIRED, 'OpenID end session endpoint uri')
->addOption('postlogouturi', 'p', InputOption::VALUE_REQUIRED, 'Post logout URI')
->addOption('scope', 'o', InputOption::VALUE_OPTIONAL, 'OpenID requested value scopes, if not set defaults to "openid email profile"');
Expand Down Expand Up @@ -206,10 +207,17 @@ 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($this->base64UrlEncode($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', 'endsessionendpointuri', 'postlogouturi', 'scope',
'identifier', 'clientid', 'clientsecret', 'discoveryuri', 'endsessionendpointuri', 'postlogouturi', 'scope', 'bearersecret',
...array_keys(self::EXTRA_OPTIONS),
]) && $value !== null;
}, ARRAY_FILTER_USE_BOTH);
Expand Down Expand Up @@ -250,7 +258,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
}
try {
$provider = $this->providerMapper->createOrUpdateProvider(
$identifier, $clientid, $clientsecret, $discoveryuri, $scope, $endsessionendpointuri, $postLogoutUri
$identifier, $clientid, $clientsecret, $discoveryuri, $scope, $endsessionendpointuri, $postLogoutUri, $bearersecret
);
// invalidate JWKS cache (even if it was just created)
$this->providerService->setSetting($provider->getId(), ProviderService::SETTING_JWKS_CACHE, '');
Expand Down Expand Up @@ -306,4 +314,8 @@ private function listProviders(InputInterface $input, OutputInterface $output) {
$table->render();
return 0;
}

private function base64UrlEncode(string $data): string {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
}
13 changes: 11 additions & 2 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function isDiscoveryEndpointValid($url) {
}

#[PasswordConfirmationRequired]
public function createProvider(string $identifier, string $clientId, string $clientSecret, string $discoveryEndpoint,
public function createProvider(string $identifier, string $clientId, string $clientSecret, string $discoveryEndpoint, string $bearerSecret,
array $settings = [], string $scope = 'openid email profile', ?string $endSessionEndpoint = null,
?string $postLogoutUri = null): JSONResponse {
if ($this->providerService->getProviderByIdentifier($identifier) !== null) {
Expand All @@ -102,6 +102,8 @@ public function createProvider(string $identifier, string $clientId, string $cli
$provider->setEndSessionEndpoint($endSessionEndpoint ?: null);
$provider->setPostLogoutUri($postLogoutUri ?: null);
$provider->setScope($scope);
$encryptedBearerSecret = $this->crypto->encrypt($this->base64UrlEncode($bearerSecret));
$provider->setBearerSecret($encryptedBearerSecret);
$provider = $this->providerMapper->insert($provider);

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

#[PasswordConfirmationRequired]
public function updateProvider(int $providerId, string $identifier, string $clientId, string $discoveryEndpoint, ?string $clientSecret = null,
public function updateProvider(int $providerId, string $identifier, string $clientId, string $discoveryEndpoint, ?string $clientSecret = null, ?string $bearerSecret = null,
array $settings = [], string $scope = 'openid email profile', ?string $endSessionEndpoint = null,
?string $postLogoutUri = null): JSONResponse {
$provider = $this->providerMapper->getProvider($providerId);
Expand All @@ -134,6 +136,9 @@ public function updateProvider(int $providerId, string $identifier, string $clie
$encryptedClientSecret = $this->crypto->encrypt($clientSecret);
$provider->setClientSecret($encryptedClientSecret);
}
if ($bearerSecret) {
$provider->setBearerSecret($this->base64UrlEncode($bearerSecret));
}
$provider->setDiscoveryEndpoint($discoveryEndpoint);
$provider->setEndSessionEndpoint($endSessionEndpoint ?: null);
$provider->setPostLogoutUri($postLogoutUri ?: null);
Expand Down Expand Up @@ -185,4 +190,8 @@ public function setAdminConfig(array $values): JSONResponse {
}
return new JSONResponse([]);
}

private function base64UrlEncode(string $data): string {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
}
5 changes: 5 additions & 0 deletions lib/Db/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
* @method \void setEndSessionEndpoint(?string $endSessionEndpoint)
* @method \string|\null getPostLogoutUri()
* @method \void setPostLogoutUri(?string $postLogoutUri)
* @method string getBearerSecret()
* @method void setBearerSecret(string $bearerSecret)
* @method string getScope()
* @method \void setScope(string $scope)
*/
class Provider extends Entity implements \JsonSerializable {
Expand All @@ -40,6 +43,8 @@ class Provider extends Entity implements \JsonSerializable {
/** @var string */
protected $postLogoutUri;
/** @var string */
protected $bearerSecret;
/** @var string */
protected $scope;

/**
Expand Down
7 changes: 6 additions & 1 deletion lib/Db/ProviderMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function getProviders() {
* @param string|null $clientid
* @param string|null $clientsecret
* @param string|null $discoveryuri
* @param string|null $bearersecret
* @param string $scope
* @param string|null $endsessionendpointuri
* @param string|null $postLogoutUri
Expand All @@ -90,7 +91,7 @@ public function getProviders() {
* @throws MultipleObjectsReturnedException
*/
public function createOrUpdateProvider(string $identifier, ?string $clientid = null,
?string $clientsecret = null, ?string $discoveryuri = null, string $scope = 'openid email profile',
?string $clientsecret = null, ?string $discoveryuri = null, string $scope = 'openid email profile', ?string $bearersecret = null,
?string $endsessionendpointuri = null, ?string $postLogoutUri = null) {
try {
$provider = $this->findProviderByIdentifier($identifier);
Expand All @@ -109,6 +110,7 @@ public function createOrUpdateProvider(string $identifier, ?string $clientid = n
$provider->setDiscoveryEndpoint($discoveryuri);
$provider->setEndSessionEndpoint($endsessionendpointuri);
$provider->setPostLogoutUri($postLogoutUri);
$provider->setBearerSecret($bearersecret ?? '');
$provider->setScope($scope);
return $this->insert($provider);
} else {
Expand All @@ -127,6 +129,9 @@ public function createOrUpdateProvider(string $identifier, ?string $clientid = n
if ($postLogoutUri !== null) {
$provider->setPostLogoutUri($postLogoutUri ?: null);
}
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 @@ -32,6 +32,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 warning-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
Loading