-
Notifications
You must be signed in to change notification settings - Fork 18
/
authentication.js
109 lines (101 loc) · 3.91 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const getAccessToken = (z, bundle) => {
const promise = z.request(`${process.env.BASE_URL}/oauth/access-token`, {
method: 'POST',
body: {
//extra data pulled from the users query string
accountDomain: bundle.cleanedRequest.querystring.accountDomain,
code: bundle.inputData.code,
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
grant_type: 'authorization_code'
},
headers: {
'content-type': 'application/x-www-form-urlencoded'
}
});
// Needs to return at minimum, `access_token`, and if your app also does refresh, then `refresh_token` too
return promise.then((response) => {
if (response.status !== 200) {
throw new Error('Unable to fetch access token: ' + response.content);
}
const result = JSON.parse(response.content);
return {
access_token: result.access_token,
refresh_token: result.refresh_token
};
});
};
const refreshAccessToken = (z, bundle) => {
const promise = z.request(`${process.env.BASE_URL}/oauth/refresh-token`, {
method: 'POST',
body: {
refresh_token: bundle.authData.refresh_token,
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
grant_type: 'refresh_token'
},
headers: {
'content-type': 'application/x-www-form-urlencoded'
}
});
// Needs to return `access_token`. If the refresh token stays constant, can skip it. If it changes, can
// return it here to update the user's auth on Zapier.
return promise.then((response) => {
if (response.status !== 200) {
throw new Error('Unable to fetch access token: ' + response.content);
}
const result = JSON.parse(response.content);
return {
access_token: result.access_token
};
});
};
const testAuth = (z /*, bundle*/) => {
// Normally you want to make a request to an endpoint that is either specifically designed to test auth, or one that
// every user will have access to, such as an account or profile endpoint like /me.
const promise = z.request({
method: 'GET',
url: `${process.env.BASE_URL}/me`,
});
// This method can return any truthy value to indicate the credentials are valid.
// Raise an error to show
return promise.then((response) => {
if (response.status === 401) {
throw new Error('The access token you supplied is not valid');
}
return z.JSON.parse(response.content);
});
};
module.exports = {
type: 'oauth2',
oauth2Config: {
// Step 1 of the OAuth flow; specify where to send the user to authenticate with your API.
// Zapier generates the state and redirect_uri, you are responsible for providing the rest.
// Note: can also be a function that returns a string
authorizeUrl: {
url: `${process.env.BASE_URL}/oauth/authorize`,
params: {
client_id: '{{process.env.CLIENT_ID}}',
state: '{{bundle.inputData.state}}',
redirect_uri: '{{bundle.inputData.redirect_uri}}',
response_type: 'code'
}
},
// Step 2 of the OAuth flow; Exchange a code for an access token.
// This could also use the request shorthand.
getAccessToken: getAccessToken,
// (Optional) If the access token expires after a pre-defined amount of time, you can implement
// this method to tell Zapier how to refresh it.
refreshAccessToken: refreshAccessToken,
// If you want Zapier to automatically invoke `refreshAccessToken` on a 401 response, set to true
autoRefresh: true
// If there is a specific scope you want to limit your Zapier app to, you can define it here.
// Will get passed along to the authorizeUrl
// scope: 'read,write'
},
// The test method allows Zapier to verify that the access token is valid. We'll execute this
// method after the OAuth flow is complete to ensure everything is setup properly.
test: testAuth,
// assuming "username" is a key returned from the test
connectionLabel: '{{username}}'
};