Skip to content

Commit 6573e96

Browse files
author
codercat
authored
Merge pull request #1 from Ekman/feature/tests
Add a unit test. Move "ext-gmp" to suggested dependency
2 parents 1df3588 + 6f5746c commit 6573e96

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/vendor
22
/.idea
33
.DS_Store
4-
phpunit.xml
54
composer.lock

composer.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "codercat/jwk-to-pem",
3+
"description": "Convert JWK to PEM format.",
34
"type": "library",
45
"license": "MIT",
56
"authors": [
@@ -11,12 +12,22 @@
1112
"minimum-stability": "dev",
1213
"require": {
1314
"php": ">=7.0.0",
14-
"phpseclib/phpseclib": "~2.0",
15-
"ext-gmp": "*"
15+
"phpseclib/phpseclib": "~2.0"
16+
},
17+
"require-dev": {
18+
"phpunit/phpunit": "^7.0"
19+
},
20+
"suggest": {
21+
"ext-gmp":""
1622
},
1723
"autoload": {
1824
"psr-4": {
1925
"CoderCat\\JWKToPEM\\": "src/"
2026
}
27+
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"CoderCat\\JWKToPEM\\Tests\\": "tests/"
31+
}
2132
}
2233
}

phpunit.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<phpunit
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.5/phpunit.xsd"
4+
bootstrap="vendor/autoload.php">
5+
<testsuites>
6+
<testsuite name="unit tests">
7+
<directory>tests</directory>
8+
</testsuite>
9+
</testsuites>
10+
</phpunit>

src/JWKConverter.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919
class JWKConverter
2020
{
2121

22+
/** @var Base64UrlDecoder */
23+
private $base64UrlDecoder;
24+
25+
public function __construct(?Base64UrlDecoder $base64UrlDecoder = null)
26+
{
27+
$this->base64UrlDecoder = $base64UrlDecoder ?? new Base64UrlDecoder();
28+
}
29+
2230
/**
2331
* @param array $jwk
2432
* @return string
@@ -40,11 +48,10 @@ public function toPEM(array $jwk): string
4048
}
4149

4250
$rsa = new RSA();
43-
$base64UrlDecoder = new Base64UrlDecoder();
4451
$rsa->loadKey(
4552
[
4653
'e' => new BigInteger(base64_decode($jwk['e']), 256),
47-
'n' => new BigInteger($base64UrlDecoder->decode($jwk['n']), 256)
54+
'n' => new BigInteger($this->base64UrlDecoder->decode($jwk['n']), 256)
4855
]
4956
);
5057
return $rsa->getPublicKey();

tests/JWKConverterTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
namespace CoderCat\JWKToPEM\Tests;
3+
4+
use CoderCat\JWKToPEM\JWKConverter;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class JWKConverterTest extends TestCase
8+
{
9+
/** @var JWKConverter */
10+
private $jwkConverter;
11+
12+
protected function setUp()
13+
{
14+
parent::setUp();
15+
$this->jwkConverter = new JwkConverter();
16+
}
17+
18+
/** @dataProvider provideToPem */
19+
public function testToPem($jwk, $expected)
20+
{
21+
$this->assertEquals($expected, $this->jwkConverter->toPem($jwk));
22+
}
23+
24+
public function provideToPem()
25+
{
26+
return [
27+
[
28+
[
29+
"kty" => "RSA",
30+
"kid" => "zhA-H1DWOSgWQAIW7mewCYeaZLGpkgW_hXfq8jmV99I",
31+
"use" => "sig",
32+
"alg" => "RS256",
33+
"e" => "AQAB",
34+
"n" => "vdv73smpkrTIBSM8ka-pVXbNi7zYalm0R6WFBH4X8PQj8C7VfdckGsA6bTBseOVCTbu187_63yU2U7vqYiqwSLmkrBVAJjYMJY_XXfncxwqDWR_aa7eIJSKh22H_6yz6kFyF1h_ZSk68CPAEQpvd9VFAr4VLEwD32Ag6MwymSOxmFWJyddEtttdGcXLSrHcya3RWyG5KAW3Ti-HgNC-xo_C5LgEsUgjeUq-rc8NBXZrNCY-LJ_R-qtB_-5NkwlMJ_fUMBDcmZuciNOH71q7xyn0FGmGjrJXnyVJwyDiTrKRO36piMuiaJE2nIRJaLvhDN5M1K2VhSKPuaqUPyxLzBw"
35+
],
36+
"-----BEGIN PUBLIC KEY-----" . PHP_EOL
37+
. "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvdv73smpkrTIBSM8ka+p" . PHP_EOL
38+
. "VXbNi7zYalm0R6WFBH4X8PQj8C7VfdckGsA6bTBseOVCTbu187/63yU2U7vqYiqw" . PHP_EOL
39+
. "SLmkrBVAJjYMJY/XXfncxwqDWR/aa7eIJSKh22H/6yz6kFyF1h/ZSk68CPAEQpvd" . PHP_EOL
40+
. "9VFAr4VLEwD32Ag6MwymSOxmFWJyddEtttdGcXLSrHcya3RWyG5KAW3Ti+HgNC+x" . PHP_EOL
41+
. "o/C5LgEsUgjeUq+rc8NBXZrNCY+LJ/R+qtB/+5NkwlMJ/fUMBDcmZuciNOH71q7x" . PHP_EOL
42+
. "yn0FGmGjrJXnyVJwyDiTrKRO36piMuiaJE2nIRJaLvhDN5M1K2VhSKPuaqUPyxLz" . PHP_EOL
43+
. "BwIDAQAB" . PHP_EOL
44+
. "-----END PUBLIC KEY-----"
45+
]
46+
];
47+
}
48+
}

0 commit comments

Comments
 (0)