finki-auth 2.0.0 → 2.0.1
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 +1 -1
- package/dist/authentication.d.ts +0 -1
- package/dist/authentication.js +14 -8
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/authentication.d.ts
CHANGED
package/dist/authentication.js
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
import * as cheerio from 'cheerio';
|
|
2
2
|
import makeFetchCookie from 'fetch-cookie';
|
|
3
3
|
import { CookieJar } from 'tough-cookie';
|
|
4
|
-
import { SERVICE_LOGIN_URLS
|
|
4
|
+
import { SERVICE_LOGIN_URLS } from './constants.js';
|
|
5
5
|
import { Service } from './lib/Service.js';
|
|
6
6
|
import { getCookieValidity } from './utils.js';
|
|
7
7
|
export class CasAuthentication {
|
|
8
8
|
cookieJar;
|
|
9
|
-
fetchWithCookies;
|
|
10
9
|
password;
|
|
11
10
|
username;
|
|
12
11
|
constructor({ password, username }) {
|
|
13
12
|
this.username = username;
|
|
14
13
|
this.password = password;
|
|
15
14
|
this.cookieJar = new CookieJar();
|
|
16
|
-
this.fetchWithCookies = makeFetchCookie(fetch, this.cookieJar);
|
|
17
15
|
}
|
|
18
16
|
static getFullLoginUrl = (service) => {
|
|
19
17
|
if (service === Service.CAS) {
|
|
@@ -22,15 +20,23 @@ export class CasAuthentication {
|
|
|
22
20
|
return `${SERVICE_LOGIN_URLS[Service.CAS]}?service=${encodeURIComponent(SERVICE_LOGIN_URLS[service])}`;
|
|
23
21
|
};
|
|
24
22
|
authenticate = async (service) => {
|
|
25
|
-
const
|
|
26
|
-
const
|
|
23
|
+
const jar = new CookieJar();
|
|
24
|
+
const fetchWithCookies = makeFetchCookie(fetch, jar);
|
|
25
|
+
const casLoginUrl = CasAuthentication.getFullLoginUrl(service);
|
|
26
|
+
const initialResponse = await fetchWithCookies(casLoginUrl);
|
|
27
27
|
const html = await initialResponse.text();
|
|
28
28
|
const $ = cheerio.load(html);
|
|
29
29
|
const urlSearchParams = this.getFormData($);
|
|
30
|
-
await
|
|
30
|
+
const postResponse = await fetchWithCookies(casLoginUrl, {
|
|
31
31
|
body: urlSearchParams,
|
|
32
32
|
method: 'POST',
|
|
33
33
|
});
|
|
34
|
+
await postResponse.body?.cancel();
|
|
35
|
+
const serviceLoginUrl = SERVICE_LOGIN_URLS[service];
|
|
36
|
+
const serviceCookies = await jar.getCookies(serviceLoginUrl);
|
|
37
|
+
for (const cookie of serviceCookies) {
|
|
38
|
+
await this.cookieJar.setCookie(cookie, serviceLoginUrl);
|
|
39
|
+
}
|
|
34
40
|
};
|
|
35
41
|
buildCookieHeader = async (service) => {
|
|
36
42
|
const cookies = await this.getCookie(service);
|
|
@@ -41,11 +47,11 @@ export class CasAuthentication {
|
|
|
41
47
|
return await this.cookieJar.getCookies(serviceLoginUrl);
|
|
42
48
|
};
|
|
43
49
|
isCookieValid = async (service) => {
|
|
44
|
-
const
|
|
50
|
+
const serviceLoginUrl = SERVICE_LOGIN_URLS[service];
|
|
45
51
|
const cookies = await this.getCookie(service);
|
|
46
52
|
const jar = new CookieJar();
|
|
47
53
|
for (const cookie of cookies) {
|
|
48
|
-
await jar.setCookie(cookie,
|
|
54
|
+
await jar.setCookie(cookie, serviceLoginUrl);
|
|
49
55
|
}
|
|
50
56
|
return await getCookieValidity({ cookieJar: jar, service });
|
|
51
57
|
};
|
package/package.json
CHANGED