DataSyncGrant
Factory for DataSync (App Context v4) PAM v3 resource grants, passed in the grants list of com.pubnub.api.PubNub.grantToken.
Every DataSync resource is granted here, on an exact resource id or on a regex pattern:
entity / entityPattern →
datasync:entitiesbucket;relationship / relationshipPattern →
datasync:relationshipsbucket;membership / membershipPattern →
datasync:membershipsbucket;channel / channelPattern → the plain
channelsbucket;user / userPattern → the plain
usersbucket.
Only the four DataSync-relevant permission flags are exposed: get, create, update and delete.
Shared channels bucket: DataSync channels share the channels bucket with pub/sub and App Context v2, so channel(id, update = true) also authorizes App Context v2 setChannelMetadata for the same id (and get / delete likewise). Grants on the same id are OR-merged, so combining a ChannelGrant and a channel grant on one id is safe: the token carries the union of both.
entity, relationship, channel and user grants (and their pattern variants) can also carry an optional projection: when this client uses the token to access this resource, they see it through this projection. A projection is a named, filtered view of a resource's fields, defined in the class schema under projections. When set, the SDK emits the corresponding pn-projections entry into the token meta automatically. Omit it (or pass null) to use the implicit __default__ projection. Don't write pn-projections into the token meta yourself: grantToken rejects a caller meta that contains it.
Which projections a resource has depends on its class:
entities and relationships: the projections declared by their custom class;
channels and users: only
__default__for the built-inChannel/Userclasses. A named projection only has an effect for a custom Channel / User subclass that declares it;memberships: always
__default__(the built-inMembershipclass has no named projections), so membership takes noprojectionparameter.
These grants authorize DataSync REST CRUD (get/create/update/delete) on the resource record only. They do not authorize subscribing to realtime events: a realtime subscribe is a plain PubSub read of the resource's ref-channel. Use subscribe / subscribePattern for that. They resolve the ref-channel names for you. See the subscription(...) methods on the DataSync handles (com.pubnub.api.v2.entities.DataSyncEntity, com.pubnub.api.v2.entities.DataSyncChannel, com.pubnub.api.v2.entities.DataSyncUser).
pubnub.grantToken(
ttl = 60,
authorizedUserId = UserId("pam-debug-admin"),
grants = listOf(
// entities
DataSyncGrant.entity("capy-001", get = true, update = true, projection = "admin"),
DataSyncGrant.entityPattern("capy-.*", get = true),
// relationships
DataSyncGrant.relationship("user.A:channel.X", get = true, projection = "admin"),
DataSyncGrant.relationshipPattern("user\\.A:.*", get = true),
// memberships
DataSyncGrant.membership("user-123:channel-X", get = true, delete = true),
DataSyncGrant.membershipPattern("user-123:.*", get = true),
// channels (plain `channels` bucket)
DataSyncGrant.channel("chat-1", get = true, update = true, projection = "admin"),
DataSyncGrant.channelPattern("chat-.*", get = true),
// users (plain `users` bucket)
DataSyncGrant.user("user-123", get = true, update = true),
DataSyncGrant.userPattern("user-.*", get = true, create = true),
// realtime subscribe (pub/sub `read` on the resolved ref-channel)
DataSyncGrant.subscribe("capy-001"), // read on "capy-001"
DataSyncGrant.subscribe("chat-1", projection = "admin"), // read on "__admin__chat-1"
// default projection publishes on the bare id, so no prefix; `^` keeps it off the `__admin__capy-…` mirrors
DataSyncGrant.subscribePattern("capy-.*"), // read on "^(?:capy-.*)"
DataSyncGrant.subscribePattern("chat-.*", projection = "admin"), // read on "^__admin__(?:chat-.*)"
),
).sync().tokenFunctions
Grants DataSync REST CRUD on a channel record. The permission bits land in the plain channels bucket, shared with pub/sub and App Context v2: update = true also authorizes App Context v2 setChannelMetadata for the same id.
Pattern (regex) variant of channel.
Grants DataSync REST CRUD on a membership record. There is no projection parameter: memberships always use the built-in Membership class, which declares no named projections, so they are always read through __default__. For a custom membership-like class with projections, use a relationship class and relationship.
Pattern (regex) variant of membership.
Grants a realtime subscribe on the ref-channel of a DataSync entity, user or channel: a pub/sub read on DataSyncNamespace.refChannel (id for the default projection, __{projection}__{id} otherwise). Relationships and memberships have no ref-channel of their own: their events are published to both endpoint refs.
Pattern variant of subscribe: a pub/sub read on every ref-channel whose id matches the regex pattern, as seen through projection. The channel regex is built by DataSyncNamespace.refChannelPattern: it is anchored at the start and wraps pattern in a non-capturing group, e.g. subscribePattern("capy-.*") → ^(?:capy-.*) and subscribePattern("capy-.*", "admin") → ^__admin__(?:capy-.*).
Pattern (regex) variant of user.