This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
authentication.js
68 lines (62 loc) · 2.52 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
/*
Zapier App to automate ERPNext.
Copyright(C) 2019 Raffael Meyer <raffael@alyf.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
'use strict';
const {
CLIENT_ID,
CLIENT_SECRET,
OAUTH_TOKEN_METHOD,
OAUTH_TEST_METHOD,
} = require('./constants');
const { getAuthorizeRequest, getMethod, postMethod } = require('./api');
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
authorizeUrl: getAuthorizeRequest(),
// Step 2 of the OAuth flow; Exchange a code for an access token.
// This could also use the request shorthand.
getAccessToken: z => {
return postMethod(z, OAUTH_TOKEN_METHOD, {
code: '{{bundle.inputData.code}}',
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
grant_type: 'authorization_code',
redirect_uri: '{{bundle.inputData.redirect_uri}}',
}).then(response => {
return response;
});
},
// (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: z => {
return postMethod(z, OAUTH_TOKEN_METHOD, {
refresh_token: '{{bundle.authData.refresh_token}}',
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
grant_type: 'refresh_token',
redirect_uri: '{{bundle.inputData.redirect_uri}}',
});
},
// If you want Zapier to automatically invoke `refreshAccessToken` on a 401 response, set to true
autoRefresh: true,
scope: 'all',
},
// The test method allows Zapier to verify that the access token is valid.Zapier will execute this
// method after the OAuth flow is complete to ensure everything is setup properly.
test: z => getMethod(z, OAUTH_TEST_METHOD),
// OAUTH_TEST_METHOD returns { "message": "user@example.com" }
connectionLabel: '{{message}}',
};