Skip to content

Commit 4d5aed9

Browse files
committed
update js sdk to v3
1 parent 1e3692c commit 4d5aed9

File tree

12 files changed

+6932
-3664
lines changed

12 files changed

+6932
-3664
lines changed

cdk/src/cdk.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class BackendStack extends cdk.Stack {
4848
const adminsGroupName = Utils.getEnv("ADMINS_GROUP_NAME", "pet-app-admins");
4949
const usersGroupName = Utils.getEnv("USERS_GROUP_NAME", "pet-app-users");
5050
const lambdaMemory = parseInt(Utils.getEnv("LAMBDA_MEMORY", "128"));
51-
const nodeRuntime: Runtime = lambda.Runtime.NODEJS_16_X;
51+
const nodeRuntime: Runtime = lambda.Runtime.NODEJS_20_X;
5252
const authorizationHeaderName = "Authorization";
5353
const groupsAttributeClaimName = "custom:" + groupsAttributeName;
5454

@@ -427,7 +427,8 @@ export class BackendStack extends cdk.Stack {
427427
"UIDistribution", {
428428
defaultBehavior: {
429429
origin: origins.S3BucketOrigin.withOriginAccessControl(uiBucket)
430-
}
430+
},
431+
defaultRootObject: "index.html"
431432
}
432433
);
433434

docs/OktaInstructions.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ This walkthrough will help guide you through creating a working Okta Application
1010

1111
1. Sign up for a developer account on [Okta](https://developer.okta.com/) using your corporate credentials.
1212
2. Activate your account and sign into your Okta domain *stated in the email*.
13+
14+
**NOTE: THE Okta UI may be slightly different, but the steps involved remain the same**
15+
1316
3. Go to the Admin dashboard by clicking on the **Admin** button on the top-right corner of the page.
1417
4. In the Admin dashboard, go to the top-left of the page where it says **Developer Console** and change it to **Classic UI**.
1518

lambda/api/package-lock.json

Lines changed: 6678 additions & 3441 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lambda/api/package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,27 @@
2121
},
2222
"keywords": [],
2323
"devDependencies": {
24+
"@aws-sdk/client-cognito-identity-provider": "^3.682.0",
25+
"@aws-sdk/client-dynamodb": "^3.682.0",
2426
"@types/aws-lambda": "^8.10.51",
2527
"@types/aws-serverless-express": "^3.3.3",
2628
"@types/chai": "^4.2.11",
2729
"@types/cors": "^2.8.6",
30+
"@types/express-serve-static-core": "^5.0.1",
2831
"@types/mocha": "^7.0.2",
29-
"@types/node": "^14.0.1",
32+
"@types/node": "^22.8.7",
3033
"@types/uuid": "^7.0.3",
31-
"aws-sdk": "^2.1354.0",
3234
"chai": "^4.2.0",
3335
"copy-node-modules": "^1.1.1",
3436
"dynamodb-local": "^0.0.31",
3537
"mocha": "^10.1.0",
3638
"nyc": "^15.0.1",
37-
"ts-node": "^8.10.1",
39+
"ts-node": "^10.9.2",
3840
"tslint": "^6.1.2",
39-
"typescript": "^3.9.2"
41+
"typescript": "^5.6.3"
4042
},
4143
"dependencies": {
44+
"@aws-sdk/lib-dynamodb": "^3.685.0",
4245
"aws-serverless-express": "^3.3.8",
4346
"cors": "^2.8.5",
4447
"express": "^4.21.1",

lambda/api/src/app.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express = require("express");
2-
import CognitoIdentityServiceProvider = require("aws-sdk/clients/cognitoidentityserviceprovider");
3-
import {Express, json, Request, Response, urlencoded} from "express";
2+
import { CognitoIdentityProviderClient, AdminUserGlobalSignOutCommand } from "@aws-sdk/client-cognito-identity-provider";
3+
import {Express, json, Request, Response, urlencoded, RequestHandler} from "express";
44
import cors from "cors";
55
import {eventContext} from "aws-serverless-express/middleware";
66

@@ -16,7 +16,7 @@ export interface AppOptions {
1616
allowedOrigin: string;
1717
userPoolId: string;
1818
storageService: StorageService;
19-
cognito: CognitoIdentityServiceProvider;
19+
cognito: CognitoIdentityProviderClient;
2020
expressApp?: Express; // intended for unit testing / mock purposes
2121
forceSignOutHandler?: ForceSignOutHandler;
2222
}
@@ -38,8 +38,8 @@ export class App {
3838
origin: [(opts.allowedOrigin)],
3939
}));
4040

41-
app.use(json());
42-
app.use(urlencoded({extended: true}));
41+
app.use(json() as RequestHandler);
42+
app.use(urlencoded({extended: true}) as RequestHandler);
4343

4444
app.use(eventContext());
4545

@@ -184,7 +184,12 @@ export class App {
184184

185185
app.post("/forceSignOut", async (req: Request, res: Response) => {
186186
// all tokens issued before this call will no longer be allowed to be used
187-
await opts.cognito.adminUserGlobalSignOut({Username: req.username, UserPoolId: opts.userPoolId}).promise();
187+
const params = {
188+
UserPoolId: opts.userPoolId,
189+
Username: req.username,
190+
}
191+
const command = new AdminUserGlobalSignOutCommand(params);
192+
await opts.cognito.send(command);
188193
if (opts.forceSignOutHandler) {
189194
await opts.forceSignOutHandler.forceSignOut(req);
190195
}

lambda/api/src/expressApp.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import {App} from "./app";
2-
import {DynamoDBStorageService} from "./services/dynamoDBStorageService";
3-
import {DynamoDBForcedSignoutHandler} from "./services/dynamoDBForcedSignoutHandler";
4-
import aws = require("aws-sdk");
1+
import { App } from "./app";
2+
import { DynamoDBStorageService } from "./services/dynamoDBStorageService";
3+
import { DynamoDBForcedSignoutHandler } from "./services/dynamoDBForcedSignoutHandler";
4+
import { CognitoIdentityProviderClient } from "@aws-sdk/client-cognito-identity-provider";
5+
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
6+
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
57

68
if (!process.env.ITEMS_TABLE_NAME) {
79
throw new Error("Required environment variable ITEMS_TABLE_NAME is missing");
@@ -15,14 +17,21 @@ if (!process.env.ALLOWED_ORIGIN) {
1517
throw new Error("Required environment variable ALLOWED_ORIGIN is missing");
1618
}
1719

20+
const ddbDocClient = DynamoDBDocumentClient.from(new DynamoDBClient());
21+
1822
export const expressApp = new App({
19-
cognito: new aws.CognitoIdentityServiceProvider(),
23+
cognito: new CognitoIdentityProviderClient(),
2024
adminsGroupName: process.env.ADMINS_GROUP_NAME || "pet-app-admins",
2125
usersGroupName: process.env.USERS_GROUP_NAME || "pet-app-users",
22-
authorizationHeaderName: process.env.AUTHORIZATION_HEADER_NAME || "Authorization",
26+
authorizationHeaderName:
27+
process.env.AUTHORIZATION_HEADER_NAME || "Authorization",
2328
userPoolId: process.env.USER_POOL_ID,
24-
forceSignOutHandler: process.env.USERS_TABLE_NAME ?
25-
new DynamoDBForcedSignoutHandler(process.env.USERS_TABLE_NAME) : undefined,
26-
storageService: new DynamoDBStorageService(process.env.ITEMS_TABLE_NAME),
29+
forceSignOutHandler: process.env.USERS_TABLE_NAME
30+
? new DynamoDBForcedSignoutHandler(
31+
process.env.USERS_TABLE_NAME,
32+
ddbDocClient
33+
)
34+
: undefined,
35+
storageService: new DynamoDBStorageService(process.env.ITEMS_TABLE_NAME, ddbDocClient),
2736
allowedOrigin: process.env.ALLOWED_ORIGIN,
2837
}).expressApp;

lambda/api/src/services/dynamoDBForcedSignoutHandler.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import {ForceSignOutHandler} from "./authorizationMiddleware";
22
import {Request} from "express";
3-
import {DocumentClient} from "aws-sdk/lib/dynamodb/document_client";
4-
import aws = require("aws-sdk");
3+
import { DynamoDBDocumentClient, PutCommand, GetCommand } from "@aws-sdk/lib-dynamodb";
54

65
export class DynamoDBForcedSignoutHandler implements ForceSignOutHandler {
76

87
constructor(private readonly tableName: string,
9-
private readonly docClient: DocumentClient = new aws.DynamoDB.DocumentClient(),
8+
private readonly docClient: DynamoDBDocumentClient,
109
private readonly keyAttributeName: string = "username",
1110
private readonly lastForceSignOutTimeAttributeName: string = "lastForceSignOutTime",
1211
private readonly ttlAttributeName: string = "ttl" ,
@@ -25,7 +24,8 @@ export class DynamoDBForcedSignoutHandler implements ForceSignOutHandler {
2524
Key: key,
2625
};
2726

28-
const result = await this.docClient.get(params).promise();
27+
const command = new GetCommand(params)
28+
const result = await this.docClient.send(command);
2929

3030
if (result.Item && typeof result.Item[this.lastForceSignOutTimeAttributeName] === "number") {
3131

@@ -56,10 +56,11 @@ export class DynamoDBForcedSignoutHandler implements ForceSignOutHandler {
5656

5757
try {
5858

59-
await this.docClient.put({
59+
const command = new PutCommand({
6060
TableName: this.tableName,
6161
Item: item,
62-
}).promise();
62+
});
63+
await this.docClient.send(command);
6364

6465
} catch (ex) {
6566
console.error("Error revoking token: ", ex);

lambda/api/src/services/dynamoDBStorageService.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
import aws = require("aws-sdk");
2-
import {DocumentClient} from "aws-sdk/lib/dynamodb/document_client";
1+
2+
import { DynamoDBDocumentClient, PutCommand, GetCommand, ScanCommand, DeleteCommand, ScanCommandInput } from "@aws-sdk/lib-dynamodb";
33
import {StorageService} from "./storageService";
4-
import {ScanInput} from "aws-sdk/clients/dynamodb";
54
import {Pet} from "../models/pet";
65

76
export class DynamoDBStorageService implements StorageService {
87

98
constructor(private readonly tableName: string,
10-
private readonly docClient: DocumentClient = new aws.DynamoDB.DocumentClient()) {
9+
private readonly docClient: DynamoDBDocumentClient) {
1110
}
1211

1312
public async getPet(id: string): Promise<Pet | null> {
1413

1514
try {
16-
const data = await this.docClient.get({
15+
const data = await this.docClient.send(new GetCommand({
1716
TableName: this.tableName,
1817
Key: {id},
1918
ConsistentRead: true,
20-
}).promise();
19+
}));
2120
if (data && data.Item) {
2221
return data.Item as Pet;
2322
}
@@ -30,10 +29,10 @@ export class DynamoDBStorageService implements StorageService {
3029

3130
public async savePet(pet: Pet): Promise<void> {
3231
try {
33-
await this.docClient.put({
32+
await this.docClient.send(new PutCommand({
3433
TableName: this.tableName,
3534
Item: pet,
36-
}).promise();
35+
}));
3736
} catch (ex) {
3837
console.warn("Error saving entry", ex);
3938
throw ex;
@@ -45,11 +44,11 @@ export class DynamoDBStorageService implements StorageService {
4544

4645
const result: Pet[] = [];
4746

48-
const params: ScanInput = {TableName: this.tableName};
47+
const params: ScanCommandInput = {TableName: this.tableName};
4948

5049
while (true) {
5150

52-
const data = await this.docClient.scan(params).promise();
51+
const data = await this.docClient.send(new ScanCommand(params));
5352
result.push(...data.Items as Pet[]);
5453

5554
if (!data.LastEvaluatedKey) {
@@ -70,7 +69,7 @@ export class DynamoDBStorageService implements StorageService {
7069

7170
public async deletePet(id: string): Promise<void> {
7271
try {
73-
await this.docClient.delete({TableName: this.tableName, Key: {id}}).promise();
72+
await this.docClient.send(new DeleteCommand({TableName: this.tableName, Key: {id}}));
7473
} catch (ex) {
7574
console.warn("Error deleting entry", ex);
7675
throw ex;

0 commit comments

Comments
 (0)