Access Manager Signature Construction

The Access Manager v2 API accepts the signature query parameter as a way to authenticate that the request was indeed created by a client that has possession of the secret-key.

Access Manager v2 only

The information on this page applies only to Access Manager v2. For information on the signature format for Access Manager v3, refer to the Access Manager v3 API documentation.

Generating the signature body

The template of the signature body consists of the following order:

{GET query params}

The GET query params are a querystring representation of the GET query params with the following rules:

  • GET query params parameters must be sorted lexicographically (case-sensitive) by key.
  • Secondly, all characters in the query string parameters must be percent-encoded except alphanumeric, hyphen, underscore, and period, for example, all characters matching the RegExp /[^0-9a-zA-Z\-_\.]/.
  • Space characters must be replaced by %20 (NOT + character).
  • Each key-value pair must be separated by ampersand character.
  • Unicode characters must be broken up into UTF-8 encoded bytes before percent-encoding.
  • Final signed value should be Base64 encoded using the URL safe characters - and _ replacing + and / respectively.

As a rule of constructing the GET query params, all query params passed to PubNub via the actual GET call must match the query params recorded in the signature body. PubNub servers perform the identical calculation to verify the secret key and will fail with signature mismatch due to lack of parity between the GET query params in the request and the signature body.

Additionally, a timestamp parameter is required to ensure NTP synchronization. The value can be generated using the following snippet in javascript:

const timestamp = Math.floor(new Date().getTime() / 1000);
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.

For example, we would like to sign the following request:

  • publish key: demoPublishKey
  • subscribe key: demoSubscribeKey
  • secret key: secretKey
  • GET query params:
    • store: 1
    • seqn: 1
    • auth: myAuth
    • timestamp: 1535125017
    • pnsdk: PubNub-Go/4.1.2
    • uuid: myUuid

The signature body will look like:


The signature body is then encrypted using HMAC + SHA256 with the secret key supplied by the PubNub Admin panel and passed to the server via the signature query param.

For the above example, the resulting signature will be: whUwGhCika3QdlVj6LRg8XE4pNvsr4m3VX1G6u-s_wU= and the request will look as follows:


Grant Example

Assuming the server would like to grant the clients read access to channels a and b using auth token key1 and the access will last for 15 minutes:

  • /v2/auth/grant/sub-key/demoSubscribeKey
  • publish key: demoPublishKey
  • subscribe key: demoSubscribeKey
  • secret key: secretKey
  • GET query params:
    • uuid: myUuid
    • auth: key1
    • ttl: 15
    • r: true
    • w: false
    • m: false
    • timestamp: 123456

The following signature body will be generated:


Once encrypted, the following signature will be calculated:


And the GET request can now be performed:

Last updated on