Access Manager v3 API for Ruby SDK

Access Manager isn't enabled by default
Without it, PubNub resources on this keyset have no access controls and clients can reach channels and metadata without permission checks — including Publish, Subscribe, and all other operations. Enable Access Manager in Admin Portal before deploying to production.

Access Manager allows you to enforce security controls for client access to resources within the PubNub Platform. With Access Manager v3, your servers (that use a PubNub instance configured with a secret key) can grant their clients tokens with embedded permissions that provide access to individual PubNub resources:

  • For a limited period of time.
  • Through resource lists or patterns (regular expressions).
  • In a single API request, even if permission levels differ (read to channel1 and write to channel2).

You can add the authorizedUuid parameter to the grant request to restrict the token usage to one client with a given userId. Once specified, only this authorizedUuid will be able to use the token to make API requests for the specified resources, according to permissions given in the grant request.

User ID / UUID

User ID is also referred to as UUID/uuid in some APIs and server responses but holds the value of the userId parameter you set during initialization.

Grant token​

Requires Access Manager add-on

This method requires that the Access Manager add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.

Requires Secret Key authentication

Granting permissions to resources should be done by administrators whose SDK instance has been initialized with a Secret Key (available on the Admin Portal on your app's keyset).

The grant_token() method generates a time-limited authorization token with an embedded access control list. The token defines time to live (ttl), authorized_user_id, and a set of permissions giving access to one or more resources:

  • For a limited time period.

  • Through resource lists or regular expression (RegEx) patterns.

  • In one API request, even when permissions differ.

  • channels

  • channel_groups

  • uuids (other users' object metadata, such as their names or avatars)

  • categories (list all channel or uuid metadata on the subscribe key, not one named resource)

Only this authorized_user_id will be able to use the token with the defined permissions. The authorized client will send the token to PubNub with each request until the token's ttl expires. Any unauthorized request or a request made with an invalid token will return a 403 with a respective error message.

#

#

#

#

Method(s)​

1grant_token(
2 ttl: ttl,
3 authorized_user_id: authorized_user_id,
4 uuids: uuids,
5 channels: channels,
6 channel_groups: channel_groups,
7 categories: categories
8)
required
ttl required#
Type: Integer
Default: no default
Total number of minutes for which the token is valid.
  • The minimum allowed value is 1.
  • The maximum is 43,200 minutes (30 days).
authorized_user_id#
Type: String
Default: no default
Single uuid which is authorized to use the token to make API requests to PubNub.
uuids#
Type: Hash
Default: no default
Hash containing uuid metadata permissions provided either as a list or a RegEx pattern, for example: {"uuid-1": Pubnub::Permissions.res(get: true, update: true, delete: true),"^uuid-2.$": Pubnub::Permissions.pat(...)}.
channels#
Type: Hash
Default: no default
Hash containing channel permissions provided either as a list or a RegEx pattern, for example: {"channel-1": Pubnub::Permissions.res(read: true, write: true, manage: true, delete: true, get: true, update: true, join: true),"^channel-2.$": Pubnub::Permissions.pat(...)}.
channel_groups#
Type: Hash
Default: no default
Hash containing channel group permissions provided either as a list or a RegEx pattern, for example: {"group-id-1": Pubnub::Permissions.res(read: true, manage: true),"^group-id-2.$": Pubnub::Permissions.pat(...)}.
categories#
Type: Hash
Default: no default
Hash containing category permissions that let the token list all metadata of a type on the subscribe key. Keys are :channels (list all channel metadata) and :uuids (list all uuid metadata), for example: {channels: Pubnub::Permissions.cat(get: true), uuids: Pubnub::Permissions.cat(get: true)}. A get permission on a named channel or uuid, or on a pattern, doesn't imply this. Refer to Category permissions.
meta#
Type: Object
Default: no default
Extra metadata to be published with the request. Values must be scalar only; arrays or objects aren't supported.

Category permissions​

A category permission covers a whole resource type instead of individual resources. Listing all channels or uuids on a subscribe key needs one, because a get on a single resource doesn't allow it. Set :channels for the channels category and :uuids for the uuids category. A category carries only the get permission.

  • A category permission lets a token list all metadata of one resource type on your subscribe key. Valid categories are channels and uuids. The only permission is get (32).
  • A get permission on one named channel or user, or on a pattern, does not grant a category.
  • A token can contain only categories. It needs no resource or pattern.
  • When enumeration enforcement is enabled for a keyset, App Context list requests from a token or auth key without the matching category permission return a 403 error.

Pubnub::Permissions.cat(get: false) sends 0 for that category.

The SDK raises Pubnub::ArgumentError before sending the request in these cases:

CauseError message
categories is not a Hash.:categories has to be kind of Hash for grant token event.
A key is not :channels or :uuids.:categories supports only :channels and :uuids keys for grant token event.
A value was not created with Pubnub::Permissions.cat.:categories values have to be created with Pubnub::Permissions.cat for grant token event.
Required key/value mappings

For a successful grant request, you must specify permissions for at least one uuid, channel, or channel_groups, either as a resource list or as a pattern (RegEx), or set categories. A categories grant alone satisfies this requirement.

Sample code​

1require 'pubnub'
2
3
4def grant_token(pubnub)
5 future_result = pubnub.grant_token(
6 ttl: 15,
7 authorized_user_id: "my-authorized-uuid",
8 channels: {
9 "my-channel" => Pubnub::Permissions.res(read: true)
10 }
11 )
12 result = future_result.value
13 result.result[:data]['token']
14end
15
show all 38 lines

Returns​

1#<Pubnub::Envelope
2 @result = {
3 :data => {
4 "message" => "Success",
5 "token" => "p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6hanNlY3JldAFDZ3Jwsample3KgQ3NwY6BDcGF0pERjaGFuoENnctokenVzcqBDc3BjoERtZXRhoENzaWdYIGOAeTyWGJI"
6 }
7 },
8 @status = {
9 :code => 200
10 }
11>

Other examples​

Grant an authorized client access to list all channel and uuid metadata​

The code below grants my-authorized-uuid the get category permission for both channels and uuids, so the client can list all channel and uuid metadata on the subscribe key.

1

Grant an authorized client different levels of access to various resources in a single call​

The code below grants my-authorized-uuid:

  • Read access to channel-a, channel-group-b, and get to uuid-c.
  • Read/write access to channel-b, channel-c, channel-d, and get/update to uuid-d.
1pubnub.grant_token(
2 ttl: 15,
3 authorized_user_id: "my-authorized-uuid",
4 channels: {
5 "channel-a": Pubnub::Permissions.res(
6 read: true
7 ),
8 "channel-b": Pubnub::Permissions.res(
9 read: true,
10 write: true
11 ),
12 "channel-c": Pubnub::Permissions.res(
13 read: true,
14 write: true
15 ),
show all 35 lines

Grant an authorized client read access to multiple channels using RegEx​

The code below grants my-authorized-uuid read access to all channels that match the channel-[A-Za-z0-9] RegEx pattern.

1pubnub.grant_token(
2 ttl: 15,
3 authorized_user_id: "my-authorized-uuid",
4 channels: {
5 "^channel-[A-Za-z0-9]$": Pubnub::Permission.pat(
6 read: true
7 )
8 },
9 );

Grant an authorized client different levels of access to various resources and read access to channels using RegEx in a single call​

The code below grants the my-authorized-uuid:

  • Read access to channel-a, channel-group-b, and get to uuid-c.
  • Read/write access to channel-b, channel-c, channel-d, and get/update to uuid-d.
  • Read access to all channels that match the channel-[A-Za-z0-9] RegEx pattern.
1pubnub.grant_token(
2 ttl: 15,
3 authorized_user_id: "my-authorized-uuid",
4 channels: {
5 "channel-a": Pubnub::Permissions.res(
6 read: true
7 ),
8 "channel-b": Pubnub::Permissions.res(
9 read: true,
10 write: true
11 ),
12 "channel-c": Pubnub::Permissions.res(
13 read: true,
14 write: true
15 ),
show all 38 lines

Error responses​

If you submit an invalid request, the server returns HTTP 400 with a message that identifies the missing or incorrect argument. Causes can include a RegEx issue, an invalid timestamp, or incorrect permissions. The server returns error details in JSON.

Grant token - spaces & users​

Requires Access Manager add-on

This method requires that the Access Manager add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.

The grantToken() method generates a time-limited authorization token with an embedded access control list. The token defines time to live (ttl), authorizedUserId, and a set of permissions giving access to one or more resources:

  • spaces
  • users (other users' metadata, such as their names or avatars)

Only this authorizedUserId will be able to use the token with the defined permissions. The authorized client will send the token to PubNub with each request until the token's ttl expires. Any unauthorized request or a request made with an invalid token will return a 403 with a respective error message.

#

#

#

#

Method(s)​

1grant_token(ttl: ttl, authorized_user_id: authorized_user_id, users_permissions: users, spaces_permissions: spaces)
required
ttl required#
Type: Integer
Default: no default
Total number of minutes for which the token is valid.
  • The minimum allowed value is 1.
  • The maximum is 43,200 minutes (30 days).
authorized_user_id#
Type: String
Default: no default
Single uuid which is authorized to use the token to make API requests to PubNub.
users_permissions#
Type: Hash
Default: no default
Hash containing all User permissions provided as a list of individual permissions or RegEx patterns, for example: {"user-1": Pubnub::Permissions.res(get: true, update: true, delete: true),"^user-2.$": Pubnub::Permissions.pat(...)}.
spaces_permissions#
Type: Hash
Default: no default
Hash containing all Space permissions provided as a list of individual permissions or RegEx patterns, for example: {"space-1": Pubnub::Permissions.res(read: true, write: true, manage: true, delete: true, get: true, update: true, join: true),"^space-2.$": Pubnub::Permissions.pat(...)}.
meta#
Type: Object
Default: no default
Extra metadata to be published with the request. Values must be scalar only; arrays or objects aren't supported.
Required key/value mappings

For a successful request, you must specify permissions for at least one User or Space, either as a resource list or as a pattern (RegEx).

Sample code​

1pubnub.grant_token(
2 ttl: 15,
3 authorized_user_id: "my-authorized-userId",
4 spaces_permissions: {
5 "my-space": Pubnub::Permissions.res(
6 read: true
7 )
8 }
9);

Returns​

1#<Pubnub::Envelope
2 @result = {
3 :data => {
4 "message" => "Success",
5 "token" => "p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6han3Jwsample3KgQ3NwY6BDcGF0pERjaG3BjoERGOAeTyWGJI"
6 }
7 },
8 @status = {
9 :code => 200
10 }
11>

Other examples​

Grant an authorized client different levels of access to various resources in a single call​

The code below grants my-authorized-userId:

  • Read access to space-a, and get to userId-c.
  • Read/write access to space-b, space-c, space-d, and get/update to userId-d.
1pubnub.grant_token(
2 ttl: 15,
3 authorized_user_id: "my-authorized-userId",
4 spaces_permissions: {
5 "space-a": Pubnub::Permissions.res(
6 read: true
7 ),
8 "space-b": Pubnub::Permissions.res(
9 read: true,
10 write: true
11 ),
12 "space-c": Pubnub::Permissions.res(
13 read: true,
14 write: true
15 ),
show all 30 lines

Grant an authorized client read access to multiple spaces using RegEx​

The code below grants my-authorized-userId read access to all channels that match the space-[A-Za-z0-9] RegEx pattern.

1pubnub.grant_token(
2 ttl: 15,
3 authorized_user_id: "my-authorized-userId",
4 spaces_permissions: {
5 "^space-[A-Za-z0-9]$": Pubnub::Permission.pat(
6 read: true
7 )
8 },
9 );

Grant an authorized client different levels of access to various resources and read access to spaces using RegEx in a single call​

The code below grants the my-authorized-userId:

  • Read access to space-a and userId-c.
  • Read/write access to space-b, space-c, space-d, and get/update to userId-d.
  • Read access to all channels that match the space-[A-Za-z0-9] RegEx pattern.
1pubnub.grant_token(
2 ttl: 15,
3 authorized_user_id: "my-authorized-userId",
4 space_permissions: {
5 "space-a": Pubnub::Permissions.res(
6 read: true
7 ),
8 "space-b": Pubnub::Permissions.res(
9 read: true,
10 write: true
11 ),
12 "space-c": Pubnub::Permissions.res(
13 read: true,
14 write: true
15 ),
show all 33 lines

Error responses​

If you submit an invalid request, the server returns HTTP 400 with a message that identifies the missing or incorrect argument. Causes can include a RegEx issue, an invalid timestamp, or incorrect permissions. The server returns error details in JSON.

Revoke token​

Requires Access Manager add-on

This method requires that the Access Manager add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.

Enable token revoke

To revoke tokens, you must first enable this feature on the Admin Portal. To do that, navigate to your app's keyset and mark the Revoke v3 Token checkbox in the ACCESS MANAGER section.

The revoke_token() method allows you to disable an existing token and revoke all permissions embedded within. You can only revoke a valid token previously obtained using the grant_token() method.

Use this method for tokens with ttl less than or equal to 30 days. If you need to revoke a token with a longer ttl, contact support.

For more information, refer to Revoke permissions.

Method(s)​

1revoke_token(
2 token: token
3)
required
token required#
Type: String
Default: no default
Existing token with embedded permissions.

Sample code​

1pubnub.revoke_token("p0thisAkFl043rhDXisRGNoYW6han3Jwsample3KgQ3NwY6BDcGF0pERjaG3BjoERGOAeTyWGJI")

Returns​

1<Pubnub::Envelope
2 @result = {
3 :data => {
4 "message" => "Success"
5 }
6 },
7 @status = {
8 :code => 200
9 }
10>

Error Responses​

If you submit an invalid request, the server returns an error status code with a descriptive message informing which of the provided arguments is missing or incorrect. Depending on the root cause, this operation may return the following errors:

  • 400 Bad Request
  • 403 Forbidden
  • 503 Service Unavailable

Parse token​

The parse_token() method decodes an existing token and returns the object containing permissions embedded in that token. The client can use this method for debugging to check the permissions to the resources or find out the token's ttl details.

Method(s)​

1parse_token(
2 token: token
3)
required
token required#
Type: String
Default: no default
Current token with embedded permissions.

Sample code​

1pubnub.parse_token("p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6hanNlY3JldAFDZ3Jwsample3KgQ3NwY6BDcGF0pERjaGFuoENnctokenVzcqBDc3BjoERtZXRhoENzaWdYIGOAeTyWGJI")

Returns​

1{
2 "v"=>2,
3 "t"=>1627968380,
4 "ttl"=>15,
5 "res"=>{
6 "chan"=>{
7 "channel-1"=>239
8 },
9 "grp"=>{
10 "channel_group-1"=>5
11 },
12 "usr"=>{},
13 "spc"=>{},
14 "uuid"=>{
15 "uuid-1"=>104}
show all 33 lines

Category permissions in parsed tokens​

A token granted with categories carries a cat key. parse_token() doesn't translate it, so the key uses the wire names chan (the channels category) and uuid (the uuids category). Each value is the permission bitmask, and 32 is the get permission:

1"cat"=>{
2 "chan"=>32,
3 "uuid"=>32
4}

The cat key appears only when the token carries at least one category.

The code below parses a token and reads its category permissions.

1

Error Responses​

If you receive an error while parsing the token, it may suggest that the token is damaged. In that case, request the server to issue a new one.

Set token​

The set_token() method is used by the client devices to update the authentication token granted by the server.

Method(s)​

1set_token(token: token)
required
token required#
Type: String
Default: no default
Current token with embedded permissions.

Sample code​

1pubnub.set_token("p0thisAkFl043rhDdHRsCkNyZXisRGNoYW6hanNlY3JldAFDZ3Jwsample3KgQ3NwY6BDcGF0pERjaGFuoENnctokenVzcqBDc3BjoERtZXRhoENzaWdYIGOAeTyWGJI")

Returns​

This method doesn't return any response value.

Was this page useful?

Last updated on