finki-auth 1.2.0 → 1.2.2
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/auth.d.ts +10 -0
- package/dist/auth.js +56 -0
- package/dist/constants.d.ts +10 -0
- package/dist/constants.js +10 -0
- package/dist/index.d.ts +3 -10
- package/dist/index.js +3 -58
- package/dist/session.d.ts +2 -0
- package/dist/session.js +16 -0
- package/package.json +2 -2
- package/dist/tests/utils.d.ts +0 -5
- package/dist/tests/utils.js +0 -13
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
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/auth.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
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_LOGIN_URLS } from './constants.js';
|
|
7
|
+
import { Service } from './lib/Service.js';
|
|
8
|
+
export class CasAuthentication {
|
|
9
|
+
password;
|
|
10
|
+
session;
|
|
11
|
+
username;
|
|
12
|
+
constructor(username, password) {
|
|
13
|
+
this.username = username;
|
|
14
|
+
this.password = password;
|
|
15
|
+
const cookieJar = new CookieJar();
|
|
16
|
+
const client = axios.create({
|
|
17
|
+
jar: cookieJar,
|
|
18
|
+
validateStatus: (status) => status >= 200 && status < 400,
|
|
19
|
+
});
|
|
20
|
+
this.session = wrapper(client);
|
|
21
|
+
}
|
|
22
|
+
authenticate = async (service) => {
|
|
23
|
+
const fullUrl = `${SERVICE_LOGIN_URLS[Service.CAS]}?service=${encodeURIComponent(SERVICE_LOGIN_URLS[service])}`;
|
|
24
|
+
const initialRequest = await this.session.get(fullUrl);
|
|
25
|
+
const { data } = z.string().safeParse(initialRequest.data);
|
|
26
|
+
if (!data) {
|
|
27
|
+
throw new Error('Failed to parse initial request data');
|
|
28
|
+
}
|
|
29
|
+
const { window } = new JSDOM(data);
|
|
30
|
+
const hiddenInputs = window.document.querySelectorAll('input[type="hidden"]');
|
|
31
|
+
const urlSearchParams = this.getFormData(hiddenInputs);
|
|
32
|
+
await this.session.post(fullUrl, urlSearchParams);
|
|
33
|
+
return this.getCookies(service);
|
|
34
|
+
};
|
|
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];
|
|
41
|
+
};
|
|
42
|
+
getFormData = (inputs) => {
|
|
43
|
+
const urlSearchParams = new URLSearchParams();
|
|
44
|
+
for (const input of inputs) {
|
|
45
|
+
const name = input.getAttribute('name');
|
|
46
|
+
const value = input.getAttribute('value');
|
|
47
|
+
if (name) {
|
|
48
|
+
urlSearchParams.append(name, value ?? '');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
urlSearchParams.append('username', this.username);
|
|
52
|
+
urlSearchParams.append('password', this.password);
|
|
53
|
+
urlSearchParams.append('submit', 'LOGIN');
|
|
54
|
+
return urlSearchParams;
|
|
55
|
+
};
|
|
56
|
+
}
|
package/dist/constants.d.ts
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
export declare const SERVICE_URLS: {
|
|
2
|
+
readonly cas: "https://cas.finki.ukim.mk";
|
|
3
|
+
readonly courses: "https://courses.finki.ukim.mk";
|
|
4
|
+
readonly diplomas: "http://diplomski.finki.ukim.mk";
|
|
5
|
+
};
|
|
6
|
+
export declare const SERVICE_LOGIN_URLS: {
|
|
2
7
|
readonly cas: "https://cas.finki.ukim.mk/cas/login";
|
|
3
8
|
readonly courses: "https://courses.finki.ukim.mk/login/index.php";
|
|
4
9
|
readonly diplomas: "http://diplomski.finki.ukim.mk/Account/LoginCAS";
|
|
5
10
|
};
|
|
11
|
+
export declare const SERVICE_USER_ELEMENT_SELECTORS: {
|
|
12
|
+
readonly cas: "";
|
|
13
|
+
readonly courses: "span.usertext.mr-1";
|
|
14
|
+
readonly diplomas: "#logoutForm > ul > li:nth-child(1) > a";
|
|
15
|
+
};
|
package/dist/constants.js
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
import { Service } from './lib/Service.js';
|
|
2
2
|
export const SERVICE_URLS = {
|
|
3
|
+
[Service.CAS]: 'https://cas.finki.ukim.mk',
|
|
4
|
+
[Service.COURSES]: 'https://courses.finki.ukim.mk',
|
|
5
|
+
[Service.DIPLOMAS]: 'http://diplomski.finki.ukim.mk',
|
|
6
|
+
};
|
|
7
|
+
export const SERVICE_LOGIN_URLS = {
|
|
3
8
|
[Service.CAS]: 'https://cas.finki.ukim.mk/cas/login',
|
|
4
9
|
[Service.COURSES]: 'https://courses.finki.ukim.mk/login/index.php',
|
|
5
10
|
[Service.DIPLOMAS]: 'http://diplomski.finki.ukim.mk/Account/LoginCAS',
|
|
6
11
|
};
|
|
12
|
+
export const SERVICE_USER_ELEMENT_SELECTORS = {
|
|
13
|
+
[Service.CAS]: '',
|
|
14
|
+
[Service.COURSES]: 'span.usertext.mr-1',
|
|
15
|
+
[Service.DIPLOMAS]: '#logoutForm > ul > li:nth-child(1) > a',
|
|
16
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
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
|
+
export { CasAuthentication } from './auth.js';
|
|
2
|
+
export { Service } from './lib/Service.js';
|
|
3
|
+
export { isCookieValid } from './session.js';
|
package/dist/index.js
CHANGED
|
@@ -1,58 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { CookieJar } from 'tough-cookie';
|
|
5
|
-
import { z } from 'zod';
|
|
6
|
-
import { SERVICE_URLS } from './constants.js';
|
|
7
|
-
import { Service } from './lib/Service.js';
|
|
8
|
-
export class CasAuthentication {
|
|
9
|
-
password;
|
|
10
|
-
session;
|
|
11
|
-
username;
|
|
12
|
-
constructor(username, password) {
|
|
13
|
-
this.username = username;
|
|
14
|
-
this.password = password;
|
|
15
|
-
const cookieJar = new CookieJar();
|
|
16
|
-
const client = axios.create({
|
|
17
|
-
jar: cookieJar,
|
|
18
|
-
validateStatus: (status) => status >= 200 && status < 400,
|
|
19
|
-
});
|
|
20
|
-
// @ts-expect-error axios-cookiejar-support has old typings. This used to be an issue in all axios related packages
|
|
21
|
-
this.session = wrapper(client);
|
|
22
|
-
}
|
|
23
|
-
authenticate = async (service) => {
|
|
24
|
-
const fullUrl = `${SERVICE_URLS[Service.CAS]}?service=${encodeURIComponent(SERVICE_URLS[service])}`;
|
|
25
|
-
const initialRequest = await this.session.get(fullUrl);
|
|
26
|
-
const { data } = z.string().safeParse(initialRequest.data);
|
|
27
|
-
if (!data) {
|
|
28
|
-
throw new Error('Failed to parse initial request data');
|
|
29
|
-
}
|
|
30
|
-
const { window } = new JSDOM(data);
|
|
31
|
-
const hiddenInputs = window.document.querySelectorAll('input[type="hidden"]');
|
|
32
|
-
const urlSearchParams = this.getFormData(hiddenInputs);
|
|
33
|
-
await this.session.post(fullUrl, urlSearchParams);
|
|
34
|
-
return this.getCookies(service);
|
|
35
|
-
};
|
|
36
|
-
getCookies = async (service) => {
|
|
37
|
-
const casCookies = (await this.session.defaults.jar?.getCookies(SERVICE_URLS[Service.CAS])) ?? [];
|
|
38
|
-
const serviceCookies = service
|
|
39
|
-
? ((await this.session.defaults.jar?.getCookies(SERVICE_URLS[service])) ??
|
|
40
|
-
[])
|
|
41
|
-
: [];
|
|
42
|
-
return [...casCookies, ...serviceCookies];
|
|
43
|
-
};
|
|
44
|
-
getFormData = (inputs) => {
|
|
45
|
-
const urlSearchParams = new URLSearchParams();
|
|
46
|
-
for (const input of inputs) {
|
|
47
|
-
const name = input.getAttribute('name');
|
|
48
|
-
const value = input.getAttribute('value');
|
|
49
|
-
if (name) {
|
|
50
|
-
urlSearchParams.append(name, value ?? '');
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
urlSearchParams.append('username', this.username);
|
|
54
|
-
urlSearchParams.append('password', this.password);
|
|
55
|
-
urlSearchParams.append('submit', 'LOGIN');
|
|
56
|
-
return urlSearchParams;
|
|
57
|
-
};
|
|
58
|
-
}
|
|
1
|
+
export { CasAuthentication } from './auth.js';
|
|
2
|
+
export { Service } from './lib/Service.js';
|
|
3
|
+
export { isCookieValid } from './session.js';
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { JSDOM } from 'jsdom';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { SERVICE_URLS, SERVICE_USER_ELEMENT_SELECTORS } from './constants.js';
|
|
5
|
+
export const isCookieValid = async (service, cookies) => {
|
|
6
|
+
const url = SERVICE_URLS[service];
|
|
7
|
+
const userElement = SERVICE_USER_ELEMENT_SELECTORS[service];
|
|
8
|
+
const response = await axios.get(url, {
|
|
9
|
+
headers: {
|
|
10
|
+
Cookie: cookies,
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
const html = z.string().parse(response.data);
|
|
14
|
+
const { window } = new JSDOM(html);
|
|
15
|
+
return window.document.querySelector(userElement) !== null;
|
|
16
|
+
};
|
package/package.json
CHANGED
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"files": [
|
|
45
45
|
"dist",
|
|
46
46
|
"!**/*.test.*",
|
|
47
|
-
"
|
|
47
|
+
"!**/tests"
|
|
48
48
|
],
|
|
49
49
|
"homepage": "https://github.com/finki-hub/finki-auth",
|
|
50
50
|
"keywords": [
|
|
@@ -73,5 +73,5 @@
|
|
|
73
73
|
},
|
|
74
74
|
"type": "module",
|
|
75
75
|
"types": "dist/index.d.ts",
|
|
76
|
-
"version": "1.2.
|
|
76
|
+
"version": "1.2.2"
|
|
77
77
|
}
|
package/dist/tests/utils.d.ts
DELETED
package/dist/tests/utils.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/* eslint-disable n/no-process-env */
|
|
2
|
-
import 'dotenv/config';
|
|
3
|
-
import assert from 'node:assert';
|
|
4
|
-
export const getCredentials = () => {
|
|
5
|
-
const username = process.env['CAS_USERNAME'];
|
|
6
|
-
const password = process.env['CAS_PASSWORD'];
|
|
7
|
-
assert(username);
|
|
8
|
-
assert(password);
|
|
9
|
-
return {
|
|
10
|
-
password,
|
|
11
|
-
username,
|
|
12
|
-
};
|
|
13
|
-
};
|