Skip to content

Commit 6c3165b

Browse files
marmichalskiakondas
authored andcommitted
Change knox token certificate argument from path to object (#4)
1 parent a5723b4 commit 6c3165b

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ More info at [Knox Cloud API Integration Guide](https://docs.samsungknox.com/clo
1515
### Sign your Client Identifier
1616

1717
```php
18+
use Proget\Samsung\KnoxToken\Certificate;
1819
use Proget\Samsung\KnoxToken\KnoxToken;
1920

20-
$clientIdentifierJwt = KnoxToken::signClientIdentifier('your-client-identifier', 'keys.json');
21+
$clientIdentifierJwt = KnoxToken::signClientIdentifier('your-client-identifier', Certificate::fromPath('keys.json'));
2122
```
2223

2324
### Sign your Access Token
2425

2526
```php
27+
use Proget\Samsung\KnoxToken\Certificate;
2628
use Proget\Samsung\KnoxToken\KnoxToken;
2729

28-
$accessTokenJwt = KnoxToken::signAccessToken('access-token', 'keys.json');
30+
$accessTokenJwt = KnoxToken::signAccessToken('access-token', Certificate::fromPath('keys.json'));
2931
```
3032

3133
### Load certificate

src/KnoxToken.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ class KnoxToken
1111
{
1212
private const AUDIENCE = 'KnoxWSM';
1313

14-
public static function signClientIdentifier(string $clientIdentifier, string $certificatePath): string
14+
public static function signClientIdentifier(string $clientIdentifier, Certificate $certificate): string
1515
{
16-
$certificate = Certificate::fromPath($certificatePath);
17-
1816
return JWT::encode([
1917
'clientIdentifier' => $clientIdentifier,
2018
'publicKey' => $certificate->publicKey(),
@@ -23,10 +21,8 @@ public static function signClientIdentifier(string $clientIdentifier, string $ce
2321
], $certificate->privateKeyPem(), 'RS512');
2422
}
2523

26-
public static function signAccessToken(string $accessToken, string $certificatePath): string
24+
public static function signAccessToken(string $accessToken, Certificate $certificate): string
2725
{
28-
$certificate = Certificate::fromPath($certificatePath);
29-
3026
return JWT::encode([
3127
'accessToken' => $accessToken,
3228
'publicKey' => $certificate->publicKey(),

tests/KnoxTokenTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Proget\Tests\Samsung\KnoxToken;
66

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

1011
class KnoxTokenTest extends TestCase
@@ -13,15 +14,15 @@ public function testSignClientIdentifier(): void
1314
{
1415
self::assertEquals(713, \strlen(KnoxToken::signClientIdentifier(
1516
'a33a7593-dbaf-457f-87be-19243a421aec',
16-
__DIR__.'/keys.json'
17+
Certificate::fromPath(__DIR__.'/keys.json')
1718
)));
1819
}
1920

2021
public function testSignAccessToken(): void
2122
{
2223
self::assertEquals(707, \strlen(KnoxToken::signAccessToken(
2324
'd13d112e-8e77-4243-b795-ed4e1cf15cf9',
24-
__DIR__.'/keys.json'
25+
Certificate::fromPath(__DIR__.'/keys.json')
2526
)));
2627
}
2728
}

0 commit comments

Comments
 (0)