-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhandler.js
208 lines (196 loc) · 5.36 KB
/
handler.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* @fileOverview implements functions to handle http requests
*/
'use strict'
const keyDao = require('./src/dao/key')
const userDao = require('./src/dao/user')
const email = require('./src/service/email')
const twilio = require('./src/service/twilio')
const dynamo = require('./src/service/dynamodb')
const { path, body, auth, response, error } = require('./src/lib/http')
const { ops, isOp, isPhone, isEmail, isCode, isId, isPin } = require('./src/lib/verify')
dynamo.init()
twilio.init()
email.init()
//
// Key functions
//
exports.createKey = async (event) => {
try {
const { pin } = body(event)
if (!isPin(pin)) {
return error(400, 'Invalid request')
}
const id = await keyDao.create({ pin })
return response(201, { id })
} catch (err) {
return error(500, 'Error creating key', err)
}
}
exports.getKey = async (event) => {
try {
const pin = auth(event).pass
const { keyId } = path(event)
if (!isId(keyId) || !isPin(pin)) {
return error(400, 'Invalid request')
}
const { key, delay } = await keyDao.get({ id: keyId, pin })
if (delay) {
return response(429, { message: 'Rate limit until', delay })
}
if (!key) {
return error(404, 'Invalid params')
}
const { id, encryptionKey } = key
return response(200, { id, encryptionKey })
} catch (err) {
return error(500, 'Error reading key', err)
}
}
exports.changePin = async (event) => {
try {
const pin = auth(event).pass
const { keyId } = path(event)
const { newPin } = body(event)
if (!isId(keyId) || !isPin(pin) || !isPin(newPin)) {
return error(400, 'Invalid request')
}
const { success, delay } = await keyDao.changePin({ id: keyId, pin, newPin })
if (delay) {
return response(429, { message: 'Rate limit until', delay })
}
if (!success) {
return error(404, 'Invalid params')
}
return response(200, 'Success')
} catch (err) {
return error(500, 'Error changing pin', err)
}
}
//
// User functions
//
exports.createUser = async (event) => {
try {
const pin = auth(event).pass
const { keyId } = path(event)
const { userId } = body(event)
if (
(!isPhone(userId) && !isEmail(userId)) ||
!isId(keyId) ||
!isPin(pin)
) {
return error(400, 'Invalid request')
}
const { key, delay } = await keyDao.get({ id: keyId, pin })
if (delay) {
return response(429, { message: 'Rate limit until', delay })
}
if (!key) {
return error(404, 'Invalid params')
}
const { salt } = key
const user = await userDao.getVerified({ userId, salt })
if (user) {
return response(409, 'User id already exists')
}
const code = await userDao.create({ userId, salt })
if (isPhone(userId)) {
await twilio.send({ userId, code })
} else {
await email.send({ userId, code })
}
return response(201, 'Success')
} catch (err) {
return error(500, 'Error creating user', err)
}
}
exports.verifyUser = async (event) => {
try {
const { keyId, userId } = path(event)
const { code, op, newPin } = body(event)
if (
(!isPhone(userId) && !isEmail(userId)) ||
!isId(keyId) ||
!isCode(code) ||
!isOp(op)
) {
return error(400, 'Invalid request')
}
const salt = await keyDao.getSalt({ id: keyId })
if (!salt) {
return error(404, 'Invalid params')
}
const { success, delay } = await userDao.verify({ userId, salt, code, op })
if (delay) {
return response(429, { message: 'Rate limit until', delay })
}
if (!success) {
return error(404, 'Invalid params')
}
if (op === ops.RESET_PIN) {
const { success, delay } = await keyDao.resetPin({ id: keyId, newPin })
if (delay) {
return response(423, { message: 'Time locked until', delay })
}
if (!success) {
return error(304, 'Invalid new pin')
}
}
return response(200, 'Success')
} catch (err) {
return error(500, 'Error verifying user', err)
}
}
exports.resetPin = async (event) => {
try {
const { keyId, userId } = path(event)
if (
(!isPhone(userId) && !isEmail(userId)) ||
!isId(keyId)
) {
return error(400, 'Invalid request')
}
const salt = await keyDao.getSalt({ id: keyId })
if (!salt) {
return error(404, 'Invalid params')
}
const code = await userDao.setNewCode({ userId, salt, op: ops.RESET_PIN })
if (!code) {
return error(404, 'Invalid params')
}
if (isPhone(userId)) {
await twilio.send({ userId, code })
} else {
await email.send({ userId, code })
}
return response(200, 'Success')
} catch (err) {
return error(500, 'Error resetting pin', err)
}
}
exports.removeUser = async (event) => {
try {
const pin = auth(event).pass
const { keyId, userId } = path(event)
if (
(!isPhone(userId) && !isEmail(userId)) ||
!isId(keyId) ||
!isPin(pin)
) {
return error(400, 'Invalid request')
}
const { key, delay } = await keyDao.get({ id: keyId, pin })
if (delay) {
return response(429, { message: 'Rate limit until', delay })
}
if (!key) {
return error(404, 'Invalid params')
}
const { salt } = key
await userDao.remove({ userId, salt })
return response(200, 'Success')
} catch (err) {
return error(500, 'Error deleting user', err)
}
}