Skip to content

Commit

Permalink
Merge pull request #2 from akondas/knox-token
Browse files Browse the repository at this point in the history
Make loadCertificate method public
  • Loading branch information
marmichalski authored Jun 25, 2019
2 parents 72e1f3c + 6468baf commit 541c22a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ use Proget\KnoxToken;
$accessTokenJwt = KnoxToken::signAccessToken('access-token', 'keys.json');
```

### Load certificate

```php
use Proget\KnoxToken;

$certificate = KnoxToken::loadCertificate('keys.json');

$certificate->publicKey();
$certificate->privateKeyPem();
````

## License

MIT
18 changes: 9 additions & 9 deletions src/Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ class Certificate
/**
* @var string
*/
private $public;
private $publicKey;

/**
* @var string
*/
private $private;
private $privateKey;

public function __construct(string $public, string $private)
public function __construct(string $publicKey, string $privateKey)
{
$this->public = $public;
$this->private = $private;
$this->publicKey = $publicKey;
$this->privateKey = $privateKey;
}

public function public(): string
public function publicKey(): string
{
return $this->public;
return $this->publicKey;
}

public function privatePem(): string
public function privateKeyPem(): string
{
return "-----BEGIN RSA PRIVATE KEY-----\n".$this->private."\n-----END RSA PRIVATE KEY-----";
return "-----BEGIN RSA PRIVATE KEY-----\n".$this->privateKey."\n-----END RSA PRIVATE KEY-----";
}
}
10 changes: 5 additions & 5 deletions src/KnoxToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public static function signClientIdentifier(string $clientIdentifier, string $ce

return JWT::encode([
'clientIdentifier' => $clientIdentifier,
'publicKey' => $certificate->public(),
'publicKey' => $certificate->publicKey(),
'aud' => self::AUDIENCE,
'jti' => Uuid::uuid1()->toString().Uuid::uuid1()->toString()
], $certificate->privatePem(), 'RS512');
], $certificate->privateKeyPem(), 'RS512');
}

public static function signAccessToken(string $accessToken, string $certificatePath): string
Expand All @@ -29,13 +29,13 @@ public static function signAccessToken(string $accessToken, string $certificateP

return JWT::encode([
'accessToken' => $accessToken,
'publicKey' => $certificate->public(),
'publicKey' => $certificate->publicKey(),
'aud' => self::AUDIENCE,
'jti' => Uuid::uuid1()->toString().Uuid::uuid1()->toString()
], $certificate->privatePem(), 'RS512');
], $certificate->privateKeyPem(), 'RS512');
}

private static function loadCertificate(string $certificatePath): Certificate
public static function loadCertificate(string $certificatePath): Certificate
{
if (!file_exists($certificatePath)) {
throw new \RuntimeException(sprintf('Missing certificate file at %s', $certificatePath));
Expand Down
10 changes: 10 additions & 0 deletions tests/KnoxTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Proget\Tests;

use PHPUnit\Framework\TestCase;
use Proget\Certificate;
use Proget\KnoxToken;

class KnoxTokenTest extends TestCase
Expand All @@ -25,6 +26,15 @@ public function testSignAccessToken(): void
)));
}

public function testLoadCertificate(): void
{
$certificate = KnoxToken::loadCertificate(__DIR__.'/keys.json');

self::assertInstanceOf(Certificate::class, $certificate);
self::assertEquals(204, strlen($certificate->publicKey()));
self::assertEquals(886, strlen($certificate->privateKeyPem()));
}

public function testLoadCertificateInvalidPath(): void
{
$this->expectException(\RuntimeException::class);
Expand Down

0 comments on commit 541c22a

Please sign in to comment.