Skip to content

Commit

Permalink
update root and ui-auth service
Browse files Browse the repository at this point in the history
  • Loading branch information
gmrabian committed Jan 11, 2024
1 parent a900abe commit a61a803
Show file tree
Hide file tree
Showing 6 changed files with 684 additions and 637 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"@types/yargs": "^15.0.10",
"@typescript-eslint/eslint-plugin": "5.18.0",
"@typescript-eslint/parser": "5.18.0",
"aws-sdk": "^2.1310.0",
"dotenv": "^8.2.0",
"eslint": "^8.33.0",
"eslint-config-airbnb": "^19.0.4",
Expand All @@ -56,7 +55,7 @@
"serverless-plugin-warmup": "^8.2.1",
"serverless-s3-bucket-helper": "github:Enterprise-CMCS/serverless-s3-bucket-helper#master",
"serverless-s3-sync": "2.0.0",
"serverless-stack-termination-protection": "^1.0.4",
"serverless-stack-termination-protection": "^2.0.2",
"typescript": "^4.6.3",
"util": "^0.12.4",
"yargs": "^16.1.1"
Expand Down
25 changes: 20 additions & 5 deletions services/ui-auth/handlers/createUsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,26 @@ async function myHandler(_event, _context, _callback) {
UserAttributes: users[i].attributes,
};

await cognitolib.createUser(poolData);
//userCreate must set a temp password first, calling setPassword to set the password configured in SSM for consistent dev login
await cognitolib.setPassword(passwordData);
//if user exists and attributes are updated in this file updateUserAttributes is needed to update the attributes
await cognitolib.updateUserAttributes(attributeData);
try {
// This may error if the user already exists
await cognitolib.createUser(poolData);
} catch {
/* swallow this exception and continue */
}

try {
//userCreate must set a temp password first, calling setPassword to set the password configured in SSM for consistent dev login
await cognitolib.setPassword(passwordData);
} catch {
/* swallow this exception and continue */
}

try {
//if user exists and attributes are updated in this file updateUserAttributes is needed to update the attributes
await cognitolib.updateUserAttributes(attributeData);
} catch {
/* swallow this exception and continue */
}
}
}

Expand Down
62 changes: 17 additions & 45 deletions services/ui-auth/libs/cognito-lib.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,29 @@
var {
CognitoIdentityProvider: CognitoIdentityServiceProvider,
} = require("@aws-sdk/client-cognito-identity-provider");
const COGNITO_CLIENT = new CognitoIdentityServiceProvider({
import {
CognitoIdentityProviderClient,
AdminCreateUserCommand,
AdminSetUserPasswordCommand,
AdminUpdateUserAttributesCommand,
} from "@aws-sdk/client-cognito-identity-provider";

const COGNITO_CLIENT = new CognitoIdentityProviderClient({
apiVersion: "2016-04-19",
region: "us-east-1",
logger: {
debug: console.debug, // eslint-disable-line no-console
info: console.info, // eslint-disable-line no-console
warn: console.warn, // eslint-disable-line no-console
error: console.error, // eslint-disable-line no-console
},
});

export async function createUser(params) {
await new Promise((resolve, _reject) => {
COGNITO_CLIENT.adminCreateUser(params, function (err, _data) {
var response;
if (err) {
console.log("FAILED ", err, err.stack); // eslint-disable-line no-console
response = { statusCode: 500, body: { message: "FAILED", error: err } };
resolve(response); //if user already exists, we still continue and ignore
} else {
response = { statusCode: 200, body: { message: "SUCCESS" } };
resolve(response);
}
});
});
await COGNITO_CLIENT.send(new AdminCreateUserCommand(params));
}

export async function setPassword(params) {
await new Promise((resolve, reject) => {
COGNITO_CLIENT.adminSetUserPassword(params, function (err, _data) {
if (err) {
console.log("FAILED to update password", err, err.stack); // eslint-disable-line no-console
var response = {
statusCode: 500,
body: { message: "FAILED", error: err },
};
reject(response);
} else {
resolve();
}
});
});
await COGNITO_CLIENT.send(new AdminSetUserPasswordCommand(params));
}

export async function updateUserAttributes(params) {
await new Promise((resolve, reject) => {
COGNITO_CLIENT.adminUpdateUserAttributes(params, function (err, _data) {
if (err) {
console.log("FAILED to update user attributes", err, err.stack); // eslint-disable-line no-console
var response = {
statusCode: 500,
body: { message: "FAILED", error: err },
};
reject(response);
} else {
resolve();
}
});
});
await COGNITO_CLIENT.send(new AdminUpdateUserAttributesCommand(params));
}
4 changes: 1 addition & 3 deletions services/ui-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
"author": "",
"license": "CC0-1.0",
"dependencies": {
"@aws-sdk/client-cognito-identity-provider": "3.362.0",
"@aws-sdk/client-sts": "3.362.0",
"fast-xml-parser": "^4.2.5"
"@aws-sdk/client-cognito-identity-provider": "^3.485.0"
},
"devDependencies": {
"serverless-plugin-common-excludes": "^4.0.0",
Expand Down
Loading

0 comments on commit a61a803

Please sign in to comment.