forked from microauth/microauth-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (77 loc) · 2.52 KB
/
index.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
const querystring = require('querystring');
const url = require('url');
const uuid = require('uuid');
const rp = require('request-promise');
const redirect = require('micro-redirect');
const provider = 'slack';
const microAuthSlack = ({ clientId, clientSecret, callbackUrl, team, path = '/auth/slack', scope = 'identity.basic' }) => {
const getRedirectUrl = state => {
let redirectUrl = `https://slack.com/oauth/authorize?client_id=${clientId}&redirect_uri=${callbackUrl}&scope=${scope}&state=${state}`;
if (team) redirectUrl += `&team=${team}`;
return redirectUrl
};
const states = [];
return fn => async (req, res, ...args) => {
const { pathname, query } = url.parse(req.url);
if (pathname === path) {
try {
const state = uuid.v4();
const redirectUrl = getRedirectUrl(state);
states.push(state);
return redirect(res, 302, redirectUrl);
} catch (err) {
args.push({ err, provider });
return fn(req, res, ...args);
}
}
const callbackPath = url.parse(callbackUrl).pathname;
if (pathname === callbackPath) {
try {
const { state, code } = querystring.parse(query);
if (!states.includes(state)) {
const err = new Error('Invalid state');
args.push({ err, provider });
return fn(req, res, ...args);
}
states.splice(states.indexOf(state), 1);
const response = await rp({
url: 'https://slack.com/api/oauth.access',
qs: {
// eslint-disable-next-line camelcase
client_id: clientId,
// eslint-disable-next-line camelcase
client_secret: clientSecret,
code,
// eslint-disable-next-line camelcase
redirect_uri: callbackUrl
},
json: true
});
if (response.error) {
args.push({ err: response.error, provider });
return fn(req, res, ...args);
}
const accessToken = response.access_token;
const user = await rp({
url: 'https://slack.com/api/users.identity',
qs: {
token: accessToken
},
json: true
});
const result = {
provider,
accessToken,
info: user
};
args.push({ result });
return fn(req, res, ...args);
} catch (err) {
args.push({ err, provider });
return fn(req, res, ...args);
}
}
return fn(req, res, ...args)
}
};
module.exports = microAuthSlack;