-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
68 lines (61 loc) · 1.89 KB
/
Copy pathhandler.js
File metadata and controls
68 lines (61 loc) · 1.89 KB
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
require("dotenv").config()
const TABLE_NAME = "ws_connection"
const AWS = require('aws-sdk');
const fookie = require("fookie")
const init = fookie.init()
module.exports.connection = async (event, context) => {
await init
const apig = new AWS.ApiGatewayManagementApi({
endpoint: `${event.requestContext.domainName}/${event.requestContext.stage}`,
region: "eu-central-1",
});
const dynamodb = new AWS.DynamoDB.DocumentClient({
region: "eu-central-1"
});
const connectionId = event.requestContext.connectionId
const routeKey = event.requestContext.routeKey
if (routeKey === "$connect") {
await dynamodb.put({
TableName: TABLE_NAME,
Item: {
connectionId,
ts: Date.now(),
ttl: parseInt((Date.now() / 1000) + 3600)
}
}).promise();
}
if (routeKey === "$disconnect") {
await dynamodb.delete({
TableName: TABLE_NAME,
Key: { connectionId }
}).promise();
}
if (routeKey === "$default") {
let payload = JSON.parse(event.body)
const res = await fookie.run(payload)
await apig.postToConnection({
ConnectionId: connectionId,
Data: Buffer.from(JSON.stringify(res), 'utf8')
}).promise();
}
return { statusCode: 200 };
};
module.exports.auth = async (event, context) => {
await init
const { headers, methodArn } = event;
//VALIDATE TOKEN HERE
if (!headers["token"] || process.env.SYSTEM_TOKEN != headers["token"]) {
throw new Error('Unauthorized');
}
return {
principalId: headers["token"],
policyDocument: {
Version: '2012-10-17',
Statement: [{
Action: 'execute-api:Invoke',
Effect: 'Allow',
Resource: methodArn
}]
}
}
};