Skip to content

Commit

Permalink
chore: change examples calls from generateAuthToken to generateApiKey (
Browse files Browse the repository at this point in the history
…#877)

* chore: change examples calls from generateAuthToken to generateApiKey
  • Loading branch information
cprice404 authored Sep 15, 2023
1 parent 142952d commit 0786a62
Show file tree
Hide file tree
Showing 24 changed files with 167 additions and 171 deletions.
2 changes: 1 addition & 1 deletion examples/cloudflare-workers/web-sdk/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default {
const momento = new CacheClient({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromString({
authToken:env.MOMENTO_API_KEY
apiKey:env.MOMENTO_API_KEY
}),
defaultTtlSeconds: 60,
});
Expand Down
6 changes: 3 additions & 3 deletions examples/deno/http-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export const handler = async (_request: Request): Promise<Response> => {
throw new Error('Missing environment variable MOMENTO_CACHE_NAME')
}

const authToken = env['MOMENTO_API_KEY']
if (!authToken) {
const apiKey = env['MOMENTO_API_KEY']
if (!apiKey) {
throw new Error('Missing environment variable MOMENTO_API_KEY')
}

Expand All @@ -26,7 +26,7 @@ export const handler = async (_request: Request): Promise<Response> => {
const key = 'foo'
const value = 'FOO'

const momento = new HttpClient(authToken, endpoint)
const momento = new HttpClient(apiKey, endpoint)

await momento.set(cacheName, key, value, 10)
console.log(`Set the key-value pair in the cache ${cacheName}`)
Expand Down
4 changes: 2 additions & 2 deletions examples/deno/web-sdk/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
},
"imports": {
"http": "https://deno.land/[email protected]/http/server.ts",
"momento": "npm:@gomomento/sdk-web@1.35.0",
"momento": "npm:@gomomento/sdk-web@1.39.2",
"xhr4sw": "npm:[email protected]",
"dotenv": "https://deno.land/std/dotenv/mod.ts"

}
}
}
6 changes: 3 additions & 3 deletions examples/deno/web-sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export const handler = async (_request: Request): Promise<Response> => {
throw new Error('Missing environment variable MOMENTO_CACHE_NAME')
}

const authToken = env['MOMENTO_API_KEY']
if (!authToken) {
const apiKey = env['MOMENTO_API_KEY']
if (!apiKey) {
throw new Error('Missing environment variable MOMENTO_API_KEY')
}

const momento = new CacheClient({
configuration: Configurations.Browser.v1(),
credentialProvider: CredentialProvider.fromString({
authToken: authToken,
apiKey: apiKey,
}),
defaultTtlSeconds: 60,
})
Expand Down
6 changes: 3 additions & 3 deletions examples/fastly-compute/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ async function handleRequest(event: FetchEvent) {

// Get all required information from the Config Store
// Note: for production environments, the Momento API key should be saved in a Fastly Secret Store
const authToken = secrets.get('MOMENTO_API_KEY');
if (!authToken) {
const apiKey = secrets.get('MOMENTO_API_KEY');
if (!apiKey) {
return new Response('Missing required env var MOMENTO_API_KEY', {
status: 500,
});
Expand Down Expand Up @@ -64,7 +64,7 @@ async function handleRequest(event: FetchEvent) {
const value = 'serverless';

try {
const momento = new MomentoFetcher(authToken, httpEndpoint, backend);
const momento = new MomentoFetcher(apiKey, httpEndpoint, backend);

const getResp = await momento.get(cacheName, key);
console.log(`Fetching the key when the cache is empty: ${getResp}`);
Expand Down
22 changes: 9 additions & 13 deletions examples/nodejs/access-control/access-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
DeleteCache,
AuthClient,
ExpiresIn,
GenerateAuthToken,
GenerateApiKey,
TokenScope,
TokenScopes,
CacheRole,
Expand Down Expand Up @@ -67,19 +67,19 @@ async function get(cacheClient: CacheClient, cacheName: string, key: string) {
}
}

async function generateAuthToken(
async function generateApiKey(
authClient: AuthClient,
scope: TokenScope,
durationSeconds: number
): Promise<[string, string]> {
const generateTokenResponse = await authClient.generateAuthToken(scope, ExpiresIn.seconds(durationSeconds));
if (generateTokenResponse instanceof GenerateAuthToken.Success) {
const generateTokenResponse = await authClient.generateApiKey(scope, ExpiresIn.seconds(durationSeconds));
if (generateTokenResponse instanceof GenerateApiKey.Success) {
console.log(`Generated an API key with ${scope.toString()} scope at time ${Date.now() / 1000}!`);
console.log('Logging only a substring of the tokens, because logging security credentials is not advisable:');
console.log(`API key starts with: ${generateTokenResponse.authToken.substring(0, 10)}`);
console.log(`API key starts with: ${generateTokenResponse.apiKey.substring(0, 10)}`);
console.log(`Refresh token starts with: ${generateTokenResponse.refreshToken.substring(0, 10)}`);
console.log(`Expires At: ${generateTokenResponse.expiresAt.epoch()}`);
return [generateTokenResponse.authToken, generateTokenResponse.refreshToken];
return [generateTokenResponse.apiKey, generateTokenResponse.refreshToken];
} else {
throw new Error(`Failed to generate API key: ${generateTokenResponse.toString()}`);
}
Expand Down Expand Up @@ -107,14 +107,14 @@ async function main() {
await set(mainCacheClient, CACHE_OPEN_DOOR, 'hello', 'world');

// Create a token valid for 600 seconds that can only read a specific cache 'open-door'
const [scopedToken, scopedRefreshToken] = await generateAuthToken(
const [scopedToken, scopedRefreshToken] = await generateApiKey(
mainAuthClient,
TokenScopes.cacheReadOnly(CACHE_OPEN_DOOR),
tokenValidForSeconds
);
const scopedTokenCacheClient = await CacheClient.create({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromString({authToken: scopedToken}),
credentialProvider: CredentialProvider.fromString({apiKey: scopedToken}),
defaultTtlSeconds: 600,
});

Expand Down Expand Up @@ -155,11 +155,7 @@ async function main() {
],
};

const [scopedToken1, scopedRefreshToken1] = await generateAuthToken(
mainAuthClient,
permissions,
tokenValidForSeconds
);
const [scopedToken1, scopedRefreshToken1] = await generateApiKey(mainAuthClient, permissions, tokenValidForSeconds);
// Do something with the token.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {CacheClient, Configurations, CredentialProvider, CacheGet, CacheSet, Cre

import {SecretsManagerClient, GetSecretValueCommand} from '@aws-sdk/client-secrets-manager';

async function example_API_retrieveAuthTokenFromSecretsManager(
async function example_API_retrieveApiKeyFromSecretsManager(
ttl = 600,
secretName = 'MOMENTO_API_KEY',
regionName = 'us-west-2'
Expand Down Expand Up @@ -33,7 +33,7 @@ async function example_API_retrieveAuthTokenFromSecretsManager(
// Gets a client connection object from Momento Cache and returns that for later use.
return await CacheClient.create({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromString({authToken: secret}),
credentialProvider: CredentialProvider.fromString({apiKey: secret}),
defaultTtlSeconds: ttl,
});
}
Expand Down Expand Up @@ -79,7 +79,7 @@ async function readFromCache(client: CacheClient, cacheName: string, key: string
// Call the various functions
async function main() {
const CACHE_NAME = 'demo-cache2';
const cacheClient = await example_API_retrieveAuthTokenFromSecretsManager();
const cacheClient = await example_API_retrieveApiKeyFromSecretsManager();

await createCache(cacheClient, CACHE_NAME);
await writeToCache(cacheClient, CACHE_NAME, 'code', '12345');
Expand Down
4 changes: 2 additions & 2 deletions examples/nodejs/cache/README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ npm install @gomomento/sdk
import { CacheClient, CacheGetStatus } from "@gomomento/sdk";

// ユーザーのMomentoオーストークン
const authToken = process.env.MOMENTO_API_KEY;
const apiKey = process.env.MOMENTO_API_KEY;

// Momentoをイニシャライズする
const DEFAULT_TTL = 60; // デフォルトTTLは60秒
const momento = await CacheClient.create(authToken, DEFAULT_TTL);
const momento = await CacheClient.create(apiKey, DEFAULT_TTL);

// "myCache"という名のキャッシュを作成する
const CACHE_NAME = "myCache";
Expand Down
Loading

0 comments on commit 0786a62

Please sign in to comment.