forked from queueit/KnownUser.V3.Akamai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
akamaiContextProvider.ts
118 lines (94 loc) · 3.44 KB
/
akamaiContextProvider.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/// <reference path="types/akamai.d.ts"/>
import { Cookies } from 'cookies';
import { IConnectorContextProvider, ICryptoProvider, IEnqueueTokenProvider, IHttpRequest, IHttpResponse } from 'queueit-knownuser';
import { AkamaiCryptoProvider } from './akamaiCryptoProvider.js';
import { AkamaiEnqueueTokenProvider } from './akamaiEnqueueTokenProvider.js';
import { Settings } from './QueueITHelpers';
export class AkamaiContextProvider implements IConnectorContextProvider {
private _httpRequest: IHttpRequest;
private _httpResponse: IHttpResponse;
private _enqueueTokenProvider: IEnqueueTokenProvider;
private _cryptoProvider: ICryptoProvider;
public constructor(akamiNativeRequest: any, akamaiNativeResponse: any) {
this._httpRequest = new AkamaiHttpRequest(akamiNativeRequest);
this._httpResponse = new AkamaiHttpResponse(akamaiNativeResponse);
this._cryptoProvider = new AkamaiCryptoProvider();
}
public getHttpRequest() {
return this._httpRequest;
}
public getHttpResponse() {
return this._httpResponse;
}
public setEnqueueTokenProvider(
setting: Settings,
validityTime: Number,
clientIp: string,
customData?: any) {
if (!setting || validityTime < -1) {
this._enqueueTokenProvider = null;
return;
}
this._enqueueTokenProvider = new AkamaiEnqueueTokenProvider(
setting,
validityTime,
clientIp,
customData
);
}
public getEnqueueTokenProvider() {
return this._enqueueTokenProvider;
}
public getCryptoProvider() {
return this._cryptoProvider;
}
}
class AkamaiHttpRequest {
constructor(private _akamiNativeRequest: any) {
}
public getUserAgent() {
return this.getHeader("user-agent");
}
public getHeader(headerNameArg: string) {
return (this._akamiNativeRequest.getHeader(headerNameArg) || []).toString();
}
public getAbsoluteUri() {
return `${this._akamiNativeRequest.scheme}://${this._akamiNativeRequest.host}${this._akamiNativeRequest.url}`;
}
public getUserHostAddress() {
return this._akamiNativeRequest.getVariable('PMUSER_TRUE_CLIENT_IP');
}
public getCookieValue(cookieKey: string) {
try {
let cookies = new Cookies(this._akamiNativeRequest.getHeader('Cookie'));
let cookieValue = cookies.get(cookieKey);
if (cookieValue)
return decodeURIComponent(cookieValue);
} catch {
return undefined;
}
}
public getRequestBodyAsString() {
return "";
}
}
class AkamaiHttpResponse {
constructor(private _akamiNativeRequest: any) {
}
public setCookie(cookieName: string, cookieValue: string, domain: string, expiration, httpOnly: boolean, isSecure: boolean) {
// expiration is in secs, but Date needs it in milisecs
let expirationDate = new Date(expiration * 1000);
let setCookieString = `${cookieName}=${encodeURIComponent(cookieValue)}; expires=${expirationDate.toUTCString()};`;
if (domain) {
setCookieString += ` domain=${domain};`;
}
if (httpOnly) {
setCookieString += " HttpOnly;"
}
if (isSecure) {
setCookieString += " Secure;"
}
setCookieString += " path=/";
this._akamiNativeRequest.storeCookie(setCookieString);
}
}