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.
- package/dist/authentication.d.ts +13 -0
- package/dist/{auth.js → authentication.js} +37 -9
- package/dist/constants.d.ts +3 -3
- package/dist/constants.js +3 -3
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -2
- package/package.json +1 -1
- package/dist/auth.d.ts +0 -10
- package/dist/lib/axios.d.ts +0 -7
- package/dist/lib/axios.js +0 -1
- package/dist/session.d.ts +0 -2
- package/dist/session.js +0 -29
|
@@ -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 =
|
|
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
|
-
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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();
|
package/dist/constants.d.ts
CHANGED
|
@@ -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
|
|
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
|
|
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
package/dist/index.js
CHANGED
package/package.json
CHANGED
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
|
-
}
|
package/dist/lib/axios.d.ts
DELETED
package/dist/lib/axios.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import 'axios';
|
package/dist/session.d.ts
DELETED
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
|
-
};
|