You are here

public function JwksEntity::getKeys in Simple OAuth (OAuth2) & OpenID Connect 5.x

Returns the keys in JWK (JSON Web Key) format.

Return value

array List of keys.

See also

https://tools.ietf.org/html/rfc7517

File

src/Entities/JwksEntity.php, line 18

Class

JwksEntity
A JSON Web Key Store entity.

Namespace

Drupal\simple_oauth\Entities

Code

public function getKeys() {
  $json_data = [];

  // Get the public key from simple_oauth settings.
  $config = \Drupal::config('simple_oauth.settings');
  if (!empty($config)) {
    $public_key_real = \Drupal::service('file_system')
      ->realpath($config
      ->get('public_key'));
    if (!empty($public_key_real)) {
      $key_info = openssl_pkey_get_details(openssl_pkey_get_public(file_get_contents($public_key_real)));
      $json_data = [
        'keys' => [
          [
            'kty' => 'RSA',
            'n' => rtrim(str_replace([
              '+',
              '/',
            ], [
              '-',
              '_',
            ], base64_encode($key_info['rsa']['n'])), '='),
            'e' => rtrim(str_replace([
              '+',
              '/',
            ], [
              '-',
              '_',
            ], base64_encode($key_info['rsa']['e'])), '='),
          ],
        ],
      ];
    }
  }
  return $json_data;
}