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
52
52
* mount and get the payload part
53
53
* @param string $audience
54
54
* @param string $subject
55
+ * @param array $customPayload
55
56
* @return string
56
57
*/
57
58
private function getPayload (
58
59
string $ audience ,
59
- string $ subject
60
+ string $ subject ,
61
+ array $ customPayload
60
62
): string {
61
63
$ payload = [
62
64
'aud ' => $ audience ,
@@ -65,7 +67,10 @@ private function getPayload(
65
67
'iss ' => $ this ->context ,
66
68
'sub ' => $ subject ,
67
69
];
70
+
71
+ $ payload = array_merge ($ customPayload , $ payload );
68
72
$ payload = json_encode ($ payload );
73
+
69
74
return $ this ->base64UrlEncode ($ payload );
70
75
}
71
76
@@ -117,14 +122,16 @@ public function getexpire(): int
117
122
* generate token
118
123
* @param string $audience
119
124
* @param string $subject
125
+ * @param array $payload
120
126
* @return string
121
127
*/
122
128
public function generate (
123
129
string $ audience ,
124
- string $ subject = ''
130
+ string $ subject = '' ,
131
+ array $ customPayload = []
125
132
): string {
126
133
$ header = $ this ->getHeader ();
127
- $ payload = $ this ->getPayload ($ audience , $ subject );
134
+ $ payload = $ this ->getPayload ($ audience , $ subject, $ customPayload );
128
135
$ signature = $ this ->getSignature ($ header , $ payload );
129
136
130
137
return $ header . '. ' . $ payload . '. ' . $ signature ;
Original file line number Diff line number Diff line change @@ -272,6 +272,38 @@ public function testTokenNotNeedToRefresh()
272
272
$ this ->assertFalse ($ need );
273
273
}
274
274
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
+
275
307
protected function tearDown (): void
276
308
{
277
309
//
You can’t perform that action at this time.
0 commit comments