You are here

class KeyGeneratorService in Simple OAuth (OAuth2) & OpenID Connect 8.4

Same name and namespace in other branches
  1. 8.3 src/Service/KeyGeneratorService.php \Drupal\simple_oauth\Service\KeyGeneratorService
  2. 5.x src/Service/KeyGeneratorService.php \Drupal\simple_oauth\Service\KeyGeneratorService

Generates the signature keys.

@internal

Hierarchy

Expanded class hierarchy of KeyGeneratorService

2 files declare their use of KeyGeneratorService
Oauth2GenerateKeyForm.php in src/Entity/Form/Oauth2GenerateKeyForm.php
SimpleOauthCommands.php in src/Commands/SimpleOauthCommands.php
1 string reference to 'KeyGeneratorService'
simple_oauth.services.yml in ./simple_oauth.services.yml
simple_oauth.services.yml
1 service uses KeyGeneratorService
simple_oauth.key.generator in ./simple_oauth.services.yml
Drupal\simple_oauth\Service\KeyGeneratorService

File

src/Service/KeyGeneratorService.php, line 14

Namespace

Drupal\simple_oauth\Service
View source
class KeyGeneratorService {

  /**
   * The filesystem service.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  private $fileSystem;

  /**
   * The filesystem checker.
   *
   * @var \Drupal\simple_oauth\Service\Filesystem\FileSystemChecker
   */
  private $fileSystemChecker;

  /**
   * The filesystem validator.
   *
   * @var \Drupal\simple_oauth\Service\Filesystem\FilesystemValidator
   */
  private $validator;

  /**
   * KeyGeneratorService constructor.
   *
   * @param \Drupal\simple_oauth\Service\Filesystem\FileSystemChecker $file_system_checker
   *   The file system checker.
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system service.
   */
  public function __construct(FileSystemChecker $file_system_checker, FileSystemInterface $file_system) {
    $this->fileSystemChecker = $file_system_checker;
    $this->fileSystem = $file_system;
    $this->validator = new FilesystemValidator($file_system_checker);
  }

  /**
   * Generate both public key and private key on the given paths.
   *
   * If no public path is given, then the private path is going to be use for
   * both keys.
   *
   * @param string $dir_path
   *   Private key path.
   *
   * @throws \Drupal\simple_oauth\Service\Exception\ExtensionNotLoadedException
   * @throws \Drupal\simple_oauth\Service\Exception\FilesystemValidationException
   */
  public function generateKeys($dir_path) {

    // Create path array.
    $key_names = [
      'private',
      'public',
    ];

    // Validate.
    $this->validator
      ->validateOpensslExtensionExist('openssl');
    $this->validator
      ->validateAreDirs([
      $dir_path,
    ]);
    $this->validator
      ->validateAreWritable([
      $dir_path,
    ]);

    // Create Keys array.
    $keys = KeyGenerator::generateKeys();

    // Create both keys.
    foreach ($key_names as $name) {

      // Key uri.
      $key_uri = "{$dir_path}/{$name}.key";

      // Remove old key, if existing.
      if (file_exists($key_uri)) {
        $this->fileSystem
          ->unlink($key_uri);
      }

      // Write key content to key file.
      $this->fileSystemChecker
        ->write($key_uri, $keys[$name]);

      // Set correct permission to key file.
      $this->fileSystem
        ->chmod($key_uri, 0600);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
KeyGeneratorService::$fileSystem private property The filesystem service.
KeyGeneratorService::$fileSystemChecker private property The filesystem checker.
KeyGeneratorService::$validator private property The filesystem validator.
KeyGeneratorService::generateKeys public function Generate both public key and private key on the given paths.
KeyGeneratorService::__construct public function KeyGeneratorService constructor.