|
| 1 | +import * as cognito from "@aws-sdk/client-cognito-identity-provider"; |
| 2 | + |
| 3 | +import Chance from "chance"; |
| 4 | +import { REGION, USER_POOL_ID, CLIENT_USER_POOL_ID } from "../constants"; |
| 5 | +const cognitoClient = new cognito.CognitoIdentityProviderClient({ |
| 6 | + region: REGION, |
| 7 | +}); |
| 8 | + |
| 9 | +const chance = new Chance(); |
| 10 | + |
| 11 | +const userpool = USER_POOL_ID; |
| 12 | +const userpoolClient = CLIENT_USER_POOL_ID; |
| 13 | + |
| 14 | +export const a_random_user = () => { |
| 15 | + const given_name = chance.first({ nationality: "en" }); |
| 16 | + const family_name = chance.first({ nationality: "en" }); |
| 17 | + const password = ensurePasswordPolicy( |
| 18 | + chance.string({ |
| 19 | + length: 12, |
| 20 | + pool: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+", |
| 21 | + }) |
| 22 | + ); |
| 23 | + console.log("password: ", password); |
| 24 | + const email = `${given_name}-${family_name}@dev.com`; |
| 25 | + return { given_name, family_name, password, email }; |
| 26 | +}; |
| 27 | + |
| 28 | +function ensurePasswordPolicy(password: string): string { |
| 29 | + let newPassword = password; |
| 30 | + if (!/[a-z]/.test(newPassword)) |
| 31 | + newPassword += chance.letter({ casing: "lower" }); |
| 32 | + if (!/[A-Z]/.test(newPassword)) |
| 33 | + newPassword += chance.letter({ casing: "upper" }); |
| 34 | + if (!/[0-9]/.test(newPassword)) |
| 35 | + newPassword += chance.integer({ min: 0, max: 9 }).toString(); |
| 36 | + if (!/[!@#$%^&*()_+]/.test(newPassword)) |
| 37 | + newPassword += chance.pickone([ |
| 38 | + "!", |
| 39 | + "@", |
| 40 | + "#", |
| 41 | + "$", |
| 42 | + "%", |
| 43 | + "^", |
| 44 | + "&", |
| 45 | + "*", |
| 46 | + "(", |
| 47 | + ")", |
| 48 | + "_", |
| 49 | + "+", |
| 50 | + ]); |
| 51 | + return newPassword; // Ensure the password is still 12 characters long |
| 52 | +} |
| 53 | + |
| 54 | +export const an_authenticated_user = async (): Promise<any> => { |
| 55 | + const { given_name, family_name, email, password } = a_random_user(); |
| 56 | + |
| 57 | + const userPoolId = userpool; |
| 58 | + const clientId = userpoolClient; |
| 59 | + console.log("userPoolId", userPoolId); |
| 60 | + console.log("clientId", clientId); |
| 61 | + |
| 62 | + console.log(`[${email}] - signing up...`); |
| 63 | + |
| 64 | + const command = new cognito.SignUpCommand({ |
| 65 | + ClientId: clientId, |
| 66 | + Username: email, |
| 67 | + Password: password, |
| 68 | + UserAttributes: [ |
| 69 | + { Name: "firstName", Value: given_name }, |
| 70 | + { |
| 71 | + Name: "lastName", |
| 72 | + Value: family_name, |
| 73 | + }, |
| 74 | + ], |
| 75 | + }); |
| 76 | + |
| 77 | + const signUpResponse = await cognitoClient.send(command); |
| 78 | + const userSub = signUpResponse.UserSub; |
| 79 | + |
| 80 | + console.log(`${userSub} - confirming sign up`); |
| 81 | + |
| 82 | + const adminCommand: cognito.AdminConfirmSignUpCommandInput = { |
| 83 | + UserPoolId: userPoolId as string, |
| 84 | + Username: userSub as string, |
| 85 | + }; |
| 86 | + |
| 87 | + await cognitoClient.send(new cognito.AdminConfirmSignUpCommand(adminCommand)); |
| 88 | + |
| 89 | + console.log(`[${email}] - confirmed sign up`); |
| 90 | + |
| 91 | + const authRequest: cognito.InitiateAuthCommandInput = { |
| 92 | + ClientId: process.env.CLIENT_USER_POOL_ID as string, |
| 93 | + AuthFlow: "USER_PASSWORD_AUTH", |
| 94 | + AuthParameters: { |
| 95 | + USERNAME: email, |
| 96 | + PASSWORD: password, |
| 97 | + }, |
| 98 | + }; |
| 99 | + |
| 100 | + const authResponse = await cognitoClient.send( |
| 101 | + new cognito.InitiateAuthCommand(authRequest) |
| 102 | + ); |
| 103 | + |
| 104 | + console.log(`${email} - signed in`); |
| 105 | + |
| 106 | + return { |
| 107 | + username: userSub as string, |
| 108 | + name: `${given_name} ${family_name}`, |
| 109 | + email, |
| 110 | + idToken: authResponse.AuthenticationResult?.IdToken as string, |
| 111 | + accessToken: authResponse.AuthenticationResult?.AccessToken as string, |
| 112 | + }; |
| 113 | +}; |
0 commit comments