Skip to content

Commit

Permalink
Change knox token certificate argument from path to object (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
marmichalski authored and akondas committed Jul 29, 2019
1 parent a5723b4 commit 6c3165b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ More info at [Knox Cloud API Integration Guide](https://docs.samsungknox.com/clo
### Sign your Client Identifier

```php
use Proget\Samsung\KnoxToken\Certificate;
use Proget\Samsung\KnoxToken\KnoxToken;

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

### Sign your Access Token

```php
use Proget\Samsung\KnoxToken\Certificate;
use Proget\Samsung\KnoxToken\KnoxToken;

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

### Load certificate
Expand Down
8 changes: 2 additions & 6 deletions src/KnoxToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ class KnoxToken
{
private const AUDIENCE = 'KnoxWSM';

public static function signClientIdentifier(string $clientIdentifier, string $certificatePath): string
public static function signClientIdentifier(string $clientIdentifier, Certificate $certificate): string
{
$certificate = Certificate::fromPath($certificatePath);

return JWT::encode([
'clientIdentifier' => $clientIdentifier,
'publicKey' => $certificate->publicKey(),
Expand All @@ -23,10 +21,8 @@ public static function signClientIdentifier(string $clientIdentifier, string $ce
], $certificate->privateKeyPem(), 'RS512');
}

public static function signAccessToken(string $accessToken, string $certificatePath): string
public static function signAccessToken(string $accessToken, Certificate $certificate): string
{
$certificate = Certificate::fromPath($certificatePath);

return JWT::encode([
'accessToken' => $accessToken,
'publicKey' => $certificate->publicKey(),
Expand Down
5 changes: 3 additions & 2 deletions tests/KnoxTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Proget\Tests\Samsung\KnoxToken;

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

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

public function testSignAccessToken(): void
{
self::assertEquals(707, \strlen(KnoxToken::signAccessToken(
'd13d112e-8e77-4243-b795-ed4e1cf15cf9',
__DIR__.'/keys.json'
Certificate::fromPath(__DIR__.'/keys.json')
)));
}
}

0 comments on commit 6c3165b

Please sign in to comment.