You are here

public function KeyGeneratorService::generateKeys 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::generateKeys()
  2. 5.x src/Service/KeyGeneratorService.php \Drupal\simple_oauth\Service\KeyGeneratorService::generateKeys()

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.

Parameters

string $dir_path: Private key path.

Throws

\Drupal\simple_oauth\Service\Exception\ExtensionNotLoadedException

\Drupal\simple_oauth\Service\Exception\FilesystemValidationException

File

src/Service/KeyGeneratorService.php, line 63

Class

KeyGeneratorService
Generates the signature keys.

Namespace

Drupal\simple_oauth\Service

Code

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);
  }
}