/
Signer.ts
602 lines (567 loc) · 18.8 KB
/
Signer.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
import * as crypto from 'crypto';
import * as fs from 'fs';
import { JSDOM } from 'jsdom';
import { URL } from 'url';
type CodesDict = {
[key: number]: {
code: string;
parent: string;
};
};
/**
* HMAC signing utility. Methods are named after what it is to be signed, to
* allow for an easy to read code in the user application.
*
* @tutorial https://wiki.foxycart.com/v/2.0/hmac_validation
* @example const signer = new Signer(mySecret); // or const signer = new Signer(); signer.setSecret(mySecret);
* signer.signHtml('<html lang="en">...</html>'); // signs a URL
* signer.signFile("/var/www/html/src/.../index.html", "/var/www/html/target/.../index.html"); // signs an HTML file
* signer.signUrl("http://..."); // signs a URL
*/
export class Signer {
public static readonly cartExcludes = [
// Analytics values
"_",
"_ga",
"_ke",
// Cart values
"cart",
"fcsid",
"empty",
"coupon",
"output",
"sub_token",
"redirect",
"callback",
"locale",
"template_set",
// Checkout pre-population values
"customer_email",
"customer_first_name",
"customer_last_name",
"customer_address1",
"customer_address2",
"customer_city",
"customer_state",
"customer_postal_code",
"customer_country",
"customer_phone",
"customer_company",
"billing_first_name",
"billing_last_name",
"billing_address1",
"billing_address2",
"billing_city",
"billing_postal_code",
"billing_region",
"billing_phone",
"billing_company",
"shipping_first_name",
"shipping_last_name",
"shipping_address1",
"shipping_address2",
"shipping_city",
"shipping_state",
"shipping_country",
"shipping_postal_code",
"shipping_region",
"shipping_phone",
"shipping_company",
];
public static readonly cartExcludePrefixes = ["h:", "x:", "__", "utm_"];
private __cartURL = '/cart?';
private __secret?: string;
/**
* Creates an instance of this class.
*
* @param secret OAuth2 client secret for your integration.
*/
constructor(secret: string | null = null) {
if (secret) {
this.setSecret(secret);
}
}
/**
* Sets the HMAC secret.
* It won't be possible to sign anything without this secret.
*
* @param secret OAuth2 client secret for your integration.
* @returns Signer to allow for convenient concatenation.
*/
public setSecret(secret: string): Signer {
this.__secret = secret;
return this;
}
/**
* Signs a whole HTML snippet.
*
* @param htmlStr HTML snippet to sign.
* @returns the HTML snippet signed.
*/
public signHtml(htmlStr: string): string {
const dom = new JSDOM(htmlStr);
this.__fragment(dom.window.document);
return dom.serialize();
}
/**
* Signs a file asynchronously.
*
* @param inputPath Path of the file to sign.
* @param outputPath Path of the file where the signed result will be stored.
* @param readFunc a function that should read from file
* @param writeFunc a function that should write to to file
* @returns a ParentNode object of the signed HTML.
*/
public signFile(
inputPath: string,
outputPath: string,
readFunc: (arg0: string) => Promise<JSDOM> = JSDOM.fromFile,
writeFunc: (path: string, content: string, callback: (err: any) => void) => void = fs.writeFile
): Promise<ParentNode> {
return new Promise((resolve, reject) => {
readFunc(inputPath).then(dom => {
const signed = this.__fragment(dom.window.document);
writeFunc(outputPath, dom.serialize(), err => {
if (err) reject(err);
else resolve(signed);
});
});
});
}
/**
* Signs a query string.
* All query fields withing the query string will be signed, provided it is a proper URL and there is a code field
*
* @param urlStr Full URL including the query string that needs to be signed.
* @returns the signed query string.
*/
public signUrl(urlStr: string): string {
// Build a URL object
if (Signer.__isSigned(urlStr)) {
console.error('Attempt to sign a signed URL', urlStr);
return urlStr;
}
const cartURLpattern = new RegExp(this.__cartURL, 'i');
if (!cartURLpattern.test(urlStr)) {
return urlStr;
}
// Do not change invalid URLs
let url;
let stripped;
try {
url = new URL(urlStr);
stripped = new URL(url.origin);
} catch (e) {
//console.assert(e.code === "ERR_INVALID_URL");
return urlStr;
}
const originalParams = url.searchParams;
const newParams = stripped.searchParams;
const code = Signer.__getCodeFromURL(url);
// If there is no code, return the same URL
if (!code) {
return urlStr;
}
// sign the url object
for (const p of originalParams.entries()) {
const signed = this.__signQueryArg(
decodeURIComponent(p[0]),
decodeURIComponent(code),
decodeURIComponent(p[1])
).split('=');
newParams.set(signed[0], signed[1]);
}
url.search = newParams.toString();
const result = decodeURIComponent(url.toString());
return result;
}
/**
* Signs input name.
*
* @param name Name of the input element.
* @param code Product code.
* @param parentCode Parent product code.
* @param value Input value.
* @returns the signed input name.
*/
public signName(name: string, code: string, parentCode = '', value?: string | number): string {
name = name.replace(/ /g, '_');
if (this.__shouldSkipInput(name)) {
return name;
} else {
const signature = this.__signProduct(code + parentCode, name, value);
const encodedName = encodeURIComponent(name);
const nameAttr = Signer.__buildSignedName(encodedName, signature, value);
return nameAttr;
}
}
/**
* Signs input value.
*
* @param name Name of the input element.
* @param code Product code.
* @param parentCode Parent product code.
* @param value Input value.
* @returns the signed value.
*/
public signValue(name: string, code: string, parentCode = '', value?: string | number): string {
name = name.replace(/ /g, '_');
if (this.__shouldSkipInput(name)) {
return (value as string);
} else {
const signature = this.__signProduct(code + parentCode, name, value);
const valueAttr = Signer.__buildSignedValue(signature, value);
return valueAttr;
}
}
/**
* Signs a product composed of code, name and value.
*
* @param code of the product.
* @param name name of the product.
* @param value of the product.
* @returns the signed product.
* @private
*/
private __signProduct(code: string, name: string, value?: string | number): string {
return this.__message(code + name + Signer.__valueOrOpen(value));
}
/**
* Signs a single query argument to be used in `GET` requests.
*
* @param name of the argument.
* @param code of the product.
* @param value of the argument.
* @returns the signed query argument.
* @private
*/
private __signQueryArg(name: string, code: string, value?: string): string {
name = name.replace(/ /g, '_');
if (this.__shouldSkipInput(name)) {
return `${name}=${value}`;
} else {
code = code.replace(/ /g, '_');
const signature = this.__signProduct(code, name, value);
const encodedName = encodeURIComponent(name).replace(/%20/g, '+');
const encodedValue = encodeURIComponent(Signer.__valueOrOpen(value)).replace(/%20/g, '+');
return Signer.__buildSignedQueryArg(encodedName, signature, encodedValue);
}
}
/**
* Signs an input element.
*
* @param el the input element
* @param codes the codes dict object containing the code and parent code
* @returns the signed element
* @private
*/
private __signInput(el: HTMLInputElement, codes: CodesDict): HTMLInputElement {
const namePrefix = Signer.__splitNamePrefix(el.name);
const nameString = namePrefix[1];
const prefix = namePrefix[0];
const code = codes[prefix].code;
const parentCode = codes[prefix].parent;
const value = el.value;
const signedName = this.signName(nameString, code, parentCode, value);
el.setAttribute('name', prefix + ':' + signedName);
return el;
}
/**
* Signs a texArea element.
*
* @param el the textArea element.
* @param codes the codes dict object containing the code and parent code
* @returns the signed textarea element.
* @private
*/
private __signTextArea(el: HTMLTextAreaElement, codes: CodesDict): HTMLTextAreaElement {
const namePrefix = Signer.__splitNamePrefix(el.name);
const nameString = namePrefix[1];
const prefix = namePrefix[0];
const code = codes[prefix].code;
const parentCode = codes[prefix].parent;
const value = '';
const signedName = this.signName(nameString, code, parentCode, value);
el.setAttribute('name', prefix + ':' + signedName);
return el;
}
/**
* Signs all option elements within a Select element.
*
* @param el the select element.
* @param codes the codes dict object containing the code and parent code.
* @returns the signed select element.
* @private
*/
private __signSelect(el: HTMLSelectElement, codes: CodesDict): HTMLSelectElement {
el.querySelectorAll('option').forEach(opt => {
this.__signOption(opt, codes);
});
return el;
}
/**
* Sign an option element.
* Signatures are added to the value attribute on options.
* This function may also be used to sign radio buttons.
*
* @param el the option element to be signed.
* @param codes the codes dict object containing the code and parent code.
* @returns the signed option element.
* @private
*/
private __signOption(
el: HTMLOptionElement | HTMLInputElement,
codes: CodesDict
): HTMLOptionElement | HTMLInputElement {
// Get the name parameter, either from the "select"
// parent element of an option tag or from the name
// attribute of the input element itself
let n = (el as HTMLInputElement).name;
if (n === undefined) {
const p = el.parentElement as HTMLSelectElement;
n = p.name;
}
const namePrefix = Signer.__splitNamePrefix(n);
const nameString = namePrefix[1];
const prefix = namePrefix[0];
const code = codes[prefix].code;
const parentCode = codes[prefix].parent;
const value = el.value;
const signedValue = this.signValue(nameString, code, parentCode, value);
el.setAttribute('value', prefix + ':' + signedValue);
return el;
}
/**
* Signs a radio button. Radio buttons use the value attribute to hold their signatures.
*
* @param el the radio button element.
* @param codes the codes dict object containing the code and parent code.
* @returns the signed radio button.
* @private
*/
private __signRadio(el: HTMLInputElement, codes: CodesDict): HTMLInputElement {
return this.__signOption(el, codes) as HTMLInputElement;
}
/**
* Splits a string using the prefix pattern for foxy store.
* The prefix pattern allows for including more than a single product in a given GET or POST request.
*
* @param name the name to be separated into prefix and name.
* @returns an array with [prefix, name]
* @private
*/
private static __splitNamePrefix(name: string): [number, string] {
const namePrefix = name.split(':');
if (namePrefix.length == 2) {
return [parseInt(namePrefix[0], 10), namePrefix[1]];
}
return [0, name];
}
/**
* Retrieve a parent code value from a form, given a prefix.
*
* @param formElement the element with the code and parent code values.
* @param prefix the prefix used in hte element.
* @returns the parentCode
* @private
*/
private static __retrieveParentCode(formElement: Element, prefix: string | number = ''): string {
let result = ''; // A blank string indicates no parent
const separator = prefix ? ':' : '';
const parentCodeEl = formElement.querySelector(`[name='${prefix}${separator}parent_code']`);
if (parentCodeEl) {
const parentCode = parentCodeEl.getAttribute('value');
if (parentCode !== null) {
result = parentCode;
}
}
return result;
}
/**
* Signs a whole form element.
*
* @param formElement the form element to be signed.
* @private
*/
private __signForm(formElement: Element): void {
// Grab all codes within the form element
const codeList: NodeList = formElement.querySelectorAll('[name$=code]');
// Store all codes in a object
const codes: CodesDict = {};
for (const node of codeList) {
const nameAttr = (node as Element).getAttribute('name');
const codeValue = (node as Element).getAttribute('value') ?? '';
if (nameAttr && nameAttr.match(/^([0-9]{1,3}:)?code/)) {
const namePrefix = nameAttr.split(':');
const prefix = parseInt(namePrefix[0]);
if (namePrefix.length == 2) {
// Store prefix in codes list
codes[prefix] = {
code: codeValue,
parent: Signer.__retrieveParentCode(formElement, prefix),
};
} else if (codes[0] === undefined) {
// Allow to push a single code without prefix
codes[0] = {
code: codeValue,
parent: Signer.__retrieveParentCode(formElement),
};
} else {
const documentationURL = 'https://wiki.foxycart.com/v/2.0/hmac_validation#multiple_products_in_one_form';
const errorMsg = `There are multiple codes in the form element. Please, check ${documentationURL}`;
throw new Error(errorMsg);
}
}
}
// Sign inputs
formElement.querySelectorAll('input[name]').forEach(i => {
if (i.getAttribute('type') === 'radio') {
this.__signRadio(i as HTMLInputElement, codes);
} else {
this.__signInput(i as HTMLInputElement, codes);
}
});
// Sign selects
formElement.querySelectorAll('select[name]').forEach(s => this.__signSelect(s as HTMLSelectElement, codes));
// Sign textAreas
formElement.querySelectorAll('textarea[name]').forEach(s => this.__signTextArea(s as HTMLTextAreaElement, codes));
}
/**
* Builds the value for the signed "name" attribute value given it components.
*
* @param name that was signed
* @param signature the resulting signature
* @param value of the field that, if equal to --OPEN-- identifies an editable field.
* @returns the signed value for the "name" attribute
* @private
*/
private static __buildSignedName(name: string, signature: string, value?: string | number) {
const open = Signer.__valueOrOpen(value) == '--OPEN--' ? '||open' : '';
return `${name}||${signature}${open}`;
}
/**
* Builds a signed name given it components.
*
* @param signature the resulting signature.
* @param value the value signed.
* @returns the built signed value
* @private
*/
private static __buildSignedValue(signature: string, value?: string | number) {
const open = Signer.__valueOrOpen(value) == '--OPEN--' ? '||open' : (value as string);
return `${open}||${signature}`;
}
/**
* Builds a signed query argument given its components.
*
* @param name the argument name.
* @param signature the resulting signature.
* @param value the value signed.
* @returns the built query string argument.
* @private
*/
private static __buildSignedQueryArg(name: string, signature: string, value: string | number) {
return `${name}||${signature}=${value}`;
}
/**
* Returns the value of a field on the `--OPEN--` string if the value is not defined.
* Please, notice that `0` is an acceptable value.
*
* @param value of the field.
* @returns '--OPEN--' or the given value.
* @private
*/
private static __valueOrOpen(value: string | number | undefined): string | number {
if (value === undefined || value === null || value === '') {
return '--OPEN--';
}
return value;
}
/**
* Check if a href string is already signed. Signed strings contain two consecutive pipes
* followed by 64 hexadecimal characters.
*
* This method **does not validates the signature**.
* It only checks if the format of the string is evidence that it is signed.
*
* @param url the potentially signed URL.
* @returns true if the string format is evidence that it is already signed.
* @private
*/
private static __isSigned(url: string): boolean {
return url.match(/^.*\|\|[0-9a-fA-F]{64}/) != null;
}
/**
* Returns the code from a URL or undefined if it does not contain a code.
*
* @param url the URL to retrieve the code from.
* @returns the code found, or undefined if no code was found.
* @private
*/
private static __getCodeFromURL(url: URL): string | undefined {
for (const p of url.searchParams) {
if (p[0] == 'code') {
return p[1];
}
}
}
/**
* Find all cart forms in a document fragment that contain an input named `code`.
*
* @param doc the document fragment potentially containing cart forms.
* @returns an array of the form elements found.
* @private
*/
private static __findCartForms(doc: ParentNode): HTMLFormElement[] {
return Array.from(doc.querySelectorAll('form')).filter(e => e.querySelector('[name=code]'));
}
/**
* Signs a document fragment. This method is used to sign HTML snippets.
*
* @param doc an HTML doc fragment
* @returns the signed HTML snippet
* @private
*/
private __fragment(doc: ParentNode): ParentNode {
doc.querySelectorAll(`a[href*='${this.__cartURL}']`).forEach(l => {
const anchor = l as HTMLAnchorElement;
anchor.href = this.signUrl(anchor.href);
});
Signer.__findCartForms(doc).forEach(this.__signForm.bind(this));
return doc;
}
/**
* Signs a simple message. This function can only be invoked after the secret has been defined. The secret can be defined either in the construction method as in `new FoxySigner(mySecret)` or by invoking the setSecret method, as in `signer.setSecret(mySecret)`
*
*
* @param message the message to be signed.
* @returns signed message.
* @private
*/
private __message(message: string): string {
if (this.__secret === undefined) {
throw new Error('No secret was provided to build the hmac');
}
const hmac = crypto.createHmac('sha256', this.__secret);
hmac.update(message);
return hmac.digest('hex');
}
/**
* Checks if a name should be skipped.
*
* @param name that could be signed
* @returns it should be skipped, i.e. not be signed
* @private
*/
private __shouldSkipInput(name: string): boolean {
const prefixStripped = name.replace(/^\d:/, "");
return (
Signer.cartExcludes.includes(prefixStripped) ||
Signer.cartExcludePrefixes.some(
(p) =>
name.toLowerCase().startsWith(p) ||
(name.startsWith("0:") && prefixStripped.toLowerCase().startsWith(p))
)
);
}
}