From 6f5746c4539257a2766e6281b0bd7fc1c2010f5b Mon Sep 17 00:00:00 2001 From: Niklas Ekman Date: Thu, 7 Feb 2019 11:00:48 +0100 Subject: [PATCH] Add ability to dependency inject Base64UrlDecoder --- src/JWKConverter.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/JWKConverter.php b/src/JWKConverter.php index 9c4ff44..750831c 100644 --- a/src/JWKConverter.php +++ b/src/JWKConverter.php @@ -19,6 +19,14 @@ class JWKConverter { + /** @var Base64UrlDecoder */ + private $base64UrlDecoder; + + public function __construct(?Base64UrlDecoder $base64UrlDecoder = null) + { + $this->base64UrlDecoder = $base64UrlDecoder ?? new Base64UrlDecoder(); + } + /** * @param array $jwk * @return string @@ -40,11 +48,10 @@ public function toPEM(array $jwk): string } $rsa = new RSA(); - $base64UrlDecoder = new Base64UrlDecoder(); $rsa->loadKey( [ 'e' => new BigInteger(base64_decode($jwk['e']), 256), - 'n' => new BigInteger($base64UrlDecoder->decode($jwk['n']), 256) + 'n' => new BigInteger($this->base64UrlDecoder->decode($jwk['n']), 256) ] ); return $rsa->getPublicKey();