Skip to content

Commit eb4c1b1

Browse files
committed
Add support for user session tokens
1 parent 5acb66c commit eb4c1b1

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

lib/GetStream/Stream/Client.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ public function setLocation($location)
103103
$this->location = $location;
104104
}
105105

106+
/**
107+
* @param string $user_id
108+
* @param array $extra_data
109+
* @return string
110+
*/
111+
public function createUserSessionToken($user_id, array $extra_data=null)
112+
{
113+
if(is_null($extra_data)){
114+
$extra_data = array();
115+
}
116+
return $this->signer->jwtUserSessionToken($user_id, $extra_data);
117+
}
118+
106119
/**
107120
* @param BaseFeed $feed
108121
* @param string $resource

lib/GetStream/Stream/Signer.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,21 @@ public function jwtScopeToken($feedId, $resource, $action)
7979

8080
return JWT::encode($payload, $this->api_secret, 'HS256');
8181
}
82+
83+
/**
84+
* @param string $user_id
85+
* @param string $extra_data
86+
* @return string
87+
*/
88+
public function jwtUserSessionToken(string $user_id, array $extra_data)
89+
{
90+
$payload = [
91+
'user_id' => $user_id,
92+
];
93+
foreach($extra_data as $name => $value){
94+
$payload[$name] = $value;
95+
}
96+
return JWT::encode($payload, $this->api_secret, 'HS256');
97+
}
98+
8299
}

tests/integration/FeedTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DateTime;
66
use DateTimeZone;
7+
use Firebase\JWT\JWT;
78
use GetStream\Stream\Client;
89
use GetStream\Stream\Feed;
910
use PHPUnit\Framework\TestCase;
@@ -188,6 +189,18 @@ public function testReadonlyToken()
188189
$this->assertStringStartsWith('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY3Rpb24iOiJyZWFkI', $token);
189190
}
190191

192+
public function testUserSessionToken()
193+
{
194+
$token = $this->client->createUserSessionToken('user');
195+
$this->assertStringStartsWith('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoidXNlci', $token);
196+
$payload = JWT::decode($token, getenv('STREAM_API_SECRET'), array('HS256'));
197+
$this->assertSame($payload->user_id, 'user');
198+
$token = $this->client->createUserSessionToken('user', array('client'=>'PHP'));
199+
$this->assertStringStartsWith('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoidXNlci', $token);
200+
$payload = JWT::decode($token, getenv('STREAM_API_SECRET'), array('HS256'));
201+
$this->assertSame($payload->client, 'PHP');
202+
}
203+
191204
public function testAddActivity()
192205
{
193206
$activity_data = ['actor' => 1, 'verb' => 'tweet', 'object' => 1];

0 commit comments

Comments
 (0)