Permissions
When you develop a chat app, you might want to set rules that let only selected users access specific channels and user metadata. You can set up a detailed permission schema for your application and decide who can do what with your data. This way, you secure and protect your application against unauthorized third-party access attempts.
For example, you can let only specific users modify public channel information or profiles of other chat users. You can also let only admins remove users from channels for offensive behavior.
Unity Chat SDK, as a purely client-side library, doesn't do anything specific to handle such permissions internally, apart from imposing some limits through client-side errors:
- The allowed channel membership (direct, group, or public channel types).
- Availability of specific features in public chats (you'll get errors when implementing such Unity Chat SDK features as typing indicator, invites, or read receipts).
Still, you can use the cryptographic, token-based permission administrator from PubNub called Access Manager to impose strict access rules for PubNub resources in your app.
We recommend that you use Unity SDK's Access Manager for server-side operations although you can use any PubNub SDK that supports Access Manager.
Required configuration
Before you start using Access Manager, you must configure your app:
- Enable Access Manager on your app's keyset in the Admin Portal.
- Initialize the Unity Chat SDK:
-
With the
SecretKey
on your servers to secure your PubNub instance.SecretKey
is a shared secret between your application's server and PubNub and it's used to administer Access Manager permissions for your client applications by signing and verifying the authenticity of messages and requests. Read Moderation for examples. -
With the
AuthKey
on your clients to authenticate users in your application and grant them access to PubNub resources (other users' metadata and channels). Read Moderation for examples.
Secret key security
The SecretKey
should only be used within a secure server and never exposed to client devices. If the SecretKey
is ever compromised, it can be an extreme security risk to your application. If you suspect your SecretKey
has been compromised, you can generate a new SecretKey
for the existing PubNub keyset on the Admin Portal.
Use Access Manager
To implement access rules in your app, refer to Access Manager API using these Unity SDK methods:
-
GrantToken()
to generate a time-limited authorization token with an embedded access control list.Channel group limitation
Unity Chat SDK doesn't support channel groups, so you can only set permissions for the channels and users.
-
RevokeToken()
to disable an existing token and revoke all permissions embedded within. -
ParseToken()
to decode an existing token and return the object containing permissions embedded in that token. -
SetAuthToken()
to update the authentication token granted by the server.
Resource permissions
You can use Access Manager in Unity SDK to define what operations (like Read
, Write
, or Get
) your chat app users can do with such PubNub resources as channels (channels
) and other users' metadata (Uuids
):
Resource type | Permissions |
---|---|
Channels | Read , Write , Get , Manage , Update , Join , Delete |
Uuids | Get , Update , Delete |
Read the Moderation documentation to learn how you can mute and ban users in your chat app and secure these restrictions with Access Manager.
Operations-to-permissions mapping
The type of access level you grant on a given resource type defines which operations users can perform in your app. For example, Write
access given to a user for the Channels
resource type (either specific channels or channel Patterns
) lets them send messages to this channel/these channels (calling the PubNub Pub/Sub API underneath and the Unity Chat SDK's SendText()
method).
The following tables show how specific permissions granted to PubNub resources translate to operations users can later perform in a chat app.
Pub/Sub
PubNub operation | Resource type(s) | Permission | Unity Chat SDK method(s) |
---|---|---|---|
Publish on channels | Channels | Write | Send text messages (SendText() ) Forward messages ( Forward() , ForwardMessage() ) Create and send events ( EmitEvent() ) Report messages ( Report() ) |
Send signals to channels | Channels | Write | Typing indicator methods Create and send events ( EmitEvent() ) |
Subscribe to channels | Channels | Read | Typing indicator methods Receive events ( StartListeningForUpdates() ) Receive messages ( Connect() ) Membership updates ( OnMembershipUpdated ) Channel updates ( Update() , UpdateChannel() ) Messages updates ( OnMessageUpdated ) User updates ( OnUserUpdated ) |
Subscribe to presence channels | Presence channels (<channel-name>-pnpres ) | Read | n/a |
Unsubscribe from channels | Channels | None required | Stop receiving typing signals, events, messages, updates on membership, channels, messages, and users |
Presence
PubNub operation | Resource type(s) | Permission | Unity Chat SDK method(s) |
---|---|---|---|
Here Now | Channels | Read | Channel presence (WhoIsPresent() ) |
Where Now | Channels | None required | Channel presence (WherePresent() , IsPresentOn() , IsPresent() ) |
Message Persistence
PubNub operation | Resource type(s) | Permission | Unity Chat SDK method(s) |
---|---|---|---|
Fetch historical messages | Channels | Read | getMessageHistory() |
Message counts | Channels | Read | Unread messages (GetUnreadMessagesCount() , GetUnreadMessagesCounts() ) |
Delete messages | Channels | Delete | Delete() |
App Context
PubNub operation | Resource type(s) | Permission | Unity Chat SDK method(s) |
---|---|---|---|
Set user metadata | Uuids | Update | Create users (CreateUser() ) Update user metadata ( Update() , UpdateUser() ) |
Delete user metadata | Uuids | Delete | DeleteUser() |
Get user metadata | Uuids | Get | Get user data (TryGetUser() ) |
Get all user metadata | Uuids | You don't need to specify permissions to enable it if you uncheck the Disallow Get All User Metadata option in the App Context configuration in the Admin Portal. | chat.GetUsers() |
Set channel metadata | Channels When working with threads, also grant permissions to PUBNUB_INTERNAL_THREAD channels. | Update , Get | Create channels (CreateDirectConversation() , CreateGroupConversation() , CreatePublicConversation() ) Update channels ( Update() , UpdateChannel() ) Pin messages ( Pin() , PinMessage() ) Threads ( CreateThread() , PinMessage() , PinMessageToParentChannel() , UnpinMessage() , UnpinMessageFromParentChannel() ) |
Delete channel metadata | Channels | Delete | Delete() , DeleteChannel() |
Get channel metadata | Channels | Get | Get channel details (TryGetChannel() ) Get pinned messages ( TryGetPinnedMessage() ) Get thread ( TryGetThread() ) |
Get all channel metadata | Channels | You don't need to specify permissions to enable it if you uncheck the Disallow Get All Channel Metadata option in the App Context configuration in the Admin Portal. | chat.GetChannels() |
Set channel members | Channels | Manage | Invite multiple users to channels (InviteMultiple() ) Mute/Ban users ( SetRestriction() ) |
Remove channel members | Channels | Manage | Unmute/Unban users (SetRestriction() ) |
Get channel members | Channels | Get | Get members (GetMemberships() ) Check restrictions ( GetUserRestriction() , GetUsersRestrictions() , GetChannelsRestrictions() , GetChannelRestriction() ) |
Set channel memberships | Channels , Uuids | Join on Channels Update on Uuids | Create channels (CreateDirectConversation() , CreateGroupConversation() ) Invite a user to a channel ( Invite() ) Join channels ( Join() ) Update membership ( Update() ) Unread messages ( SetLastReadMessage() , MarkAllMessagesAsRead() ) |
Remove channel memberships | Channels , Uuids | Join on Channels Update on Uuids | Leave channels (Leave() ) |
Get channel memberships | Uuids | Get | List channels (GetChannels() ), GetMemberships() |
Message Reactions
PubNub operation | Resource type(s) | Permission | Unity Chat SDK method(s) |
---|---|---|---|
Add message reaction | Channels | Write | ToggleReaction() |
Remove message reaction | Channels | Delete | ToggleReaction() |
Get history with reactions | Channels | Read | getMessageHistory() |