Posix CPosix CWindows CFreeRTOSmbedCPosix C Security with Access Control, AES & TLS/SSL encryption for Realtime Apps

 
Grant API is not available for Posix C SDK, the REST API should be used instead.
 
Requires the Access Manager add-on to be enabled for your key. To enable add-on features for your keys, refer to http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
OperationPermission required
Subscribe To Channel/ChannelGroup
Read permission on the subscribe channel
Subscribe To presence Channel/ChannelGroup
Read permission on the presence channel
Unsubscribe From Channel/ChannelGroup
Read permission on the subscribe channel
Unsubscribe From presence Channel/ChannelGroup
Read permission on the presence channel
Publish to Channel
Write permission on the subscribe channel
Here Now
Read permission on the channel
Where Now
Read permission on the presence channel
State
Read permission on the channel
History
Read permission on the subscribe channel
Add Channels to Channel Groups
Manage permission on the channel group
List Channels of Channel Groups
Read permission on the channel group
Remove Channels from Channel Groups
Manage permission on the channel group
Remove Channel Groups
Manage permission on the channel group
PubNub provides builtin enterprise-grade security with fine-grained access control to all of your PubNub applications with PubNub Access Manager, Message Layer encryption with AES256, and Transport Layer Security with TLS and SSL. Before using this feature it must be enabled in the PubNub Admin Console.
PubNub Access Manager (PAM) extends PubNub's existing security framework by allowing developers to create and enforce secure access to channels throughout the PubNub Real Time Network. PAM provides the ability to enforce security at three different levels and in any combination of:
  • Application
  • Channel
  • Individual User
Access to a resource is granted or denied via the "authorization token" (aka auth key) currently set on the client.
PAM enables the developer to generate an arbitrary value for auth key using the method of his choice. This makes it possible to integrate a PubNub application with a pre-existing user authentication scheme or Security Authority.

As an example, OAuth and Facebook Connect both provide their own authentication token that could be re-used as a PAM auth key. Alternatively, a user's uuid (taken from a pre-existing DB) could also be re-used (or hashed) and used as the PAM auth key.

Your application would then use PAM grant or revoke functionality to enable or disable access to a particular channel based on the value of the connecting PubNub client's auth key.

The figure below demonstrates the use of a Security Authority leveraging PubNub Access Manager API to administer application privileges and delegate authorization tokens to users:

PubNub Access Manager
  • Security Authority issues a PAM grant to allow privileges based on a custom authentication token.
  • User requests authorization from Security Authority.
  • Security Authority delegates auth_key to user.
  • User sends credentials to PubNub and subscribes to channel.
  • PubNub verifies user privileges and sends waiting messages.
PAM operates via a grant-only (whitelist) permission scheme, where the first grant (rule) found in the hiearcht "wins". Permissions are evaluated for both publish and subscribe based on this hierarchy:
  • Subscribe key level - (access for all users on all channels)
  • Channel level - (access granted for any user but only one specific channel)
  • Channel & authorization key (access granted to specific user(s) on a given channel)
It is important to note that the more broad levels override the more granular levels -- this makes sense if you think about it as "the first matching rule wins". For example, privileges granted on an app's Subscription Key always take precedence over privileges granted on a channel or Authorization Key. Additionally, when Access Manager is first enabled there are no pre-existing permissions granted, so all read (subscribe) and write (publish) attempts to a channel will fail until explicitly granted (implicit whitelist).
Transport Layer Security (TLS) and it's predecessor Secure Socket Layer (SSL) are methods of encrypting messages while in transport across the Internet. Using PubNub TLS/SSL ensures that client messages are protected when being sent to and from the PubNub Real-Time network. This prevents intercepted messages from being viewed by unauthorized parties.

Just enable TLS/SSL at client instance initialization, and the PubNub API takes care of the rest.
With TLS/SSL your data is encrypted as it travels through the Internet, but it must be encrypted (and re-encrypted) as it passes through the PubNub servers and back out again. This is a limitation of TLS/SSL. To ensure the highest levels of message integrity TLS/SSL should be used in combination with PubNub Message Level Encryption with AES to guarantee end-to-end data security.
Using PubNub Access Manager (PAM) requires initializing your instance with the secret_key ONLY if you intend on performing "administrative" PAM functions, such as granting and revoking.
 
PAM features are not available for Posix C
If your client instance will only be performing PAM "consumer" functions, such as subscribing, history/storage calls, publishing, etc, then you will not need to initialize with the secret_key.

 
Anyone with the secret key can grant and revoke permissions to your app. Never let your secret key be discovered, and to only exchange it / deliver it securely. On JavaScript, this means explicitly don't allow anyone to "View Source" or "View Generated Source" or "Debug" to enable viewing your secret key.
In order for the PAM consumer to operate correctly, an "Administrative" authority (normally a server) must first issue the appropriate permissions for a given PAM channel/auth token combination.
In order to perform these administrative functions, you must initialize with at least your subscribe and secret keys
All PAM operations occur at either:
  • a global level (no auth key, and no channel/channel group is defined)
  • a channel/channel group level (only a channel/channel group is defined)
  • a channel/channel group and key level (where both the channel/channel group and key are defined).
 
If there is an error performing PAM operations, you may receive a 403 error. If you do, be sure you have set the correct secret key, and the issuing computer's clock is synced with NTP.
Right after initialization the auth key is not yet set, it's default value is an empty string, which means that auth key will not be used in any Pubnub transaction. You can assign your value for auth key using the pubnub_set_auth() method:
pubnub_t *ctx = pubnub_alloc();
if (NULL == ctx) {
    puts("Couldn’t allocate a Pubnub context");
    return -1;
}
pubnub_init(ctx, "demo", "demo");

pubnub_set_auth(ctx, "my_authkey");
From this point on, all PubNub operations will make requests using my_authkey for the auth key. If you’d like to not use the auth, just set it to an empty string again.
pubnub_t *ctx = pubnub_alloc();
if (NULL == ctx) {
    puts("Couldn’t allocate a Pubnub context");
    return -1;
}
pubnub_init(ctx, "demo", "demo");
pubnub_set_auth(ctx, "my_new_authkey");
Enabling TLS/SSL is as easy as setting the 2nd attribute to true in pubnub_set_ssl_options():
 pubnub_t *ctx = pubnub_alloc();
if (NULL == ctx) {
    puts("Couldn't allocate a Pubnub context");
    return -1;
}
pubnub_init(ctx, "demo", "demo");

pubnub_set_ssl_options(ctx, true, true, true);
Once in production, its best practice to enable SSL.