diff --git a/.gitignore b/.gitignore index 51b3172..9d9db20 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ /vendor /.idea .DS_Store -phpunit.xml composer.lock diff --git a/composer.json b/composer.json index 6ba772f..4896d86 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,6 @@ { "name": "codercat/jwk-to-pem", + "description": "Convert JWK to PEM format.", "type": "library", "license": "MIT", "authors": [ @@ -11,12 +12,22 @@ "minimum-stability": "dev", "require": { "php": ">=7.0.0", - "phpseclib/phpseclib": "~2.0", - "ext-gmp": "*" + "phpseclib/phpseclib": "~2.0" + }, + "require-dev": { + "phpunit/phpunit": "^7.0" + }, + "suggest": { + "ext-gmp":"" }, "autoload": { "psr-4": { "CoderCat\\JWKToPEM\\": "src/" } + }, + "autoload-dev": { + "psr-4": { + "CoderCat\\JWKToPEM\\Tests\\": "tests/" + } } } diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..c9fe927 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,10 @@ + + + + tests + + + 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(); diff --git a/tests/JWKConverterTest.php b/tests/JWKConverterTest.php new file mode 100644 index 0000000..d8b5c12 --- /dev/null +++ b/tests/JWKConverterTest.php @@ -0,0 +1,48 @@ +jwkConverter = new JwkConverter(); + } + + /** @dataProvider provideToPem */ + public function testToPem($jwk, $expected) + { + $this->assertEquals($expected, $this->jwkConverter->toPem($jwk)); + } + + public function provideToPem() + { + return [ + [ + [ + "kty" => "RSA", + "kid" => "zhA-H1DWOSgWQAIW7mewCYeaZLGpkgW_hXfq8jmV99I", + "use" => "sig", + "alg" => "RS256", + "e" => "AQAB", + "n" => "vdv73smpkrTIBSM8ka-pVXbNi7zYalm0R6WFBH4X8PQj8C7VfdckGsA6bTBseOVCTbu187_63yU2U7vqYiqwSLmkrBVAJjYMJY_XXfncxwqDWR_aa7eIJSKh22H_6yz6kFyF1h_ZSk68CPAEQpvd9VFAr4VLEwD32Ag6MwymSOxmFWJyddEtttdGcXLSrHcya3RWyG5KAW3Ti-HgNC-xo_C5LgEsUgjeUq-rc8NBXZrNCY-LJ_R-qtB_-5NkwlMJ_fUMBDcmZuciNOH71q7xyn0FGmGjrJXnyVJwyDiTrKRO36piMuiaJE2nIRJaLvhDN5M1K2VhSKPuaqUPyxLzBw" + ], + "-----BEGIN PUBLIC KEY-----" . PHP_EOL + . "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvdv73smpkrTIBSM8ka+p" . PHP_EOL + . "VXbNi7zYalm0R6WFBH4X8PQj8C7VfdckGsA6bTBseOVCTbu187/63yU2U7vqYiqw" . PHP_EOL + . "SLmkrBVAJjYMJY/XXfncxwqDWR/aa7eIJSKh22H/6yz6kFyF1h/ZSk68CPAEQpvd" . PHP_EOL + . "9VFAr4VLEwD32Ag6MwymSOxmFWJyddEtttdGcXLSrHcya3RWyG5KAW3Ti+HgNC+x" . PHP_EOL + . "o/C5LgEsUgjeUq+rc8NBXZrNCY+LJ/R+qtB/+5NkwlMJ/fUMBDcmZuciNOH71q7x" . PHP_EOL + . "yn0FGmGjrJXnyVJwyDiTrKRO36piMuiaJE2nIRJaLvhDN5M1K2VhSKPuaqUPyxLz" . PHP_EOL + . "BwIDAQAB" . PHP_EOL + . "-----END PUBLIC KEY-----" + ] + ]; + } +}