-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignout.ts
128 lines (114 loc) · 3.5 KB
/
signout.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 { lambda, sdk } from '@pulumi/aws';
import type { CUser, IUser } from '#tables/tables/user';
import type { lambdaEvent } from '#utils/util';
import { TokenTable, UsersTable } from '#tables/index';
import { validateUserBody } from '#tables/validation/users';
import {
currentEndpoint,
CUSTOM_ERROR_CODES,
makeCustomError,
jwtSign,
updateObject,
cryptoDecrypt,
cryptoEncrypt,
decodeJWT,
populateResponse,
STATUS_CODES,
} from '#utils/util';
/**
* The signout lambda
* @description
* - The lambda is used to sign out a user
* - The lambda is triggered by a POST request to /users/signout
*
* @see https://www.pulumi.com/docs/guides/crosswalk/aws/api-gateway/#lambda-request-handling
*/
export const signout = new lambda.CallbackFunction<
lambdaEvent,
{
body: string;
statusCode: number;
}
>('signout', {
runtime: lambda.Runtime.NodeJS16dX,
callback: async event => {
const { parsed, error } = validateUserBody(event, { email: true, password: true });
if (!parsed || error) {
return populateResponse(
STATUS_CODES.BAD_REQUEST,
makeCustomError(error ?? 'Bad Request', CUSTOM_ERROR_CODES.BODY_NOT_VALID),
);
}
const { email, password } = parsed as IUser & Pick<CUser, 'email' | 'password'>;
try {
const client = new sdk.DynamoDB.DocumentClient(currentEndpoint);
// Get user from database
const Item = await client
.get({
TableName: UsersTable.get(),
Key: {
email,
},
})
.promise();
const { Item: user } = Item;
if (!user) {
return populateResponse(
STATUS_CODES.NOT_FOUND,
makeCustomError('User not found', CUSTOM_ERROR_CODES.USER_NOT_FOUND),
);
}
const { password: hashedPassword, userID, token: hashedToken } = user as CUser;
if (cryptoDecrypt(hashedPassword) === password) {
// sign token
const token = jwtSign(userID, email);
const updateObj: IUser = {
token: cryptoEncrypt(token),
};
const { ExpressionAttributeNames, ExpressionAttributeValues, UpdateExpression } = updateObject(updateObj);
const userDbToken = cryptoDecrypt(hashedToken);
const expires = decodeJWT(userDbToken).data?.exp;
if (!expires) {
return populateResponse(
STATUS_CODES.INTERNAL_SERVER_ERROR,
makeCustomError('Seems like an invalid token', CUSTOM_ERROR_CODES.USER_ERROR),
);
}
// Update token in database
await client
.update({
TableName: UsersTable.get(),
Key: {
email,
},
ExpressionAttributeNames,
ExpressionAttributeValues,
UpdateExpression,
})
.promise();
// Delete token from database
await client
.put({
TableName: TokenTable.get(),
Item: {
token: userDbToken,
userID,
expires,
},
})
.promise();
return populateResponse(STATUS_CODES.OK, 'User signed out');
}
return populateResponse(
STATUS_CODES.UNAUTHORIZED,
makeCustomError('Wrong password', CUSTOM_ERROR_CODES.USER_NOT_AUTHORIZED),
);
} catch (error) {
console.error(error);
return populateResponse(
STATUS_CODES.INTERNAL_SERVER_ERROR,
makeCustomError('Something went wrong', CUSTOM_ERROR_CODES.USER_ERROR),
);
}
},
});