Repository navigation
docs: add Access Manager categories snippets #531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -231,3 +231,53 @@ try { | |
| ); | ||
| } | ||
| // snippet.end | ||
|
|
||
| // snippet.grantTokenWithCategories | ||
| // Grants `my-authorized-uuid` permission to list all channel metadata and all uuid metadata. | ||
| // Category permissions support only the `get` operation. | ||
| try { | ||
| const token = await pubnub.grantToken({ | ||
| ttl: 15, | ||
| authorizedUserId: 'my-authorized-uuid', | ||
| categories: { | ||
| channels: { get: true }, | ||
| uuids: { get: true }, | ||
| }, | ||
| }); | ||
| console.log('Granted token:', token); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win Do not log the granted token. The token grants access to list all channel and UUID metadata for its 15-minute lifetime. If application logs are exposed, logging the token exposes that credential. Log a success message instead, and pass the token directly to the code that needs it. (pubnub.com) 🧰 Tools
🤖 Prompt for AI Agents
Source: Linters/SAST tools |
||
| } catch (error) { | ||
| console.error( | ||
| `Grant token error: ${error}.${ | ||
| (error as PubNubError).status ? ` Additional information: ${(error as PubNubError).status}` : '' | ||
| }`, | ||
| ); | ||
| } | ||
| // snippet.end | ||
|
|
||
| // snippet.parseTokenWithCategories | ||
| // Decodes a token that was granted with category permissions. | ||
| const parsedToken = pubnub.parseToken('use-token-string-generated-by-grantToken()'); | ||
|
|
||
| console.log('Can list all channel metadata:', parsedToken?.categories?.channels?.get); | ||
| console.log('Can list all uuid metadata:', parsedToken?.categories?.uuids?.get); | ||
| // snippet.end | ||
|
|
||
| // snippet.grantWithCategories | ||
| // Grants the auth key permission to list all channel metadata and all uuid metadata. | ||
| // This legacy `grant` method requires the secret key to be set on the client. | ||
| try { | ||
| const response = await pubnub.grant({ | ||
| authKeys: ['my-auth-key'], | ||
| categories: ['channels', 'uuids'], | ||
| get: true, | ||
| ttl: 15, | ||
| }); | ||
| console.log('Grant response:', response); | ||
| } catch (error) { | ||
| console.error( | ||
| `Grant error: ${error}.${ | ||
| (error as PubNubError).status ? ` Additional information: ${(error as PubNubError).status}` : '' | ||
| }`, | ||
| ); | ||
| } | ||
| // snippet.end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Configure an administrator client for both grant examples.
The shared
pubnubinstance at lines 3–7 has nosecretKey. Both new grant calls use that instance, so the examples cannot issue grants as written. Show a server-side instance configured with a valid keyset and a secret from secure configuration. Keep the secret out of client code. (pubnub.com)Also applies to: 269-269
🤖 Prompt for AI Agents