-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
264 lines (224 loc) · 7.1 KB
/
api.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
const serverless = require('serverless-http');
const bodyParser = require('body-parser');
const express = require('express')
const AWS = require('aws-sdk');
const app = express()
const USERS_TABLE = process.env.USERS_TABLE;
const QUOTES_TABLE = process.env.QUOTES_TABLE;
const ALLOW_ORIGIN = process.env.ALLOW_ORIGIN;
console.info(`USERS_TABLE: ${USERS_TABLE}`);
console.info(`QUOTES_TABLE: ${QUOTES_TABLE}`);
const IS_OFFLINE = process.env.IS_OFFLINE;
let dynamoDb;
if (IS_OFFLINE === 'true') {
dynamoDb = new AWS.DynamoDB.DocumentClient({
region: 'localhost',
endpoint: 'http://localhost:8000'
})
console.info(dynamoDb);
} else {
dynamoDb = new AWS.DynamoDB.DocumentClient();
};
// Enable CORS for all methods
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", ALLOW_ORIGIN)
res.header("Access-Control-Allow-Headers", "*")
res.header("Access-Control-Allow-Credentials", true)
next()
});
app.use(bodyParser.json({ strict: false }));
app.get('/users/:userId', function (req, res) {
const params = {
TableName: USERS_TABLE,
Key: {
userId: req.params.userId,
},
}
dynamoDb.get(params, (error, result) => {
if (error) {
console.error(error);
res.status(400).json({ error: error.message });
}
if (result.Item) {
res.json(result.Item);
} else {
res.status(404).json({ error: "User not found" });
}
});
})
app.post('/users', function (req, res) {
const { userId, email } = req.body;
console.info("User POST Request");
console.debug(req);
if(req.requestContext.authorizer && req.requestContext.authorizer.claims) {
const username = req.requestContext.authorizer.claims['cognito:username'];
console.info(`Authenicated Username: ${username}`);
if(username !== userId) {
res.status(400).json({ error: 'Not authorized to update other users' });
}
}
if (!userId) {
res.status(400).json({ error: '"userId" must be specified' });
}
if (!email) {
res.status(400).json({ error: '"email" must be specified' });
}
if (typeof userId !== 'string') {
res.status(400).json({ error: '"userId" must be a string' });
} else if (typeof email !== 'string') {
res.status(400).json({ error: '"email" must be a string' });
}
const params = {
TableName: USERS_TABLE,
Item: {
userId: userId,
email: email,
lastWorkspaceIp: undefined,
lastWorkspaceDate: Date(),
providerType: undefined,
providerId: undefined
},
};
if(req.requestContext.authorizer && req.requestContext.authorizer.claims) {
const username = req.requestContext.authorizer.claims['cognito:username'];
console.info(`Authenicated Username: ${username}`);
try{
params.Item.lastWorkspaceIp = req.client.remoteAddress;
console.debug("identities:");
console.debug(req.requestContext.authorizer.claims.identities);
const claims = JSON.parse(req.requestContext.authorizer.claims.identities);
console.debug("claims:");
console.debug(claims);
params.Item.providerType = claims.providerType;
params.Item.providerId = claims.userId;
} catch(error) {
console.error("Unable to set user meta properties");
console.error(error);
}
}
dynamoDb.put(params, (error) => {
if (error) {
console.error(error);
res.status(400).json({ error: 'Could not create user' });
}
res.json(params.Item);
});
})
app.get('/quotes/:quoteId', function (req, res) {
console.info(`Quote(s) GET Request: ${req.params.quoteId}`);
const params = {
TableName: QUOTES_TABLE,
};
if(req.params.quoteId !== "all") {
params.Key = {
quoteId: req.params.quoteId,
};
dynamoDb.get(params, (error, result) => {
if (error) {
console.error(error);
res.status(400).json({ error: error.message });
}
if (result.Item) {
res.json(result.Item);
} else {
res.status(404).json({ error: "Quotes not found!" });
}
});
} else {
dynamoDb.scan(params, (error, result) => {
if (error) {
console.log(error);
res.status(400).json({ error: error.message });
}
if (result.Items) {
res.json(result.Items);
} else {
res.status(404).json({ error: "Quote not found" });
}
});
}
})
app.post('/quotes', function (req, res) {
const { quoteId, text, author, submittedBy } = req.body;
console.info("Quote POST Request");
if(req.requestContext.authorizer && req.requestContext.authorizer.claims) {
const username = req.requestContext.authorizer.claims['cognito:username'];
console.info(`Authenicated Username: ${username}`);
if(username !== submittedBy) {
res.status(400).json({ error: 'You can only update quotes you have submitted.' });
}
}
if (!quoteId) {
res.status(400).json({ error: '"quoteId" must be specified' });
}
if (!text) {
res.status(400).json({ error: '"text" must be specified' });
}
if (!submittedBy) {
res.status(400).json({ error: '"submittedBy" must be specified' });
}
let adjustedAuthor = author !== undefined ? author : "";
if (typeof quoteId !== 'string') {
res.status(400).json({ error: '"userId" must be a string' });
} else if (typeof text !== 'string') {
res.status(400).json({ error: '"text" must be a string' });
} else if (typeof adjustedAuthor !== 'string') {
res.status(400).json({ error: '"author" must be a string' });
} else if (typeof submittedBy !== 'string') {
res.status(400).json({ error: '"submittedBy" must be a string' });
}
const params = {
TableName: QUOTES_TABLE,
Item: {
quoteId: quoteId,
text: text,
author: author !== undefined ? author : "",
submittedBy: submittedBy,
submittedDate: Date()
},
};
dynamoDb.put(params, (error) => {
if (error) {
console.error(error);
res.status(400).json({ error: error.message });
}
res.json(params.Item);
});
})
app.delete("/quotes/:quoteId", function (req, res) {
console.log("Quote DELETE Request");
if (!req.params.quoteId) {
res.status(400).json({ error: '"quoteId" must be specified' });
}
const params = {
TableName: QUOTES_TABLE,
Key: {
quoteId: req.params.quoteId
},
};
dynamoDb.get(params, (error, result) => {
if (error) {
console.error(error);
res.status(400).json({ error: error.message });
}
if (result.Item) {
if(req.requestContext.authorizer && req.requestContext.authorizer.claims) {
const username = req.requestContext.authorizer.claims['cognito:username'];
console.info(`Authenicated Username: ${username}`);
if(username !== result.Item.submittedBy) {
res.status(400).json({ error: 'You can only delete quotes you have submitted.' });
}
}
dynamoDb.delete(params, (error, data) => {
if (error) {
console.error(error);
res.status(400).json({ error: error.message });
}
res.json(data);
});
} else {
res.status(404).json({ error: "Quote not found!" });
}
});
})
module.exports.handler = serverless(app);