# pip install stream-chat
import stream_chat
server_client = stream_chat.StreamChat(
api_key="{{ api_key }}", api_secret="{{ api_secret }}"
)
token = server_client.create_token("john")Tokens & Authentication
Authentication
Stream uses JWT (JSON Web Tokens) to authenticate users so they can open WebSocket connections and send API requests. When a user opens your app, they first pass through your own authentication system. After that, the Stream SDK is initialized and a client instance is created. The device then requests a Stream token from your server. Your server verifies the request and returns a valid token. Once the device receives this token, the user is authenticated and ready to start using chat.
Generating Tokens
You can generate tokens on the server by creating a Server Client and then using the Create Token method.
If generating a token to use client-side, the token must include the userID claim in the token payload, whereas server tokens do not. When using the create token method, pass the user_id parameter to generate a client-side token.
# gem install stream-chat-ruby
require 'stream-chat'
server_client = StreamChat::Client.new(api_key='STREAM_KEY', api_secret='STREAM_SECRET')
server_client.create_token('john')// composer require get-stream/stream-chat
$server_client = new GetStream\StreamChat\Client("STREAM_API_KEY", "STREAM_API_SECRET");
$token = $server_client->createToken("john");// github.com/GetStream/stream-chat-go/v3
serverClient, _ := stream.NewClient(APIKey, APISecret)
token := serverClient.CreateToken("john", expiredAt, issuedAt)// nuget install stream-chat-net
using StreamChat.Clients;
// Instantiate your Stream client factory using the API key and secret
// the secret is only used server side and gives you full access to the API.
var factory = new StreamClientFactory("{{ api_key }}", "{{ api_secret }}");
var userClient = factory.GetUserClient();
var token = userClient.CreateToken("john");// Define values.
const api_key = "{{ api_key }}";
const api_secret = "{{ api_secret }}";
const user_id = "john";
// Initialize a Server Client
const serverClient = StreamChat.getInstance(api_key, api_secret);
// Create User Token
const token = serverClient.createToken(user_id);// You need to have io.getstream.chat.apiKey and io.getstream.chat.secretKey
// properties available, or STREAM_KEY and STREAM_SECRET environmental variables.
// For more info: https://github.com/GetStream/stream-chat-java
var token = User.createToken("john", null, null);// User Tokens must be generated server-side, check server-side SDKs for examples
// For Testing/Development - read below how you can enable "Developer Tokens" and skip token generation for development purposesManually Generating Tokens
You can use the JWT generator on this page to generate a User Token JWT without needing to set up a server client. You can use this token for prototyping and debugging; usually by hardcoding this into your application or passing it as an environment value at initialization.
You will need the following values to generate a token:
User ID: A unique string to identify a user.API Secret: You can find this value in the Dashboard.
To generate a token, provide a User ID and your API Secret to the following generator:
For more information on how JWT works, please visit https://jwt.io.
Setting Automatic Token Expiration
By default, user tokens are valid indefinitely. You can set an expiration to tokens by passing it as the second parameter. The expiration should contain the number of seconds since Unix epoch (00:00:00 UTC on 1 January 1970).
// creates a token that expires in 1 hour using moment.js
const timestamp = Number(moment().add("1h").format("X"));
const token1 = client.createToken("john", timestamp);
// the same can be done with plain javascript
const token2 = client.createToken(
"john",
Math.floor(Date.now() / 1000) + 60 * 60,
);# creates a token valid for 1 hour
token = chat_client.create_token(
'john',
exp=datetime.datetime.utcnow() + datetime.timedelta(hours=1)
)# creates a token valid for 1 hour
client.create_token('john', exp: Time.now.to_i + 3600)// creates a token valid for 1 hour
$expiration = (new DateTime())->getTimestamp() + 3600;
$token = $client->createToken("john", $expiration);var calendar = new GregorianCalendar();
calendar.add(Calendar.MINUTE, 60);
var token = User.createToken("john", calendar.getTime(), null);// creates a token valid for 1 hour
token := client.CreateToken("john", time.Now().UTC().Add(time.Hour))// at the moment we don't have a Swift client for server side usage
// You can use our user token generator here during development: https://getstream.io/chat/docs/token_generator/?language=swift// creates a token valid for 1 hour
var token = userClient.CreateToken("john", DateTimeOffset.UtcNow.AddHours(1));// user tokens must be generated server-side, check other languages for examples// user tokens must be generated server-side, check other languages for examples// User Tokens must be generated server-side, check server-side SDKs for examples
// For Testing/Development - read below how you can enable "Developer Tokens" and skip token generation for development purposesToken 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.
To learn more about Token Providers, read on in our Initialization & Users section.
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 and ensure it is in Development mode
Click App name to enter the Chat Overview
Scroll to the General section
Toggle Disable Authentication Checks
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.
val user = User(
id = "bender",
name = "Bender",
image = "https://bit.ly/321RmWb",
)
val token = client.devToken(user.id)
client.connectUser(user, token).enqueue { /* ... */ }await client.connectUser(
{
id: "john",
name: "John Doe",
image: "https://getstream.io/random_svg/?name=John",
},
client.devToken("john"),
);User user = new User();
user.setId("bender");
user.setName("Bender");
user.setImage("https://bit.ly/321RmWb");
String token = client.devToken(user.getId());
client.connectUser(user, token).enqueue(result -> { /* ... */ });import StreamChat
client.connectUser(userInfo: .init(id: "john-doe"), token: .development(userId: "john-doe"))final user = User(id: "john", extraData: {
"name": "John Doe",
"image": "https://getstream.io/random_svg/?name=John",
});
await client.setUser(
user,
client.devToken("john"),
);const FUser User{TEXT("john")};
const FString Token = Client->DevToken(User.Id);
Client->ConnectUser(
User,
Token,
[](const FOwnUser& UserRef)
{
// Connection established
});var userName = "The Amazing Tom";
var userId = StreamChatClient.SanitizeUserId(userName); // Remove disallowed characters
var userToken = StreamChatClient.CreateDeveloperAuthToken(userId);
var credentials = new AuthCredentials("API_KEY", userId, userToken);
// Create chat client
var client = StreamChatClient.CreateDefaultClient();
// Connect user
var localUserData = await client.ConnectUserAsync(credentials);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 a certain user or a list of users.
await client.revokeUserToken("user-id", revokeDate);
await client.revokeUsersToken(["user1-id", "user2-id"], revokeDate);client.revoke_user_token("user-id", revokeDate)
client.revoke_users_token(["user1-id", "user2-id"], revokeDate)client.RevokeUserToken("user-id", &revokeTime)
client.RevokeUsersToken([]string{"user1-id", "user2-id"}, &revokeTime)$client->revokeUserToken("user-id", $revokeTime);
$client->revokeUsersToken(["user1-id", "user2-id"], $revokeTime);client.revoke_user_token("user-id", before)
client.revoke_users_token(["user1-id", "user2-id"], before)
# before should be a datetime objectvar issuedBefore = DateTimeOffset.UtcNow.AddHours(-1);
await userClient.RevokeUserTokenAsync("<user-id>", issuedBefore);
await userClient.RevokeManyUserTokensAsync(new[] { user1.Id, user2.Id }, issuedBefore);var calendar = new GregorianCalendar();
User.revokeToken("<user-id>", calendar.getTime()).request();
User.revokeTokens(List.of("<user1-id>", "<user2-id>"), calendar.getTime()).request();// This is a server-side featureNote: 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. 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.revokeUserToken("user-id", null);
await client.revokeUsersToken(["user1-id", "user2-id"], null);client.revoke_user_token("user-id", None)
client.revoke_users_token(["user1-id", "user2-id"], None)client.RevokeUserToken("user-id", nil)
client.RevokeUsersToken([]string{"user1-id", "user2-id"}, nil)$client->revokeUserToken("user-id", null);
$client->revokeUsersToken(["user1-id", "user2-id"], null);client.revoke_user_token("user-id", nil)
client.revoke_users_token(["user1-id", "user2-id"], nil)await userClient.RevokeUserTokenAsync("<user-id>", null);
await userClient.RevokeManyUserTokensAsync(new[] { user1.Id, user2.Id }, null);User.revokeToken("<user-id>", null).request();
User.revokeTokens(List.of("<user1-id>", "<user2-id>"), null).request();// This is a server-side featureToken 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.revokeTokens(revokeDate);
// you can pass Date or ISOstring as value hereclient.revoke_tokens(revokeTime)
# revokeTime is a datetime objectclient.RevokeTokens(&revokeTime)
// revokeTime is a time.Time object$client->revokeTokens($revokeTime)
// revokeTime is a DateTime objectclient.revoke_tokens(before)
#before is a datetime objectawait appClient.RevokeTokensAsync(DateTimeOffset.UtcNow.AddHours(-1));var calendar = new GregorianCalendar();
App.update().revokeTokensIssuedBefore(calendar.getTime()).request();// This is a server-side featureUndoing the revoke
To undo app-level token revocation, you can simply set revocation date to null:
await client.revokeTokens(null);client.revoke_tokens(None)client.RevokeTokens(nil)$client->revokeTokens(null);client.revoke_tokens(nil)await appClient.RevokeTokensAsync(null);App.update().revokeTokensIssuedBefore(null).request();// This is a server-side featureAdding iat claim to token
By default, user tokens generated through the createToken function do not contain information about time of issue. You can change that by passing the issue date as the third parameter while creating tokens. This is a security best practice, as it enables revoking tokens.
client.createToken("user-id", expireTime, issuedAt);
// issuedAt should be unix timestamp
// issuedAt = Math.floor(Date.now() / 1000)client.create_token(self, user_id, exp=expiryTime, iat=issuedAt)
#issueTime should be a Unix timestampclient.CreateToken("user-id", expiryTime, issuedAt)
// issuedAt should be unix timestamp
// issuedAt := time.Now().Unix()$client->createToken("user-id", $expiry, $issuedAt);
// issuedAt should be unix timestampclient.create_token('john', exp: expTime, iat: issuedAt)
# issuedAt should be a unix timestampvar token = userClient.CreateToken(user1.Id,
expiration: DateTimeOffset.UtcNow.AddHours(2),
issuedAt: DateTimeOffset.UtcNow);var expiry = new GregorianCalendar();
expiry.add(Calendar.MINUTE, 60);
var issuedAt = new GregorianCalendar();
var token = User.createToken("john", expiry.getTime(), issuedAt.getTime());// This is a server-side feature