Skip to content

Commit a5723b4

Browse files
marmichalskiakondas
authored andcommitted
Move certificate loading logic to Certificate class & update namespace (#3)
1 parent 541c22a commit a5723b4

File tree

7 files changed

+66
-56
lines changed

7 files changed

+66
-56
lines changed

.php_cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ return PhpCsFixer\Config::create()
1111
'concat_space' => ['spacing' => 'none'],
1212
'declare_strict_types' => true,
1313
'method_separation' => true,
14+
'native_function_invocation' => ['include' => ['@all'], 'strict' => false],
1415
'no_blank_lines_after_class_opening' => true,
1516
'no_spaces_around_offset' => ['positions' => ['inside', 'outside']],
1617
'no_unneeded_control_parentheses' => true,
@@ -30,4 +31,4 @@ return PhpCsFixer\Config::create()
3031
)
3132
->setRiskyAllowed(true)
3233
->setUsingCache(false)
33-
;
34+
;

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,30 @@ More info at [Knox Cloud API Integration Guide](https://docs.samsungknox.com/clo
1515
### Sign your Client Identifier
1616

1717
```php
18-
use Proget\KnoxToken;
18+
use Proget\Samsung\KnoxToken\KnoxToken;
1919

2020
$clientIdentifierJwt = KnoxToken::signClientIdentifier('your-client-identifier', 'keys.json');
2121
```
2222

2323
### Sign your Access Token
2424

2525
```php
26-
use Proget\KnoxToken;
26+
use Proget\Samsung\KnoxToken\KnoxToken;
2727

2828
$accessTokenJwt = KnoxToken::signAccessToken('access-token', 'keys.json');
2929
```
3030

3131
### Load certificate
3232

3333
```php
34-
use Proget\KnoxToken;
34+
use Proget\Samsung\KnoxToken\Certificate;
3535

36-
$certificate = KnoxToken::loadCertificate('keys.json');
36+
$certificate = Certificate::fromPath('keys.json');
3737

3838
$certificate->publicKey();
3939
$certificate->privateKeyPem();
4040
````
4141

4242
## License
4343

44-
MIT
44+
MIT

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
],
2121
"autoload": {
2222
"psr-4": {
23-
"Proget\\": "src/"
23+
"Proget\\Samsung\\KnoxToken\\": "src/"
2424
}
2525
},
2626
"autoload-dev": {
2727
"psr-4": {
28-
"Proget\\Tests\\": "tests/"
28+
"Proget\\Tests\\Samsung\\KnoxToken\\": "tests/"
2929
}
3030
},
3131
"scripts": {

src/Certificate.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Proget;
5+
namespace Proget\Samsung\KnoxToken;
66

77
class Certificate
88
{
@@ -22,6 +22,20 @@ public function __construct(string $publicKey, string $privateKey)
2222
$this->privateKey = $privateKey;
2323
}
2424

25+
public static function fromPath(string $certificatePath): Certificate
26+
{
27+
if (!\file_exists($certificatePath)) {
28+
throw new \RuntimeException(\sprintf('Missing certificate file at %s', $certificatePath));
29+
}
30+
31+
$certificate = \json_decode(\file_get_contents($certificatePath), true);
32+
if (!isset($certificate['Public'], $certificate['Private'])) {
33+
throw new \RuntimeException(\sprintf('Invalid certificate file at %s', $certificatePath));
34+
}
35+
36+
return new Certificate($certificate['Public'], $certificate['Private']);
37+
}
38+
2539
public function publicKey(): string
2640
{
2741
return $this->publicKey;

src/KnoxToken.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Proget;
5+
namespace Proget\Samsung\KnoxToken;
66

77
use Firebase\JWT\JWT;
88
use Ramsey\Uuid\Uuid;
@@ -13,7 +13,7 @@ class KnoxToken
1313

1414
public static function signClientIdentifier(string $clientIdentifier, string $certificatePath): string
1515
{
16-
$certificate = self::loadCertificate($certificatePath);
16+
$certificate = Certificate::fromPath($certificatePath);
1717

1818
return JWT::encode([
1919
'clientIdentifier' => $clientIdentifier,
@@ -25,7 +25,7 @@ public static function signClientIdentifier(string $clientIdentifier, string $ce
2525

2626
public static function signAccessToken(string $accessToken, string $certificatePath): string
2727
{
28-
$certificate = self::loadCertificate($certificatePath);
28+
$certificate = Certificate::fromPath($certificatePath);
2929

3030
return JWT::encode([
3131
'accessToken' => $accessToken,
@@ -34,18 +34,4 @@ public static function signAccessToken(string $accessToken, string $certificateP
3434
'jti' => Uuid::uuid1()->toString().Uuid::uuid1()->toString()
3535
], $certificate->privateKeyPem(), 'RS512');
3636
}
37-
38-
public static function loadCertificate(string $certificatePath): Certificate
39-
{
40-
if (!file_exists($certificatePath)) {
41-
throw new \RuntimeException(sprintf('Missing certificate file at %s', $certificatePath));
42-
}
43-
$certificate = json_decode(file_get_contents($certificatePath), true);
44-
45-
if (!isset($certificate['Public'], $certificate['Private'])) {
46-
throw new \RuntimeException(sprintf('Invalid certificate file at %s', $certificatePath));
47-
}
48-
49-
return new Certificate($certificate['Public'], $certificate['Private']);
50-
}
5137
}

tests/CertificateTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Proget\Tests\Samsung\KnoxToken;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Proget\Samsung\KnoxToken\Certificate;
9+
10+
class CertificateTest extends TestCase
11+
{
12+
public function testLoad(): void
13+
{
14+
$certificate = Certificate::fromPath(__DIR__.'/keys.json');
15+
16+
self::assertEquals(204, \strlen($certificate->publicKey()));
17+
self::assertEquals(886, \strlen($certificate->privateKeyPem()));
18+
}
19+
20+
public function testLoadFromInvalidPath(): void
21+
{
22+
$this->expectException(\RuntimeException::class);
23+
$this->expectExceptionMessage('Missing certificate file at /some/invalid/path');
24+
25+
Certificate::fromPath('/some/invalid/path');
26+
}
27+
28+
public function testLoadFromInvalidFile(): void
29+
{
30+
$this->expectException(\RuntimeException::class);
31+
$this->expectExceptionMessage('Invalid certificate file at '.__FILE__);
32+
33+
Certificate::fromPath(__FILE__);
34+
}
35+
}

tests/KnoxTokenTest.php

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,26 @@
22

33
declare(strict_types=1);
44

5-
namespace Proget\Tests;
5+
namespace Proget\Tests\Samsung\KnoxToken;
66

77
use PHPUnit\Framework\TestCase;
8-
use Proget\Certificate;
9-
use Proget\KnoxToken;
8+
use Proget\Samsung\KnoxToken\KnoxToken;
109

1110
class KnoxTokenTest extends TestCase
1211
{
1312
public function testSignClientIdentifier(): void
1413
{
15-
self::assertEquals(713, strlen(KnoxToken::signClientIdentifier(
14+
self::assertEquals(713, \strlen(KnoxToken::signClientIdentifier(
1615
'a33a7593-dbaf-457f-87be-19243a421aec',
1716
__DIR__.'/keys.json'
1817
)));
1918
}
2019

2120
public function testSignAccessToken(): void
2221
{
23-
self::assertEquals(707, strlen(KnoxToken::signAccessToken(
22+
self::assertEquals(707, \strlen(KnoxToken::signAccessToken(
2423
'd13d112e-8e77-4243-b795-ed4e1cf15cf9',
2524
__DIR__.'/keys.json'
2625
)));
2726
}
28-
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-
38-
public function testLoadCertificateInvalidPath(): void
39-
{
40-
$this->expectException(\RuntimeException::class);
41-
$this->expectExceptionMessage('Missing certificate file at /some/invalid/path');
42-
43-
KnoxToken::signAccessToken('access-token', '/some/invalid/path');
44-
}
45-
46-
public function testLoadCertificateInvalidFile(): void
47-
{
48-
$this->expectException(\RuntimeException::class);
49-
$this->expectExceptionMessage('Invalid certificate file at '.__FILE__);
50-
51-
KnoxToken::signAccessToken('access-token', __FILE__);
52-
}
5327
}

0 commit comments

Comments
 (0)