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
17 changes: 15 additions & 2 deletions lib/Command/UpsertProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ protected function configure(): void {
->addOption('clientsecret-file', null, InputOption::VALUE_REQUIRED, 'File that contains the OpenID client secret')
->addOption('clientsecret-env', null, InputOption::VALUE_REQUIRED, 'Environment variable that contains the 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 @@ -217,10 +218,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
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 @@ -261,7 +270,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
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 All @@ -283,6 +292,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

private function base64UrlEncode(string $data): string {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}

private function listProviders(InputInterface $input, OutputInterface $output): int {
$outputFormat = $input->getOption('output') ?? 'table';
$providers = $this->providerMapper->getProviders();
Expand Down
14 changes: 12 additions & 2 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private function isDiscoveryEndpointValid($url) {
*/
#[PasswordConfirmationRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['user_oidc_settings'])]
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): DataResponse {
if ($this->providerService->getProviderByIdentifier($identifier) !== null) {
Expand All @@ -126,6 +126,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 Down Expand Up @@ -153,7 +155,7 @@ public function createProvider(string $identifier, string $clientId, string $cli
*/
#[PasswordConfirmationRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['user_oidc_settings'])]
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): DataResponse {
$provider = $this->providerMapper->getProvider($providerId);
Expand All @@ -177,6 +179,10 @@ public function updateProvider(int $providerId, string $identifier, string $clie
$encryptedClientSecret = $this->crypto->encrypt($clientSecret);
$provider->setClientSecret($encryptedClientSecret);
}
if ($bearerSecret) {
$encryptedBearerSecret = $this->crypto->encrypt($this->base64UrlEncode($bearerSecret));
$provider->setBearerSecret($encryptedBearerSecret);
}
$provider->setDiscoveryEndpoint($discoveryEndpoint);
$provider->setEndSessionEndpoint($endSessionEndpoint ?: null);
$provider->setPostLogoutUri($postLogoutUri ?: null);
Expand All @@ -191,6 +197,10 @@ public function updateProvider(int $providerId, string $identifier, string $clie
return new DataResponse(array_merge($provider->jsonSerialize(), ['settings' => $providerSettings]));
}

private function base64UrlEncode(string $data): string {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}

/**
* Delete a provider
*
Expand Down
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|\null getBearerSecret()
* @method \void setBearerSecret(string $bearerSecret)
* @method \string|\null 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 @@ -75,13 +75,14 @@ public function getProviders(): array {
* @throws MultipleObjectsReturnedException
*/
public function createOrUpdateProvider(
string $identifier,
?string $identifier = null,
?string $clientId = null,
?string $clientSecret = null,
?string $discoveryUri = null,
string $scope = 'openid email profile',
?string $endSessionEndpointUri = null,
?string $postLogoutUri = null,
?string $bearersecret = null,
): Provider {
try {
$provider = $this->findProviderByIdentifier($identifier);
Expand All @@ -102,6 +103,9 @@ public function createOrUpdateProvider(
if ($postLogoutUri !== null) {
$provider->setPostLogoutUri($postLogoutUri);
}
if ($bearersecret !== null) {
$provider->setBearerSecret($bearersecret);
}
$provider->setScope($scope);

return $this->update($provider);
Expand All @@ -118,6 +122,7 @@ public function createOrUpdateProvider(
$provider->setDiscoveryEndpoint($discoveryUri);
$provider->setEndSessionEndpoint($endSessionEndpointUri);
$provider->setPostLogoutUri($postLogoutUri);
$provider->setBearerSecret($bearersecret ?? '');
$provider->setScope($scope);

return $this->insert($provider);
Expand Down
26 changes: 26 additions & 0 deletions lib/Migration/Version00008Date20211114183344.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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,
'default' => '',
]);

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

declare(strict_types=1);

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