-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth2.js
319 lines (304 loc) · 10.2 KB
/
oauth2.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
const routes = require("express").Router();
const fetch = (...args) =>
import("node-fetch").then(({ default: fetch }) => fetch(...args)); // consider upgrage nodejs to use internal fetch
const fxmlparser = require("fast-xml-parser");
let database = require("../services/database");
const entities = require("html-entities");
let debug = process.env.DEBUG || false;
//Settings
let gsisSetings = {
path: process.env.OAUTH2_LOGIN_PATH,
redirectUrl: process.env.SITE + process.env.OAUTH2_LOGIN_PATH,
scope: "read",
grant_type: "authorization_code",
accessTokenUrl:
process.env.GSIS_SITE +
"/oauth2server" +
process.env.GSIS_GOV +
"/oauth/token",
authorizationUrl:
process.env.GSIS_SITE +
"/oauth2server" +
process.env.GSIS_GOV +
"/oauth/authorize",
profileUrl:
process.env.GSIS_SITE +
"/oauth2server" +
process.env.GSIS_GOV +
"/userinfo",
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
};
////////////////////////////////////////////////////////////
const makeTag = () => {
const length = 10;
let result = "";
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
};
const setTagCookie = (res, tag) => {
const setCookie = "GSIS_TAG=" + tag + "; Secure; path=/;";
res.setHeader("Set-Cookie", setCookie);
};
function getTagcookie(req) {
const cookies = req.headers.cookie;
const cookiesArr = cookies ? cookies.split("; ") : [];
let retCookieVal = "undefined";
cookiesArr.forEach((cookie, _index) => {
const cookieArr = cookie ? cookie.split("=") : [];
if (cookieArr[0] === "GSIS_TAG") retCookieVal = cookieArr[1];
});
return retCookieVal;
}
const makeQuery = (query_obj) => {
let str = [];
for (let p in query_obj)
if (query_obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(query_obj[p]));
}
return str.join("&");
};
const postData = async (url = "", data = "") => {
// Default options are marked with *
const response = await fetch(url, {
method: "POST",
mode: "no-cors",
cache: "no-cache",
credentials: "same-origin", // include, *same-origin, omit
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
redirect: "follow", // manual, *follow, error
referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
body: data, // body data type must match "Content-Type" header
});
return response.json(); // parses JSON response into native JavaScript objects
};
//////////////////////////////////////////////////////////////////
const step4 = (_req, res, userXML) => {
if (debug) console.log("ON STEP 4");
//To keimeno edo exei toso entities oso kai UTF-8 keimeno!!!!!!
//<root><userinfo userid="User068933130 " taxid="068933130 " lastname="ΒΑΒΟΥΛΑ" firstname="ΕΥΤΥΧΙΑ" fathername="ΕΜΜΑΝΟΥΗΛ" mothername="ΑΝΝΑ" birthyear="1950"/></root>
//episis exei kena sta values pou prepei na ta kano trim!!!!!
const options = {
attributeNamePrefix: "",
ignoreAttributes: false,
ignoreNameSpace: true,
parseNodeValue: true,
parseAttributeValue: true,
trimValues: true,
parseTrueNumberOnly: true,
attrValueProcessor: (val, _attrName) =>
he.decode(val, { isAttributeValue: true }),
};
try {
const parser = new fxmlparser.XMLParser(options);
const user = parser.parse(userXML).root.userinfo;
let session = {
taxid: "" + user.taxid, //make it sure taxid is string!
lastname: "" + user.lastname,
firstname: "" + user.firstname,
};
if (debug) console.log("STEP4: ", session);
return session;
} catch (error) {
if (debug) console.log(error);
res
.status(400)
.json(
"ERR: 009 Problem in userinfo from the systems of the General Secretariat for Information Systems."
);
res.end();
return;
}
};
////////////////////////////////////////////////////////////////////
const step2 = async (req, res, GSIS_TAG) => {
if (debug) console.log("ON STEP2");
const querys = req.query;
const code = querys ? (querys.code ? querys.code : undefined) : undefined;
const state = querys ? (querys.state ? querys.state : undefined) : undefined;
const qerror = querys ? (querys.error ? querys.error : undefined) : undefined;
const qerror_description = querys
? querys.error_description
? querys.error_description
: undefined
: undefined;
// step2 success get token with code.
if (typeof code !== "undefined" && typeof state !== "undefined") {
if (debug) console.log("ON STEP2 with code and state", code, state);
//check our tag
if (state !== GSIS_TAG) {
if (debug) console.log("ERR: 002", state, GSIS_TAG);
res
.status(400)
.json(
"ERR: 002 Problem in response to the systems of the General Secretariat for Information Systems."
);
res.end();
return;
}
const data = {
code: "" + code,
redirect_uri: gsisSetings.redirectUrl,
client_id: "" + gsisSetings.clientId,
client_secret: "" + gsisSetings.clientSecret,
scope: "",
grant_type: "authorization_code",
};
const body_content = makeQuery(data);
if (debug) console.log("ON STEP2 before post", body_content);
try {
const tokenjson = await postData(
gsisSetings.accessTokenUrl,
body_content
);
if (debug) console.log("ON STEP2 after post", tokenjson);
if (!tokenjson) {
res
.status(400)
.json(
"ERR: 006 Problem of entering data from the General Secretariat of Information Systems."
);
res.end();
return;
}
if (tokenjson.error) {
if (debug) console.log(tokenjson);
res
.status(400)
.json(
"ERR: 007 Data entry problem by the General Secretariat of Information Systems."
);
res.end();
return;
}
//call step3
if (debug) console.log("ON STEP2 before 3", tokenjson.access_token);
return step3(req, res, tokenjson.access_token);
} catch (error) {
if (debug) console.log(error.message);
res.status(500).json("ERR: 008 Unknown error.");
res.end();
return;
}
} else {
if (qerror) {
res.redirect(
"/?error=" +
qerror +
"&error_description=" +
qerror_description +
"&state=" +
state
);
res.end();
return;
}
// Step 1
const path =
gsisSetings.authorizationUrl +
"?client_id=" +
gsisSetings.clientId +
"&redirect_uri=" +
gsisSetings.redirectUrl +
"&response_type=code&scope=read&state=" +
GSIS_TAG;
if (debug) console.log("ON STEP 1 redirect to gsis server", path, GSIS_TAG);
//const redirectTo = '<html><head><meta http-equiv="Refresh" content="30; URL="'+path+'"></head><a href="'+path+'">'+path+'</a></html>'
res.redirect(path);
res.end();
return;
}
};
////////////////////////////////////////////////////////////
const step3 = async (req, res, access_token) => {
try {
const getuserurl =
gsisSetings.profileUrl + "?format=xml&access_token=" + access_token;
const xml = await fetch(getuserurl);
const userXML = await xml.text();
if (debug) console.log("BEFORE step 4", userXML);
return step4(req, res, userXML);
} catch (error) {
// In case of error gsis sends JSON !!!
if (debug) console.log(error);
res
.status(400)
.json(
"ERR: 005 Data collection problem from the General Secretariat of Information Systems."
);
res.end();
return;
}
};
////////////////////////////////////////////////////////////
const gsis_flow = async (req, res, GSIS_TAG) => {
//Step2 //Step1 //Step3 //Step4
return await step2(req, res, GSIS_TAG);
};
////////////////////////////////////////////////////////////
const gsispa = async (req, res) => {
// set tag cookie if not exist
let GSIS_TAG = "" + getTagcookie(req);
if (GSIS_TAG === undefined || GSIS_TAG === "undefined") {
GSIS_TAG = makeTag();
setTagCookie(res, GSIS_TAG);
if (debug) console.log("Set GSIS_TAG cookie", GSIS_TAG);
}
if (debug) console.log("start oauthpa2 flow");
const userinfo = await gsis_flow(req, res, GSIS_TAG);
if (debug) console.log("GSIS UserInfo", userinfo);
if (userinfo) {
delete req.session.errors;
let user;
try {
user = await database.user.findOne({
where: {
taxId: userinfo.taxid,
},
});
//TODO: refactor catch
} catch (err) {
console.log(err);
res.status(400).send({msg: 'Υπήρξε κάποιο σφάλμα κατά την είσοδο σας στην εφαρμογή. Παρακαλώ δοκιμάστε ξανά.'});
}
if (debug) console.log("App UserInfo", user);
if (user) {
if (!user.role) {
req.session.errors = {
msg: "Δε σας έχει ανατεθεί ρόλος για να περιηγηθείτε στην εφαρμογή.",
};
}
if (!user.agency) {
req.session.errors = {
msg: "Δε σας έχει ανατεθεί φορέας για να περιηγηθείτε στην εφαρμογή.",
};
}
req.session.user = user;
if (debug) console.log("App Session", req.session);
res.redirect(302, "/user_views/dashboard?ref=gsis");
} else {
await database.user.create({
fname: userinfo.firstname,
lname: entities.decode(userinfo.lastname),
taxId: userinfo.taxid,
});
req.session.errors = {
msg: "Δε σας έχει ανατεθεί ρόλος για να περιηγηθείτε στην εφαρμογή.",
};
res.redirect(302, "login");
}
}
res.end();
};
routes.get("/", async function (req, res, next) {
req.session.errors = null;
gsispa(req, res);
});
module.exports = routes;