Skip to content

Commit a9c971d

Browse files
committed
feat(upsert): new options to read client secret from env var or file
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
1 parent 831a5cc commit a9c971d

1 file changed

Lines changed: 51 additions & 7 deletions

File tree

lib/Command/UpsertProvider.php

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ protected function configure() {
178178
->addArgument('identifier', InputArgument::OPTIONAL, 'Administrative identifier name of the provider in the setup')
179179
->addOption('clientid', 'c', InputOption::VALUE_REQUIRED, 'OpenID client identifier')
180180
->addOption('clientsecret', 's', InputOption::VALUE_REQUIRED, 'OpenID client secret')
181+
->addOption('clientsecret-file', null, InputOption::VALUE_REQUIRED, 'File that contains the OpenID client secret')
182+
->addOption('clientsecret-env', null, InputOption::VALUE_REQUIRED, 'Environment variable that contains the OpenID client secret')
181183
->addOption('discoveryuri', 'd', InputOption::VALUE_REQUIRED, 'OpenID discovery endpoint uri')
182184
->addOption('endsessionendpointuri', 'e', InputOption::VALUE_REQUIRED, 'OpenID end session endpoint uri')
183185
->addOption('postlogouturi', 'p', InputOption::VALUE_REQUIRED, 'Post logout URI')
@@ -192,10 +194,11 @@ protected function execute(InputInterface $input, OutputInterface $output) {
192194
$outputFormat = $input->getOption('output') ?? 'table';
193195

194196
$identifier = $input->getArgument('identifier');
195-
$clientid = $input->getOption('clientid');
196-
$clientsecret = $input->getOption('clientsecret');
197-
if ($clientsecret !== null) {
198-
$clientsecret = $this->crypto->encrypt($clientsecret);
197+
$clientId = $input->getOption('clientid');
198+
try {
199+
$clientSecret = $this->getClientSecretInput($input, $output);
200+
} catch (\Exception $e) {
201+
return 1;
199202
}
200203
$discoveryuri = $input->getOption('discoveryuri');
201204
$endsessionendpointuri = $input->getOption('endsessionendpointuri');
@@ -218,7 +221,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
218221
try {
219222
$provider = $this->providerMapper->findProviderByIdentifier($identifier);
220223
} catch (DoesNotExistException $e) {
221-
$output->writeln('Provider not found');
224+
$output->writeln('<error>Provider not found</error>');
222225
return -1;
223226
}
224227
$provider = $this->providerService->getProviderWithSettings($provider->getId());
@@ -250,7 +253,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
250253
}
251254
try {
252255
$provider = $this->providerMapper->createOrUpdateProvider(
253-
$identifier, $clientid, $clientsecret, $discoveryuri, $scope, $endsessionendpointuri, $postLogoutUri
256+
$identifier, $clientId, $clientSecret, $discoveryuri, $scope, $endsessionendpointuri, $postLogoutUri
254257
);
255258
// invalidate JWKS cache (even if it was just created)
256259
$this->providerService->setSetting($provider->getId(), ProviderService::SETTING_JWKS_CACHE, '');
@@ -287,7 +290,7 @@ private function listProviders(InputInterface $input, OutputInterface $output) {
287290
}
288291

289292
if (count($providers) === 0) {
290-
$output->writeln('No providers configured');
293+
$output->writeln('<error>No providers configured</error>');
291294
return 0;
292295
}
293296

@@ -306,4 +309,45 @@ private function listProviders(InputInterface $input, OutputInterface $output) {
306309
$table->render();
307310
return 0;
308311
}
312+
313+
private function getClientSecretInput(InputInterface $input, OutputInterface $output): ?string {
314+
$clientSecret = $input->getOption('clientsecret');
315+
$clientSecretFile = $input->getOption('clientsecret-file');
316+
$clientSecretEnv = $input->getOption('clientsecret-env');
317+
if (
318+
($clientSecret !== null && $clientSecretFile !== null)
319+
|| ($clientSecret !== null && $clientSecretEnv !== null)
320+
|| ($clientSecretFile !== null && $clientSecretEnv !== null)
321+
) {
322+
$output->writeln('<comment>Only one of "--clientsecret", "--clientsecret-file" or "--clientsecret-env" can be used.</comment>');
323+
throw new \Exception();
324+
}
325+
if ($clientSecret !== null) {
326+
$clientSecret = $this->crypto->encrypt($clientSecret);
327+
}
328+
if ($clientSecretFile) {
329+
$clientSecret = file_get_contents($clientSecretFile);
330+
if (is_string($clientSecret) && $clientSecret !== '') {
331+
$clientSecret = trim($clientSecret);
332+
$clientSecret = $this->crypto->encrypt($clientSecret);
333+
$output->writeln('<info>Client secret loaded from file "' . $clientSecretFile . '"</info>');
334+
} else {
335+
$output->writeln('<error>Client secret file "' . $clientSecretFile . '" could not be read or is empty</error>');
336+
throw new \Exception();
337+
}
338+
}
339+
if ($clientSecretEnv) {
340+
$clientSecret = getenv($clientSecretEnv);
341+
if (is_string($clientSecret) && $clientSecret !== '') {
342+
$clientSecret = trim($clientSecret);
343+
$clientSecret = $this->crypto->encrypt($clientSecret);
344+
$output->writeln('<info>Client secret loaded from environment variable "' . $clientSecretEnv . '"</info>');
345+
} else {
346+
$output->writeln('<error>Client secret environment variable "' . $clientSecretFile . '" could not be read or is empty</error>');
347+
throw new \Exception();
348+
}
349+
}
350+
351+
return $clientSecret;
352+
}
309353
}

0 commit comments

Comments
 (0)