Skip to content

Commit 541c22a

Browse files
authored
Merge pull request #2 from akondas/knox-token
Make loadCertificate method public
2 parents 72e1f3c + 6468baf commit 541c22a

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ use Proget\KnoxToken;
2828
$accessTokenJwt = KnoxToken::signAccessToken('access-token', 'keys.json');
2929
```
3030

31+
### Load certificate
32+
33+
```php
34+
use Proget\KnoxToken;
35+
36+
$certificate = KnoxToken::loadCertificate('keys.json');
37+
38+
$certificate->publicKey();
39+
$certificate->privateKeyPem();
40+
````
41+
3142
## License
3243

3344
MIT

src/Certificate.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@ class Certificate
99
/**
1010
* @var string
1111
*/
12-
private $public;
12+
private $publicKey;
1313

1414
/**
1515
* @var string
1616
*/
17-
private $private;
17+
private $privateKey;
1818

19-
public function __construct(string $public, string $private)
19+
public function __construct(string $publicKey, string $privateKey)
2020
{
21-
$this->public = $public;
22-
$this->private = $private;
21+
$this->publicKey = $publicKey;
22+
$this->privateKey = $privateKey;
2323
}
2424

25-
public function public(): string
25+
public function publicKey(): string
2626
{
27-
return $this->public;
27+
return $this->publicKey;
2828
}
2929

30-
public function privatePem(): string
30+
public function privateKeyPem(): string
3131
{
32-
return "-----BEGIN RSA PRIVATE KEY-----\n".$this->private."\n-----END RSA PRIVATE KEY-----";
32+
return "-----BEGIN RSA PRIVATE KEY-----\n".$this->privateKey."\n-----END RSA PRIVATE KEY-----";
3333
}
3434
}

src/KnoxToken.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public static function signClientIdentifier(string $clientIdentifier, string $ce
1717

1818
return JWT::encode([
1919
'clientIdentifier' => $clientIdentifier,
20-
'publicKey' => $certificate->public(),
20+
'publicKey' => $certificate->publicKey(),
2121
'aud' => self::AUDIENCE,
2222
'jti' => Uuid::uuid1()->toString().Uuid::uuid1()->toString()
23-
], $certificate->privatePem(), 'RS512');
23+
], $certificate->privateKeyPem(), 'RS512');
2424
}
2525

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

3030
return JWT::encode([
3131
'accessToken' => $accessToken,
32-
'publicKey' => $certificate->public(),
32+
'publicKey' => $certificate->publicKey(),
3333
'aud' => self::AUDIENCE,
3434
'jti' => Uuid::uuid1()->toString().Uuid::uuid1()->toString()
35-
], $certificate->privatePem(), 'RS512');
35+
], $certificate->privateKeyPem(), 'RS512');
3636
}
3737

38-
private static function loadCertificate(string $certificatePath): Certificate
38+
public static function loadCertificate(string $certificatePath): Certificate
3939
{
4040
if (!file_exists($certificatePath)) {
4141
throw new \RuntimeException(sprintf('Missing certificate file at %s', $certificatePath));

tests/KnoxTokenTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Proget\Tests;
66

77
use PHPUnit\Framework\TestCase;
8+
use Proget\Certificate;
89
use Proget\KnoxToken;
910

1011
class KnoxTokenTest extends TestCase
@@ -25,6 +26,15 @@ public function testSignAccessToken(): void
2526
)));
2627
}
2728

29+
public function testLoadCertificate(): void
30+
{
31+
$certificate = KnoxToken::loadCertificate(__DIR__.'/keys.json');
32+
33+
self::assertInstanceOf(Certificate::class, $certificate);
34+
self::assertEquals(204, strlen($certificate->publicKey()));
35+
self::assertEquals(886, strlen($certificate->privateKeyPem()));
36+
}
37+
2838
public function testLoadCertificateInvalidPath(): void
2939
{
3040
$this->expectException(\RuntimeException::class);

0 commit comments

Comments
 (0)