-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathapple-portal-cookie-service.ts
41 lines (33 loc) · 1.66 KB
/
apple-portal-cookie-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
export class ApplePortalCookieService implements IApplePortalCookieService {
private userSessionCookies: IStringDictionary = {};
private validUserSessionCookieNames = ["myacinfo", "dqsid", "itctx", "itcdq", "acn01"];
private validWebSessionCookieNames = ["wosid", "woinst", "itctx"];
public getWebSessionCookie(cookiesData: string[]): string {
const webSessionCookies = _.cloneDeep(this.userSessionCookies);
const parsedCookies = this.parseCookiesData(cookiesData, this.validWebSessionCookieNames);
_.each(parsedCookies, parsedCookie => webSessionCookies[parsedCookie.key] = parsedCookie.cookie);
return _.values(webSessionCookies).join("; ");
}
public getUserSessionCookie(): string {
return _.values(this.userSessionCookies).join("; ");
}
public updateUserSessionCookie(cookiesData: string[]): void {
const parsedCookies = this.parseCookiesData(cookiesData, this.validUserSessionCookieNames);
_.each(parsedCookies, parsedCookie => this.userSessionCookies[parsedCookie.key] = parsedCookie.cookie);
}
private parseCookiesData(cookiesData: string[], validCookieNames: string[]): IDictionary<{key: string, value: string, cookie: string}> {
const result: IDictionary<{key: string, value: string, cookie: string}> = {};
for (const c of cookiesData) {
const parts = c.split(";");
for (const cookie of parts) {
const trimmedCookie = cookie.trim();
const [cookieKey, cookieValue] = trimmedCookie.split("=");
if (_.includes(validCookieNames, cookieKey)) {
result[cookieKey] = { key: cookieKey, value: cookieValue, cookie: trimmedCookie };
}
}
}
return result;
}
}
$injector.register("applePortalCookieService", ApplePortalCookieService);