finki-auth 1.1.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/LICENSE +22 -0
- package/README.md +11 -0
- package/dist/constants.d.ts +5 -0
- package/dist/constants.js +6 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +58 -0
- package/dist/lib/Service.d.ts +7 -0
- package/dist/lib/Service.js +8 -0
- package/dist/lib/axios.d.ts +7 -0
- package/dist/lib/axios.js +1 -0
- package/dist/tests/utils.d.ts +5 -0
- package/dist/tests/utils.js +13 -0
- package/package.json +77 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
MIT License
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2025 Delemangi
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Service } from './lib/Service.js';
|
|
2
|
+
export const SERVICE_URLS = {
|
|
3
|
+
[Service.CAS]: 'https://cas.finki.ukim.mk/cas/login',
|
|
4
|
+
[Service.COURSES]: 'https://courses.finki.ukim.mk/login/index.php',
|
|
5
|
+
[Service.DIPLOMAS]: 'http://diplomski.finki.ukim.mk/Account/LoginCAS',
|
|
6
|
+
};
|
package/dist/index.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/index.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
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 } 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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import 'axios';
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": {
|
|
3
|
+
"email": "milev.stefan@gmail.com",
|
|
4
|
+
"name": "Delemangi",
|
|
5
|
+
"url": "https://delemangi.com"
|
|
6
|
+
},
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/Delemangi/finki-auth/issues"
|
|
9
|
+
},
|
|
10
|
+
"config": {
|
|
11
|
+
"commitizen": {
|
|
12
|
+
"path": "cz-conventional-changelog"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"axios": "^1.8.4",
|
|
17
|
+
"axios-cookiejar-support": "^5.0.5",
|
|
18
|
+
"jsdom": "^26.0.0",
|
|
19
|
+
"tough-cookie": "^5.1.2",
|
|
20
|
+
"zod": "^3.24.2"
|
|
21
|
+
},
|
|
22
|
+
"description": "Authentication for FCSE services",
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@commitlint/cli": "^19.8.0",
|
|
25
|
+
"@commitlint/config-conventional": "^19.8.0",
|
|
26
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
27
|
+
"@semantic-release/git": "^10.0.1",
|
|
28
|
+
"@types/jsdom": "^21.1.7",
|
|
29
|
+
"@types/superagent": "^8.1.9",
|
|
30
|
+
"commitizen": "^4.3.1",
|
|
31
|
+
"cz-conventional-changelog": "^3.3.0",
|
|
32
|
+
"dotenv": "^16.5.0",
|
|
33
|
+
"eslint": "^9.24.0",
|
|
34
|
+
"eslint-config-imperium": "^2.2.0",
|
|
35
|
+
"husky": "^9.1.7",
|
|
36
|
+
"rimraf": "^6.0.1",
|
|
37
|
+
"semantic-release": "^24.2.3",
|
|
38
|
+
"typescript": "~5.8.3",
|
|
39
|
+
"vitest": "^3.1.1"
|
|
40
|
+
},
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": "^20 || ^22"
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"dist",
|
|
46
|
+
"!**/*.test.*",
|
|
47
|
+
"!test"
|
|
48
|
+
],
|
|
49
|
+
"homepage": "https://github.com/Delemangi/finki-auth",
|
|
50
|
+
"keywords": [
|
|
51
|
+
"finki",
|
|
52
|
+
"auth",
|
|
53
|
+
"authentication"
|
|
54
|
+
],
|
|
55
|
+
"license": "MIT",
|
|
56
|
+
"main": "dist/index.js",
|
|
57
|
+
"name": "finki-auth",
|
|
58
|
+
"repository": {
|
|
59
|
+
"type": "git",
|
|
60
|
+
"url": "git+https://github.com/Delemangi/finki-auth.git"
|
|
61
|
+
},
|
|
62
|
+
"scripts": {
|
|
63
|
+
"build": "rimraf dist && tsc",
|
|
64
|
+
"commit": "cz",
|
|
65
|
+
"commitlint": "commitlint --edit",
|
|
66
|
+
"format": "eslint --ignore-pattern \"**/test/*\" --fix .",
|
|
67
|
+
"lint": "eslint . --ignore-pattern \"**/test/*\" --no-warn-ignored --cache",
|
|
68
|
+
"prepare": "husky",
|
|
69
|
+
"package": "npm run build && npm pack",
|
|
70
|
+
"release": "semantic-release",
|
|
71
|
+
"release:dry": "npm run release -- --dry-run",
|
|
72
|
+
"test": "vitest run --exclude dist"
|
|
73
|
+
},
|
|
74
|
+
"type": "module",
|
|
75
|
+
"types": "dist/index.d.ts",
|
|
76
|
+
"version": "1.1.0"
|
|
77
|
+
}
|