-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathauthentication.js
More file actions
89 lines (78 loc) · 3.21 KB
/
authentication.js
File metadata and controls
89 lines (78 loc) · 3.21 KB
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
const getAccessToken = async (z, bundle) => {
const response = await z.request({
url: 'https://github.com/login/oauth/access_token',
method: 'POST',
body: {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
grant_type: 'authorization_code',
code: bundle.inputData.code,
// Extra data can be pulled from the querystring. For instance:
// 'accountDomain': bundle.cleanedRequest.querystring.accountDomain
},
headers: {
'content-type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
},
});
// If you're using core v9.x or older, you should call response.throwForStatus()
// or verify response.status === 200 before you continue.
// This function should return `access_token`.
// If your app does an app refresh, then `refresh_token` should be returned here
// as well
return {
access_token: response.data.access_token,
};
};
// This function runs before every outbound request. You can have as many as you
// need. They'll need to each be registered in your index.js file.
const includeBearerToken = (request, z, bundle) => {
if (bundle.authData.access_token) {
request.headers.Authorization = `Bearer ${bundle.authData.access_token}`;
}
return request;
};
// 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. eg: `/me`.
// By returning the entire request object, you have access to the request and
// response data for testing purposes. Your connection label can access any data
// from the returned response using the `json.` prefix. eg: `{{json.username}}`.
const test = async (z, bundle) => {
const response = await z.request({ url: 'https://api.github.com/user' });
return response;
}
module.exports = {
config: {
// OAuth2 is a web authentication standard. There are a lot of configuration
// options that will fit most any situation.
type: 'oauth2',
oauth2Config: {
authorizeUrl: {
url: 'https://github.com/login/oauth/authorize',
params: {
client_id: '{{process.env.CLIENT_ID}}',
state: '{{bundle.inputData.state}}',
redirect_uri: '{{bundle.inputData.redirect_uri}}',
response_type: 'code',
},
},
getAccessToken,
},
// Define any input app's auth requires here. The user will be prompted to enter
// this info when they connect their account.
fields: [],
// The test method allows Zapier to verify that the credentials a user provides
// are valid. We'll execute this method whenever a user connects their account for
// the first time.
test,
// This template string can access all the data returned from the auth test. If
// you return the test object, you'll access the returned data with a label like
// `{{json.X}}`. If you return `response.data` from your test, then your label can
// be `{{X}}`. This can also be a function that returns a label. That function has
// the standard args `(z, bundle)` and data returned from the test can be accessed
// in `bundle.inputData.X`.
connectionLabel: '{{json.login}}',
},
befores: [includeBearerToken],
afters: [],
};