finki-auth 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,13 @@
1
+ import { Service } from './lib/Service.js';
2
+ export declare class CasAuthentication {
3
+ private readonly password;
4
+ private readonly session;
5
+ private readonly username;
6
+ constructor(username: string, password: string);
7
+ private static readonly getFullLoginUrl;
8
+ authenticate: (service: Service) => Promise<void>;
9
+ readonly buildCookieHeader: (service: Service) => Promise<string>;
10
+ readonly getCookie: (service: Service) => Promise<import("tough-cookie").Cookie[]>;
11
+ readonly isCookieValid: (service: Service) => Promise<boolean>;
12
+ private readonly getFormData;
13
+ }
@@ -3,7 +3,7 @@ import { wrapper } from 'axios-cookiejar-support';
3
3
  import { JSDOM } from 'jsdom';
4
4
  import { CookieJar } from 'tough-cookie';
5
5
  import { z } from 'zod';
6
- import { SERVICE_LOGIN_URLS } from './constants.js';
6
+ import { SERVICE_LOGIN_URLS, SERVICE_SUCCESS_SELECTORS, SERVICE_URLS, } from './constants.js';
7
7
  import { Service } from './lib/Service.js';
8
8
  export class CasAuthentication {
9
9
  password;
@@ -19,8 +19,14 @@ export class CasAuthentication {
19
19
  });
20
20
  this.session = wrapper(client);
21
21
  }
22
+ static getFullLoginUrl = (service) => {
23
+ if (service === Service.CAS) {
24
+ return SERVICE_LOGIN_URLS[Service.CAS];
25
+ }
26
+ return `${SERVICE_LOGIN_URLS[Service.CAS]}?service=${encodeURIComponent(SERVICE_LOGIN_URLS[service])}`;
27
+ };
22
28
  authenticate = async (service) => {
23
- const fullUrl = `${SERVICE_LOGIN_URLS[Service.CAS]}?service=${encodeURIComponent(SERVICE_LOGIN_URLS[service])}`;
29
+ const fullUrl = CasAuthentication.getFullLoginUrl(service);
24
30
  const initialRequest = await this.session.get(fullUrl);
25
31
  const { data } = z.string().safeParse(initialRequest.data);
26
32
  if (!data) {
@@ -30,14 +36,36 @@ export class CasAuthentication {
30
36
  const hiddenInputs = window.document.querySelectorAll('input[type="hidden"]');
31
37
  const urlSearchParams = this.getFormData(hiddenInputs);
32
38
  await this.session.post(fullUrl, urlSearchParams);
33
- return this.getCookies(service);
34
39
  };
35
- getCookies = async (service) => {
36
- const casCookies = (await this.session.defaults.jar?.getCookies(SERVICE_LOGIN_URLS[Service.CAS])) ?? [];
37
- const serviceCookies = service
38
- ? ((await this.session.defaults.jar?.getCookies(SERVICE_LOGIN_URLS[service])) ?? [])
39
- : [];
40
- return [...casCookies, ...serviceCookies];
40
+ buildCookieHeader = async (service) => {
41
+ const cookies = await this.getCookie(service);
42
+ return cookies.map(({ key, value }) => `${key}=${value}`).join('; ');
43
+ };
44
+ getCookie = async (service) => {
45
+ const serviceLoginUrl = SERVICE_LOGIN_URLS[service];
46
+ const serviceCookies = await this.session.defaults.jar?.getCookies(serviceLoginUrl);
47
+ return serviceCookies ?? [];
48
+ };
49
+ isCookieValid = async (service) => {
50
+ const url = SERVICE_URLS[service];
51
+ const userElementSelector = SERVICE_SUCCESS_SELECTORS[service];
52
+ const cookies = await this.getCookie(service);
53
+ const jar = new CookieJar();
54
+ for (const cookie of cookies) {
55
+ await jar.setCookie(cookie, url);
56
+ }
57
+ const client = wrapper(axios.create({ jar }));
58
+ const response = await client.get(url);
59
+ const html = z.string().parse(response.data);
60
+ const { window } = new JSDOM(html);
61
+ const userElement = window.document.querySelector(userElementSelector);
62
+ switch (userElement?.textContent) {
63
+ case undefined:
64
+ case 'Најава':
65
+ return false;
66
+ default:
67
+ return true;
68
+ }
41
69
  };
42
70
  getFormData = (inputs) => {
43
71
  const urlSearchParams = new URLSearchParams();
@@ -1,5 +1,5 @@
1
1
  export declare const SERVICE_URLS: {
2
- readonly cas: "https://cas.finki.ukim.mk";
2
+ readonly cas: "https://cas.finki.ukim.mk/cas";
3
3
  readonly consultations: "https://consultations.finki.ukim.mk";
4
4
  readonly courses: "https://courses.finki.ukim.mk";
5
5
  readonly diplomas: "http://diplomski.finki.ukim.mk";
@@ -16,8 +16,8 @@ export declare const SERVICE_LOGIN_URLS: {
16
16
  readonly masters: "https://magisterski.finki.ukim.mk/login";
17
17
  readonly old_courses: "https://oldcourses.finki.ukim.mk/login/index.php";
18
18
  };
19
- export declare const SERVICE_USER_ELEMENT_SELECTORS: {
20
- readonly cas: "";
19
+ export declare const SERVICE_SUCCESS_SELECTORS: {
20
+ readonly cas: "div.success";
21
21
  readonly consultations: "a#username";
22
22
  readonly courses: "span.usertext.me-1";
23
23
  readonly diplomas: "#logoutForm > ul > li:nth-child(1) > a";
package/dist/constants.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Service } from './lib/Service.js';
2
2
  export const SERVICE_URLS = {
3
- [Service.CAS]: 'https://cas.finki.ukim.mk',
3
+ [Service.CAS]: 'https://cas.finki.ukim.mk/cas',
4
4
  [Service.CONSULTATIONS]: 'https://consultations.finki.ukim.mk',
5
5
  [Service.COURSES]: 'https://courses.finki.ukim.mk',
6
6
  [Service.DIPLOMAS]: 'http://diplomski.finki.ukim.mk',
@@ -17,8 +17,8 @@ export const SERVICE_LOGIN_URLS = {
17
17
  [Service.MASTERS]: 'https://magisterski.finki.ukim.mk/login',
18
18
  [Service.OLD_COURSES]: 'https://oldcourses.finki.ukim.mk/login/index.php',
19
19
  };
20
- export const SERVICE_USER_ELEMENT_SELECTORS = {
21
- [Service.CAS]: '',
20
+ export const SERVICE_SUCCESS_SELECTORS = {
21
+ [Service.CAS]: 'div.success',
22
22
  [Service.CONSULTATIONS]: 'a#username',
23
23
  [Service.COURSES]: 'span.usertext.me-1',
24
24
  [Service.DIPLOMAS]: '#logoutForm > ul > li:nth-child(1) > a',
package/dist/index.d.ts CHANGED
@@ -1,3 +1,2 @@
1
- export { CasAuthentication } from './auth.js';
1
+ export { CasAuthentication } from './authentication.js';
2
2
  export { Service } from './lib/Service.js';
3
- export { isCookieValid } from './session.js';
package/dist/index.js CHANGED
@@ -1,3 +1,2 @@
1
- export { CasAuthentication } from './auth.js';
1
+ export { CasAuthentication } from './authentication.js';
2
2
  export { Service } from './lib/Service.js';
3
- export { isCookieValid } from './session.js';
package/package.json CHANGED
@@ -72,5 +72,5 @@
72
72
  },
73
73
  "type": "module",
74
74
  "types": "dist/index.d.ts",
75
- "version": "1.6.0"
75
+ "version": "1.7.0"
76
76
  }
package/dist/auth.d.ts DELETED
@@ -1,10 +0,0 @@
1
- import { Service } from './lib/Service.js';
2
- export declare class CasAuthentication {
3
- private readonly password;
4
- private readonly session;
5
- private readonly username;
6
- constructor(username: string, password: string);
7
- authenticate: (service: Service) => Promise<import("tough-cookie").Cookie[]>;
8
- private readonly getCookies;
9
- private readonly getFormData;
10
- }
@@ -1,7 +0,0 @@
1
- import type { CookieJar } from 'tough-cookie';
2
- import 'axios';
3
- declare module 'axios' {
4
- interface AxiosRequestConfig {
5
- jar?: CookieJar;
6
- }
7
- }
package/dist/lib/axios.js DELETED
@@ -1 +0,0 @@
1
- import 'axios';
package/dist/session.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import type { Service } from './lib/Service.js';
2
- export declare const isCookieValid: (service: Service, cookies: string) => Promise<boolean>;
package/dist/session.js DELETED
@@ -1,29 +0,0 @@
1
- import axios from 'axios';
2
- import { wrapper } from 'axios-cookiejar-support';
3
- import { JSDOM } from 'jsdom';
4
- import { CookieJar } from 'tough-cookie';
5
- import { z } from 'zod';
6
- import { SERVICE_URLS, SERVICE_USER_ELEMENT_SELECTORS } from './constants.js';
7
- export const isCookieValid = async (service, cookies) => {
8
- const url = SERVICE_URLS[service];
9
- const userElementSelector = SERVICE_USER_ELEMENT_SELECTORS[service];
10
- const jar = new CookieJar();
11
- if (cookies) {
12
- const cookiePairs = cookies.split('; ');
13
- for (const pair of cookiePairs) {
14
- await jar.setCookie(pair, url);
15
- }
16
- }
17
- const client = wrapper(axios.create({ jar }));
18
- const response = await client.get(url);
19
- const html = z.string().parse(response.data);
20
- const { window } = new JSDOM(html);
21
- const userElement = window.document.querySelector(userElementSelector);
22
- switch (userElement?.textContent) {
23
- case undefined:
24
- case 'Најава':
25
- return false;
26
- default:
27
- return true;
28
- }
29
- };