Skip to content
Snippets Groups Projects
Select Git revision
  • 1a70031983001e10469b2d186dcf30de10aec3db
  • main default protected
  • pdf-annotieren
  • pdf-annotieren-2.0
  • issue-4244
  • issues-4244-b
  • pdf-annotieren-old
  • biest-4274
  • issue-2982
  • issue-660
  • issue-3326
  • issue-3270
  • issue-3616
  • 5.1
  • 5.2
  • 5.3
  • 5.4
  • 5.5
  • issue-4255
  • issue-4261
  • issue-4262
  • v5.4.2
  • v5.3.5
  • v5.2.7
  • v5.1.8
  • v5.4.1
  • v5.3.4
  • v5.2.6
  • v5.1.7
  • v5.0.9
  • v5.4
  • v5.3.3
  • v5.2.5
  • v5.1.6
  • v5.0.8
  • v5.3.2
  • v5.2.4
  • v5.1.5
  • v5.0.7
  • v5.3.1
  • v5.2.3
41 results

Keys.php

Blame
  • Forked from Stud.IP / Stud.IP
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    Keys.php 2.20 KiB
    <?php
    
    namespace Studip\Cli\Commands\OAuth2;
    
    use phpseclib3\Crypt\RSA;
    use Studip\OAuth2\Container;
    use Studip\OAuth2\KeyInformation;
    use Studip\OAuth2\SetupInformation;
    use Symfony\Component\Console\Command\Command;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Input\InputOption;
    use Symfony\Component\Console\Output\OutputInterface;
    use Symfony\Component\Console\Style\SymfonyStyle;
    
    class Keys extends Command
    {
        protected static $defaultName = 'oauth2:keys';
    
        protected function configure(): void
        {
            $this->setDescription(
                'Erstelle alle kryptografischen Schlüssel, um Stud.IP als OAuth2-Authorization-Server zu verwenden.'
            );
            $this->addOption('force', null, InputOption::VALUE_NONE, 'Überschreibe ggf. vorhandene Schlüssel');
        }
    
        protected function execute(InputInterface $input, OutputInterface $output): int
        {
            $io = new SymfonyStyle($input, $output);
    
            $container = new Container();
            $setup = $container->get(SetupInformation::class);
    
            $encryptionKey = $setup->encryptionKey();
            $publicKey = $setup->publicKey();
            $privateKey = $setup->privateKey();
    
            $force = $input->getOption('force');
    
            if (($encryptionKey->exists() || $publicKey->exists() || $privateKey->exists()) && !$force) {
                $io->error(
                    'Schlüsseldateien liegen bereits vor. Verwenden Sie die Option --force, um diese zu überschreiben.'
                );
                return Command::FAILURE;
            }
    
            $this->storeKeyContentsToFile($encryptionKey, $this->generateEncryptionKey());
    
            $keys = RSA::createKey(4096);
            $this->storeKeyContentsToFile($publicKey, (string) $key->getPublicKey());
            $this->storeKeyContentsToFile($privateKey, (string) $key);
    
            $io->info('Schlüsseldateien erfolgreich angelegt.');
    
            return Command::SUCCESS;
        }
    
        private function storeKeyContentsToFile(KeyInformation $key, string $contents)
        {
            file_put_contents($key->filename(), $contents);
            chmod($key->filename(), 0660);
        }
    
        private function generateEncryptionKey(): string
        {
            return "<?php return '" . randomString(48) . "';";
        }
    }