-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
102 lines (90 loc) · 2.23 KB
/
handler.js
File metadata and controls
102 lines (90 loc) · 2.23 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
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
const querystring = require('querystring');
const crypto = require('crypto');
const dotenv = require('dotenv');
dotenv.config();
const { authorizer, getSuccessHTML } = require('./authorizer/authorizeRequest');
const scoresHandler = require('./scores/scoresHandler');
const gameHandler = require('./game/gameHandler');
const errorResp = {
responseType: 'ephemeral',
text: 'An unknown error has occurred. Sorry! ¯_(ツ)_/¯'
};
const verifySignature = event => {
const slackSigningSecret = process.env.SLACK_SIGNING_KEY;
const {
'X-Slack-Signature': signature,
'X-Slack-Request-Timestamp': timestamp
} = event.headers;
const str = `v0:${timestamp}:${event.body}`;
const computedHash =
'v0=' +
crypto
.createHmac('sha256', slackSigningSecret)
.update(str)
.digest('hex');
return computedHash === signature;
};
module.exports.authorization = async event => {
try {
// I don't understand oauth much, do I need to do anything with this?
const code = event.queryStringParameters.code;
const resp = await authorizer(code); // eslint-disable-line no-unused-vars
const html = await getSuccessHTML();
return {
statusCode: 200,
headers: {
'Content-Type': 'text/html'
},
body: html
};
} catch (err) {
console.error(err); // eslint-disable-line no-console
return {
statusCode: 500,
body:
'Error! Authorization probably did not happen, for an unknown reason. Please try again?\n\nContact github.com/danab/basebot to report ongoing issues.'
};
}
};
module.exports.game = async event => {
if (!verifySignature(event)) {
return {
statusCode: 500,
body: 'Fail'
};
}
try {
const { text } = querystring.parse(event.body);
const resp = await gameHandler(text);
return {
statusCode: 200,
body: JSON.stringify(resp)
};
} catch (err) {
return {
statusCode: 200,
body: JSON.stringify(errorResp)
};
}
};
module.exports.scores = async event => {
if (!verifySignature(event)) {
return {
statusCode: 500,
body: 'Fail'
};
}
try {
const { text } = querystring.parse(event.body);
const resp = await scoresHandler(text);
return {
statusCode: 200,
body: JSON.stringify(resp)
};
} catch (err) {
return {
statusCode: 200,
body: JSON.stringify(errorResp)
};
}
};