public static function Utility::generateKeys in OAuth2 Server 8
Same name and namespace in other branches
- 2.0.x src/Utility.php \Drupal\oauth2_server\Utility::generateKeys()
Generates a pair of private and public keys using OpenSSL.
The public key is stored in a PEM encoded X.509 certificate, following Google's example. The certificate can be passed to openssl_verify() directly.
Return value
array An array with the following keys:
- private_key: The generated private key.
- public_key: The generated public key certificate (PEM encoded X.509).
2 calls to Utility::generateKeys()
- oauth2_server_cron in ./
oauth2_server.module - Implements hook_cron().
- Utility::getKeys in src/
Utility.php - Returns the pair of private and public keys used to sign tokens.
File
- src/
Utility.php, line 118
Class
- Utility
- Contains utility methods for the OAuth2 Server.
Namespace
Drupal\oauth2_serverCode
public static function generateKeys() {
$module_path = drupal_get_path('module', 'oauth2_server');
$config = [
'config' => DRUPAL_ROOT . '/' . $module_path . '/oauth2_server.openssl.cnf',
];
// Generate a private key.
$resource = openssl_pkey_new($config);
openssl_pkey_export($resource, $private_key);
// Generate a public key certificate valid for 2 days.
$serial = \Drupal::state()
->get('oauth2_server.next_certificate_id', 0);
$uri = new Url('<front>', [], [
'absolute' => TRUE,
'https' => TRUE,
]);
$dn = [
'CN' => $uri
->toString(),
];
$csr = openssl_csr_new($dn, $resource, $config);
$x509 = openssl_csr_sign($csr, NULL, $resource, 2, $config, $serial);
openssl_x509_export($x509, $public_key_certificate);
// Increment the id for next time. db_next_id() is not used since it can't
// guarantee sequential numbers.
\Drupal::state()
->set('oauth2_server.next_certificate_id', ++$serial);
return [
'private_key' => $private_key,
'public_key' => $public_key_certificate,
];
}