/
signInTest.ts
128 lines (110 loc) · 3.12 KB
/
signInTest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import {
AdminGetUserCommand,
CognitoIdentityProviderClient,
InitiateAuthCommand,
RespondToAuthChallengeCommand,
SignUpCommand,
} from "@aws-sdk/client-cognito-identity-provider";
const bcrypt = require("bcrypt");
const CONFIG_VALUES = {
awsRegion: "ap-northeast-1",
userPoolId: "xxxxxxxxxxxxxx",
cognitoClientId: "xxxxxxxxxxxxxx",
passwordSalt: "xxxxxxxxxxxxxx",
};
const AUTH_DATA = {
line: {
id: "xxxxxxxxxxxxxx",
token: "xxxxxxxxxxxxxx",
},
apple: {
id: "xxxxxxxxxxxxxx",
token: "xxxxxxxxxxxxxx",
},
};
const cognitoClient = new CognitoIdentityProviderClient({
region: CONFIG_VALUES.awsRegion,
});
const main = async () => {
const authMethod = "line";
// const authMethod = "apple";
const { id, token } = AUTH_DATA[authMethod];
const userName = getUserName(authMethod, id);
const userExists = await checkUserExistence(userName);
if (!userExists) {
console.log("user doesn't exists");
await signUpUser(userName, authMethod);
}
const password = hashPassword(userName);
console.log("password", password);
const command1 = new InitiateAuthCommand({
AuthFlow: "CUSTOM_AUTH",
ClientId: CONFIG_VALUES.cognitoClientId,
AuthParameters: {
USERNAME: userName,
PASSWORD: password,
},
});
const response1 = await cognitoClient.send(command1);
console.log("InitiateAuth result: ", response1);
if (response1.ChallengeName === "CUSTOM_CHALLENGE") {
console.log("respond to auth challenge");
const command2 = new RespondToAuthChallengeCommand({
ChallengeName: "CUSTOM_CHALLENGE",
ClientId: CONFIG_VALUES.cognitoClientId,
ChallengeResponses: {
USERNAME: userName,
ANSWER: token,
},
Session: response1.Session,
});
try {
const response2 = await cognitoClient.send(command2);
console.log("RespondToAuthChallengeCommand result:", response2);
} catch (e) {
console.log("failed", e.message);
}
}
};
async function signUpUser(userName: string, authMethod: "apple" | "line") {
const password = hashPassword(userName);
console.log("sign up. password:", password);
const customAttributes = {
apple: "custom:isApple",
line: "custom:isLine",
};
const command = new SignUpCommand({
ClientId: CONFIG_VALUES.cognitoClientId,
Username: userName,
Password: password,
UserAttributes: [
{
Name: customAttributes[authMethod],
Value: "true",
},
],
});
const response = await cognitoClient.send(command);
console.log("sign-up response", response);
}
async function checkUserExistence(userName: string) {
const getUserCommand = new AdminGetUserCommand({
Username: userName,
UserPoolId: CONFIG_VALUES.userPoolId,
});
try {
await cognitoClient.send(getUserCommand);
return true;
} catch (e) {
return false;
}
}
function getUserName(authMethod: "apple" | "line", id: string) {
return `${authMethod}:${id}`;
}
function hashPassword(source: string): string {
// 生成文字列は常に60文字になる
const hashedPassword = bcrypt.hashSync(source, CONFIG_VALUES.passwordSalt);
return hashedPassword;
}
main();