Skip to content

Commit

Permalink
chore: augment tokenID to token vending machine (#978)
Browse files Browse the repository at this point in the history
* chore: augment tokenID to token vending machine
  • Loading branch information
pratik151192 authored Oct 24, 2023
1 parent 64bc797 commit 336374a
Show file tree
Hide file tree
Showing 4 changed files with 430 additions and 43 deletions.
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`


## 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

0 comments on commit 336374a

Please sign in to comment.