const userId = "john";
// validity is optional, in this case we set it to 1 day
const validity = 24 * 60 * 60;
client.generateUserToken({ user_id: userId, validity_in_seconds: validity });
Tokens & Authentication
Stream uses JWT (JSON Web Tokens) to authenticate feeds users, enabling them to login. Knowing whether a user is authorized to perform certain actions is managed separately via a role based permissions system.
The exception is Anonymous and Guest users who require no authentication to login.
Token Providers
A concept we will refer to throughout the docs is a Token Provider. At a high level, the Token Provider is an endpoint on your server that can perform the following sequence of tasks:
Receive information about a user from the front end.
Validate that user information against your system.
Provide a User-ID corresponding to that user to the server client’s token creation method.
Return that token to the front end.
User Tokens can only be safely generated from a server. This means you will need to implement a Token Provider prior to deploying your application to production. To conduct development before implementing a Token Provider, you will need to disable token authentication.
Generating Tokens
You can generate tokens server-side. You need to pass the user id the token belongs to. Optionally you can specify when the token should expire, by default it’s set to 1 hour.
import io.getstream.services.framework.StreamSDKClient;
import io.getstream.services.framework.TokenBuilder;
// Initialize client
StreamSDKClient client = new StreamSDKClient("api_key", "api_secret");
// Generate user token
String userId = "john";
// validity is optional, in this case we set it to 1 day
Integer validity = 24 * 60 * 60;
TokenBuilder tokenBuilder = new TokenBuilder("api_secret");
String token = tokenBuilder.createToken(userId, validity);
using GetStream;
// Initialize client
var client = new StreamClient("api_key", "api_secret");
// Generate user token - C# SDK doesn't have a direct method
// You need to implement JWT generation manually or use the server token
// For now, refer to the Node.js example for the API structure
import (
"github.com/GetStream/getstream-go/v3"
"time"
)
// Initialize client
client, err := getstream.NewClient("api_key", "api_secret")
if err != nil {
log.Fatal(err)
}
// Generate user token
userId := "john"
// validity is optional, in this case we set it to 1 day
expiration := time.Hour * 24
token, err := client.CreateToken(userId, getstream.WithExpiration(expiration))
use GetStream\Client;
// Initialize client
$client = new Client('api_key', 'api_secret');
// Generate user token
$userId = 'john';
// validity is optional, in this case we set it to 1 day
$validity = 24 * 60 * 60;
$token = $client->getJWTGenerator()->generateUserToken($userId, [], $validity);
from getstream import Stream
# Initialize client
client = Stream('api_key', 'api_secret')
# Generate user token
user_id = 'john'
# validity is optional, in this case we set it to 1 day
validity = 24 * 60 * 60
token = client.create_token(user_id, expiration=validity)
Manual Token Expiration
Token Revocation is a way to manually expire tokens for a single user or for many users by setting a revoke_tokens_issued_before
time, and any tokens issued before this will be considered expired and will fail to authenticate. This can be reversed by setting the field to null.
Token Revocation by User
You can revoke all tokens that belong to certain user or list of users
await client.updateUsersPartial({
users: [
{
id: "user1",
set: {
revoke_tokens_issued_before: new Date(),
},
},
{
id: "user2",
set: {
revoke_tokens_issued_before: new Date(),
},
},
],
});
import io.getstream.client.Client;
import java.util.*;
Client client = Client.builder("api_key", "api_secret").build();
Map<String, Object> user1Updates = new HashMap<>();
user1Updates.put("revoke_tokens_issued_before", new Date());
Map<String, Object> user2Updates = new HashMap<>();
user2Updates.put("revoke_tokens_issued_before", new Date());
List<Map<String, Object>> users = Arrays.asList(
Map.of("id", "user1", "set", user1Updates),
Map.of("id", "user2", "set", user2Updates)
);
client.updateUsersPartial(Map.of("users", users));
using Stream;
using Stream.Models;
var client = new StreamClient("api_key", "api_secret");
await client.UpdateUsersPartialAsync(new UpdateUsersPartialRequest
{
Users = new[]
{
new UpdateUserPartialRequest
{
Id = "user1",
Set = new Dictionary<string, object>
{
["revoke_tokens_issued_before"] = DateTime.UtcNow
}
},
new UpdateUserPartialRequest
{
Id = "user2",
Set = new Dictionary<string, object>
{
["revoke_tokens_issued_before"] = DateTime.UtcNow
}
}
}
});
import (
"context"
"github.com/GetStream/getstream-go/v3"
"time"
)
client, err := getstream.NewClient("api_key", "api_secret")
if err != nil {
log.Fatal(err)
}
request := &getstream.UpdateUsersPartialRequest{
Users: []getstream.UpdateUserPartialRequest{
{
ID: "user1",
Set: map[string]any{
"revoke_tokens_issued_before": time.Now(),
},
},
{
ID: "user2",
Set: map[string]any{
"revoke_tokens_issued_before": time.Now(),
},
},
},
}
_, err = client.UpdateUsersPartial(context.Background(), request)
use GetStream\Client;
use GetStream\GeneratedModels\UpdateUsersPartialRequest;
$client = new Client('api_key', 'api_secret');
$request = new UpdateUsersPartialRequest([
'users' => [
[
'id' => 'user1',
'set' => [
'revoke_tokens_issued_before' => new DateTime()
]
],
[
'id' => 'user2',
'set' => [
'revoke_tokens_issued_before' => new DateTime()
]
]
]
]);
$client->updateUsersPartial($request);
from getstream import Stream
from getstream.models import UpdateUserPartialRequest
from datetime import datetime
client = Stream('api_key', 'api_secret')
users = [
UpdateUserPartialRequest(
id='user1',
set={'revoke_tokens_issued_before': datetime.utcnow()}
),
UpdateUserPartialRequest(
id='user2',
set={'revoke_tokens_issued_before': datetime.utcnow()}
)
]
client.update_users_partial(users)
Note: Your tokens must include the iat
(issued at time) claim, which will be compared to the time in the revoke_tokens_issued_before
field to determine whether the token is valid or expired. If you use the Node SDK to generate tokens, iat
will be set for you. Tokens which have no iat
will be considered invalid.
Undoing the revoke
To undo user-level token revocation, you can simply set revocation date to null
:
await client.updateUsersPartial({
users: [
{
id: "user1",
set: {
revoke_tokens_issued_before: null,
},
},
{
id: "user2",
set: {
revoke_tokens_issued_before: null,
},
},
],
});
import io.getstream.services.framework.StreamSDKClient;
import io.getstream.services.CommonImpl;
import io.getstream.models.UpdateAppRequest;
import java.util.Date;
StreamSDKClient client = new StreamSDKClient("api_key", "api_secret");
CommonImpl common = new CommonImpl(client.getHttpClient());
// Revoke tokens for all users
UpdateAppRequest request = UpdateAppRequest.builder()
.revokeTokensIssuedBefore(new Date())
.build();
common.updateApp(request).execute();
// Please refer to the Node.js examples above for the API structure
using GetStream;
var client = new StreamClient("api_key", "api_secret");
// Revoke tokens for all users
var request = new UpdateAppRequest
{
RevokeTokensIssuedBefore = DateTime.UtcNow
};
await client.UpdateAppAsync(request);
// Please refer to the Node.js examples above for the API structure
Token Revocation by Application
It is possible to revoke tokens for all users of an application. This should be used with caution as it will expire every user’s token, regardless of whether the token has an iat
claim
await client.updateApp({
revoke_tokens_issued_before: new Date(),
});
import io.getstream.services.framework.StreamSDKClient;
import io.getstream.services.CommonImpl;
import io.getstream.models.UpdateAppRequest;
import java.util.Date;
StreamSDKClient client = new StreamSDKClient("api_key", "api_secret");
CommonImpl common = new CommonImpl(client.getHttpClient());
// Revoke tokens for all users
UpdateAppRequest request = UpdateAppRequest.builder()
.revokeTokensIssuedBefore(new Date())
.build();
common.updateApp(request).execute();
// Please refer to the Node.js examples above for the API structure
using GetStream;
var client = new StreamClient("api_key", "api_secret");
// Revoke tokens for all users
var request = new UpdateAppRequest
{
RevokeTokensIssuedBefore = DateTime.UtcNow
};
await client.UpdateAppAsync(request);
// Please refer to the Node.js examples above for the API structure
Undoing the revoke
To undo app-level token revocation, you can simply set revocation date to null
:
await client.updateApp({
revoke_tokens_issued_before: null,
});
import io.getstream.services.framework.StreamSDKClient;
import io.getstream.services.CommonImpl;
import io.getstream.models.UpdateAppRequest;
import java.util.Date;
StreamSDKClient client = new StreamSDKClient("api_key", "api_secret");
CommonImpl common = new CommonImpl(client.getHttpClient());
// Revoke tokens for all users
UpdateAppRequest request = UpdateAppRequest.builder()
.revokeTokensIssuedBefore(new Date())
.build();
common.updateApp(request).execute();
// Please refer to the Node.js examples above for the API structure
using GetStream;
var client = new StreamClient("api_key", "api_secret");
// Revoke tokens for all users
var request = new UpdateAppRequest
{
RevokeTokensIssuedBefore = DateTime.UtcNow
};
await client.UpdateAppAsync(request);
// Please refer to the Node.js examples above for the API structure
Developer Tokens
For development applications, it is possible to disable token authentication and use client-side generated tokens or a manually generated static token. Disabling auth checks is not suitable for a production application and should only be done for proofs-of-concept and applications in the early development stage. To enable development tokens, you need to change your application configuration.
On the Dashboard:
Select the App you want to enable developer tokens on
Click Appname to enter the Chat Overview
Scroll to the Authenticationsection
Toggle Disable Auth Checks
Click Save
This disables the authentication check, but does not remove the requirement to send a token. Send either a client generated development token, or manually create one and hard code it into your application.
Manually Generating Tokens
You can generate a token manually using the JWT generator.
If you need to test your app before you have a Token Provider and you don’t want to enable developer tokens, you can hardcode tokens from this generator into your app for testing.
How to Refresh Expired Tokens
If you’re using tokens with an expiration date you’ll want to update tokens as soon as a token exception occurs. Our React, RN, iOS, Android and Flutter libraries have built-in support for this.
Here is the regular flow to handle tokens with expiration with a token provider:
Feeds is initialized using the API Key and the token provider
The Feeds client will use the token provider to fetch the token when
connectUser
is calledWhen the token expires, the API will return a specific Authentication error code
The client will pause API requests and use the token provider to obtain a fresh token
The token provider returns a new token (ie. from your backend)
Feeds client replaces the old token with the new one and use it for all waiting and future API calls
A token provider is a function or class that you implement and that is responsible for requesting a new token from your own login infrastructure.
The most common token provider implementation does an HTTP call to your backend with the ID of the user as well as a valid session id or secret needed to authenticate them.