Skip to content
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

chore: augment tokenID to token vending machine #978

Merged
merged 4 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/nodejs/token-vending-machine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ MomentoTokenVendingMachine.MomentoTokenVendingMachineApiEndpointE36C2123 = https
```

This is the URL of the API Gateway endpoint for your Token Vending Machine. Now you should be able to `curl` this endpoint, and the response will be a temporary Momento API key suitable for use in a browser!
You should see an output like:

```
{"authToken":"someShortLivedDisposableToken","expiresAt":1698119397}
```

You can also pass a tokenId as a query string to your curl command to add context to your token. This can be particularly useful when using [Momento Topics](https://www.gomomento.com/services/topics) as the tokenId can be retrieved from subscription messages, allowing your application to distinguish between different subscribers. In this case, a `name` for the user is the `tokenId` passed as a query string.

`https://9jkmukxn68.execute-api.us-west-2.amazonaws.com/prod?name=Taylor`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we be consistent in the name of the query param and what you say in the docs? you say tokenId and then the QP is name. kinda seems like the most clear thing to do is just have the QP be named tokenId as well. just a nit though, i don't feel too strongly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll clarify that in this case the tokenId is the name. Good call out. The reason I want it to be more relatable to the real world is for customers to be able to derive some sense of the use case



## Example Apps That Use The Token Vending Machine

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayPr
throw new Error("Missing required env var 'MOMENTO_API_KEY_SECRET_NAME");
}
console.log('headers in handler:', event.headers);
const vendedApiKey = await vendDisposableToken(vendorApiKeySecretName, event.headers);
const tokenId = event.queryStringParameters?.name;
console.log('tokenID inferred from queryStringParameters: ', tokenId);
const vendedApiKey = await vendDisposableToken(vendorApiKeySecretName, event.headers, tokenId);
return {
statusCode: 200,
headers: {
Expand All @@ -41,7 +43,8 @@ interface VendedToken {

async function vendDisposableToken(
vendorApiKeySecretName: string,
headers: APIGatewayProxyEventHeaders
headers: APIGatewayProxyEventHeaders,
tokenId: string | undefined
): Promise<VendedToken> {
const momentoAuthClient = await getMomentoAuthClient(vendorApiKeySecretName);

Expand All @@ -50,10 +53,15 @@ async function vendDisposableToken(
const cognitoUserTokenPermissions = determineCognitoUserTokenScope(headers);
generateTokenResponse = await momentoAuthClient.generateDisposableToken(
cognitoUserTokenPermissions,
tokenExpiresIn
tokenExpiresIn,
{
tokenId: tokenId,
}
);
} else {
generateTokenResponse = await momentoAuthClient.generateDisposableToken(tokenPermissions, tokenExpiresIn);
generateTokenResponse = await momentoAuthClient.generateDisposableToken(tokenPermissions, tokenExpiresIn, {
tokenId: tokenId,
});
}

if (generateTokenResponse instanceof GenerateDisposableToken.Success) {
Expand Down
Loading
Loading