Skip to content

Commit

Permalink
Merge pull request #1 from Ekman/feature/tests
Browse files Browse the repository at this point in the history
Add a unit test. Move "ext-gmp" to suggested dependency
  • Loading branch information
codercat authored Mar 11, 2019
2 parents 1df3588 + 6f5746c commit 6573e96
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/vendor
/.idea
.DS_Store
phpunit.xml
composer.lock
15 changes: 13 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "codercat/jwk-to-pem",
"description": "Convert JWK to PEM format.",
"type": "library",
"license": "MIT",
"authors": [
Expand All @@ -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/"
}
}
}
10 changes: 10 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.5/phpunit.xsd"
bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="unit tests">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
11 changes: 9 additions & 2 deletions src/JWKConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
48 changes: 48 additions & 0 deletions tests/JWKConverterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace CoderCat\JWKToPEM\Tests;

use CoderCat\JWKToPEM\JWKConverter;
use PHPUnit\Framework\TestCase;

class JWKConverterTest extends TestCase
{
/** @var JWKConverter */
private $jwkConverter;

protected function setUp()
{
parent::setUp();
$this->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-----"
]
];
}
}

0 comments on commit 6573e96

Please sign in to comment.