-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
139 lines (117 loc) · 3.58 KB
/
auth.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
129
130
131
132
133
134
135
136
137
138
139
"use strict";
import { APIGatewayEvent } from "aws-lambda";
import { serverless_DTO } from "./DTO";
import { UserModel } from "./model/users";
import { CodeModel } from "./model/code";
import { getAccessTokenByCode, getUserByToken } from "./util/github";
import { generateToken, verifyToken } from "./util/token";
import { sendAuthMessage } from "./util/email";
import {
createUser,
createToken,
updateUserInformation,
findUserByNickname,
testIsGSMEmail,
} from "./util/user";
import { connectMongoDB } from "./util/db";
const createRes: Function = (
status: number,
body?: Object,
headers?: Object,
): serverless_DTO.Response => {
return {
statusCode: status,
body: JSON.stringify(body),
headers: headers || {},
};
};
exports.authUserByOAuth = async (
event: APIGatewayEvent,
_: any,
__: Function,
) => {
const data = event.queryStringParameters;
if (data?.code === undefined) {
return createRes(404, {}, { message: "bad request" });
}
const access_token = (await getAccessTokenByCode(data.code)).access_token;
const { name, nickname } = await getUserByToken(access_token);
const code = generateToken({ nickname: nickname }, "180m");
let page = "complete.html";
const user = await findUserByNickname(nickname);
if (!user?.certified) {
if (!user) {
await createUser({
accessToken: access_token,
name: name ?? " ",
nickname: nickname,
});
}
page = "email_auth.html";
}
return createRes(
302,
{},
{ Location: `${process.env.AUTH_BASEURL}${page}?code=${code}` },
);
};
exports.authEmail = async (event: APIGatewayEvent, _: any, __: Function) => {
if (event.body === null) {
return createRes(404, {}, { message: "bad request" });
}
const searchPrams = new URLSearchParams(event.body);
const code = searchPrams.get("code");
const email = searchPrams.get("email");
if (!testIsGSMEmail(email)) {
return createRes(400, { detail: "GSM 학생 계정이어야합니다." });
}
const nickname = verifyToken(code).nickname;
const token = await createToken({ email: email, nickname: nickname });
await sendAuthMessage({
receiver: email,
nickname: nickname,
token: token.id,
});
return createRes(204);
};
exports.authUserByEmail = connectMongoDB(
async (event: APIGatewayEvent, _: any) => {
if (event.pathParameters === null) {
return createRes(404, {}, { message: "bad request" });
}
const dataId = event.pathParameters["token"];
const data = await CodeModel.findById(dataId);
if (data === null) {
return createRes(404, {}, { message: "bad request" });
}
const email = data.email;
const nickname = data.nickname;
const generation = testIsGSMEmail(email)
? Number(email.replace(/[^0-9]/g, "").slice(0, 2)) - 16
: 0;
if (generation === 0) {
createRes(404, { message: "GSM 학생이 아닙니다." });
}
const user = await UserModel.findUserFromNickname(nickname);
if (user === null) {
return createRes(404, {}, { message: "bad request" });
}
try {
await user.updateGeneration(generation);
console.log("Success update Generation");
await user.setCertifiedTrue();
console.log("Success Set Certified True");
await updateUserInformation(user);
console.log("Update User Information");
} catch (e: any) {
console.error(e);
}
await CodeModel.findByIdAndDelete(dataId);
console.log("Success find By Id and delete data Id");
return createRes(
302,
{},
{ Location: `${process.env.AUTH_BASEURL}complete.html` },
);
},
);