You are here

public function JwtTranscoder::setPrivateKey in JSON Web Token Authentication (JWT) 8

Same name and namespace in other branches
  1. 8.0 src/Transcoder/JwtTranscoder.php \Drupal\jwt\Transcoder\JwtTranscoder::setPrivateKey()

Sets the private key used to create signatures for an asymmetric algorithm.

This key is only used when an asymmetric algorithm is selected. Currently supported asymmetric algorithms are:

  • RS256

Parameters

string $private_key: A PEM encoded private key.

bool $derive_public_key: (Optional) Derive the public key from the private key. Defaults to true.

Return value

bool Function does some validation of the key. Returns TRUE on success.

Overrides JwtTranscoderInterface::setPrivateKey

1 call to JwtTranscoder::setPrivateKey()
JwtTranscoder::__construct in src/Transcoder/JwtTranscoder.php
Constructs a new JwtTranscoder.

File

src/Transcoder/JwtTranscoder.php, line 144

Class

JwtTranscoder
Class JwtTranscoder.

Namespace

Drupal\jwt\Transcoder

Code

public function setPrivateKey($private_key, $derive_public_key = TRUE) {
  $key_context = openssl_pkey_get_private($private_key);
  if ($key_context === FALSE) {
    return FALSE;
  }
  $key_details = openssl_pkey_get_details($key_context);
  if ($key_details === FALSE || $key_details['type'] != OPENSSL_KEYTYPE_RSA) {
    return FALSE;
  }
  $this->privateKey = $private_key;
  if ($derive_public_key) {
    $this->publicKey = $key_details['key'];
  }
  return TRUE;
}