summaryrefslogtreecommitdiff
path: root/src/session.js
blob: d55b7724c06394b463e2b2ced2e24ed8b3faf3c9 (plain)
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
import { Builder, Browser, By, Key, until } from "selenium-webdriver";
import { Cookie } from "tough-cookie";

import {
  AGGIETIME_AUTH_COOKIE_NAME,
  AGGIETIME_DOMAIN,
  AGGIETIME_URI,
  AGGIETIME_URL_CONTAINS_SIGNIFIES_AUTH_COMPLETE,
  LOGIN_PATH,
  SAML_SIGN_IN_TITLE,
  SAML_SUBMIT_SELECTOR,
  SAML_EMAIL_SELECTOR,
  SAML_PASSWORD_SELECTOR,
} from "./constants.js";
import { jar } from "./axios_client.js";
import * as aggietime from "./aggietime.js";

export const refresh_jwt = () => {
  console.log("Refreshing JWT...");

  return aggietime.get_user_info();
};

export const setCookie = (jwt) =>
  jar.setCookie(`${AGGIETIME_AUTH_COOKIE_NAME}=${jwt}`, AGGIETIME_URI);

export const logout = () => client.get(`${AGGIETIME_URI}/${LOGOUT_PATH}`);

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) =>
          driver.wait(until.elementLocated(By.css(selector))),
        ),
      );

      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!");
      await driver
        .wait(until.elementLocated(By.css(SAML_SUBMIT_SELECTOR)))
        .then(() => driver.findElement(By.css(SAML_SUBMIT_SELECTOR)).click());
    }

    await driver.wait(
      until.urlContains(AGGIETIME_URL_CONTAINS_SIGNIFIES_AUTH_COMPLETE),
    );

    console.log("Retrieving cookie...");
    cookie = await driver.manage().getCookie(AGGIETIME_AUTH_COOKIE_NAME);

    await jar.setCookie(
      new Cookie({
        ...cookie,
        key: cookie.name,
      }),
      AGGIETIME_URI,
    );
    console.log("Got it!");
  } finally {
    await driver.quit();
  }

  return cookie;
};