Access Manager v2 API for PubNub Go SDK
PubNub Access Manager (PAM) allows you to enforce secure controls for client access to resources within the PubNub network. With PAM, your servers can grant their clients access to individual PubNub resources for a limited duration, with the ability to extend access or add permissions for additional resources.
As soon as PAM is enabled, no publish/subscribe operations can be done without first explicitly providing an authorization token to the PubNub object. If an invalid token is provided, the requesting client will receive a Forbidden error.
For more information about PAM, refer to Managing Permissions with Access Manager.
Grant
Requires Access Manager add-onRequires that the Access Manager add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
The grant
method allows you to grant permissions for one or more resources to one or more authKeys
. You may need to make multiple grant calls with an authKey
so that you can specify explicit permissions for each channel
, channelGroup
and uuid
resources. For common permissions, you can normally use a single request.
Privileges specifically granted to an application's SubscribeKey
always take precedence over privileges granted to channel
or AuthKey
. Therefore an application that requires authentication at the user level should not grant access at either the Application or Channel levels. View the Grant Levels section for more details.
The grant request allows your server to securely grant access for your clients on the following resources within the platform. Each resource allows a limited set of permissions that control the operations that can be performed by the clients. For permissions and API operations mapping information, refer to Managing Permissions with Access Manager.
Resource | Permissions |
---|---|
channel | read , write , get , manage , update , join , delete |
uuid | get , update , delete |
channelGroup | read , manage |
Wildcard notation allows you to grant permissions to multiple channels at a time and later revoke these permissions in the same manner. You can only go one level deep using wildcards. In other words:
a.*
grants access on all channels that begin witha.
.*
ora.b.*
won't work this way. If you grant on*
ora.b.*
, the grant treats*
ora.b.*
as a channel name, not a wildcard.
Wildcard revokes
You can revoke permissions with wildcards from one level deep, like a.*
, only when you initially used wildcards to grant permissions to a.*
.
When a user attempts to access a PubNub resource, PAM will evaluate any existing rules using the following order of precedence before access to a channel is granted to the user:
- Application Level - PAM privileges are always evaluated first at the
Application
level. If eitherread
orwrite
attribute is set totrue
for aSubscribeKey
, PAM will immediately grant access for that attribute without proceeding to check permissions at eitherChannel
orUser
levels. If an attribute is set tofalse
at the Application level, PAM proceeds to evaluate the attribute at the next most granular level. - Channel Level - After first verifying an attribute at the Application level, PAM evaluates the attribute at the Channel level. If an attribute is set to
true
for a combination ofSubscribeKey
, andchannel
, PAM grants access for that attribute at theChannel
level without proceeding to check whether there may beuser
level permissions. - User Level (Recommended) - As a final step PAM evaluates the attributes at the
User
level. If an attribute is set totrue
forSubscribeKey
,channel
andAuthKey
, access is granted for that attribute. Similarly, user level grants also allow you granting anAuthKey
access tochannelGroups
anduuids
. User level grants requireAuthKey
and are the recommended approach if you need to manage permissions for individual users within your application. Each user should be assigned a uniqueAuthKey
and securely passed back to the user to perform operations on the platform.
Method(s)
To Grant Permissions on a Channel
you can use the following method(s) in the Go V4 SDK:
pn.Grant() .Uuids([]string) .Channels([]string) .ChannelGroups([]string) .AuthKeys([]string) .Read(bool) .Write(bool) .Manage(bool) .Get(bool) .Update(bool) .Join(bool) .TTL(int) .QueryParam(queryParam) .Execute()
Parameter Type Required Default Description Uuids
[]string Optional Specifies a list of uuids
on which to grant permissions. You can't grant permissions to channels and channel groups in the same request if you decide to useuuids
.
This parameter does not support wildcards.Channels
[]string Optional Specifies the channels
on which togrant
permissions. If nochannels/channelGroups
are specified, then thegrant
applies to any and allchannels/channelGroups
that have been or will be created for thatpublish/subscribe
key set. Furthermore, any existing or future grants on specificchannels
are ignored, until theall channels
grant is revoked. It is possible togrant
permissions to multiplechannels
simultaneously. Wildcard notation likea.*
can be used togrant
access on channels. You cangrant
one level deep.a.*
- you can grant on this.*
anda.b.*
- grant will not work on these levels. If you grant on*
ora.b.*
, the grant will treat*
ora.b.*
as a single channel named either*
ora.b.*
.
ChannelGroups
[]string Optional Specifies the channelGroups
togrant
permissions. If nochannels/channelGroups
are specified, then thegrant
applies to any and allchannels/channelGroups
that have been or will be created for thatpublish/subscribe
key set. Furthermore, any existing or future grants on specificchannelGroups
are ignored, until theall channelGroups
grant is revoked. It is possible togrant
permissions to multiplechannelGroups
simultaneously.
This parameter does not support wildcards.AuthKeys
[]string Optional Specifies auth
Key to grant permissions. It is possible to specify multipleauth
keys. You can also grant access to a singleauth
key for multiplechannels
at the same time. Zero or morechannels
with zero or moreauth
tokens are allowed.Read
bool Optional false
Read
permissions are granted by setting totrue
.Read
permissions are removed by setting tofalse
.Write
bool Optional false
Write
permissions are granted by setting totrue
.Write
permissions are removed by setting tofalse
.Delete
bool Optional false
Delete
permissions are granted by setting totrue
.Delete
permissions are removed by setting tofalse
.Manage
bool Optional false
Manage
permissions are granted by setting totrue
.Manage
permissions are removed by setting tofalse
.Get
bool Optional false
Get
permissions are granted by setting totrue
.Get
permissions are removed by setting tofalse
.Update
bool Optional false
Update
permissions are granted by setting totrue
.Update
permissions are removed by setting tofalse
.Join
bool Optional false
Join
permissions are granted by setting totrue
.Join
permissions are removed by setting tofalse
.TTL
int Optional 1440 (24hrs)
Time in minutes for which granted permissions are valid. Default is 1440 (24hrs)
, Max is525600
, Min is1
. SettingTTL
to0
will apply the grant indefinitely.QueryParam
map[string]string Optional nil
QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API.
Basic Usage
Grant PAM Permissions for channel and AuthKey
res, status, err := pn.Grant().
Channels([]string{"ch1", "ch2", "ch3"}). // channels to allow grant on
ChannelGroups([]string{"cg1", "cg2", "cg3"}). // groups to allow grant on
AuthKeys([]string{"my-key"}). // the keys we are provisioning
Read(true). // allow those keys to write (false by default)
Write(true). // allow those keys to manage channel groups (false by default)
Manage(true). // allow keys to read the subscribe feed (false by default)
TTL(123). // how long those keys will remain valid (0 for eternity)
Execute()
fmt.Println(res, status, err)
Response
The grant()
operation returns a PNAccessManagerGrantResult
which contains the following fields:
Method | Type | Description |
---|---|---|
Level | string | Permissions level, one of subkey , subkey+auth , channel , channel-group , channel-group+auth level. |
TTL | int | TTL of grant. |
SubscribeKey | string | The subscribe key . |
Channels | map[string]*PNPAMEntityData | Access rights per channel. See PNAccessManagerChannelData for more details. |
ChannelGroups | map[string]*PNPAMEntityData | Access rights per channel groups. See PNAccessManagerChannelData for more details. |
ReadEnabled | bool | subkey level read permissions. |
WriteEnabled | bool | subkey level write permissions. |
DeleteEnabled | bool | subkey level delete permissions. |
ManageEnabled | bool | subkey level manage permissions. |
Method | Type | Description |
---|---|---|
AuthKeys | map[string]*PNAccessManagerKeyData | Access rights per auth-key. See PNAccessManagerKeyData for more details. |
Name | string | Channel or group name . |
ReadEnabled | bool | Channel or group level read permissions. |
WriteEnabled | bool | Channel or group level write permissions. |
DeleteEnabled | bool | Channel or group level delete permissions. |
ManageEnabled | bool | Channel or group level manage permissions. |
TTL | int | Time to live value. |
Method | Type | Description |
---|---|---|
ReadEnabled | bool | auth-key read permissions |
WriteEnabled | bool | auth-key write permissions. |
ManageEnabled | bool | auth-key manage permissions. |
DeleteEnabled | bool | auth-key delete permissions. |
TTL | int | Time to live value. |
Caution
PAM grant or revoke requests must be less than ~32KiB at a time, or the client will return an error. For requests greater than 32KiB, break them into smaller batched requests. When the message size exceeds 32KiB the server returns the HTTP Error code 500, instead of the correct error code 414.
Warning
For NTP synchronization, please ensure that you have configured NTP on your server to keep the clock in sync. This is to prevent system clock drift leading to 400 Invalid Timestamp
error response.
Other Examples
Grant subscribe privileges to all users on all channel(s) with default TTL (1440 minutes):
res, status, err := pn.Grant(). Read(true). Execute() fmt.Println(res, status, err)
Allow subscribe and publish to a specific Grant subscribe and publish to a specific channel for all users (no AuthKey required) with default TTL (1440 minutes):
res, status, err := pn.Grant(). Channels([]string{"my_channel"}). Read(true). Write(true). Execute() fmt.Println(res, status, err)
Grant subscribe access to a channel only for clients with a specific AuthKey with a 5 minute TTL:
res, status, err := pn.Grant(). Channels([]string{"my_channel"}). Read(true). Write(false). AuthKeys([]string{"my_authkeys"}). TTL(5). Execute() fmt.Println(res, status, err)
The above code would return the following response to the client:
{ Level:user SubscribeKey:sub-c-90c51098-c040-11e5-a316-0619f8945a4f Ttl:1440 Channels:map[ my_channel:{ Name:ch1 AuthKeys:map[ my_authkeys:{ ReadEnabled:true WriteEnabled:false ManageEnabled:<nil> Ttl:5 } ] ReadEnabled:false WriteEnabled:false ManageEnabled:false Ttl:1440 } ] ChannelGroups:map[] ReadEnabled:false WriteEnabled:false ManageEnabled:false }
Allow access to a specific channel for Presence:
res, status, err := pn.Grant(). Channels([]string{"my_channel-pnpres"}). Read(false). Write(true). Execute() fmt.Println(res, status, err)
Grant PAM Permissions for channel group:
res, status, err := pn.Grant(). ChannelGroups([]string{"cg1", "cg2", "cg3"}). AuthKeys([]string{"my-key"}). Read(true). Write(true). Manage(true). TTL(123). Execute() fmt.Println(res, status, err)
The above code would return the following response to the client:
{ Level:channel-group+auth SubscribeKey:sub-c-90c51098-c040-11e5-a316-0619f8945a4f Ttl:123 Channels:map[ my_channel-pnpres:{ Name:my_channel-pnpres AuthKeys:map[ my-key:{ ReadEnabled:true WriteEnabled:true ManageEnabled:false Ttl:0 } ] ReadEnabled:false WriteEnabled:false ManageEnabled:false Ttl:123 } ] ChannelGroups:map[] ReadEnabled:false WriteEnabled:false ManageEnabled:false }
Grant get and update access to a uuid only for clients with a specific AuthKeys with a 24 hrs ttl:
res, status, err := pn.Grant(). Uuids([]string{"my_uuid"}). Get(true). Update(false). AuthKeys([]string{"my_authkeys"}). TTL(1440). Execute() fmt.Println(res, status, err)
-
Use application level grants with caution
When access is granted on an application level,
all channels and users
will have access.Application level grants can also happen due to a bug in your code. For example, Null parameters for channels and auth-keys can cause accidental application level grants.
res, status, err := pn.Grant(). Read(true). Write(true). Execute() fmt.Println(res, status, err)
-
res, status, err := pn.Grant(). Channels([]string{"my_channel"}). Read(true). Write(true). Execute() fmt.Println(res, status, err)
-
res, status, err := pn.Grant(). Channels([]string{"my_channel"}). AuthKeys([]string{"my_key"}). Read(true). Write(true). TTL(5). Execute() fmt.Println(res, status, err)