-
Notifications
You must be signed in to change notification settings - Fork 30
/
routes_oauth.js
73 lines (62 loc) · 2.06 KB
/
routes_oauth.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
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
/**
* This module implements the oauth routes needed to install an app
*/
module.exports = function(controller) {
// controller.webserver.get('/install', (req, res) => {
// // getInstallLink points to slack's oauth endpoint and includes clientId and scopes
// res.redirect(controller.adapter.getInstallLink());
// });
// controller.webserver.get('/install/auth', async (req, res) => {
// try {
// const results = await controller.adapter.validateOauthCode(req.query.code);
// console.log('FULL OAUTH DETAILS', results);
// // Store token by team in bot state.
// tokenCache[results.team_id] = results.bot.bot_access_token;
// // Capture team to bot id
// userCache[results.team_id] = results.bot.bot_user_id;
// res.json('Success! Bot installed.');
// } catch (err) {
// console.error('OAUTH ERROR:', err);
// res.status(401);
// res.send(err.message);
// }
// });
};
/**
* This is a placeholder implementation for getTokenForTeam and getBotUserByTeam
* TODO: how to expose these to botkit tho?
*/
let tokenCache = {};
let userCache = {};
if (process.env.TOKENS) {
tokenCache = JSON.parse(process.env.TOKENS);
}
if (process.env.USERS) {
userCache = JSON.parse(process.env.USERS);
}
async function getTokenForTeam(teamId) {
if (tokenCache[teamId]) {
return new Promise((resolve) => {
setTimeout(function() {
resolve(tokenCache[teamId]);
}, 150);
});
} else {
console.error('Team not found in tokenCache: ', teamId);
}
}
async function getBotUserByTeam(teamId) {
if (userCache[teamId]) {
return new Promise((resolve) => {
setTimeout(function() {
resolve(userCache[teamId]);
}, 150);
});
} else {
console.error('Team not found in userCache: ', teamId);
}
}