Access Manager API: spaces and users scopes that are not recommended for Python SDK
This page documents the spaces scope, the User class, and the authorized_user() builder method of grant_token() in the PubNub Python SDK. The Python SDK source does not tag them as deprecated, but their use is not recommended. Access Manager v3 is a current API, and these are an older way to call it. They remain for apps that already use them. New apps use channels, uuids, and authorized_user_id, described in Grant token.
- Grant token - spaces & users (not recommended) covers the
spacesscope and theusersscope.
Grant token - spaces & users (not recommended)
Not recommended
Granting tokens with the spaces scope is an older way to name channels, and the SDK does not mark it deprecated. Use the channels scope of grant_token() instead.
Requires Access Manager add-on
This method requires that the Access Manager add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.
Requires Secret Key authentication
Granting permissions to resources should be done by administrators whose SDK instance has been initialized with a Secret Key (available on the Admin Portal on your app's keyset).
The secret key grants privileged access to your PubNub application. It must remain on a trusted server and must never be included in client applications.
grant_token() generates a time-limited authorization token with an embedded access control list. The token defines time to live (ttl), an authorized user ID, and a set of permissions giving access to one or more resources:
spacesusers(other users' metadata, such as their names or avatars)
The spaces scope maps to the channels scope, and the users scope maps to the uuids scope. The spaces() and users() methods fill the same lists as channels() and uuids(). When you parse the token with parse_token(), spaces appear under channels and users appear under uuids.
A token that names an authorized user ID works only for that user ID, with the permissions that it defines. A request that the token does not authorize fails with a 403 status and an error message.
Permissions
The grant request lets your server give your clients access to PubNub resources. Clients can perform a limited set of operations on every resource:
| Resource | Permissions |
|---|---|
space | read, write, get, manage, update, join, delete |
user | get, update, delete |
For the mapping of permissions to API operations, see Manage Permissions with Access Manager v3.
TTL
Thettl (time to live) parameter defines how many minutes the permissions remain valid. After expiration, the client must get a new token to maintain access.
Every Access Manager token requires a ttl in minutes, and there is no default.
An Access Manager token's ttl ranges from 1 minute to 43,200 minutes (30 days).
For more details, see TTL in Access Manager v3.
RegEx
To set permissions with patterns instead of listing each resource, use regular expressions. Create a resource with Space.pattern() or User.pattern() before making a grant request. The server evaluates RegEx patterns with RE2-style syntax, which does not support backreferences or lookaround assertions.
For more details, see Scope in the Access Manager permission model.
Authorized user ID
The authorized user ID in the token specifies which client device uses the token in every request to PubNub. PubNub authorizes every request before it processes it. If you don't set it in the grant request, any client with any user_id can use the token. Restrict tokens to a single user ID to prevent impersonation. The authorized_user() builder method sets the same value as authorized_uuid().
Method(s)
1pubnub.grant_token(
2 ttl: int,
3 authorized_user_id: str = None,
4 spaces: List[Space] = None,
5 users: List[User] = None,
6 meta: dict = None
7) -> GrantToken
ttlrequired#- Total number of minutes for which the token is valid. An Access Manager token's
ttlranges from 1 minute to 43,200 minutes (30 days). A request with no ttl or a ttl of 0 fails validation with the messageTTL missing. spaces#- List of
Spaceobjects, each holding the permissions for one space. Create an object withSpace.id()for an exact ID orSpace.pattern()for a RegEx pattern. Pass a list even for one object, because a single object raises aTypeError. Set permissions withread(),write(),manage(),delete(),get(),update(), andjoin(). users#- List of
Userobjects, each holding the permissions for one user ID. Create an object withUser.id()for an exact ID orUser.pattern()for a RegEx pattern. Pass a list even for one object, because a single object raises aTypeError. Set permissions withget(),update(), anddelete(). meta#- Extra metadata that the token carries. Values must be scalar. Arrays and objects are not supported. A value that is not a dictionary raises a
PubNubExceptionwith the messageInvalid meta parameter.
A grant must include at least one space or user, either as an ID or as a RegEx pattern. A grant that includes none fails validation with the message Resources missing.
Sample code
- Builder Pattern
- Named Arguments
1from pubnub.models.consumer.v3.space import Space
2
3envelope = pubnub.grant_token() \
4 .authorized_user('some_user_id') \
5 .spaces([
6 Space.id('some_space_id').read().write(),
7 Space.pattern('some_*').read().write()
8 ]) \
9 .ttl(60) \
10 .sync()
1from pubnub.models.consumer.v3.space import Space
2
3grant = pubnub.grant_token(
4 spaces=[Space.id('some_space_id').read().write(), Space.pattern('some_*').read().write()],
5 authorized_user_id="some_user_id",
6 ttl=60
7).sync()
Returns
grant_token() returns an Envelope whose result is a PNGrantTokenResult and whose status is a PNStatus.
token#- Access Manager token that the grant returns.
get_token()returns the same value.
Other examples
Grant an authorized client different levels of access to various resources in a single call
The code below grants my-authorized-userId:
- Read access to
space-a, and get touserId-c. - Read/write access to
space-b,space-c,space-d, and get/update touserId-d.
1from pubnub.models.consumer.v3.user import User
2from pubnub.models.consumer.v3.space import Space
3
4spaces = [
5 Space.id("space-a").read(),
6 Space.id("space-b").read().write(),
7 Space.id("space-c").read().write(),
8 Space.id("space-d").read().write()
9]
10users = [
11 User.id("userId-c").get(),
12 User.id("userId-d").get().update()
13]
14envelope = pubnub.grant_token() \
15 .spaces(spaces) \
show all 19 linesGrant an authorized client read access to multiple spaces using RegEx
The code below grants my-authorized-userId read access to all spaces that match the space-[A-Za-z0-9] RegEx pattern.
1from pubnub.models.consumer.v3.space import Space
2
3envelope = pubnub.grant_token() \
4 .spaces([Space.pattern("space-[A-Za-z0-9]").read()]) \
5 .ttl(15) \
6 .authorized_user("my-authorized-userId") \
7 .sync()
Grant an authorized client different levels of access to various resources and read access to spaces using RegEx in a single call
The code below grants the my-authorized-userId:
- Read access to
space-a, and get touser-c. - Read/write access to
space-b,space-c,space-d, and get/update touser-d. - Read access to all spaces that match the
space-[A-Za-z0-9]RegEx pattern.
1from pubnub.models.consumer.v3.space import Space
2from pubnub.models.consumer.v3.user import User
3
4spaces = [
5 Space.id("space-a").read(),
6 Space.pattern("space-[A-Za-z0-9]").read(),
7 Space.id("space-b").read().write(),
8 Space.id("space-c").read().write(),
9 Space.id("space-d").read().write()
10]
11users = [
12 User.id("user-c").get(),
13 User.id("user-d").get().update()
14]
15envelope = pubnub.grant_token() \
show all 20 linesError responses
If you submit an invalid request, the server returns HTTP 400 with a message that identifies the missing or incorrect argument. Causes can include a RegEx issue, an invalid timestamp, or incorrect permissions. .sync() raises a PubNubException. Its get_status_code() method returns the HTTP status, and get_error_message() returns the details of the error.