This repository has been archived by the owner on Aug 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
137 lines (121 loc) · 4.48 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
import request from "request";
import randomstring from "randomstring";
class CoSign {
/**
*
* @param {Object} config Holds the user info
* @param {String} config.user Username
* @param {String} config.pass Password
*/
constructor(config) {
this.config = config;
this.tokenGenerated = false;
}
/**
* Generates a User Token
* @returns {Promise} Token in a String
*/
getUserToken() {
return new Promise((resolve, reject) => {
_cosignCookie().then(d => {
_cosignLogin(this.config, d).then(c => {
_getJWT("https://portal.lancaster.ac.uk/portal/api/profile", c).then(p => {
this.tokenGenerated = true;
this.config.token = p[0];
this.config.cookie = p[1];
resolve(p[0]);
}).catch(e => {
reject(e);
});
}).catch(e => {
reject(e);
});
}).catch(e => {
reject(e);
});
});
}
/**
* The token of the user
* Must have run getUserToken() Beforehand
*/
get token() {
return (this.tokenGenerated) ? this.config.token : new Error("Token not Generated... Please use getUserToken()");
}
/**
* The CoSign cookie for the user
* Must have run getUserToken() Beforehand
*/
get cookie() {
return (this.tokenGenerated) ? this.config.cookie : new Error("Cookie not Generated... Please use getUserToken()");
}
}
function _cosignCookie() {
return new Promise((resolve, reject) => {
const uri = "https://weblogin.lancs.ac.uk/login/?cosign-https-portal.lancaster.ac.uk&https://portal.lancaster.ac.uk/student_portal";
request(uri, (err, resp, body) => {
if (err) reject(err);
resolve(resp.headers["set-cookie"]);
});
});
}
function _cosignLogin(config, cookie) {
return new Promise((resolve, reject) => {
const user = config.user;
const pass = config.pass;
const loginUrl = "https://weblogin.lancs.ac.uk/login/";
const opts = {
method: "POST",
url: loginUrl,
headers: {
"Cookie": cookie
},
form: {
required: "",
ref: "https://myaccount.lancs.ac.uk",
service: "cosign-https-myaccount.lancs.ac.uk",
state: "login",
login: user,
password: pass,
otp: "",
doLogin: "Login"
},
};
request(opts, (err, resp, body) => {
if (err) reject(err);
resolve(resp.headers["set-cookie"]);
});
});
}
function _getJWT(t, cookie) {
return new Promise((resolve, reject) => {
const url = `https://cisweb.lancaster.ac.uk/jwt-proxy-auth?issuer=DSP&return_to=`;
const e = randomstring.generate({
length: 50,
charset: "alphabetic"
});
const n = `${url}${t}&state=${e}`;
request(n, { followRedirect: false, headers: { "Cookie": cookie } }, (err, resp, body) => {
if (err) reject(err);
request(resp.headers["location"], { followRedirect: false, headers: { "Cookie": cookie } }, (err, resp, body) => {
if (err) reject(err);
request("https://weblogin.lancs.ac.uk" + resp.headers["location"], { followRedirect: false, headers: { "Cookie": cookie } }, (err, resp, body) => {
if (err) reject(err);
const sc = resp.headers["set-cookie"];
request(resp.headers["location"], { followRedirect: false, headers: { "Cookie": sc } }, (err, resp, body) => {
if (err) reject(err);
const sc = resp.headers["set-cookie"].toString().replace("\u0000", "");
request(n, { followRedirect: false, headers: { "Cookie": sc } }, (err, resp, body) => {
if (err) reject(err);
console.log(sc);
const u = new URL(resp.headers["location"].toString());
const c = u.searchParams.get("jwt");
resolve([c, sc]);
});
});
});
});
})
});
}
module.exports = CoSign;