-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
233 lines (193 loc) · 6.95 KB
/
index.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import path from "path";
import fs from "fs";
import { fileURLToPath } from "url";
import express from "express";
import axios from "axios";
import cookieSession from "cookie-session";
import bodyParser from 'body-parser';
const urlencodedParser = bodyParser.urlencoded({ extended: false })
let app = express();
app.use(
cookieSession({
name: "aps_session",
keys: ["aps_secure_key"],
maxAge: 24 * 60 * 60 * 1000 // 24 hours
})
);
let clientId = process.env.APS_CLIENT_ID || "YOUR CLIENT ID";
let clientSecret = process.env.APS_CLIENT_SECRET || "YOUR CLIENT SECRET";
let serverPort = process.env.PORT || 3000;
let serverUrl = process.env.BASE_URL || "http://localhost:" + serverPort;
let callbackUrl = process.env.APS_CALLBACK_URL || `${serverUrl}/callback/oauth`;
const apsUrl = process.env.APS_BASE_URL || "https://developer.api.autodesk.com";
let dataEndpoint = process.env.APS_DATA_ENDPOINT;
app.get("/callback/oauth", async (req, res) => {
console.log("/callback/oauth", req.session);
const { code, state } = req.query;
try {
let cId = req.session.client_id ? req.session.client_id : clientId;
let cSecret = req.session.client_secret ? req.session.client_secret : clientSecret;
let cApsUrl = req.session.apsUrl ? req.session.apsUrl : apsUrl;
const clientIdSecret = btoa(`${cId}:${cSecret}`);
const response = await axios({
method: "POST",
url: `${cApsUrl}/authentication/v2/token`,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic " + clientIdSecret
},
data: `grant_type=authorization_code&code=${code}&redirect_uri=${callbackUrl}`
});
req.session = req.session || {};
req.session.access_token = response.data.access_token;
req.session.refresh_token = response.data.refresh_token;
res.redirect(`/` + (state ? `${state}` : ""));
} catch (error) {
console.log(error);
res.end();
}
});
app.get("/oauth/token2LO", async (req, res) => {
console.log("/oauth/token2LO", req.session);
let cId = req.session.client_id ? req.session.client_id : clientId;
let cSecret = req.session.client_secret ? req.session.client_secret : clientSecret;
let cApsUrl = req.session.apsUrl ? req.session.apsUrl : apsUrl;
const clientIdSecret = btoa(`${cId}:${cSecret}`);
const response = await axios({
method: "POST",
url: `${cApsUrl}/authentication/v2/token`,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic " + clientIdSecret
},
data: `grant_type=client_credentials&scope=data:read data:write data:create data:search`
});
res.end(response.data.access_token);
})
app.get("/oauth/token", async (req, res) => {
console.log("/oauth/token", req.session);
if (req.query.refresh) {
try {
let cId = req.session.client_id ? req.session.client_id : clientId;
let cSecret = req.session.client_secret ? req.session.client_secret : clientSecret;
let cApsUrl = req.session.apsUrl ? req.session.apsUrl : apsUrl;
let rToken = req.session.refresh_token;
const clientIdSecret = btoa(`${cId}:${cSecret}`);
const response = await axios({
method: "POST",
url: `${cApsUrl}/authentication/v2/token`,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic " + clientIdSecret
},
data: `grant_type=refresh_token&refresh_token=${rToken}`
});
req.session = req.session || {};
req.session.access_token = response.data.access_token;
req.session.refresh_token = response.data.refresh_token;
res.end(response.data.access_token);
return;
} catch (err) {
// If refresh failed
delete req.session.access_token;
delete req.session.refresh_token;
res.status(400).end();
return;
}
}
let access_token = req.session?.access_token;
if (!access_token) {
res.status(401).end();
return;
}
res.end(access_token);
});
app.get("/oauth/url", (req, res) => {
console.log("/oauth/url", req.session);
let cId = req.session.client_id ? req.session.client_id : clientId;
let cApsUrl = req.session.apsUrl ? req.session.apsUrl : apsUrl;
const url =
cApsUrl +
"/authentication/v2/authorize?response_type=code" +
"&client_id=" +
cId +
"&redirect_uri=" +
callbackUrl +
"&scope=data:read data:write data:create data:search";
res.end(url);
});
app.get("/", (req, res) => {
let cDataEndpoint = req.session.dataEndpoint ? req.session.dataEndpoint : dataEndpoint;
const fileName = cDataEndpoint.includes("/fusiondata/") ? "index-mono.html" : "index.html";
fs.readFile(path.dirname(fileURLToPath(import.meta.url)) + '/public/' + fileName, 'utf8', (err, text) => {
text = text.replace("%APS_DATA_ENDPOINT%", cDataEndpoint);
res.send(text);
});
});
app.get("/voyager", (req, res) => {
let cDataEndpoint = req.session.dataEndpoint ? req.session.dataEndpoint : dataEndpoint;
fs.readFile(path.dirname(fileURLToPath(import.meta.url)) + '/public/voyager.html', 'utf8', (err, text) => {
text = text.replace("%APS_DATA_ENDPOINT%", cDataEndpoint);
res.send(text);
});
});
app.get("/credentials", (req, res) => {
fs.readFile(path.dirname(fileURLToPath(import.meta.url)) + '/public/credentials.html', 'utf8', (err, text) => {
text = text.replace(/\%CALLBACK_URL\%/g, callbackUrl);
res.send(text);
});
});
app.post("/credentials", urlencodedParser, (req, res) => {
req.session.client_id = req.body.clientId;
req.session.client_secret = req.body.clientSecret;
req.session.dataEndpoint = req.body.gqlUrl || dataEndpoint;
req.session.apsUrl = req.body.baseUrl || apsUrl;
delete req.session.access_token;
delete req.session.refresh_token;
if (req.body.clientId === "") {
delete req.session.client_id;
delete req.session.client_secret;
delete req.session.dataEndpoint;
delete req.session.apsUrl;
}
if (req.originalUrl.includes("/credentials"))
res.redirect("/");
else
res.end();
});
app.get("/userprofile", async (req, res) => {
try {
let env = "";
if (req.session.apsUrl) {
if (req.session.apsUrl.includes("-stg")) {
env = "-stg";
} else if (req.session.apsUrl.includes("-dev")) {
env = "-dev";
}
}
const response = await axios({
method: "POST",
url: `https://api.userprofile${env}.autodesk.com/userinfo`,
headers: {
"Authorization": "Bearer " + req.session.access_token
}
});
res.json(response.data);
} catch (error) {
res.status(500).end();
}
});
app.get("/logout", async (req, res) => {
let cApsUrl = req.session.apsUrl ? req.session.apsUrl : apsUrl;
req.session = null;
res.end(`${cApsUrl}/authentication/v2/logout`);
});
app.use(
express.static(
path.join(path.dirname(fileURLToPath(import.meta.url)), "public")
)
);
app.listen(serverPort);
console.log(
`Open ${serverUrl} in a web browser in order to log in with your Autodesk account!`
);