-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
auth.js
55 lines (47 loc) · 1.33 KB
/
auth.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
const crypto = require("crypto");
const Cache = require("./cache");
const fetch = require("node-fetch");
const CONFIG = require("./config.js");
var CACHE_AUTH = new Cache("AUTH", 0.08);
function isJSON(str) {
try {
return (JSON.parse(str) && !!str);
} catch (e) {
return false;
}
}
function getBearerToken(id, cobalt) {
return new Promise((resolve) => {
if (cobalt && cobalt !== "" && !isJSON(`{ "cobalt": "${cobalt}" }`)) {
console.log(`Invalid token for ${id}`);
return null;
} else if (cobalt && cobalt !== "") {
fetch(CONFIG.urls.authService, {
method: "POST",
headers: {
"Content-Type": "application/json",
Cookie: `CobaltSession=${cobalt}`,
},
})
.then((response) => response.json())
.then((data) => {
if (!data.token || !data.token.length) resolve(null);
CACHE_AUTH.add(id, data.token);
resolve(data.token);
});
} else {
console.log("NO COBALT TOKEN");
resolve(null);
//reject('No cobaltID token!');
}
});
}
function getCacheId(value) {
const hash = crypto.createHash("sha256");
hash.update(value);
const cacheId = hash.digest("hex");
return cacheId;
}
exports.CACHE_AUTH = CACHE_AUTH;
exports.getBearerToken = getBearerToken;
exports.getCacheId = getCacheId;