Skip to content

Commit 5d1dcb1

Browse files
committed
Simplify token creation
This array manipulation is just unnecessary now.
1 parent 0c41904 commit 5d1dcb1

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

Diff for: src/Builder.php

+15-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
use function array_key_exists;
1919
use function current;
20-
use function implode;
2120
use function in_array;
2221
use function is_array;
2322
use function trigger_error;
@@ -494,19 +493,21 @@ public function getToken(Signer $signer = null, Key $key = null)
494493
$signer->modifyHeader($this->headers);
495494
}
496495

497-
$payload = [
498-
$this->encoder->base64UrlEncode($this->encoder->jsonEncode($this->convertItems($this->headers))),
499-
$this->encoder->base64UrlEncode($this->encoder->jsonEncode($this->convertItems($this->claims)))
500-
];
496+
$headers = new DataSet(
497+
$this->headers,
498+
$this->encoder->base64UrlEncode($this->encoder->jsonEncode($this->convertItems($this->headers)))
499+
);
501500

502-
$signature = $this->createSignature($payload, $signer, $key);
503-
$payload[] = $signature->toString();
501+
$claims = new DataSet(
502+
$this->claims,
503+
$this->encoder->base64UrlEncode($this->encoder->jsonEncode($this->convertItems($this->claims)))
504+
);
504505

505506
return new Token(
506-
new DataSet($this->headers, $payload[0]),
507-
new DataSet($this->claims, $payload[1]),
508-
$signature,
509-
$payload,
507+
$headers,
508+
$claims,
509+
$this->createSignature($headers->toString() . '.' . $claims->toString(), $signer, $key),
510+
['', ''],
510511
$this->claimFactory
511512
);
512513
}
@@ -534,17 +535,17 @@ private function convertItems(array $items)
534535
}
535536

536537
/**
537-
* @param string[] $payload
538+
* @param string $payload
538539
*
539540
* @return Signature
540541
*/
541-
private function createSignature(array $payload, Signer $signer = null, Key $key = null)
542+
private function createSignature($payload, Signer $signer = null, Key $key = null)
542543
{
543544
if ($signer === null || $key === null) {
544545
return Signature::fromEmptyData();
545546
}
546547

547-
$hash = $signer->sign(implode('.', $payload), $key)->hash();
548+
$hash = $signer->sign($payload, $key)->hash();
548549

549550
return new Signature($hash, $this->encoder->base64UrlEncode($hash));
550551
}

Diff for: src/Parser.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,11 @@ public function parse($jwt)
6666
}
6767
}
6868

69-
if ($signature === null) {
70-
unset($data[2]);
71-
}
72-
7369
return new Token(
7470
new DataSet($header, $data[0]),
7571
new DataSet($claims, $data[1]),
7672
$signature,
77-
$data
73+
['', '']
7874
);
7975
}
8076

0 commit comments

Comments
 (0)