Grant, change, and revoke permissions

Showing JavaScript examples.

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:

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:

  • read on channel-a, channel-group-b, and uuid-c.
  • read and write on channel-b, channel-c, channel-d, and uuid-d.
  • read on all channels matching the RE2 pattern ^channel-[A-Za-z0-9]$.
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 lines
Authorized 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
Token revocation can take up to one minute to take effect, because PubNub caches a token's non-revoked state for that long.A revoked token's deny-list entry persists until the token's original TTL would have expired.A revoked token can't be re-enabled.
1const token = await pubnub.revokeToken("p0AkFl043rhDdHRsple3KgQ3NwY6BDcENnctokenVzcqBDczaWdYIGOAeTyWGJI");
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.

Was this page useful?

Last updated on