Skip to content

Commit 1bd7890

Browse files
authored
Merge pull request #75 from mcetkovsky/master
Improvements to getJwtVerificationKeys
2 parents 4f34c3b + 3f1bc43 commit 1bd7890

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/Provider/Azure.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,32 @@ public function getJwtVerificationKeys()
245245
foreach ($response['keys'] as $i => $keyinfo) {
246246
if (isset($keyinfo['x5c']) && is_array($keyinfo['x5c'])) {
247247
foreach ($keyinfo['x5c'] as $encodedkey) {
248-
$key = "-----BEGIN CERTIFICATE-----\n";
249-
$key .= wordwrap($encodedkey, 64, "\n", true);
250-
$key .= "\n-----END CERTIFICATE-----";
251-
$keys[$keyinfo['kid']] = $key;
248+
$cert =
249+
'-----BEGIN CERTIFICATE-----' . PHP_EOL
250+
. chunk_split($encodedkey, 64, PHP_EOL)
251+
. '-----END CERTIFICATE-----' . PHP_EOL;
252+
253+
$cert_object = openssl_x509_read($cert);
254+
255+
if ($cert_object === false) {
256+
throw new \RuntimeException('An attempt to read ' . $encodedkey . ' as a certificate failed.');
257+
}
258+
259+
$pkey_object = openssl_pkey_get_public($cert_object);
260+
261+
if ($pkey_object === false) {
262+
throw new \RuntimeException('An attempt to read a public key from a ' . $encodedkey . ' certificate failed.');
263+
}
264+
265+
$pkey_array = openssl_pkey_get_details($pkey_object);
266+
267+
if ($pkey_array === false) {
268+
throw new \RuntimeException('An attempt to get a public key as an array from a ' . $encodedkey . ' certificate failed.');
269+
}
270+
271+
$publicKey = $pkey_array ['key'];
272+
273+
$keys[$keyinfo['kid']] = $publicKey;
252274
}
253275
}
254276
}

0 commit comments

Comments
 (0)