2023-08-30 12:28:49 +01:00
|
|
|
import { API_BASE_STUDENT, BASE_URL } from "../utils/consts.ts";
|
|
|
|
|
import { BaseClient } from "./baseClient.ts";
|
|
|
|
|
import { parseCookies } from "../utils/utils.ts";
|
2023-04-16 20:47:40 +01:00
|
|
|
|
2022-03-12 11:30:03 +00:00
|
|
|
/**
|
2023-04-07 13:47:08 +01:00
|
|
|
* Student Client
|
2022-03-12 11:30:03 +00:00
|
|
|
*/
|
2023-04-16 20:47:40 +01:00
|
|
|
export class StudentClient extends BaseClient {
|
|
|
|
|
/**
|
2023-05-14 00:59:21 +01:00
|
|
|
* @property studentCode ClassCharts student code
|
2023-04-16 20:47:40 +01:00
|
|
|
* @internal
|
|
|
|
|
*/
|
|
|
|
|
private studentCode = "";
|
|
|
|
|
/**
|
|
|
|
|
* @property dateOfBirth Student's date of birth
|
|
|
|
|
* @internal
|
|
|
|
|
*/
|
|
|
|
|
private dateOfBirth = "";
|
2022-03-12 11:30:03 +00:00
|
|
|
|
|
|
|
|
/**
|
2023-05-14 00:59:21 +01:00
|
|
|
* @param studentCode ClassCharts student code
|
2022-03-12 11:30:03 +00:00
|
|
|
* @param dateOfBirth Student's date of birth
|
|
|
|
|
*/
|
2023-04-16 15:13:42 +01:00
|
|
|
constructor(studentCode: string, dateOfBirth?: string) {
|
|
|
|
|
super(API_BASE_STUDENT);
|
2022-03-12 11:30:03 +00:00
|
|
|
this.studentCode = String(studentCode);
|
|
|
|
|
this.dateOfBirth = String(dateOfBirth);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-05-14 00:59:21 +01:00
|
|
|
* Authenticates with ClassCharts
|
2022-03-12 11:30:03 +00:00
|
|
|
*/
|
|
|
|
|
async login(): Promise<void> {
|
2023-08-30 12:28:49 +01:00
|
|
|
if (!this.studentCode) throw new Error("Student Code not provided");
|
2022-03-12 11:30:03 +00:00
|
|
|
const formData = new URLSearchParams();
|
|
|
|
|
formData.append("_method", "POST");
|
|
|
|
|
formData.append("code", this.studentCode.toUpperCase());
|
|
|
|
|
formData.append("dob", this.dateOfBirth);
|
|
|
|
|
formData.append("remember_me", "1");
|
2023-05-14 00:59:21 +01:00
|
|
|
formData.append("recaptcha-token", "no-token-available");
|
2023-05-30 18:37:24 +01:00
|
|
|
const request = await fetch(BASE_URL + "/student/login", {
|
2022-03-12 11:30:03 +00:00
|
|
|
method: "POST",
|
2023-04-16 15:13:42 +01:00
|
|
|
body: formData,
|
|
|
|
|
redirect: "manual",
|
2022-03-12 11:30:03 +00:00
|
|
|
});
|
2023-04-16 15:13:42 +01:00
|
|
|
if (request.status != 302 || !request.headers.get("set-cookie")) {
|
2023-08-30 12:28:49 +01:00
|
|
|
await request.body?.cancel(); // Make deno tests happy by closing the body, unsure whether this is needed for the actual library
|
2023-04-16 15:13:42 +01:00
|
|
|
throw new Error(
|
2023-08-30 12:28:49 +01:00
|
|
|
"Unauthenticated: ClassCharts didn't return authentication cookies"
|
2023-04-16 15:13:42 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
const cookies = String(request.headers.get("set-cookie"));
|
|
|
|
|
this.authCookies = cookies.split(",");
|
2022-08-03 16:53:46 +01:00
|
|
|
const sessionCookies = parseCookies(cookies);
|
|
|
|
|
const sessionID = JSON.parse(
|
|
|
|
|
String(sessionCookies["student_session_credentials"])
|
2022-03-12 11:30:03 +00:00
|
|
|
);
|
|
|
|
|
this.sessionId = sessionID.session_id;
|
2023-04-07 12:43:46 +01:00
|
|
|
await this.getNewSessionId();
|
2022-03-12 11:30:03 +00:00
|
|
|
const user = await this.getStudentInfo();
|
2023-04-07 13:47:08 +01:00
|
|
|
this.studentId = user.data.user.id;
|
2022-03-12 11:30:03 +00:00
|
|
|
}
|
|
|
|
|
}
|