This repository has been archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathauthentication.js
87 lines (79 loc) · 2.56 KB
/
authentication.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
const querystring = require('querystring');
const _ = require('lodash');
const REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token';
const AUTHORIZE_URL = 'https://api.twitter.com/oauth/authorize';
const ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token';
const getRequestToken = async (z, bundle) => {
const response = await z.request({
url: REQUEST_TOKEN_URL,
method: 'POST',
auth: {
oauth_consumer_key: process.env.CLIENT_ID,
oauth_consumer_secret: process.env.CLIENT_SECRET,
oauth_signature_method: 'HMAC-SHA1',
oauth_callback: bundle.inputData.redirect_uri,
oauth_version: '1.0' // Twitter says this should be 1.0
}
});
response.throwForStatus();
return querystring.parse(response.content);
};
const getAccessToken = async (z, bundle) => {
const response = await z.request({
url: ACCESS_TOKEN_URL,
method: 'POST',
auth: {
oauth_consumer_key: process.env.CLIENT_ID,
oauth_consumer_secret: process.env.CLIENT_SECRET,
oauth_token: bundle.inputData.oauth_token,
oauth_token_secret: bundle.inputData.oauth_token_secret,
oauth_verifier: bundle.inputData.oauth_verifier
}
});
response.throwForStatus();
return querystring.parse(response.content);
};
const config = {
type: 'oauth1',
oauth1Config: {
// We have to define getRequestToken and getAccessToken functions to explicitly
// parse the response like it has a form body here, since Twitter responds
// 'text/html' for the Content-Type header
getRequestToken,
getAccessToken,
authorizeUrl: {
url: AUTHORIZE_URL,
params: {
oauth_token: '{{bundle.inputData.oauth_token}}'
}
}
},
test: {
url: 'https://api.twitter.com/1.1/account/settings.json'
},
connectionLabel: '{{screen_name}}'
};
// A middleware that is run before z.request() actually makes the request. Here we're
// adding necessary OAuth1 parameters to `auth` property of the request object.
const includeAccessToken = (req, z, bundle) => {
if (
bundle.authData &&
bundle.authData.oauth_token &&
bundle.authData.oauth_token_secret
) {
// Just put your OAuth1 credentials in req.auth, Zapier will sign the request for
// you.
req.auth = req.auth || {};
_.defaults(req.auth, {
oauth_consumer_key: process.env.CLIENT_ID,
oauth_consumer_secret: process.env.CLIENT_SECRET,
oauth_token: bundle.authData.oauth_token,
oauth_token_secret: bundle.authData.oauth_token_secret
});
}
return req;
};
module.exports = {
config,
includeAccessToken
};