@queue-it/fastly 1.0.4 → 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.
@@ -1,245 +1,245 @@
1
- import {Utils, QueueUrlParams, QueueParameterHelper} from './QueueITHelpers'
2
- import {ActionTypes, RequestValidationResult, QueueEventConfig, CancelEventConfig, ValidationResult} from './Models'
3
- import {StateInfo, UserInQueueStateCookieRepository} from './UserInQueueStateCookieRepository'
4
-
5
- export class UserInQueueService {
6
- static readonly SDK_VERSION: string = "v3-asmscrpt-3.6.1";
7
-
8
- constructor(private userInQueueStateRepository: UserInQueueStateCookieRepository) {
9
- }
10
-
11
- private getValidTokenResult(
12
- config: QueueEventConfig,
13
- queueParams: QueueUrlParams,
14
- secretKey: string)
15
- : RequestValidationResult {
16
-
17
- this.userInQueueStateRepository.store(
18
- config.eventId,
19
- queueParams.queueId,
20
- queueParams.cookieValidityMinutes,
21
- config.cookieDomain,
22
- queueParams.redirectType,
23
- secretKey);
24
-
25
- return new RequestValidationResult(
26
- ActionTypes.QueueAction,
27
- config.eventId,
28
- queueParams.queueId,
29
- "",
30
- queueParams.redirectType,
31
- config.actionName,
32
- );
33
- }
34
-
35
- private getErrorResult(
36
- customerId: string,
37
- targetUrl: string,
38
- config: QueueEventConfig,
39
- qParams: QueueUrlParams,
40
- errorCode: string)
41
- : RequestValidationResult {
42
-
43
- let query = this.getQueryString(customerId, config.eventId, config.version, config.culture, config.layoutName, config.actionName) +
44
- "&queueittoken=" + qParams.queueITToken +
45
- "&ts=" + Utils.getCurrentTime().toString() +
46
- (targetUrl.length > 0 ? "&t=" + Utils.encodeUrl(targetUrl) : "");
47
- const uriPath = "error/" + errorCode + "/";
48
- const redirectUrl = this.generateRedirectUrl(config.queueDomain, uriPath, query);
49
-
50
- return new RequestValidationResult(
51
- ActionTypes.QueueAction,
52
- config.eventId,
53
- "",
54
- redirectUrl,
55
- "",
56
- config.actionName
57
- );
58
- }
59
-
60
- private getQueueResult(
61
- targetUrl: string,
62
- config: QueueEventConfig,
63
- customerId: string)
64
- : RequestValidationResult {
65
-
66
- let query = this.getQueryString(customerId, config.eventId, config.version, config.culture, config.layoutName, config.actionName) +
67
- (targetUrl.length > 0 ? "&t=" + Utils.encodeUrl(targetUrl) : "");
68
-
69
- const redirectUrl = this.generateRedirectUrl(config.queueDomain, "", query);
70
-
71
- return new RequestValidationResult(
72
- ActionTypes.QueueAction,
73
- config.eventId,
74
- "",
75
- redirectUrl,
76
- "",
77
- config.actionName
78
- );
79
- }
80
-
81
- private getQueryString(
82
- customerId: string,
83
- eventId: string,
84
- configVersion: i64,
85
- culture: string,
86
- layoutName: string,
87
- actionName: string)
88
- : string {
89
- const queryStringList = new Array<string>();
90
- queryStringList.push("c=" + Utils.encodeUrl(customerId));
91
- queryStringList.push("e=" + Utils.encodeUrl(eventId));
92
- queryStringList.push("ver=" + UserInQueueService.SDK_VERSION);
93
- queryStringList.push("cver=" + configVersion.toString());
94
- queryStringList.push("man=" + Utils.encodeUrl(actionName));
95
-
96
- if (culture.length > 0) {
97
- queryStringList.push("cid=" + Utils.encodeUrl(culture));
98
- }
99
-
100
-
101
- if (layoutName.length > 0) {
102
- queryStringList.push("l=" + Utils.encodeUrl(layoutName));
103
- }
104
-
105
- return queryStringList.join("&");
106
- }
107
-
108
-
109
- private generateRedirectUrl(queueDomain: string, uriPath: string, query: string): string {
110
- if (!Utils.endsWith(queueDomain, "/"))
111
- queueDomain = queueDomain + "/";
112
-
113
- return "https://".concat(queueDomain).concat(uriPath).concat("?").concat(query);
114
- }
115
-
116
- public validateQueueRequest(
117
- targetUrl: string,
118
- queueitToken: string,
119
- config: QueueEventConfig,
120
- customerId: string,
121
- secretKey: string): ValidationResult {
122
- const state: StateInfo = this.userInQueueStateRepository.getState(config.eventId,
123
- config.cookieValidityMinute, secretKey, true);
124
- if (state.isValid) {
125
- if (state.isStateExtendable() && config.extendCookieValidity) {
126
- this.userInQueueStateRepository.store(config.eventId,
127
- state.queueId,
128
- 0,
129
- config.cookieDomain,
130
- state.redirectType,
131
- secretKey);
132
- }
133
- return new ValidationResult(new RequestValidationResult(
134
- ActionTypes.QueueAction,
135
- config.eventId,
136
- state.queueId,
137
- "",
138
- state.redirectType,
139
- config.actionName
140
- ), null);
141
- }
142
-
143
- const queueParams = QueueParameterHelper.extractQueueParams(queueitToken);
144
-
145
- let requestValidationResult: RequestValidationResult | null;
146
- let isTokenValid: bool = false;
147
-
148
- if (queueParams != null) {
149
- const tokenValidationResult = this.validateToken(config, queueParams, secretKey);
150
- isTokenValid = tokenValidationResult.isValid;
151
-
152
- if (isTokenValid) {
153
- requestValidationResult = this.getValidTokenResult(config, queueParams, secretKey);
154
- } else {
155
- requestValidationResult = this.getErrorResult(customerId, targetUrl, config, queueParams, tokenValidationResult.errorCode);
156
- }
157
- } else {
158
- requestValidationResult = this.getQueueResult(targetUrl, config, customerId);
159
- }
160
-
161
- if (state.isFound && !isTokenValid) {
162
- this.userInQueueStateRepository.cancelQueueCookie(config.eventId, config.cookieDomain);
163
- }
164
-
165
- return new ValidationResult(requestValidationResult, null);
166
- }
167
-
168
- public validateCancelRequest(
169
- targetUrl: string,
170
- config: CancelEventConfig,
171
- customerId: string,
172
- secretKey: string): ValidationResult {
173
- //we do not care how long cookie is valid while canceling cookie
174
- const state = this.userInQueueStateRepository.getState(config.eventId, -1, secretKey, false);
175
-
176
- if (state.isValid) {
177
-
178
- this.userInQueueStateRepository.cancelQueueCookie(config.eventId, config.cookieDomain);
179
- const query = this.getQueryString(customerId, config.eventId, config.version, '', '', config.actionName) +
180
- (targetUrl.length > 0 ? "&r=" + Utils.encodeUrl(targetUrl) : "");
181
-
182
- const uriPath = "cancel/" + customerId + "/" + config.eventId + "/";
183
- const redirectUrl = this.generateRedirectUrl(config.queueDomain, uriPath, query);
184
- return new ValidationResult(new RequestValidationResult(
185
- ActionTypes.CancelAction,
186
- config.eventId,
187
- state.queueId,
188
- redirectUrl,
189
- state.redirectType,
190
- config.actionName), null);
191
- } else {
192
- return new ValidationResult(new RequestValidationResult(
193
- ActionTypes.CancelAction,
194
- config.eventId,
195
- "",
196
- "",
197
- "",
198
- config.actionName), null);
199
- }
200
- }
201
-
202
- public extendQueueCookie(
203
- eventId: string,
204
- cookieValidityMinutes: i64,
205
- cookieDomain: string,
206
- secretKey: string): void {
207
- this.userInQueueStateRepository.reissueQueueCookie(eventId, cookieValidityMinutes, cookieDomain, secretKey)
208
- }
209
-
210
- public getIgnoreResult(
211
- actionName: string): RequestValidationResult {
212
- return new RequestValidationResult(ActionTypes.IgnoreAction, "", "", "", "", actionName);
213
- }
214
-
215
- private validateToken(
216
- config: QueueEventConfig,
217
- queueParams: QueueUrlParams,
218
- secretKey: string): TokenValidationResult {
219
-
220
- const calculatedHash = Utils.generateSHA256Hash(secretKey, queueParams.queueITTokenWithoutHash);
221
-
222
- if (calculatedHash != queueParams.hashCode) {
223
- return new TokenValidationResult(false, "hash");
224
- }
225
-
226
-
227
- if (queueParams.eventId != config.eventId)
228
- return new TokenValidationResult(false, "eventid");
229
-
230
- if (queueParams.timeStamp < Utils.getCurrentTime())
231
- return new TokenValidationResult(false, "timestamp");
232
-
233
- return new TokenValidationResult(true, "");
234
- }
235
-
236
- }
237
-
238
- class TokenValidationResult {
239
- constructor(
240
- public isValid: bool,
241
- public errorCode: string) {
242
-
243
- }
244
-
245
- }
1
+ import {Utils, QueueUrlParams, QueueParameterHelper} from './QueueITHelpers'
2
+ import {ActionTypes, RequestValidationResult, QueueEventConfig, CancelEventConfig, ValidationResult} from './Models'
3
+ import {StateInfo, UserInQueueStateCookieRepository} from './UserInQueueStateCookieRepository'
4
+
5
+ export class UserInQueueService {
6
+ static readonly SDK_VERSION: string = "v3-asmscrpt-3.6.2";
7
+
8
+ constructor(private userInQueueStateRepository: UserInQueueStateCookieRepository) {
9
+ }
10
+
11
+ private getValidTokenResult(
12
+ config: QueueEventConfig,
13
+ queueParams: QueueUrlParams,
14
+ secretKey: string)
15
+ : RequestValidationResult {
16
+
17
+ this.userInQueueStateRepository.store(
18
+ config.eventId,
19
+ queueParams.queueId,
20
+ queueParams.cookieValidityMinutes,
21
+ config.cookieDomain,
22
+ queueParams.redirectType,
23
+ secretKey);
24
+
25
+ return new RequestValidationResult(
26
+ ActionTypes.QueueAction,
27
+ config.eventId,
28
+ queueParams.queueId,
29
+ "",
30
+ queueParams.redirectType,
31
+ config.actionName,
32
+ );
33
+ }
34
+
35
+ private getErrorResult(
36
+ customerId: string,
37
+ targetUrl: string,
38
+ config: QueueEventConfig,
39
+ qParams: QueueUrlParams,
40
+ errorCode: string)
41
+ : RequestValidationResult {
42
+
43
+ let query = this.getQueryString(customerId, config.eventId, config.version, config.culture, config.layoutName, config.actionName) +
44
+ "&queueittoken=" + qParams.queueITToken +
45
+ "&ts=" + Utils.getCurrentTime().toString() +
46
+ (targetUrl.length > 0 ? "&t=" + Utils.encodeUrl(targetUrl) : "");
47
+ const uriPath = "error/" + errorCode + "/";
48
+ const redirectUrl = this.generateRedirectUrl(config.queueDomain, uriPath, query);
49
+
50
+ return new RequestValidationResult(
51
+ ActionTypes.QueueAction,
52
+ config.eventId,
53
+ "",
54
+ redirectUrl,
55
+ "",
56
+ config.actionName
57
+ );
58
+ }
59
+
60
+ private getQueueResult(
61
+ targetUrl: string,
62
+ config: QueueEventConfig,
63
+ customerId: string)
64
+ : RequestValidationResult {
65
+
66
+ let query = this.getQueryString(customerId, config.eventId, config.version, config.culture, config.layoutName, config.actionName) +
67
+ (targetUrl.length > 0 ? "&t=" + Utils.encodeUrl(targetUrl) : "");
68
+
69
+ const redirectUrl = this.generateRedirectUrl(config.queueDomain, "", query);
70
+
71
+ return new RequestValidationResult(
72
+ ActionTypes.QueueAction,
73
+ config.eventId,
74
+ "",
75
+ redirectUrl,
76
+ "",
77
+ config.actionName
78
+ );
79
+ }
80
+
81
+ private getQueryString(
82
+ customerId: string,
83
+ eventId: string,
84
+ configVersion: i64,
85
+ culture: string,
86
+ layoutName: string,
87
+ actionName: string)
88
+ : string {
89
+ const queryStringList = new Array<string>();
90
+ queryStringList.push("c=" + Utils.encodeUrl(customerId));
91
+ queryStringList.push("e=" + Utils.encodeUrl(eventId));
92
+ queryStringList.push("ver=" + UserInQueueService.SDK_VERSION);
93
+ queryStringList.push("cver=" + configVersion.toString());
94
+ queryStringList.push("man=" + Utils.encodeUrl(actionName));
95
+
96
+ if (culture.length > 0) {
97
+ queryStringList.push("cid=" + Utils.encodeUrl(culture));
98
+ }
99
+
100
+
101
+ if (layoutName.length > 0) {
102
+ queryStringList.push("l=" + Utils.encodeUrl(layoutName));
103
+ }
104
+
105
+ return queryStringList.join("&");
106
+ }
107
+
108
+
109
+ private generateRedirectUrl(queueDomain: string, uriPath: string, query: string): string {
110
+ if (!Utils.endsWith(queueDomain, "/"))
111
+ queueDomain = queueDomain + "/";
112
+
113
+ return "https://".concat(queueDomain).concat(uriPath).concat("?").concat(query);
114
+ }
115
+
116
+ public validateQueueRequest(
117
+ targetUrl: string,
118
+ queueitToken: string,
119
+ config: QueueEventConfig,
120
+ customerId: string,
121
+ secretKey: string): ValidationResult {
122
+ const state: StateInfo = this.userInQueueStateRepository.getState(config.eventId,
123
+ config.cookieValidityMinute, secretKey, true);
124
+ if (state.isValid) {
125
+ if (state.isStateExtendable() && config.extendCookieValidity) {
126
+ this.userInQueueStateRepository.store(config.eventId,
127
+ state.queueId,
128
+ 0,
129
+ config.cookieDomain,
130
+ state.redirectType,
131
+ secretKey);
132
+ }
133
+ return new ValidationResult(new RequestValidationResult(
134
+ ActionTypes.QueueAction,
135
+ config.eventId,
136
+ state.queueId,
137
+ "",
138
+ state.redirectType,
139
+ config.actionName
140
+ ), null);
141
+ }
142
+
143
+ const queueParams = QueueParameterHelper.extractQueueParams(queueitToken);
144
+
145
+ let requestValidationResult: RequestValidationResult | null;
146
+ let isTokenValid: bool = false;
147
+
148
+ if (queueParams != null) {
149
+ const tokenValidationResult = this.validateToken(config, queueParams, secretKey);
150
+ isTokenValid = tokenValidationResult.isValid;
151
+
152
+ if (isTokenValid) {
153
+ requestValidationResult = this.getValidTokenResult(config, queueParams, secretKey);
154
+ } else {
155
+ requestValidationResult = this.getErrorResult(customerId, targetUrl, config, queueParams, tokenValidationResult.errorCode);
156
+ }
157
+ } else {
158
+ requestValidationResult = this.getQueueResult(targetUrl, config, customerId);
159
+ }
160
+
161
+ if (state.isFound && !isTokenValid) {
162
+ this.userInQueueStateRepository.cancelQueueCookie(config.eventId, config.cookieDomain);
163
+ }
164
+
165
+ return new ValidationResult(requestValidationResult, null);
166
+ }
167
+
168
+ public validateCancelRequest(
169
+ targetUrl: string,
170
+ config: CancelEventConfig,
171
+ customerId: string,
172
+ secretKey: string): ValidationResult {
173
+ //we do not care how long cookie is valid while canceling cookie
174
+ const state = this.userInQueueStateRepository.getState(config.eventId, -1, secretKey, false);
175
+
176
+ if (state.isValid) {
177
+
178
+ this.userInQueueStateRepository.cancelQueueCookie(config.eventId, config.cookieDomain);
179
+ const query = this.getQueryString(customerId, config.eventId, config.version, '', '', config.actionName) +
180
+ (targetUrl.length > 0 ? "&r=" + Utils.encodeUrl(targetUrl) : "");
181
+
182
+ const uriPath = "cancel/" + customerId + "/" + config.eventId + "/";
183
+ const redirectUrl = this.generateRedirectUrl(config.queueDomain, uriPath, query);
184
+ return new ValidationResult(new RequestValidationResult(
185
+ ActionTypes.CancelAction,
186
+ config.eventId,
187
+ state.queueId,
188
+ redirectUrl,
189
+ state.redirectType,
190
+ config.actionName), null);
191
+ } else {
192
+ return new ValidationResult(new RequestValidationResult(
193
+ ActionTypes.CancelAction,
194
+ config.eventId,
195
+ "",
196
+ "",
197
+ "",
198
+ config.actionName), null);
199
+ }
200
+ }
201
+
202
+ public extendQueueCookie(
203
+ eventId: string,
204
+ cookieValidityMinutes: i64,
205
+ cookieDomain: string,
206
+ secretKey: string): void {
207
+ this.userInQueueStateRepository.reissueQueueCookie(eventId, cookieValidityMinutes, cookieDomain, secretKey)
208
+ }
209
+
210
+ public getIgnoreResult(
211
+ actionName: string): RequestValidationResult {
212
+ return new RequestValidationResult(ActionTypes.IgnoreAction, "", "", "", "", actionName);
213
+ }
214
+
215
+ private validateToken(
216
+ config: QueueEventConfig,
217
+ queueParams: QueueUrlParams,
218
+ secretKey: string): TokenValidationResult {
219
+
220
+ const calculatedHash = Utils.generateSHA256Hash(secretKey, queueParams.queueITTokenWithoutHash);
221
+
222
+ if (calculatedHash != queueParams.hashCode) {
223
+ return new TokenValidationResult(false, "hash");
224
+ }
225
+
226
+
227
+ if (queueParams.eventId != config.eventId)
228
+ return new TokenValidationResult(false, "eventid");
229
+
230
+ if (queueParams.timeStamp < Utils.getCurrentTime())
231
+ return new TokenValidationResult(false, "timestamp");
232
+
233
+ return new TokenValidationResult(true, "");
234
+ }
235
+
236
+ }
237
+
238
+ class TokenValidationResult {
239
+ constructor(
240
+ public isValid: bool,
241
+ public errorCode: string) {
242
+
243
+ }
244
+
245
+ }