2023-02-28 12:49:06 -07:00
|
|
|
import { Builder, Browser, By, Key, until } from "selenium-webdriver";
|
|
|
|
import { Cookie } from "tough-cookie";
|
|
|
|
|
2023-02-14 17:37:40 -07:00
|
|
|
import {
|
2023-02-28 12:49:06 -07:00
|
|
|
AGGIETIME_AUTH_COOKIE_NAME,
|
|
|
|
AGGIETIME_DOMAIN,
|
2023-02-14 17:37:40 -07:00
|
|
|
AGGIETIME_URI,
|
2023-02-28 12:49:06 -07:00
|
|
|
AGGIETIME_URL_CONTAINS_SIGNIFIES_AUTH_COMPLETE,
|
2023-02-14 17:37:40 -07:00
|
|
|
LOGIN_PATH,
|
2023-02-28 12:49:06 -07:00
|
|
|
SAML_SIGN_IN_TITLE,
|
|
|
|
SAML_SUBMIT_SELECTOR,
|
|
|
|
SAML_EMAIL_SELECTOR,
|
|
|
|
SAML_PASSWORD_SELECTOR,
|
2023-02-14 17:37:40 -07:00
|
|
|
} from "./constants.js";
|
2023-02-28 12:49:06 -07:00
|
|
|
import { jar } from "./axios_client.js";
|
2023-02-15 18:03:46 -07:00
|
|
|
import * as aggietime from "./aggietime.js";
|
2023-02-14 17:37:40 -07:00
|
|
|
|
2023-02-15 18:03:46 -07:00
|
|
|
export const refresh_jwt = () => {
|
|
|
|
console.log("Refreshing JWT...");
|
|
|
|
|
|
|
|
return aggietime.get_user_info();
|
|
|
|
};
|
|
|
|
|
2023-10-11 11:06:35 -06:00
|
|
|
export const setCookie = (jwt) =>
|
|
|
|
jar.setCookie(`${AGGIETIME_AUTH_COOKIE_NAME}=${jwt}`, AGGIETIME_URI);
|
|
|
|
|
2023-02-15 18:03:46 -07:00
|
|
|
export const logout = () => client.get(`${AGGIETIME_URI}/${LOGOUT_PATH}`);
|
2023-02-15 11:04:32 -07:00
|
|
|
|
2023-02-28 12:49:06 -07:00
|
|
|
export const login = async (a_number, password) => {
|
|
|
|
const driver = await new Builder().forBrowser(Browser.CHROME).build();
|
|
|
|
let cookie;
|
|
|
|
|
|
|
|
try {
|
|
|
|
console.log("Navigating to login path...");
|
|
|
|
await driver.get(`${AGGIETIME_URI}/${LOGIN_PATH}`);
|
|
|
|
|
|
|
|
if (a_number && password) {
|
|
|
|
console.log("Waiting until we eventually redirect to SAML...");
|
|
|
|
await driver.wait(until.titleIs(SAML_SIGN_IN_TITLE));
|
|
|
|
|
|
|
|
console.log("Waiting until email field is located...");
|
|
|
|
await driver.wait(until.elementLocated(By.css(SAML_EMAIL_SELECTOR)));
|
|
|
|
|
|
|
|
console.log("Filling email field...");
|
|
|
|
await driver
|
|
|
|
.findElement(By.css(SAML_EMAIL_SELECTOR))
|
|
|
|
.sendKeys(`${a_number}@usu.edu`);
|
|
|
|
await driver.findElement(By.css(SAML_SUBMIT_SELECTOR)).click();
|
|
|
|
|
|
|
|
console.log("Waiting until password field is located...");
|
|
|
|
await Promise.all(
|
|
|
|
[SAML_PASSWORD_SELECTOR, SAML_SUBMIT_SELECTOR].map((selector) =>
|
2023-10-11 11:06:35 -06:00
|
|
|
driver.wait(until.elementLocated(By.css(selector))),
|
|
|
|
),
|
2023-02-28 12:49:06 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
console.log("Filling password...");
|
|
|
|
await driver
|
|
|
|
.findElement(By.css(SAML_PASSWORD_SELECTOR))
|
|
|
|
.sendKeys(password);
|
|
|
|
|
|
|
|
console.log("Debouncing a bit...");
|
|
|
|
await new Promise((res) => setTimeout(res, 500));
|
|
|
|
|
|
|
|
console.log("Submit!");
|
2023-03-24 15:44:30 -06:00
|
|
|
await driver
|
|
|
|
.wait(until.elementLocated(By.css(SAML_SUBMIT_SELECTOR)))
|
|
|
|
.then(() => driver.findElement(By.css(SAML_SUBMIT_SELECTOR)).click());
|
2023-02-28 12:49:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
await driver.wait(
|
2023-10-11 11:06:35 -06:00
|
|
|
until.urlContains(AGGIETIME_URL_CONTAINS_SIGNIFIES_AUTH_COMPLETE),
|
2023-02-28 12:49:06 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
console.log("Retrieving cookie...");
|
|
|
|
cookie = await driver.manage().getCookie(AGGIETIME_AUTH_COOKIE_NAME);
|
|
|
|
|
|
|
|
await jar.setCookie(
|
|
|
|
new Cookie({
|
|
|
|
...cookie,
|
|
|
|
key: cookie.name,
|
|
|
|
}),
|
2023-10-11 11:06:35 -06:00
|
|
|
AGGIETIME_URI,
|
2023-02-15 11:04:32 -07:00
|
|
|
);
|
2023-02-28 12:49:06 -07:00
|
|
|
console.log("Got it!");
|
|
|
|
} finally {
|
|
|
|
await driver.quit();
|
|
|
|
}
|
2023-02-14 17:37:40 -07:00
|
|
|
|
2023-02-28 12:49:06 -07:00
|
|
|
return cookie;
|
2023-02-14 17:37:40 -07:00
|
|
|
};
|