finki-auth 1.2.1 → 1.3.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/README.md +25 -6
- 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 +6 -6
- package/dist/tests/utils.d.ts +0 -5
- package/dist/tests/utils.js +0 -13
package/README.md
CHANGED
|
@@ -1,15 +1,34 @@
|
|
|
1
1
|
# FINKI Auth
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
NPM (Node) package for managing authentication and sessions for FCSE CAS and the other services. Uses an HTTP client under the hood, and not a full browser (such as Puppeteer).
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
Currently supports the following services:
|
|
8
|
+
|
|
9
|
+
- [CAS](https://cas.finki.ukim.mk/)
|
|
10
|
+
- [Courses](https://courses.finki.ukim.mk/)
|
|
11
|
+
- [Diplomas](https://diplomski.finki.ukim.mk/)
|
|
8
12
|
|
|
9
13
|
## Installation
|
|
10
14
|
|
|
11
|
-
`npm i finki-auth
|
|
15
|
+
You can add the package to your NPM project by running `npm i finki-auth`.
|
|
16
|
+
|
|
17
|
+
## Example
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { CasAuthentication, Service } from "finki-auth";
|
|
21
|
+
|
|
22
|
+
const auth = new CasAuthentication(credentials.username, credentials.password);
|
|
23
|
+
const rawCookies = await auth.authenticate(Service.COURSES);
|
|
24
|
+
|
|
25
|
+
const cookies: Record<string, string> = {};
|
|
26
|
+
|
|
27
|
+
for (const { key, value } of rawCookies) {
|
|
28
|
+
cookies[key] = value;
|
|
29
|
+
}
|
|
30
|
+
```
|
|
12
31
|
|
|
13
32
|
## License
|
|
14
33
|
|
|
15
|
-
This project is licensed under the MIT license.
|
|
34
|
+
This project is licensed under the terms of the MIT license.
|
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
|
@@ -17,12 +17,12 @@
|
|
|
17
17
|
"axios-cookiejar-support": "^6.0.0",
|
|
18
18
|
"jsdom": "^26.0.0",
|
|
19
19
|
"tough-cookie": "^5.1.2",
|
|
20
|
-
"zod": "^3.
|
|
20
|
+
"zod": "^3.25.31"
|
|
21
21
|
},
|
|
22
22
|
"description": "Authentication for FCSE services",
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@commitlint/cli": "^19.8.0",
|
|
25
|
-
"@commitlint/config-conventional": "^19.8.
|
|
25
|
+
"@commitlint/config-conventional": "^19.8.1",
|
|
26
26
|
"@semantic-release/changelog": "^6.0.3",
|
|
27
27
|
"@semantic-release/git": "^10.0.1",
|
|
28
28
|
"@types/jsdom": "^21.1.7",
|
|
@@ -39,12 +39,12 @@
|
|
|
39
39
|
"vitest": "^3.1.1"
|
|
40
40
|
},
|
|
41
41
|
"engines": {
|
|
42
|
-
"node": "^20 || ^22"
|
|
42
|
+
"node": "^20 || ^22 || ^24"
|
|
43
43
|
},
|
|
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": [
|
|
@@ -67,11 +67,11 @@
|
|
|
67
67
|
"lint": "eslint . --ignore-pattern \"**/test/*\" --no-warn-ignored --cache",
|
|
68
68
|
"prepare": "husky",
|
|
69
69
|
"package": "npm run build && npm pack",
|
|
70
|
-
"release": "semantic-release",
|
|
70
|
+
"release": "npm run build && semantic-release",
|
|
71
71
|
"release:dry": "npm run release -- --dry-run",
|
|
72
72
|
"test": "vitest run --exclude dist"
|
|
73
73
|
},
|
|
74
74
|
"type": "module",
|
|
75
75
|
"types": "dist/index.d.ts",
|
|
76
|
-
"version": "1.
|
|
76
|
+
"version": "1.3.0"
|
|
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
|
-
};
|