This repository has been archived by the owner on May 31, 2023. It is now read-only.
forked from yuca-live/tapioca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauthLambda.js
106 lines (98 loc) · 3.33 KB
/
oauthLambda.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
const Slack = require('slack');
const request = require('request-promise-native');
const AWS = require('aws-sdk');
// S3 configs
const s3 = new AWS.S3();
const S3_BUCKET = process.env.S3_BUCKET_NAME;
const S3_TOKENS_FILE = process.env.S3_TOKEN_FILE_NAME;
// SLACK configs
const appClientId = process.env.APP_CLIENT_ID;
const appClientSecret = process.env.APP_CLIENT_SECRET;
const tapiocaChannelName = 'tapioca-time';
const successTapiocaPageRedirect = process.env.SUCCESS_TAPIOCA_PAGE;
const failtTapiocaPageRedirect = process.env.FAIL_TAPIOCA_PAGE;
const getTokensFile = async () => {
const params = {
Bucket: S3_BUCKET,
Key: S3_TOKENS_FILE,
};
const data = await s3.getObject(params).promise();
const fileContents = JSON.parse(data.Body.toString('utf-8'));
return fileContents;
};
const putToTokensFile = async (body) => {
console.log(`Getting ${S3_TOKENS_FILE} file on S3`);
const tokenFiles = await getTokensFile();
if (!tokenFiles) {
throw new Error('Could not get token file');
}
tokenFiles.push(body);
const params = {
Bucket: S3_BUCKET,
Key: S3_TOKENS_FILE,
Body: JSON.stringify(tokenFiles),
ContentType: 'application/json',
};
console.log(`Saving new ${S3_TOKENS_FILE} file with new Slack token on S3`);
const uploadResult = await s3.upload(params).promise();
return uploadResult;
};
exports.handler = async (event, context, callback) => {
console.log('Request received to register Slack workspace on app');
const { code } = event.queryStringParameters;
try {
// When a user authorizes an app, a code query parameter is passed on the oAuth endpoint. If that code is not there, we respond with an error message
if (!code) {
const msg = "Looks like we're not getting code.";
return callback(msg);
}
const oauthResult = await request.post({
url: 'https://slack.com/api/oauth.v2.access',
qs: { code, client_id: appClientId, client_secret: appClientSecret },
header: {
'Content-type': 'application/x-www-form-urlencoded',
},
});
const body = JSON.parse(oauthResult);
const slack = new Slack({ token: body.access_token });
const tokensData = {
accessToken: body.access_token,
teamId: body.team.id,
teamName: body.team.name,
createdAt: (new Date()).toISOString(),
};
try {
const channelReqData = await slack.conversations.create({ name: tapiocaChannelName });
if (!channelReqData.ok) {
throw new Error('could not create channel');
}
tokensData.channelId = channelReqData.channel.id;
} catch (error) {
const channelsListReqData = await slack.channels.list({ limit: 1000 });
if (!channelsListReqData.ok) {
throw new Error('could not list channels');
}
const channel = channelsListReqData.channels.find((c) => c.name === tapiocaChannelName);
if (!channel) {
throw new Error('could not find donut channel');
}
tokensData.channelId = channel.id;
}
await putToTokensFile(tokensData);
const response = {
statusCode: 301,
headers: {
Location: successTapiocaPageRedirect,
},
};
return callback(null, response);
} catch (error) {
const response = {
statusCode: 500,
headers: {
Location: failtTapiocaPageRedirect,
},
};
return callback(null, response);
}
};