SwiftSwift Native Security with Access Control, AES & TLS/SSL encryption for Realtime Apps

 
These docs are for the new PubNub Swift SDK written purely in Swift. To view the docs for the older version (the PubNub Objective-C SDK wrapper for Swift), have a look here.
 
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
 

To enable SSL/TLS please check this section and to use AES please check this section.

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 authKey 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 hierarchy "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).
Advanced Encryption Standard (AES) is a specification for the encryption of electronic data. It provides a symmetric encryption functionality (via a shared cipher key) strong enough to be certified for use by the U.S. government for Top Secret level information.
PubNub client libraries offer built-in AES 256-bit encryption. Data is encrypted at the time of publish, before it is received by the PubNub Real-Time Network, and remains encrypted as it is routed through multiple data centers and regions. This means that unencrypted data never leaves a trusted authority, providing for full circle data encryption.
The complexity of encrypting and decrypting the data is built into the PubNub client libraries (since our libraries are open source, you are welcome to see how we did it). Encryption and decryption between all PubNub client libraries is fully supported. For example, publish encrypted from Ruby, receive the data, and decrypt on Android, iOS, and JavaScript.
 
It is important that the cipher key is kept secret and that the key is never exchanged over the network unencrypted.
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 enabled, your data is encrypted as it travels through the Internet, but is decrypted, processed, and encrypted again, as it passes through the PubNub network, 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 secretKey ONLY if you intend on performing administrative PAM functions, such as granting and revoking. 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 secretKey.

 

Anyone with the secretKey can grant and revoke permissions to your app. Never let your secret key be discovered, and to only exchange it / deliver it securely. Only use the secretKey on secure server-side platforms.

When you init with secretKey, you get root permissions for the Access Manager. With this feature you don't have to grant access to your servers to access channel data. The servers get all access on all channels.

You can set a default auth key at instantiation time using the auth attribute:
 Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
var config = PubNubConfiguration(
  publishKey: "YOUR-PUB-KEY",
  subscribeKey: "YOUR-SUB-KEY",
  authKey: "YOUR-AUTH-KEY"
)
var pubnub = PubNub(configuration: config)
From this point on, all PubNub operations will make requests using my_authkey for the auth key.
If at any point during runtime the auth key should need to be reset, you can call the AuthKey() method to set it again:
var config = PubNubConfiguration(
  publishKey: "demo",
  subscribeKey: "demo"
)
config.authKey = "my-new-authkey"

let pubnub = PubNub(configuration: config)
From this point on, all PubNub operations will make requests using my_newauthkey for the auth key.
TLS is enabled by default via the useSecureConnection flag, to turn it off set it to false.
 Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
var config = PubNubConfiguration(
  publishKey: "demo",
  subscribeKey: "demo"
)
config.uuid = "myUniqueUUID"

let pubnub = PubNub(configuration: config)
Once in production, its best practice to enable SSL.