@queue-it/fastly 1.0.4 → 2.0.0-beta.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.
@@ -1,189 +1,188 @@
1
- import {IHttpContextProvider} from './HttpContextProvider'
2
- import {Utils, CookieHelper} from './QueueITHelpers'
3
- import {Date as WasiDate} from "as-wasi";
4
- import {KeyValuePair} from "./Models";
5
-
6
- export class UserInQueueStateCookieRepository {
7
- private static readonly _QueueITDataKey: string = "QueueITAccepted-SDFrts345E-V3";
8
- private static readonly _HashKey: string = "Hash";
9
- private static readonly _IssueTimeKey: string = "IssueTime";
10
- private static readonly _QueueIdKey: string = "QueueId";
11
- private static readonly _EventIdKey: string = "EventId";
12
- private static readonly _RedirectTypeKey: string = "RedirectType";
13
- private static readonly _FixedCookieValidityMinutesKey: string = "FixedValidityMins";
14
-
15
- constructor(private httpContextProvider: IHttpContextProvider) {
16
- }
17
-
18
- public static getCookieKey(eventId: string): string {
19
- return UserInQueueStateCookieRepository._QueueITDataKey + "_" + eventId;
20
- }
21
-
22
- public store(eventId: string, queueId: string, fixedCookieValidityMinutes: i64,
23
- cookieDomain: string, redirectType: string, secretKey: string): void {
24
- this.createCookie(
25
- eventId, queueId,
26
- fixedCookieValidityMinutes > 0 ? fixedCookieValidityMinutes.toString() : "",
27
- redirectType, cookieDomain, secretKey);
28
- }
29
-
30
- private createCookie(
31
- eventId: string,
32
- queueId: string,
33
- fixedCookieValidityMinutes: string,
34
- redirectType: string,
35
- cookieDomain: string,
36
- secretKey: string): void {
37
- let cookieKey = UserInQueueStateCookieRepository.getCookieKey(eventId);
38
-
39
- let issueTime = Utils.getCurrentTime().toString();
40
-
41
- let cookieValues = new Array<KeyValuePair>();
42
- cookieValues.push(new KeyValuePair(UserInQueueStateCookieRepository._EventIdKey, eventId));
43
- cookieValues.push(new KeyValuePair(UserInQueueStateCookieRepository._QueueIdKey, queueId));
44
- if (fixedCookieValidityMinutes != '') {
45
- cookieValues.push(new KeyValuePair(
46
- UserInQueueStateCookieRepository._FixedCookieValidityMinutesKey, fixedCookieValidityMinutes
47
- ));
48
- }
49
- cookieValues.push(new KeyValuePair(UserInQueueStateCookieRepository._RedirectTypeKey, redirectType.toLowerCase()));
50
- cookieValues.push(new KeyValuePair(UserInQueueStateCookieRepository._IssueTimeKey, issueTime));
51
- cookieValues.push(new KeyValuePair(
52
- UserInQueueStateCookieRepository._HashKey,
53
- this.generateHash(eventId.toLowerCase(), queueId, fixedCookieValidityMinutes, redirectType.toLowerCase(), issueTime, secretKey)
54
- ));
55
- const tomorrow: Date = new Date(i64(WasiDate.now()) + 1000 * 60 * 60 * 24);
56
- const expire: i32 = Math.floor((tomorrow.getTime() / 1000) as f64) as i32;
57
- let cookieValue = CookieHelper.toValueFromKeyValueCollection(cookieValues);
58
-
59
- this.httpContextProvider.getHttpResponse().setCookie(cookieKey,
60
- cookieValue,
61
- cookieDomain, expire);
62
- }
63
-
64
- public getState(eventId: string, cookieValidityMinutes: i64, secretKey: string, validateTime: bool): StateInfo {
65
- const cookieKey = UserInQueueStateCookieRepository.getCookieKey(eventId);
66
- const cookie = this.httpContextProvider.getHttpRequest().getCookieValue(cookieKey);
67
-
68
- if (cookie == "") {
69
- return new StateInfo(false, false, "", 0, "");
70
- }
71
- const cookieValues = CookieHelper.toMapFromValue(cookie);
72
-
73
- if (!this.isCookieValid(secretKey, cookieValues, eventId, cookieValidityMinutes, validateTime)) {
74
- return new StateInfo(true, false, "", 0, "");
75
- }
76
-
77
- const fixedCookieValidity: i64 = cookieValues.has(UserInQueueStateCookieRepository._FixedCookieValidityMinutesKey)
78
- ? I64.parseInt(cookieValues.get(UserInQueueStateCookieRepository._FixedCookieValidityMinutesKey))
79
- : 0;
80
-
81
- return new StateInfo(
82
- true,
83
- true,
84
- cookieValues.get(UserInQueueStateCookieRepository._QueueIdKey),
85
- fixedCookieValidity,
86
- cookieValues.get(UserInQueueStateCookieRepository._RedirectTypeKey));
87
- }
88
-
89
- private isCookieValid(
90
- secretKey: string,
91
- cookieValueMap: Map<string, string>,
92
- eventId: string,
93
- cookieValidityMinutes: i64,
94
- validateTime: bool): bool {
95
-
96
- const storedHash = cookieValueMap.has(UserInQueueStateCookieRepository._HashKey) ?
97
- cookieValueMap.get(UserInQueueStateCookieRepository._HashKey) : "";
98
- const issueTimeString = cookieValueMap.has(UserInQueueStateCookieRepository._IssueTimeKey) ?
99
- cookieValueMap.get(UserInQueueStateCookieRepository._IssueTimeKey) : "";
100
- const queueId = cookieValueMap.has(UserInQueueStateCookieRepository._QueueIdKey) ?
101
- cookieValueMap.get(UserInQueueStateCookieRepository._QueueIdKey) : "";
102
- const eventIdFromCookie = cookieValueMap.has(UserInQueueStateCookieRepository._EventIdKey) ?
103
- cookieValueMap.get(UserInQueueStateCookieRepository._EventIdKey) : "";
104
- const redirectType = cookieValueMap.has(UserInQueueStateCookieRepository._RedirectTypeKey) ?
105
- cookieValueMap.get(UserInQueueStateCookieRepository._RedirectTypeKey) : "";
106
- let fixedCookieValidityMinutes = cookieValueMap.has(UserInQueueStateCookieRepository._FixedCookieValidityMinutesKey) ?
107
- cookieValueMap.get(UserInQueueStateCookieRepository._FixedCookieValidityMinutesKey) : "";
108
-
109
- const expectedHash = this.generateHash(
110
- eventIdFromCookie,
111
- queueId,
112
- fixedCookieValidityMinutes,
113
- redirectType,
114
- issueTimeString,
115
- secretKey);
116
- if (expectedHash.length == 0) {
117
- return false
118
- }
119
-
120
- if (expectedHash != storedHash)
121
- return false;
122
-
123
- if (eventId.toLowerCase() != eventIdFromCookie.toLowerCase())
124
- return false;
125
-
126
- if (validateTime) {
127
- let validity: i64 = fixedCookieValidityMinutes.length > 0 ? I64.parseInt(fixedCookieValidityMinutes) : cookieValidityMinutes;
128
- let expirationTime: i64 = I64.parseInt(issueTimeString) + validity * 60;
129
-
130
- if (expirationTime < Utils.getCurrentTime())
131
- return false;
132
- }
133
- return true;
134
- }
135
-
136
- public cancelQueueCookie(eventId: string, cookieDomain: string): void {
137
- const cookieKey = UserInQueueStateCookieRepository.getCookieKey(eventId);
138
- this.httpContextProvider.getHttpResponse().setCookie(cookieKey, "", cookieDomain, 0);
139
- }
140
-
141
- public reissueQueueCookie(eventId: string, cookieValidityMinutes: i64, cookieDomain: string, secretKey: string): void {
142
- const cookieKey = UserInQueueStateCookieRepository.getCookieKey(eventId);
143
- const cookie = this.httpContextProvider.getHttpRequest().getCookieValue(cookieKey);
144
-
145
- if (cookie == '')
146
- return;
147
-
148
- const cookieValues = CookieHelper.toMapFromValue(cookie);
149
-
150
- if (!this.isCookieValid(secretKey, cookieValues, eventId, cookieValidityMinutes, true))
151
- return;
152
-
153
- let fixedCookieValidityMinutes = "";
154
- if (cookieValues.has(UserInQueueStateCookieRepository._FixedCookieValidityMinutesKey)) {
155
- fixedCookieValidityMinutes = cookieValues.get(UserInQueueStateCookieRepository._FixedCookieValidityMinutesKey).toString();
156
- }
157
-
158
- this.createCookie(
159
- eventId,
160
- cookieValues.get(UserInQueueStateCookieRepository._QueueIdKey),
161
- fixedCookieValidityMinutes,
162
- cookieValues.get(UserInQueueStateCookieRepository._RedirectTypeKey),
163
- cookieDomain, secretKey);
164
- }
165
-
166
- private generateHash(
167
- eventId: string,
168
- queueId: string,
169
- fixedCookieValidityMinutes: string,
170
- redirectType: string,
171
- issueTime: string,
172
- secretKey: string): string {
173
- let valueToHash = eventId + queueId + fixedCookieValidityMinutes + redirectType + issueTime;
174
- return Utils.generateSHA256Hash(secretKey, valueToHash);
175
- }
176
- }
177
-
178
- export class StateInfo {
179
- constructor(public isFound: bool,
180
- public isValid: bool,
181
- public queueId: string,
182
- public fixedCookieValidityMinutes: i64,
183
- public redirectType: string) {
184
- }
185
-
186
- isStateExtendable(): bool {
187
- return this.isValid && !this.fixedCookieValidityMinutes;
188
- }
189
- }
1
+ import {IHttpContextProvider} from './HttpContextProvider'
2
+ import {Utils, CookieHelper} from './QueueITHelpers'
3
+ import {KeyValuePair} from "./Models";
4
+
5
+ export class UserInQueueStateCookieRepository {
6
+ private static readonly _QueueITDataKey: string = "QueueITAccepted-SDFrts345E-V3";
7
+ private static readonly _HashKey: string = "Hash";
8
+ private static readonly _IssueTimeKey: string = "IssueTime";
9
+ private static readonly _QueueIdKey: string = "QueueId";
10
+ private static readonly _EventIdKey: string = "EventId";
11
+ private static readonly _RedirectTypeKey: string = "RedirectType";
12
+ private static readonly _FixedCookieValidityMinutesKey: string = "FixedValidityMins";
13
+
14
+ constructor(private httpContextProvider: IHttpContextProvider) {
15
+ }
16
+
17
+ public static getCookieKey(eventId: string): string {
18
+ return UserInQueueStateCookieRepository._QueueITDataKey + "_" + eventId;
19
+ }
20
+
21
+ public store(eventId: string, queueId: string, fixedCookieValidityMinutes: number,
22
+ cookieDomain: string, redirectType: string, secretKey: string): void {
23
+ this.createCookie(
24
+ eventId, queueId,
25
+ fixedCookieValidityMinutes > 0 ? fixedCookieValidityMinutes.toString() : "",
26
+ redirectType, cookieDomain, secretKey);
27
+ }
28
+
29
+ private createCookie(
30
+ eventId: string,
31
+ queueId: string,
32
+ fixedCookieValidityMinutes: string,
33
+ redirectType: string,
34
+ cookieDomain: string,
35
+ secretKey: string): void {
36
+ let cookieKey = UserInQueueStateCookieRepository.getCookieKey(eventId);
37
+
38
+ let issueTime = Utils.getCurrentTime().toString();
39
+
40
+ let cookieValues = new Array<KeyValuePair>();
41
+ cookieValues.push(new KeyValuePair(UserInQueueStateCookieRepository._EventIdKey, eventId));
42
+ cookieValues.push(new KeyValuePair(UserInQueueStateCookieRepository._QueueIdKey, queueId));
43
+ if (fixedCookieValidityMinutes != '') {
44
+ cookieValues.push(new KeyValuePair(
45
+ UserInQueueStateCookieRepository._FixedCookieValidityMinutesKey, fixedCookieValidityMinutes
46
+ ));
47
+ }
48
+ cookieValues.push(new KeyValuePair(UserInQueueStateCookieRepository._RedirectTypeKey, redirectType.toLowerCase()));
49
+ cookieValues.push(new KeyValuePair(UserInQueueStateCookieRepository._IssueTimeKey, issueTime));
50
+ cookieValues.push(new KeyValuePair(
51
+ UserInQueueStateCookieRepository._HashKey,
52
+ this.generateHash(eventId.toLowerCase(), queueId, fixedCookieValidityMinutes, redirectType.toLowerCase(), issueTime, secretKey)
53
+ ));
54
+ const tomorrow: Date = new Date(Date.now() + 1000 * 60 * 60 * 24);
55
+ const expire: number = Math.floor(tomorrow.getTime() / 1000);
56
+ let cookieValue = CookieHelper.toValueFromKeyValueCollection(cookieValues);
57
+
58
+ this.httpContextProvider.getHttpResponse().setCookie(cookieKey,
59
+ cookieValue,
60
+ cookieDomain, expire);
61
+ }
62
+
63
+ public getState(eventId: string, cookieValidityMinutes: number, secretKey: string, validateTime: boolean): StateInfo {
64
+ const cookieKey = UserInQueueStateCookieRepository.getCookieKey(eventId);
65
+ const cookie = this.httpContextProvider.getHttpRequest().getCookieValue(cookieKey);
66
+
67
+ if (cookie == "") {
68
+ return new StateInfo(false, false, "", 0, "");
69
+ }
70
+ const cookieValues = CookieHelper.toMapFromValue(cookie);
71
+
72
+ if (!this.isCookieValid(secretKey, cookieValues, eventId, cookieValidityMinutes, validateTime)) {
73
+ return new StateInfo(true, false, "", 0, "");
74
+ }
75
+
76
+ const fixedCookieValidity: number = cookieValues.has(UserInQueueStateCookieRepository._FixedCookieValidityMinutesKey)
77
+ ? parseInt(cookieValues.get(UserInQueueStateCookieRepository._FixedCookieValidityMinutesKey)!, 10)
78
+ : 0;
79
+
80
+ return new StateInfo(
81
+ true,
82
+ true,
83
+ cookieValues.get(UserInQueueStateCookieRepository._QueueIdKey)!,
84
+ fixedCookieValidity,
85
+ cookieValues.get(UserInQueueStateCookieRepository._RedirectTypeKey)!);
86
+ }
87
+
88
+ private isCookieValid(
89
+ secretKey: string,
90
+ cookieValueMap: Map<string, string>,
91
+ eventId: string,
92
+ cookieValidityMinutes: number,
93
+ validateTime: boolean): boolean {
94
+
95
+ const storedHash = cookieValueMap.has(UserInQueueStateCookieRepository._HashKey) ?
96
+ cookieValueMap.get(UserInQueueStateCookieRepository._HashKey)! : "";
97
+ const issueTimeString = cookieValueMap.has(UserInQueueStateCookieRepository._IssueTimeKey) ?
98
+ cookieValueMap.get(UserInQueueStateCookieRepository._IssueTimeKey)! : "";
99
+ const queueId = cookieValueMap.has(UserInQueueStateCookieRepository._QueueIdKey) ?
100
+ cookieValueMap.get(UserInQueueStateCookieRepository._QueueIdKey)! : "";
101
+ const eventIdFromCookie = cookieValueMap.has(UserInQueueStateCookieRepository._EventIdKey) ?
102
+ cookieValueMap.get(UserInQueueStateCookieRepository._EventIdKey)! : "";
103
+ const redirectType = cookieValueMap.has(UserInQueueStateCookieRepository._RedirectTypeKey) ?
104
+ cookieValueMap.get(UserInQueueStateCookieRepository._RedirectTypeKey)! : "";
105
+ let fixedCookieValidityMinutes = cookieValueMap.has(UserInQueueStateCookieRepository._FixedCookieValidityMinutesKey) ?
106
+ cookieValueMap.get(UserInQueueStateCookieRepository._FixedCookieValidityMinutesKey)! : "";
107
+
108
+ const expectedHash = this.generateHash(
109
+ eventIdFromCookie,
110
+ queueId,
111
+ fixedCookieValidityMinutes,
112
+ redirectType,
113
+ issueTimeString,
114
+ secretKey);
115
+ if (expectedHash.length == 0) {
116
+ return false
117
+ }
118
+
119
+ if (expectedHash != storedHash)
120
+ return false;
121
+
122
+ if (eventId.toLowerCase() != eventIdFromCookie.toLowerCase())
123
+ return false;
124
+
125
+ if (validateTime) {
126
+ let validity: number = fixedCookieValidityMinutes.length > 0 ? parseInt(fixedCookieValidityMinutes, 10) : cookieValidityMinutes;
127
+ let expirationTime: number = parseInt(issueTimeString, 10) + validity * 60;
128
+
129
+ if (expirationTime < Utils.getCurrentTime())
130
+ return false;
131
+ }
132
+ return true;
133
+ }
134
+
135
+ public cancelQueueCookie(eventId: string, cookieDomain: string): void {
136
+ const cookieKey = UserInQueueStateCookieRepository.getCookieKey(eventId);
137
+ this.httpContextProvider.getHttpResponse().setCookie(cookieKey, "", cookieDomain, 0);
138
+ }
139
+
140
+ public reissueQueueCookie(eventId: string, cookieValidityMinutes: number, cookieDomain: string, secretKey: string): void {
141
+ const cookieKey = UserInQueueStateCookieRepository.getCookieKey(eventId);
142
+ const cookie = this.httpContextProvider.getHttpRequest().getCookieValue(cookieKey);
143
+
144
+ if (cookie == '')
145
+ return;
146
+
147
+ const cookieValues = CookieHelper.toMapFromValue(cookie);
148
+
149
+ if (!this.isCookieValid(secretKey, cookieValues, eventId, cookieValidityMinutes, true))
150
+ return;
151
+
152
+ let fixedCookieValidityMinutes = "";
153
+ if (cookieValues.has(UserInQueueStateCookieRepository._FixedCookieValidityMinutesKey)) {
154
+ fixedCookieValidityMinutes = cookieValues.get(UserInQueueStateCookieRepository._FixedCookieValidityMinutesKey)!.toString();
155
+ }
156
+
157
+ this.createCookie(
158
+ eventId,
159
+ cookieValues.get(UserInQueueStateCookieRepository._QueueIdKey)!,
160
+ fixedCookieValidityMinutes,
161
+ cookieValues.get(UserInQueueStateCookieRepository._RedirectTypeKey)!,
162
+ cookieDomain, secretKey);
163
+ }
164
+
165
+ private generateHash(
166
+ eventId: string,
167
+ queueId: string,
168
+ fixedCookieValidityMinutes: string,
169
+ redirectType: string,
170
+ issueTime: string,
171
+ secretKey: string): string {
172
+ let valueToHash = eventId + queueId + fixedCookieValidityMinutes + redirectType + issueTime;
173
+ return Utils.generateSHA256Hash(secretKey, valueToHash);
174
+ }
175
+ }
176
+
177
+ export class StateInfo {
178
+ constructor(public isFound: boolean,
179
+ public isValid: boolean,
180
+ public queueId: string,
181
+ public fixedCookieValidityMinutes: number,
182
+ public redirectType: string) {
183
+ }
184
+
185
+ isStateExtendable(): boolean {
186
+ return this.isValid && !this.fixedCookieValidityMinutes;
187
+ }
188
+ }
@@ -0,0 +1,4 @@
1
+ export const encodeURI = globalThis.encodeURI;
2
+ export const decodeURI = globalThis.decodeURI;
3
+ export const encodeURIComponent = globalThis.encodeURIComponent;
4
+ export const decodeURIComponent = globalThis.decodeURIComponent;