-
Notifications
You must be signed in to change notification settings - Fork 20
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
feat: allow configurable tokens in nextjs app example #666
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
42920ef
feat: allow configurable tokens in nextjs app example
anitarua df88fea
added example for using TokenScopes functions to get token permissions
anitarua a34bf81
update default token scope for nextjs app
anitarua 274d179
created separate line in the config example comment
anitarua 927c626
Merge branch 'main' into nextjs-configurable-tokens
anitarua cb94038
fix: small wording change
anitarua b74e695
Merge branch 'main' into nextjs-configurable-tokens
anitarua 87d5a99
Merge branch 'main' into nextjs-configurable-tokens
anitarua File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
examples/web/nextjs-chat/src/app/api/momento/token/config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { | ||
AllDataReadWrite, | ||
ExpiresIn, | ||
TopicRole, | ||
CacheRole, | ||
TokenScope, | ||
AllTopics, | ||
AllCaches | ||
} from "@gomomento/sdk"; | ||
|
||
/** | ||
* First, set the scope of permissions for your tokens. | ||
* | ||
* AllDataReadWrite provides read and write permissions to all of your caches: | ||
* export const tokenPermissions: TokenScope = AllDataReadWrite; | ||
* | ||
* You may also provide a bespoke list of permissions for each cache and topic that you have: | ||
* export const tokenPermissions: TokenScope = { | ||
* permissions: [ | ||
* { | ||
* role: CacheRole.ReadWrite | CacheRole.ReadOnly, | ||
* cache: AllCaches | "your-cache-name" | ||
* }, | ||
* { | ||
* role: TopicRole.PublishSubscribe | TopicRole.SubscribeOnly, | ||
* cache: AllCaches | "your-cache-name", | ||
* topic: AllTopics | "your-topic-name" | ||
* } | ||
* ] | ||
* }; | ||
* | ||
* More information here: https://docs.momentohq.com/develop/api-reference/auth-tokens#tokenscope-objects | ||
*/ | ||
export const tokenPermissions: TokenScope = { | ||
permissions: [ | ||
{ | ||
role: CacheRole.ReadWrite, | ||
cache: "default-cache" | ||
}, | ||
{ | ||
role: TopicRole.PublishSubscribe, | ||
cache: "default-cache", | ||
topic: AllTopics | ||
} | ||
]}; | ||
|
||
/** | ||
* Second, set the TTL for your tokens in terms of seconds, minutes, hours, | ||
* days, or using epoch format. You may also set tokens to never expire. | ||
* More information here: https://docs.momentohq.com/develop/api-reference/auth-tokens#generateauthtoken-api | ||
*/ | ||
export const tokenExpiresIn: ExpiresIn = ExpiresIn.minutes(5); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This would actually be cool to be part of the api, maybe query params or something. The user could select a cache and a topic, and then we can make a request to get a scoped token for that specific cache and topic
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.
Would that be similar to this?
client-sdk-javascript/packages/core/src/auth/tokens/token-scopes.ts
Line 4 in 2f6541f
Would there need to be a way to stack multiple permissions though?
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.
Well I was thinking for the demo would be cool for it to just scope it down as low as possible. But we could also just accept a post body of all the permissions that the user wants the token to have
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.
I think the point of Anita's config file is to show app owners/maintainers how to restrict the scope (at app deploy time) so that the browser can't get tokens that have permissions to unexpected/unwanted caches. If we make it super dynamic then it's not really that different from just giving out an AllDataReadWrite token.
We can discuss this more for follow-up PRs, if there is something dynamic that is cool and worth demo'ing I'm open to talking about it! But for now this is more about showing how to make it secure.
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.
I think the issue that I see here is that now the demo only works if a user has a cause
default-cache
already createdThere 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.
yeah I agree with that Matt, see my other comment and see if that addresses your concern
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.
tbh i wonder if we should just take the cache chooser out of the app. doesn't have to be in this PR but just thinking.