-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthentication.js
138 lines (122 loc) · 3.64 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const { getAPIURL, getFrontendURL } = require('./environments');
const scopes = require('./scope');
const test = async (z, bundle) => {
const options = {
url: `${getAPIURL(bundle.inputData.environment || bundle.authData.environment)}/v1/oauth2/userinfo`,
method: 'GET',
headers: {},
params: {},
removeMissingValuesFrom: {
body: false,
params: false,
},
};
return z.request(options).then((response) => {
const results = response.json;
// You can do any parsing you need for results here before returning them
return results;
});
};
const authorizeUrl = async (z, bundle) => {
const url = `${getFrontendURL(bundle.inputData.environment || bundle.authData.environment)}/oauth2/authorize?sub_type=organization&client_id=${
process.env.CLIENT_ID
}&state=${bundle.inputData.state}&redirect_uri=${encodeURIComponent(
bundle.inputData.redirect_uri,
)}&response_type=code`;
return url;
};
const getAccessToken = async (z, bundle) => {
const options = {
url: `${getAPIURL(bundle.inputData.environment || bundle.authData.environment)}/v1/oauth2/token`,
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded',
accept: 'application/json',
},
params: {},
body: {
code: bundle.inputData.code,
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
grant_type: 'authorization_code',
redirect_uri: bundle.inputData.redirect_uri,
code_verifier: bundle.inputData.code_verifier,
},
removeMissingValuesFrom: {
body: false,
params: false,
},
};
return z.request(options).then((response) => {
const results = response.json;
// You can do any parsing you need for results here before returning them
return results;
});
};
const refreshAccessToken = async (z, bundle) => {
const options = {
url: `${getAPIURL(bundle.inputData.environment || bundle.authData.environment)}/v1/oauth2/token`,
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded',
accept: 'application/json',
},
params: {},
body: {
refresh_token: bundle.authData.refresh_token,
grant_type: 'refresh_token',
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
},
removeMissingValuesFrom: {
body: false,
params: false,
},
};
return z.request(options).then((response) => {
const results = response.json;
// You can do any parsing you need for results here before returning them
return results;
});
};
const connectionLabel = async (z, bundle) => {
const environment = bundle.inputData.environment || bundle.authData.environment;
const isSandbox = environment === 'sandbox';
return `${isSandbox ? '[SANDBOX] ' : ''}${bundle.inputData.name}`;
};
module.exports = {
type: 'oauth2',
test: test,
fields: [
{
choices: [
{
label: 'Production',
sample: 'production',
value: 'production',
},
{
label: 'Sandbox',
sample: 'sandbox',
value: 'sandbox',
},
],
default: 'production',
computed: false,
key: 'environment',
required: true,
label: 'Environment',
type: 'string',
helpText: 'Use the sandbox environment for testing and development. Read more: https://docs.polar.sh/sandbox',
},
],
oauth2Config: {
authorizeUrl: authorizeUrl,
getAccessToken: getAccessToken,
refreshAccessToken: refreshAccessToken,
enablePkce: true,
scope: scopes.join(' '),
autoRefresh: true,
},
connectionLabel: connectionLabel,
};