File tree Expand file tree Collapse file tree 2 files changed +42
-3
lines changed Expand file tree Collapse file tree 2 files changed +42
-3
lines changed Original file line number Diff line number Diff line change @@ -52,11 +52,13 @@ private function getHeader(): string
5252 * mount and get the payload part
5353 * @param string $audience
5454 * @param string $subject
55+ * @param array $customPayload
5556 * @return string
5657 */
5758 private function getPayload (
5859 string $ audience ,
59- string $ subject
60+ string $ subject ,
61+ array $ customPayload
6062 ): string {
6163 $ payload = [
6264 'aud ' => $ audience ,
@@ -65,7 +67,10 @@ private function getPayload(
6567 'iss ' => $ this ->context ,
6668 'sub ' => $ subject ,
6769 ];
70+
71+ $ payload = array_merge ($ customPayload , $ payload );
6872 $ payload = json_encode ($ payload );
73+
6974 return $ this ->base64UrlEncode ($ payload );
7075 }
7176
@@ -117,14 +122,16 @@ public function getexpire(): int
117122 * generate token
118123 * @param string $audience
119124 * @param string $subject
125+ * @param array $payload
120126 * @return string
121127 */
122128 public function generate (
123129 string $ audience ,
124- string $ subject = ''
130+ string $ subject = '' ,
131+ array $ customPayload = []
125132 ): string {
126133 $ header = $ this ->getHeader ();
127- $ payload = $ this ->getPayload ($ audience , $ subject );
134+ $ payload = $ this ->getPayload ($ audience , $ subject, $ customPayload );
128135 $ signature = $ this ->getSignature ($ header , $ payload );
129136
130137 return $ header . '. ' . $ payload . '. ' . $ signature ;
Original file line number Diff line number Diff line change @@ -272,6 +272,38 @@ public function testTokenNotNeedToRefresh()
272272 $ this ->assertFalse ($ need );
273273 }
274274
275+ /**
276+ * @covers JwtManager\JwtManager::isValid
277+ * @covers JwtManager\JwtManager::splitParts
278+ * @covers JwtManager\JwtManager::getSignature
279+ * @covers JwtManager\JwtManager::base64UrlEncode
280+ */
281+ public function testCustomPayload ()
282+ {
283+ $ JwtManager = new JwtManager (
284+ $ this ->appSecret ,
285+ $ this ->context
286+ );
287+
288+ $ token = $ JwtManager ->generate (
289+ 'token ' ,
290+ '68162dc1-a392-491f-9d46-639f0e0f179d0 ' ,
291+ [
292+ 'test ' => 'test ' ,
293+ ]
294+ );
295+
296+ $ JwtManager = new JwtManager (
297+ $ this ->appSecret ,
298+ $ this ->context
299+ );
300+
301+ $ payload = $ JwtManager ->decodePayload ($ token );
302+
303+ $ this ->assertIsArray ($ payload );
304+ $ this ->assertEquals ($ payload ['test ' ], 'test ' );
305+ }
306+
275307 protected function tearDown (): void
276308 {
277309 //
You can’t perform that action at this time.
0 commit comments