Grant, change, and revoke permissions
Use a server-side PubNub instance initialized with a secretKey to grant time-limited tokens to your clients, update their permissions by issuing new tokens, and revoke tokens when access must be removed immediately.
Before you start
Confirm you have:
- Access Manager enabled on your keyset. See Configure access control.
- A server-side PubNub instance initialized with a
secretKey. See Initialize a server SDK with a secret key. - For token revocation: the Revoke v3 Token option enabled on the keyset. See Configure access control.
Grant a token
Call grantToken from your server. Specify the authorized User ID, the resource permissions, and the TTL.
The example below grants my-authorized-uuid:
readonchannel-a,channel-group-b, anduuid-c.readandwriteonchannel-b,channel-c,channel-d, anduuid-d.readon all channels matching the RE2 pattern^channel-[A-Za-z0-9]$.
- JavaScript
- Python
- Java
- Kotlin
- Go
- C-Core (legacy)
- C#
- Dart
- PHP
- Ruby
1pubnub.grantToken(
2 {
3 ttl: 15,
4 authorized_uuid: "my-authorized-uuid",
5 resources: {
6 channels: {
7 "channel-a": { read: true },
8 "channel-b": { read: true, write: true },
9 "channel-c": { read: true, write: true },
10 "channel-d": { read: true, write: true }
11 },
12 groups: {
13 "channel-group-b": { read: true }
14 },
15 uuids: {
show all 28 lines1from pubnub.models.consumer.v3.channel import Channel
2from pubnub.models.consumer.v3.group import Group
3from pubnub.models.consumer.v3.uuid import UUID
4
5channels = [
6 Channel.id("channel-a").read(),
7 Channel.pattern("channel-[A-Za-z0-9]").read(),
8 Channel.id("channel-b").read().write(),
9 Channel.id("channel-c").read().write(),
10 Channel.id("channel-d").read().write()
11]
12channel_groups = [
13 Group.id("channel-group-b").read()
14]
15uuids = [
show all 27 lines1pubnub.grantToken()
2 .ttl(15)
3 .authorizedUUID("my-authorized-uuid")
4 .channels(Arrays.asList(
5 ChannelGrant.name("channel-a").read(),
6 ChannelGrant.name("channel-b").read().write(),
7 ChannelGrant.name("channel-c").read().write(),
8 ChannelGrant.name("channel-d").read().write(),
9 ChannelGrant.pattern("^channel-[A-Za-z0-9]*$").read()))
10 .channelGroups(Collections.singletonList(
11 ChannelGroupGrant.id("channel-group-b").read()))
12 .uuids(Arrays.asList(
13 UUIDGrant.id("uuid-c").get(),
14 UUIDGrant.id("uuid-d").get().update()))
15 .async(result -> { /* check result */ });
1pubnub.grantToken(
2 ttl = 15,
3 authorizedUUID = "my-authorized-uuid",
4 channels = listOf(
5 ChannelGrant.name(name = "channel-a", read = true),
6 ChannelGrant.name(name = "channel-b", read = true, write = true),
7 ChannelGrant.name(name = "channel-c", read = true, write = true),
8 ChannelGrant.name(name = "channel-d", read = true, write = true),
9 ChannelGrant.pattern(pattern = "^channel-[A-Za-z0-9]*$", read = true)
10 ),
11 channelGroups = listOf(
12 ChannelGroupGrant.id(id = "channel-group-b", read = true)
13 ),
14 uuids = listOf(
15 UUIDGrant.id(id = "uuid-c", get = true),
show all 18 lines1res, status, err := pn.GrantToken().
2 TTL(15).
3 AuthorizedUUID("my-authorized-uuid").
4 Channels(map[string]pubnub.ChannelPermissions{
5 "channel-a": { Read: true },
6 "channel-b": { Read: true, Write: true },
7 "channel-c": { Read: true, Write: true },
8 "channel-d": { Read: true, Write: true },
9 }).
10 ChannelGroups(map[string]pubnub.GroupPermissions{
11 "channel-group-b": { Read: true },
12 }).
13 UUIDs(map[string]pubnub.UUIDPermissions{
14 "uuid-c": { Get: true },
15 "uuid-d": { Get: true, Update: true },
show all 20 linesNew SDK available
C-Core (legacy) is still supported. If you are starting a new project, use the new C SDK.
1char* author_uuid = "my-authorized-uuid";
2struct pam_permission cha_perm = {.read=true };
3struct pam_permission cgb_perm = {.read=true };
4struct pam_permission uidc_perm = {.get=true };
5struct pam_permission chb_perm = {.read=true, .write=true };
6struct pam_permission chc_perm = {.read=true, .write=true };
7struct pam_permission chd_perm = {.read=true, .write=true };
8struct pam_permission uidd_perm = {.get=true, .update=true };
9struct pam_permission pat_ch_perm = {.read=true };
10
11int perm_cha = pubnub_get_grant_bit_mask_value(cha_perm);
12int perm_chb = pubnub_get_grant_bit_mask_value(chb_perm);
13int perm_chc = pubnub_get_grant_bit_mask_value(chc_perm);
14int perm_chd = pubnub_get_grant_bit_mask_value(chd_perm);
15int perm_cgb = pubnub_get_grant_bit_mask_value(cgb_perm);
show all 30 lines1PNResult<PNAccessManagerTokenResult> grantTokenResponse = await pubnub.GrantToken()
2 .TTL(15)
3 .AuthorizedUuid("my-authorized-uuid")
4 .Resources(new PNTokenResources()
5 {
6 Channels = new Dictionary<string, PNTokenAuthValues>() {
7 { "channel-a", new PNTokenAuthValues() { Read = true } },
8 { "channel-b", new PNTokenAuthValues() { Read = true, Write = true } },
9 { "channel-c", new PNTokenAuthValues() { Read = true, Write = true } },
10 { "channel-d", new PNTokenAuthValues() { Read = true, Write = true } }},
11 ChannelGroups = new Dictionary<string, PNTokenAuthValues>() {
12 { "channel-group-b", new PNTokenAuthValues() { Read = true } } },
13 Uuids = new Dictionary<string, PNTokenAuthValues>() {
14 { "uuid-c", new PNTokenAuthValues() { Get = true } },
15 { "uuid-d", new PNTokenAuthValues() { Get = true, Update = true } }}
show all 32 lines1var request = pubnub.requestToken(
2 ttl: 15, authorizedUUID: 'my-authorized-uuid')
3..add(ResourceType.channel, name: 'channel-a', read: true)
4..add(ResourceType.channelGroup, name: 'channel-group-b', read: true)
5..add(ResourceType.uuid, name: 'uuid-c', get: true)
6..add(ResourceType.channel, name: 'channel-b', read: true, write: true)
7..add(ResourceType.channel, name: 'channel-c', read: true, write: true)
8..add(ResourceType.channel, name: 'channel-d', read: true, write: true)
9..add(ResourceType.uuid, name: 'uuid-d', get: true, update: true)
10..add(ResourceType.channel, pattern: 'channel-[A-Za-z0-9]', read: true);
11
12var token = await pubnub.grantToken(request);
13print('grant token = $token');
1$pubnub->grantToken()
2 ->ttl(15)
3 ->authorizedUuid('my-authorized-uuid')
4 ->addChannelResources([
5 'channel-a' => ['read' => true],
6 'channel-b' => ['read' => true, 'write' => true],
7 'channel-c' => ['read' => true, 'write' => true],
8 'channel-d' => ['read' => true, 'write' => true],
9 ])
10 ->addChannelGroupResources([
11 'channel-group-b' => ['read' => true],
12 ])
13 ->addUuidResources([
14 'uuid-c' => ['get' => true],
15 'uuid-d' => ['get' => true, 'update' => true],
show all 19 lines1pubnub.grant_token(
2 ttl: 15,
3 authorized_uuid: "my-authorized-uuid",
4 channels: {
5 "channel-a": Pubnub::Permissions.res(read: true),
6 "channel-b": Pubnub::Permissions.res(read: true, write: true),
7 "channel-c": Pubnub::Permissions.res(read: true, write: true),
8 "channel-d": Pubnub::Permissions.res(read: true, write: true),
9 "^channel-[A-Za-z0-9]$": Pubnub::Permission.pat(read: true)
10 },
11 channel_groups: {
12 "channel-group-b": Pubnub::Permissions.res(read: true)
13 },
14 uuids: {
15 "uuid-c": Pubnub::Permissions.res(get: true),
show all 19 linesAuthorized UUID vs UUIDs
The authorized_uuid field binds the token to one User ID that can use it. The uuids key inside resources grants permissions to access other users' metadata. These are different fields with different purposes.
Token size limit
Access Manager grantToken() requests over 32 KiB return HTTP 414 (URI Too Long). Prefer RE2 patterns over long resource lists, and keep resource names short.
Change permissions
To change a user's permissions, grant a new token with the updated resource list and return it to the client. The client sets the new token using setToken and uses it for subsequent calls.
To update a token on the client after receiving it, see Initialize a client SDK with an auth key.
Revoke a token
Revoking a token immediately blocks all requests that use it. A request that uses a revoked token fails with 403 Revoked Token. Each revoke call accepts a single token, so batch revocation isn't supported.
Revocation delay
- JavaScript
- Python
- Java
- Kotlin
- Go
- C#
- Dart
- PHP
- Ruby
1const token = await pubnub.revokeToken("p0AkFl043rhDdHRsple3KgQ3NwY6BDcENnctokenVzcqBDczaWdYIGOAeTyWGJI");
1envelope = pubnub.revoke_token("p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6hanNlY3JldAFDZ3Jwsample3KgQ3NwY6BDcGF0pERjaGFuoENnctokenV")
2 .sync()
1pubnub.revokeToken()
2 .token("p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6hanNlY3JldAFDZ3Jwsample3KgQ3NwY6BDcGF0pERjaGFuoENnctokenV")
3 .sync()
1pubnub.revokeToken("p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6hanNlY3JldAFDZ3Jwsample3KgQ3NwY6BDcGF0pERjaGFuoENnctokenV")
2 .sync()
1res, status, err := pn.RevokeToken().
2 Token("p0thisAkFl043rhDdHRsCNlY3JldAFDZ3Jwsample3KgQ3NwY6BDENnctokenV").
3 Execute()
1PNResult<PNAccessManagerRevokeTokenResult> revokeTokenResponse = await pubnub
2 .RevokeToken()
3 .Token("p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6hanNlY3JldAFDZ3Jwsample3KgQ3NwY6BDcGF0pERjaGFuoENnctokenV")
4 .ExecuteAsync();
5PNAccessManagerRevokeTokenResult revokeTokenResult = revokeTokenResponse.Result;
6PNStatus revokeTokenStatus = revokeTokenResponse.Status;
7if (!revokeTokenStatus.Error && revokeTokenResult != null)
8{
9 Console.WriteLine("Revoke token success");
10}
11else
12{
13 Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(revokeTokenStatus));
14}
1await pubnub.revokeToken("p0AkFl043rhDdHRsple3KgQ3NwY6BDcENnctokenVzcqBDczaWdYIGOAeTyWGJI");
1$pubnub->revokeToken("p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6hanNlY3JldAFDZ3Jwsample3KgQ3NwY6BDcGF0pERjaGFuoENnctokenV")
2 ->sync();
1pubnub.revoke_token(token: "p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6hanNlY3JldAFDZ3Jwsample3KgQ3NwY6BDcGF0pERjaGFuoENnctokenV", http_sync: true)
Debug Console
To generate or revoke a test token without writing code, use the Access Manager tab of the Debug Console in the Admin Portal.
Related pages
- Initialize a client SDK with an auth key. Set the granted token on the client.
- Check available permissions. Decode a token to inspect its embedded permissions.
- Change or extend access. Strategies for updating tokens before they expire.
- Update an expired token. Handle
403errors when a token has expired. - Permission model. Available permission flags per resource type.
- Operations to permissions mapping. Which permission each PubNub API operation requires.