This repository was archived by the owner on Jan 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
200 lines (182 loc) · 5.9 KB
/
Copy pathindex.js
File metadata and controls
200 lines (182 loc) · 5.9 KB
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
const axios = require("axios");
const EventEmitter = require("events").EventEmitter;
const eventManager = new EventEmitter();
const { User } = require("./utils/user");
class Client {
constructor(options) {
var instance = this;
EventEmitter.call(instance);
this.eventManager = eventManager;
if (!options.url) throw new Error('"url" cannot be undefined');
if (!options.username && !options.svcToken)
throw new Error('"username" cannot be undefined');
if (!options.password && !options.svcToken)
throw new Error('"password" cannot be undefined');
this.url = options.url;
this.username = options.username;
this.password = options.password;
this.svcToken = options.svcToken;
this.clientData = {
url: this.url,
username: this.username,
password: this.password,
};
var default_http = {
User_Agent:
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36",
};
this.http = options.http ? options.http : default_http;
this.httpInstance = axios.create({
baseURL: "https://" + this.url,
headers: { "User-Agent": this.http.User_Agent, origin: this.url },
});
this.defaultAxios = axios.create();
/* Try Login Details */
if (this.svcToken) {
var getLoginToken = this.defaultAxios
.post("https://sts-sso.myschoolapp.com/oauth2/token", null, {
headers: {
"User-Agent": this.http.User_Agent,
origin: this.url,
Cookie: `AuthSvcToken=${this.svcToken}`,
"content-type": "application/json",
"x-csrf": "token_needed",
},
})
.catch(function (error) {
if (error.response.status == 401) {
throw new Error("Invalid login details provided");
} else {
throw error;
}
});
this.loggedIn = false;
(async () => {
let getLoginTokenData = await getLoginToken;
if (!getLoginTokenData.data || !getLoginTokenData.data.access_token) {
this.loggedIn = false;
throw new Error("Invalid login details provided");
}
this.bbidToken = getLoginTokenData.data.access_token;
var login = this.httpInstance
.get("/api/bbid/login", {
headers: {
"User-Agent": this.http.User_Agent,
origin: this.url,
Authorization: `Bearer ${this.bbidToken}`,
},
})
.catch(function (error) {
throw error;
});
let loginData = await login;
let res = loginData.data;
this.login_tokens = loginData.headers["set-cookie"];
for (let i of loginData.headers["set-cookie"]) {
if (i.startsWith("t")) {
this.token = i.split(";")[0].split("t=")[1];
break;
}
}
if (!res.LoginSuccessful) {
this.loggedIn = false;
throw new Error("Invalid login details provided");
} else {
var fetchToken = this.httpInstance
.get("/api/webapp/context", {
headers: {
"User-Agent": this.http.User_Agent,
origin: this.url,
Cookie: this.login_tokens,
},
})
.catch(function (error) {
throw error;
});
let tokenData = await fetchToken;
let tokenRes = tokenData.data;
if (tokenRes.MasterUserInfo) {
this.loggedIn = true;
this.user = new User(this, tokenRes.MasterUserInfo);
eventManager.emit("ready", this);
} else {
}
}
})();
} else {
var login = this.httpInstance
.post("/api/SignIn", {
From: "",
Username: this.username,
Password: this.password,
InterfaceSource: "WebApp",
})
.catch(function (error) {
throw error;
});
this.loggedIn = false;
(async () => {
let loginData = await login;
let res = loginData.data;
this.login_tokens = loginData.headers["set-cookie"];
for (let i of loginData.headers["set-cookie"]) {
if (i.startsWith("t")) {
this.token = i.split(";")[0].split("t=")[1];
break;
}
}
if (!res.LoginSuccessful) {
this.loggedIn = false;
throw new Error("Invalid login details provided");
} else if (res.PasswordExpired) {
this.loggedIn = false;
throw new Error(
"Passsword provided is expired. Set a new one by logging in at https://" +
this.url
);
} else {
var fetchToken = this.httpInstance
.get("/api/webapp/context", {
headers: {
"User-Agent": this.http.User_Agent,
origin: this.url,
Cookie: this.login_tokens,
},
})
.catch(function (error) {
throw error;
});
this.authroziationHeader = { Cookie: this.login_tokens };
let tokenData = await fetchToken;
let tokenRes = tokenData.data;
if (tokenRes.MasterUserInfo) {
this.loggedIn = true;
this.user = new User(this, tokenRes.MasterUserInfo);
eventManager.emit("ready", this);
} else {
}
}
})();
}
/* Require Files */
this.functions = require("./utils/functions/index");
this.UserManager = require("./src/users");
this.SchoolManager = require("./src/schools");
this.ClassManager = require("./src/classes");
}
on(event, f) {
try {
eventManager.on(event, f);
} catch (err) {
return err;
}
}
once(event, f) {
try {
eventManager.once(event, f);
} catch (err) {
return err;
}
}
}
module.exports = { Client };