3 namespace BookStack\Auth\Access\Oidc;
5 use phpseclib3\Crypt\Common\PublicKey;
6 use phpseclib3\Crypt\PublicKeyLoader;
7 use phpseclib3\Crypt\RSA;
8 use phpseclib3\Math\BigInteger;
10 class OidcJwtSigningKey
18 * Can be created either from a JWK parameter array or local file path to load a certificate from.
20 * 'file:///var/www/cert.pem'
21 * ['kty' => 'RSA', 'alg' => 'RS256', 'n' => 'abc123...'].
23 * @param array|string $jwkOrKeyPath
25 * @throws OidcInvalidKeyException
27 public function __construct($jwkOrKeyPath)
29 if (is_array($jwkOrKeyPath)) {
30 $this->loadFromJwkArray($jwkOrKeyPath);
31 } elseif (is_string($jwkOrKeyPath) && strpos($jwkOrKeyPath, 'file://') === 0) {
32 $this->loadFromPath($jwkOrKeyPath);
34 throw new OidcInvalidKeyException('Unexpected type of key value provided');
39 * @throws OidcInvalidKeyException
41 protected function loadFromPath(string $path)
44 $key = PublicKeyLoader::load(
45 file_get_contents($path)
47 } catch (\Exception $exception) {
48 throw new OidcInvalidKeyException("Failed to load key from file path with error: {$exception->getMessage()}");
51 if (!$key instanceof RSA) {
52 throw new OidcInvalidKeyException('Key loaded from file path is not an RSA key as expected');
55 $this->key = $key->withPadding(RSA::SIGNATURE_PKCS1);
59 * @throws OidcInvalidKeyException
61 protected function loadFromJwkArray(array $jwk)
63 // 'alg' is optional for a JWK, but we will still attempt to validate if
64 // it exists otherwise presume it will be compatible.
65 $alg = $jwk['alg'] ?? null;
66 if ($jwk['kty'] !== 'RSA' || !(is_null($alg) || $alg === 'RS256')) {
67 throw new OidcInvalidKeyException("Only RS256 keys are currently supported. Found key using {$alg}");
70 if (empty($jwk['use'])) {
71 throw new OidcInvalidKeyException('A "use" parameter on the provided key is expected');
74 if ($jwk['use'] !== 'sig') {
75 throw new OidcInvalidKeyException("Only signature keys are currently supported. Found key for use {$jwk['use']}");
78 if (empty($jwk['e'])) {
79 throw new OidcInvalidKeyException('An "e" parameter on the provided key is expected');
82 if (empty($jwk['n'])) {
83 throw new OidcInvalidKeyException('A "n" parameter on the provided key is expected');
86 $n = strtr($jwk['n'] ?? '', '-_', '+/');
89 $key = PublicKeyLoader::load([
90 'e' => new BigInteger(base64_decode($jwk['e']), 256),
91 'n' => new BigInteger(base64_decode($n), 256),
93 } catch (\Exception $exception) {
94 throw new OidcInvalidKeyException("Failed to load key from JWK parameters with error: {$exception->getMessage()}");
97 if (!$key instanceof RSA) {
98 throw new OidcInvalidKeyException('Key loaded from file path is not an RSA key as expected');
101 $this->key = $key->withPadding(RSA::SIGNATURE_PKCS1);
105 * Use this key to sign the given content and return the signature.
107 public function verify(string $content, string $signature): bool
109 return $this->key->verify($content, $signature);
113 * Convert the key to a PEM encoded key string.
115 public function toPem(): string
117 return $this->key->toString('PKCS8');