Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/JwtManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ private function getHeader(): string
* mount and get the payload part
* @param string $audience
* @param string $subject
* @param array $customPayload
* @return string
*/
private function getPayload(
string $audience,
string $subject
string $subject,
array $customPayload
): string {
$payload = [
'aud' => $audience,
Expand All @@ -65,7 +67,10 @@ private function getPayload(
'iss' => $this->context,
'sub' => $subject,
];

$payload = array_merge($customPayload, $payload);
$payload = json_encode($payload);

return $this->base64UrlEncode($payload);
}

Expand Down Expand Up @@ -117,14 +122,16 @@ public function getexpire(): int
* generate token
* @param string $audience
* @param string $subject
* @param array $payload
* @return string
*/
public function generate(
string $audience,
string $subject = ''
string $subject = '',
array $customPayload = []
): string {
$header = $this->getHeader();
$payload = $this->getPayload($audience, $subject);
$payload = $this->getPayload($audience, $subject, $customPayload);
$signature = $this->getSignature($header, $payload);

return $header . '.' . $payload . '.' . $signature;
Expand Down
32 changes: 32 additions & 0 deletions tests/JwtManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,38 @@ public function testTokenNotNeedToRefresh()
$this->assertFalse($need);
}

/**
* @covers JwtManager\JwtManager::isValid
* @covers JwtManager\JwtManager::splitParts
* @covers JwtManager\JwtManager::getSignature
* @covers JwtManager\JwtManager::base64UrlEncode
*/
public function testCustomPayload()
{
$JwtManager = new JwtManager(
$this->appSecret,
$this->context
);

$token = $JwtManager->generate(
'token',
'68162dc1-a392-491f-9d46-639f0e0f179d0',
[
'test' => 'test',
]
);

$JwtManager = new JwtManager(
$this->appSecret,
$this->context
);

$payload = $JwtManager->decodePayload($token);

$this->assertIsArray($payload);
$this->assertEquals($payload['test'], 'test');
}

protected function tearDown(): void
{
//
Expand Down