Access Manager v3 API for PHP SDK
Access Manager v3 allows you to enforce security controls for client access to resources within the PubNub Platform. With Access Manager v3, your servers can grant their clients tokens with embedded permissions that provide access to individual PubNub resources:
- For a limited period of time.
- Through resource lists or patterns (regular expressions).
- In a single API request, even if permission levels differ (
read
tochannel1
andwrite
tochannel2
).
You can add the authorizedUuid
parameter to the grant request to restrict the token usage to only one client with a given Uuid
. Once specified, only this authorizedUuid
will be able to use the token to make API requests for the specified resources, according to permissions given in the grant request.
For more information about Access Manager v3, refer to Manage Permissions with Access Manager v3.
Grant Token
Requires Access Manager add-on
This method requires that the Access Manager add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.
The grantToken()
method generates a time-limited authorization token with an embedded access control list. The token defines time to live (ttl
), authorizedUuid
, and a set of permissions giving access to one or more resources:
Channels
ChannelGroups
Uuids
(other users' object metadata, such as their names or avatars)
Only this authorizedUuid
will be able to use the token with the defined permissions. The authorized client will send the token to PubNub with each request until the token's ttl
expires. Any unauthorized request or a request made with an invalid token will return a 403
with a respective error message.
Permissions
The grant request allows your server to securely grant your clients access to the resources within the PubNub Platform. There is a limited set of operations the clients can perform on every resource:
Resource | Permissions |
---|---|
Channels | read , write , get , manage , update , join , delete |
ChannelGroups | read , manage |
Uuids | get , update , delete |
For permissions and API operations mapping, refer to Manage Permissions with Access Manager v3.
TTL
The ttl
(time to live) parameter is the number of minutes before the granted permissions expire. The client will require a new token to be granted before expiration to ensure continued access. ttl
is a required parameter for every grant call and there is no default value set for it. The max value for ttl
is 43,200 (30 days).
danger
ttl
valueFor security reasons, it's recommended to set ttl
between 10
and 60
, and create a new token before this ttl
elapses.
For more details, see TTL in Access Manager v3.
RegEx
If you prefer to specify permissions by setting patterns, rather than listing all resources one by one, you can use regular expressions. To do this, set RegEx permissions as patterns
before making a grant request.
For more details, see RegEx in Access Manager v3.
Authorized UUID
Setting an authorizedUuid
in the token helps you specify which client device should use this token in every request to PubNub. This will ensure that all requests to PubNub are authorized before PubNub processes them. If authorizedUuid
isn't specified during the grant request, the token can be used by any client with any Uuid
. It's recommended to restrict tokens to a single authorizedUuid
to prevent impersonation.
For more details, see Authorized UUID in Access Manager v3.
Method(s)
$pubnub->grantToken()
->ttl($ttl)
->authorizedUuid($uuid)
->addChannelResources(Array[String => String])
->addChannelGroupResources(Array[String => String])
->addUuidResources(Array[String => String])
->addChannelPatterns(Array[String => String])
->addChannelGroupPatterns(Array[String => String])
->addUuidPatterns(Array[String => String])
->meta(Array[String => String])
->sync();
Parameter | Description |
---|---|
ttl *Type: Number Default: n/a | Total number of minutes for which the token is valid.
|
authorizedUuid Type: String Default: n/a | Single UUID which is authorized to use the token to make API requests to PubNub. |
addChannelResources Type: Array Default: n/a | Array containing explicit channels resource permissions. |
addChannelGroupResources Type: Array Default: n/a | Array containing explicit channel groups resource permissions. |
addUuidResources Type: Array Default: n/a | Array containing explicit UUID resource permissions. |
addChannelPatterns Type: Array Default: n/a | Array containing channels permissions expressed as a RegEx pattern. |
addChannelGroupPatterns Type: Array Default: n/a | Array containing channel groups permissions expressed as a RegEx pattern. |
addUuidPatterns Type: Array Default: n/a | Array containing UUID permissions expressed as a RegEx pattern. |
meta Type: Array Default: n/a | Extra metadata to be published with the request. Values must be scalar only; arrays or objects aren't supported. |
An array for resource/permission should contain the resource name as the key and an array of rights as a value. You don't need to specify the false
permissions as that's the default value for all rights.
[
'channel-1' => ['read' => true, 'write' => true]
'channel-2' => ['read' => true, 'write' => false]
'channel-3' => ['read' => true]
]
Required key/value mappings
For a successful grant request, you must specify permissions for at least one Uuid
, Channel
, or ChannelGroup
, either as a resource list or as a pattern (RegEx).
Basic Usage
Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.
<?php
// Include Composer autoloader (adjust path if needed)
require_once 'vendor/autoload.php';
use PubNub\PNConfiguration;
use PubNub\PubNub;
use PubNub\Exceptions\PubNubServerException;
// Create configuration
$pnConfig = new PNConfiguration();
$pnConfig->setSubscribeKey("demo");
$pnConfig->setPublishKey("demo");
$pnConfig->setSecretKey("demo"); // Required for Access Manager operations
$pnConfig->setUserId("php-token-granter");
show all 51 lines