maskdata 1.3.2 → 1.3.4

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.
package/README.md CHANGED
@@ -2,49 +2,44 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
2
2
  <br/>Also, it provides utility methods to get a field, or replace a field from any complex/nested JSON.
3
3
 
4
4
  # Table of Contents
5
- - [Features](#features)
6
5
  - [Install maskdata](#install-maskdata)
7
- - [Version 1.3.2 Features](#release-features)
8
- - [How to Use](#how-to-use)
6
+ - [Version 1.3.4 Features](#release-features)
9
7
  - [Maskdata for Typescript](#maskdata-for-typescript)
10
- - [Mask Card number](#mask-card-number)
11
- - [Mask Email](#mask-email-id)
12
- - [Mask Email id with the default configuration](#mask-email-id-with-the-default-configuration)
13
- - [Mask JSON fields](#mask-json)
14
- - [JSON mask examples with nested fields](#json-mask-examples)
15
- - [Mask multiple fields at once using .* and [*].](#mask-multiple-fields)
16
- - [Recursive masking / Mask all the fields with a name in the json](#recursive-masking)
17
- - [Mask Password](#mask-password)
18
- - [Mask Password with the default configuration](#mask-password-with-the-default-configuration)
19
- - [Mask Phone Number](#mask-phone-number)
20
- - [Mask Phone Number with the default configuration](#mask-phone-number-with-the-default-configuration)
21
- - [Generic String masking](#generic-string-masking)
22
- - [Mask words/characters in a string](#mask-the-characters-or-words-in-the-string)
23
- - [Mask UUID](#mask-uuid)
24
- - [Mask JWT Token](#mask-jwt-token)
25
- - [Get Nested JSON Property](#get-nested-json-property)
26
- - [Replace the value of a JSON field](#replace-the-value-of-a-json-field)
8
+ - [How to Mask](#how-to-mask)
9
+ - [Card number](#mask-card-number)
10
+ - [Email](#mask-email-id)
11
+ - [Password](#mask-password)
12
+ - [Phone Number](#mask-phone-number)
13
+ - [Generic String](#generic-string-masking)
14
+ - [Words/characters in a string](#mask-the-characters-or-words-in-the-string)
15
+ - [UUID](#mask-uuid)
16
+ - [JWT Token](#mask-jwt-token)
17
+ - [Object / JSON fields](#mask-json)
18
+ - [JSON mask examples with nested fields](#json-mask-examples)
19
+ - [Mask multiple fields at once using .* and [*].](#mask-multiple-fields)
20
+ - [Recursive masking / Mask all the fields with a name in the json](#recursive-masking)
21
+ - [Get Nested JSON Property](#get-nested-json-property)
22
+ - [Replace the value of a JSON field](#replace-the-value-of-a-json-field)
27
23
  - [Report Bugs](#report-bugs)
28
24
  - [Give a Star](#give-a-star)
29
25
  - [LICENSE - "MIT"](#license---mit)
30
26
 
31
- # Features
32
- * Mask Card numbers
33
- * Mask Email ids
34
- * Mask Password
35
- * Mask Phone numbers
36
- * Mask the given words/substrings from throughout a String
37
- * Mask UUIDs
38
- * Mask JWT tokens
39
- * Mask JSON - JSON can contain cards, emails, passwords, phones, strings, and UUIDs. Mask all of them with a single call using - [Mask JSON fields](#mask-json)
40
- * Get nested field from JSON
41
- * Set/Replace nested field values from JSON
42
- * [Maskdata for typescript](#maskdata-for-typescript)
27
+ <br/>
43
28
 
44
29
  # Install maskdata
45
30
  > npm i maskdata
46
31
 
32
+ <br/>
33
+
47
34
  # Release Features
35
+ ### Version 1.3.4
36
+ - Dependency version upgrades
37
+ ### Version 1.3.3
38
+ - Better documentation
39
+ - Bug fix and enabling strings with regex characters in the string mask functionality
40
+ - https://github.com/Sumukha1496/maskdata/issues/49
41
+ - Deprecate stringmask v1
42
+
48
43
  ### Version 1.3.2
49
44
  - Recursive masking feature: [Details](#recursive-masking)
50
45
  - https://github.com/Sumukha1496/maskdata/issues/45
@@ -59,18 +54,18 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
59
54
  - https://github.com/Sumukha1496/maskdata/issues/42
60
55
  - Supporting generic string masking(string mask V2) as part of maskJson2: [generic String masking in a JSON](#json-mask-examples)
61
56
 
62
- # How to Use
63
- ```javascript
64
- const MaskData = require('./maskdata');
65
- ```
66
-
67
57
  <br/>
68
58
 
69
59
  ## Maskdata for Typescript
70
60
  > Follow this document for more details:
71
61
  [Maskdata for typescript](./MASKDATA_FOR_TYPESCRIPT.md)
72
62
 
73
- <br/>
63
+ <br/><br/>
64
+
65
+ # How to Mask
66
+ ```javascript
67
+ const MaskData = require('./maskdata');
68
+ ```
74
69
 
75
70
  ## Mask card number
76
71
  This will mask the digits in a card number.<br/>This will mask only the numerical data and not any non-numeric delimiters, alphabets, or any other types of data
@@ -102,14 +97,20 @@ const cardAfterMasking = MaskData.maskCard(cardNumber, maskCardOptions);
102
97
 
103
98
 
104
99
  ## Mask Email id
105
- Use this method instead of maskEmail(). To mask with the default options, don't pass the configurations.
100
+ Function to mask email id. Input must have a '@' in it.
106
101
  ```javascript
107
102
  const MaskData = require('./maskdata');
108
103
 
109
104
  const emailMask2Options = {
105
+ // Character to mask the data. The default value is '*'
110
106
  maskWith: "*",
107
+ // If we want to keep the starting 'n' characters before the '@' unmasked.
108
+ // Default value is 3
111
109
  unmaskedStartCharactersBeforeAt: 3,
110
+ // If we want to keep the last 'n' characters AFTER the '@' unmasked.
111
+ // Default value is 2
112
112
  unmaskedEndCharactersAfterAt: 2,
113
+ // Flag to mask or show '@'. Default value is false means do not mask
113
114
  maskAtTheRate: false
114
115
  };
115
116
 
@@ -130,380 +131,25 @@ Here,
130
131
  2. **unmaskedEndCharactersAfterAt** --> number of characters not to be masked starting from the end till @.
131
132
  * If unmaskedEndCharactersAfterAt > number of characters after @, then it will not mask the characters after @
132
133
 
133
- ### Mask only the characters before '@'
134
+ ### Mask only the characters before '@' / Show only the domain
134
135
  ```javascript
135
136
  const MaskData = require('./maskdata');
136
137
 
137
138
  const emailMask2Options = {
138
139
  maskWith: "*",
139
- unmaskedStartCharactersBeforeAt: 0,
140
- unmaskedEndCharactersAfterAt: 257, // Give a number which is more than the characters after @
140
+ unmaskedStartCharactersBeforeAt: 0, // Set this to 0 to mask the first part
141
+ unmaskedEndCharactersAfterAt: 257, // Enter a large number which is always more than the characters after @
141
142
  maskAtTheRate: false
142
143
  };
143
144
 
144
145
  const email = "abcd@email.com";
145
146
 
146
- const maskedEmail = MaskData.maskEmail2(email, emailMask2Options);
147
-
148
- //Output: ****@email.com
149
-
150
- ```
151
-
152
- ## Mask Email id with the default configuration
153
- To mask with the default options, don't pass the configurations.
154
- ```javascript
155
- const MaskData = require('./maskdata');
156
-
157
- /** Default Options
158
- maskWith: "*",
159
- unmaskedStartCharactersBeforeAt: 3,
160
- unmaskedEndCharactersAfterAt: 2,
161
- maskAtTheRate: false
162
- **/
163
- const email = "my.test.email@testEmail.com";
164
-
165
- const maskedEmail = MaskData.maskEmail2(email);
166
-
167
- //Output: my.********@**********om
168
-
169
- ```
170
-
171
- ## Mask JSON
172
- This is the new functionality in version 1.2.0+ to handle masking of multiple types of data in the JSON object with a single mask function call.
173
-
174
- ```javascript
175
- const MaskData = require('./maskdata');
176
-
177
- // Default configs are as below. If specific masking changes are needed, use the corresponding configs for each type of field.
178
- const defaultjsonMask2Configs = {
179
- cardMaskOptions: defaultCardMaskOptions, // Optional
180
- cardFields: [], // List of card fields to be masked
181
-
182
- emailMaskOptions: defaultEmailMask2Options, // Optional
183
- emailFields: [], // List of email fields to be masked
184
-
185
- // For passwords, tokens, etc...
186
- passwordMaskOptions: defaultPasswordMaskOptions, // Optional
187
- passwordFields: [], // List of password fields to be masked
188
-
189
- phoneMaskOptions: defaultPhoneMaskOptions, // Optional
190
- phoneFields: [], // List of phone fields to be masked
191
-
192
- stringMaskOptions: defaultStringMaskOptions, // Mandatory if stringFields are given. Otherwise, stringFields won't be masked
193
- stringFields: [], // List of String fields to be masked
194
-
195
- uuidMaskOptions: defaultUuidMaskOptions, // Optional
196
- uuidFields: [], // List of UUID fields to be masked
197
-
198
- jwtMaskOptions: defaultJwtMaskOptions, // Optional
199
- jwtFields: [], // List of JWT fields to be masked
200
-
201
- genericStrings: [
202
- {
203
- config: defaultStringMaskV2Options,
204
- fields: []
205
- }
206
- ]
207
- };
208
- ```
209
- > NOTE: For details on the configs mentioned above, refer: <br /> [defaultCardMaskOptions](#mask-card-number)<br/>[defaultEmailMask2Options](#mask-email-id)<br/> [defaultPasswordMaskOptions](#mask-password)<br/> [defaultPhoneMaskOptions](#mask-phone-number)<br/> [defaultStringMaskOptions](#mask-the-characters-or-words-in-the-string)<br/> [defaultUuidMaskOptions](#mask-uuid)<br /> [defaultJwtMaskOptions](#mask-jwt-token)<br /> [defaultStringMaskV2Options / generic String Maksing](#generic-string-masking)
210
-
211
- ```javascript
212
- const defaultjsonMask2Configs = {
213
- cardMaskOptions: {
214
- maskWith: "*",
215
- unmaskedStartDigits: 4,
216
- unmaskedEndDigits: 1
217
- },
218
- cardFields: [],
219
-
220
- emailMaskOptions: {
221
- maskWith: "*",
222
- unmaskedStartCharactersBeforeAt: 3,
223
- unmaskedEndCharactersAfterAt: 2,
224
- maskAtTheRate: false
225
- },
226
- emailFields: [],
227
-
228
- passwordMaskOptions: {
229
- maskWith: "*",
230
- maxMaskedCharacters: 16,
231
- unmaskedStartCharacters: 0,
232
- unmaskedEndCharacters: 0
233
- },
234
- passwordFields: [],
235
-
236
- phoneMaskOptions: {
237
- maskWith: "*",
238
- unmaskedStartDigits: 4,
239
- unmaskedEndDigits: 1
240
- },
241
- phoneFields: [],
242
-
243
- stringMaskOptions: {
244
- maskWith: "*",
245
- maskOnlyFirstOccurance: false,
246
- values: [],
247
- maskAll: false,
248
- maskSpace: true
249
- },
250
- stringFields: [],
251
-
252
- uuidMaskOptions: {
253
- maskWith: "*",
254
- unmaskedStartCharacters: 0,
255
- unmaskedEndCharacters: 0
256
- },
257
- uuidFields: [],
258
-
259
- jwtMaskOptions: {
260
- maskWith: '*',
261
- maxMaskedCharacters: 512,
262
- maskDot: true,
263
- maskHeader: true,
264
- maskPayload: true,
265
- maskSignature: true
266
- },
267
- jwtFields: [],
268
- // To extend the mask function to other types of data.
269
- genericStrings: [
270
- {
271
- config: {
272
- maskWith: "*",
273
- maxMaskedCharacters: 256,
274
- unmaskedStartDigits: 0,
275
- unmaskedEndDigits: 0
276
- },
277
- fields: []
278
- }
279
- ]
280
- };
281
-
282
- ```
283
-
284
- ### JSON mask examples
285
- Example1:
286
- ```javascript
287
- const jsonInput = {
288
- 'credit': '1234-5678-8765-1234',
289
- 'debit': '0000-1111-2222-3333',
290
- 'primaryEmail': 'primary@Email.com',
291
- 'secondaryEmail': 'secondary@Email.com',
292
- 'password': 'dummyPassword',
293
- 'homePhone': "+1 1234567890",
294
- 'workPhone': "+1 9876543210",
295
- 'addressLine1': "This is my addressline 1. This is my home",
296
- 'addressLine2': "AddressLine 2",
297
- 'uuid1': '123e4567-e89b-12d3-a456-426614174000',
298
- 'randomStrings': {
299
- 'row1': 'This is row 1 random string',
300
- 'row2': ['Entry1', 'Entry2', 'Entry3'],
301
- 'row3': {
302
- 'key1': 'Row3 Object 1',
303
- 'key2': 'Row3 Object 2',
304
- 'key3': ['Entry1', 'Entry2', 'Entry3']
305
- }
306
- }
307
- };
308
-
309
- const jsonMaskConfig = {
310
- cardFields: ['credit', 'debit'],
311
- emailFields: ['primaryEmail', 'secondaryEmail'],
312
- passwordFields: ['password'],
313
- phoneFields: ['homePhone', 'workPhone'],
314
- stringMaskOptions: {
315
- maskWith: "*",
316
- maskOnlyFirstOccurance: false,
317
- values: ["This"]
318
- },
319
- stringFields: ['addressLine1', 'addressLine2'],
320
- uuidFields: ['uuid1'],
321
- genericStrings: [
322
- {
323
- fields: ['randomStrings.row1'],
324
- config: {
325
- maskWith: '*',
326
- unmaskedStartCharacters: 2,
327
- unmaskedEndCharacters: 3,
328
- maxMaskedCharacters: 8
329
- }
330
- },
331
- { fields: ['randomStrings.row2.*'], config: { maskWith: 'X', unmaskedEndCharacters: 1 } },
332
- { fields: ['randomStrings.row3.key1'] },
333
- {
334
- fields: ['randomStrings.row3.key3.*'],
335
- config: { maskWith: '@', unmaskedEndCharacters: 1 }
336
- }
337
- ]
338
- };
339
-
340
- const maskedJsonOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
341
-
342
- Output:
343
- {
344
- credit: '1234-****-****-***4',
345
- debit: '0000-****-****-***3',
346
- primaryEmail: 'pri****@*******om',
347
- secondaryEmail: 'sec******@*******om',
348
- password: '*************',
349
- homePhone: '+1 1********0',
350
- workPhone: '+1 9********0',
351
- addressLine1: '**** is my addressline 1. **** is my home',
352
- addressLine2: 'AddressLine 2',
353
- uuid1: '********-****-****-****-************',
354
- randomStrings: {
355
- row1: 'Th***ing',
356
- row2: ['XXXXX1', 'XXXXX2', 'XXXXX3'],
357
- row3: {
358
- key1: '*************',
359
- key2: 'Row3 Object 2',
360
- key3: ['@@@@@1', '@@@@@2', '@@@@@3']
361
- }
362
- }
363
- }
364
-
365
-
366
- Example2: Mask with custom configs for each/any type of fields
367
-
368
- const jsonInput2 = {
369
- 'credit': '1234-5678-8765-1234',
370
- 'debit': '0000-1111-2222-3333',
371
- 'primaryEmail': 'primary@Email.com',
372
- 'secondaryEmail': 'secondary@Email.com',
373
- 'password': 'dummyPasswordANDdummyPassword',
374
- 'homePhone': "+1 1234567890",
375
- 'workPhone': "+1 9876543210",
376
- 'addressLine1': "This is my addressline 1. This is my home",
377
- 'addressLine2': "AddressLine 2",
378
- 'uuid1': '123e4567-e89b-12d3-a456-426614174000'
379
- };
380
-
381
- const jsonMaskConfig2 = {
382
- // Card
383
- cardMaskOptions: { maskWith: "X", unmaskedStartDigits: 2,unmaskedEndDigits: 4 },
384
- cardFields: ['credit', 'debit'],
385
-
386
- // Email
387
- emailMaskOptions: { maskWith: "*", unmaskedStartCharactersBeforeAt: 2, unmaskedEndCharactersAfterAt: 2, maskAtTheRate: false },
388
- emailFields: ['primaryEmail', 'secondaryEmail'],
389
-
390
- // Password
391
- passwordMaskOptions: { maskWith: "*", maxMaskedCharacters: 10, unmaskedStartCharacters: 0, unmaskedEndCharacters: 0 },
392
- passwordFields: ['password'],
393
-
394
- // Phone
395
- phoneMaskOptions: { maskWith: "*", unmaskedStartDigits: 2, unmaskedEndDigits: 1 },
396
- phoneFields: ['homePhone', 'workPhone'],
397
-
398
- // String
399
- stringMaskOptions: { maskWith: "*", maskOnlyFirstOccurance: false, values: [], maskAll: true, maskSpace: false },
400
- stringFields: ['addressLine1', 'addressLine2'],
401
-
402
- // UUID
403
- uuidMaskOptions: { maskWith: "*", unmaskedStartCharacters: 4, unmaskedEndCharacters: 2 },
404
- uuidFields: ['uuid1']
405
- };
406
-
407
- const maskedJsonOutput2 = maskData.maskJSON2(jsonInput2, jsonMaskConfig2);
408
-
409
- Output:
410
-
411
- {
412
- credit: '12XX-XXXX-XXXX-1234',
413
- debit: '00XX-XXXX-XXXX-3333',
414
- primaryEmail: 'pr*****@*******om',
415
- secondaryEmail: 'se*******@*******om',
416
- password: '**********',
417
- homePhone: '+1**********0',
418
- workPhone: '+1**********0',
419
- addressLine1: '**** ** ** *********** ** **** ** ** ****',
420
- addressLine2: '*********** *',
421
- uuid1: '123e****-****-****-****-**********00'
422
- }
423
- ```
424
-
425
- Example3: Mask nested json fields -> Use dot(.) and Array([]) notation to specify the inner fields.
426
-
427
- ```javascript
428
-
429
- const jsonInput2 = {
430
- cards: {
431
- creditCards: ['1234-5678-8765-1234', '1111-2222-1111-2222'],
432
- debitCards: ['0000-1111-2222-3333', '2222-1111-3333-4444']
433
- },
434
- emails: {
435
- primaryEmail: 'primary@Email.com',
436
- secondaryEmail: 'secondary@Email.com'
437
- },
438
- passwords: [
439
- ['dummyPasswordANDdummyPassword', 'dummyPasswordANDdummyPassword']
440
- ],
441
- phones: {
442
- homePhone: "+1 1234567890",
443
- workPhone: "+1 9876543210",
444
- },
445
- address: {
446
- addressLine1: "This is my addressline 1. This is my home",
447
- addressLine2: "AddressLine 2"
448
- },
449
- uuids: {
450
- uuid1: '123e4567-e89b-12d3-a456-426614174000'
451
- },
452
- jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ'
453
- };
454
-
455
- const jsonMaskConfig2 = {
456
- // Card
457
- cardMaskOptions: { maskWith: "X", unmaskedStartDigits: 2,unmaskedEndDigits: 4 },
458
- cardFields: ['cards.creditCards[0]', 'cards.creditCards[1]', 'cards.debitCards[0]', 'cards.debitCards[1]'],
459
-
460
- // Email
461
- emailMaskOptions: { maskWith: "*", unmaskedStartCharactersBeforeAt: 2, unmaskedEndCharactersAfterAt: 2, maskAtTheRate: false },
462
- emailFields: ['emails.primaryEmail', 'emails.secondaryEmail'],
463
-
464
- // Password
465
- passwordMaskOptions: { maskWith: "*", maxMaskedCharacters: 10, unmaskedStartCharacters: 0, unmaskedEndCharacters: 0 },
466
- passwordFields: ['passwords[0][0]]', 'passwords[0][1]'],
467
-
468
- // Phone
469
- phoneMaskOptions: { maskWith: "*", unmaskedStartDigits: 2, unmaskedEndDigits: 1 },
470
- phoneFields: ['phones.homePhone', 'phones.workPhone'],
471
-
472
- // String
473
- stringMaskOptions: { maskWith: "*", maskOnlyFirstOccurance: false, values: [], maskAll: true, maskSpace: false },
474
- stringFields: ['address.addressLine1', 'address.addressLine2'],
475
-
476
- // UUID
477
- uuidMaskOptions: { maskWith: "*", unmaskedStartCharacters: 4, unmaskedEndCharacters: 2 },
478
- uuidFields: ['uuids.uuid1']
479
-
480
- // JWT
481
- jwtMaskOptions: { maskWith: '*', maxMaskedCharacters: 512, maskDot: true, maskHeader: true, maskPayload: true, maskSignature: true},
482
- jwtFields: ['jwt']
483
- };
147
+ const maskedEmail = MaskData.maskEmail2(email, emailMask2Options);
484
148
 
485
- const maskedJsonOutput2 = MaskData.maskJSON2(jsonInput2, jsonMaskConfig2);
486
- // OUTPUT:
487
- {
488
- cards: {
489
- creditCards: [ '12XX-XXXX-XXXX-1234', '11XX-XXXX-XXXX-2222' ],
490
- debitCards: [ '00XX-XXXX-XXXX-3333', '22XX-XXXX-XXXX-4444' ]
491
- },
492
- emails: {
493
- primaryEmail: 'pr*****@*******om',
494
- secondaryEmail: 'se*******@*******om'
495
- },
496
- passwords: [ [ '**********', '**********' ] ],
497
- phones: { homePhone: '+1**********0', workPhone: '+1**********0' },
498
- address: {
499
- addressLine1: '**** ** ** *********** ** **** ** ** ****',
500
- addressLine2: '*********** *'
501
- },
502
- uuids: { uuid1: '123e****-****-****-****-**********00' },
503
- jwt: '*********************************************************************************************************'
504
- }
149
+ //Output: ****@email.com
505
150
 
506
151
  ```
152
+
507
153
  ## Mask Password
508
154
  ```javascript
509
155
  const MaskData = require('./maskdata');
@@ -869,111 +515,399 @@ eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.******************
869
515
 
870
516
  #### Example: Mask header and the signature part
871
517
 
872
- ```javascript
518
+ ```javascript
519
+
520
+ const MaskData = require('./maskdata');
521
+
522
+ const jwtMaskOptions = {
523
+ maskWith: '*',
524
+ maxMaskedCharacters: 512,
525
+ maskDot: false,
526
+ maskHeader: true,
527
+ maskPayload: false,
528
+ maskSignature: true
529
+ };
530
+
531
+ const jwt =
532
+ 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ';
533
+
534
+ /** In this example,
535
+ * Header = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
536
+ * Payload = eyJpYXQiOjE1MTYyMzkwMjJ9
537
+ * Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
538
+ */
539
+ const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
540
+
541
+ // Output:
542
+ ************************************.eyJpYXQiOjE1MTYyMzkwMjJ9.*******************************************
543
+
544
+ ```
545
+
546
+ #### Example: Mask all parts except dots and limit the max masked characters in the output to lower value, say 16
547
+
548
+ ```javascript
549
+
550
+ const MaskData = require('./maskdata');
551
+
552
+ const jwtMaskOptions = {
553
+ maskWith: '*',
554
+ maxMaskedCharacters: 16,
555
+ maskDot: false,
556
+ maskHeader: true,
557
+ maskPayload: true,
558
+ maskSignature: true
559
+ };
560
+
561
+ const jwt =
562
+ 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ';
563
+
564
+ /** In this example,
565
+ * Header = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
566
+ * Payload = eyJpYXQiOjE1MTYyMzkwMjJ9
567
+ * Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
568
+ */
569
+ const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
570
+
571
+ // Output:
572
+ ******.*****.*****
573
+
574
+ ```
575
+
576
+ ## Mask JSON
577
+ This is the functionality in version 1.2.0+ to handle masking of multiple types of data in a JSON with a single function call.
578
+
579
+ ```javascript
580
+ const MaskData = require('./maskdata');
581
+
582
+ // Default configs are as below. If specific masking changes are needed, use the corresponding configs for each type of field.
583
+ const defaultjsonMask2Configs = {
584
+ cardMaskOptions: defaultCardMaskOptions, // Optional
585
+ cardFields: [], // List of card fields to be masked
586
+
587
+ emailMaskOptions: defaultEmailMask2Options, // Optional
588
+ emailFields: [], // List of email fields to be masked
589
+
590
+ passwordMaskOptions: defaultPasswordMaskOptions, // Optional
591
+ passwordFields: [], // List of password fields to be masked
592
+
593
+ phoneMaskOptions: defaultPhoneMaskOptions, // Optional
594
+ phoneFields: [], // List of phone fields to be masked
595
+
596
+ stringMaskOptions: defaultStringMaskOptions, // Mandatory if stringFields are given. Otherwise, stringFields won't be masked
597
+ stringFields: [], // List of String fields to be masked
598
+
599
+ uuidMaskOptions: defaultUuidMaskOptions, // Optional
600
+ uuidFields: [], // List of UUID fields to be masked
601
+
602
+ jwtMaskOptions: defaultJwtMaskOptions, // Optional
603
+ jwtFields: [], // List of JWT fields to be masked
604
+
605
+ genericStrings: [ // This list can have multiple entries of { config: defaultStringMaskV2Options, fields: [] }
606
+ {
607
+ config: defaultStringMaskV2Options,
608
+ fields: []
609
+ }
610
+ ]
611
+ };
612
+ ```
613
+ > NOTE: For details on the configs mentioned above, refer: <br /> [defaultCardMaskOptions](#mask-card-number)<br/>[defaultEmailMask2Options](#mask-email-id)<br/> [defaultPasswordMaskOptions](#mask-password)<br/> [defaultPhoneMaskOptions](#mask-phone-number)<br/> [defaultStringMaskOptions](#mask-the-characters-or-words-in-the-string)<br/> [defaultUuidMaskOptions](#mask-uuid)<br /> [defaultJwtMaskOptions](#mask-jwt-token)<br /> [defaultStringMaskV2Options / generic String Maksing](#generic-string-masking)
614
+
615
+ ```javascript
616
+ const defaultjsonMask2Configs = {
617
+ cardMaskOptions: {
618
+ maskWith: "*",
619
+ unmaskedStartDigits: 4,
620
+ unmaskedEndDigits: 1
621
+ },
622
+ cardFields: [],
623
+
624
+ emailMaskOptions: {
625
+ maskWith: "*",
626
+ unmaskedStartCharactersBeforeAt: 3,
627
+ unmaskedEndCharactersAfterAt: 2,
628
+ maskAtTheRate: false
629
+ },
630
+ emailFields: [],
631
+
632
+ passwordMaskOptions: {
633
+ maskWith: "*",
634
+ maxMaskedCharacters: 16,
635
+ fixedOutputLength: undefined,
636
+ unmaskedStartCharacters: 0,
637
+ unmaskedEndCharacters: 0
638
+ },
639
+ passwordFields: [],
640
+
641
+ phoneMaskOptions: {
642
+ maskWith: "*",
643
+ unmaskedStartDigits: 4,
644
+ unmaskedEndDigits: 1
645
+ },
646
+ phoneFields: [],
647
+
648
+ stringMaskOptions: {
649
+ maskWith: "*",
650
+ maskOnlyFirstOccurance: false,
651
+ values: [],
652
+ maskAll: false,
653
+ maskSpace: true
654
+ },
655
+ stringFields: [],
656
+
657
+ uuidMaskOptions: {
658
+ maskWith: "*",
659
+ unmaskedStartCharacters: 0,
660
+ unmaskedEndCharacters: 0
661
+ },
662
+ uuidFields: [],
663
+
664
+ jwtMaskOptions: {
665
+ maskWith: '*',
666
+ maxMaskedCharacters: 512,
667
+ maskDot: true,
668
+ maskHeader: true,
669
+ maskPayload: true,
670
+ maskSignature: true
671
+ },
672
+ jwtFields: [],
673
+ // To extend the mask function to generic strings.
674
+ genericStrings: [
675
+ {
676
+ config: {
677
+ maskWith: "*",
678
+ maxMaskedCharacters: 256,
679
+ fixedOutputLength: undefined,
680
+ unmaskedStartDigits: 0,
681
+ unmaskedEndDigits: 0
682
+ },
683
+ fields: []
684
+ }
685
+ ]
686
+ };
687
+
688
+ ```
689
+
690
+ ### JSON mask examples
691
+ Example1:
692
+ ```javascript
693
+ const jsonInput = {
694
+ 'credit': '1234-5678-8765-1234',
695
+ 'debit': '0000-1111-2222-3333',
696
+ 'primaryEmail': 'primary@Email.com',
697
+ 'secondaryEmail': 'secondary@Email.com',
698
+ 'password': 'dummyPassword',
699
+ 'homePhone': "+1 1234567890",
700
+ 'workPhone': "+1 9876543210",
701
+ 'addressLine1': "This is my addressline 1. This is my home",
702
+ 'addressLine2': "AddressLine 2",
703
+ 'uuid1': '123e4567-e89b-12d3-a456-426614174000',
704
+ 'randomStrings': {
705
+ 'row1': 'This is row 1 random string',
706
+ 'row2': ['Entry1', 'Entry2', 'Entry3'],
707
+ 'row3': {
708
+ 'key1': 'Row3 Object 1',
709
+ 'key2': 'Row3 Object 2',
710
+ 'key3': ['Entry1', 'Entry2', 'Entry3']
711
+ }
712
+ }
713
+ };
714
+
715
+ const jsonMaskConfig = {
716
+ cardFields: ['credit', 'debit'],
717
+ emailFields: ['primaryEmail', 'secondaryEmail'],
718
+ passwordFields: ['password'],
719
+ phoneFields: ['homePhone', 'workPhone'],
720
+ stringMaskOptions: {
721
+ maskWith: "*",
722
+ maskOnlyFirstOccurance: false,
723
+ values: ["This"]
724
+ },
725
+ stringFields: ['addressLine1', 'addressLine2'],
726
+ uuidFields: ['uuid1'],
727
+ genericStrings: [
728
+ {
729
+ fields: ['randomStrings.row1'],
730
+ config: {
731
+ maskWith: '*',
732
+ unmaskedStartCharacters: 2,
733
+ unmaskedEndCharacters: 3,
734
+ maxMaskedCharacters: 8
735
+ }
736
+ },
737
+ { fields: ['randomStrings.row2.*'], config: { maskWith: 'X', unmaskedEndCharacters: 1 } },
738
+ { fields: ['randomStrings.row3.key1'] },
739
+ {
740
+ fields: ['randomStrings.row3.key3.*'],
741
+ config: { maskWith: '@', unmaskedEndCharacters: 1 }
742
+ }
743
+ ]
744
+ };
745
+
746
+ const maskedJsonOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
747
+
748
+ Output:
749
+ {
750
+ credit: '1234-****-****-***4',
751
+ debit: '0000-****-****-***3',
752
+ primaryEmail: 'pri****@*******om',
753
+ secondaryEmail: 'sec******@*******om',
754
+ password: '*************',
755
+ homePhone: '+1 1********0',
756
+ workPhone: '+1 9********0',
757
+ addressLine1: '**** is my addressline 1. **** is my home',
758
+ addressLine2: 'AddressLine 2',
759
+ uuid1: '********-****-****-****-************',
760
+ randomStrings: {
761
+ row1: 'Th***ing',
762
+ row2: ['XXXXX1', 'XXXXX2', 'XXXXX3'],
763
+ row3: {
764
+ key1: '*************',
765
+ key2: 'Row3 Object 2',
766
+ key3: ['@@@@@1', '@@@@@2', '@@@@@3']
767
+ }
768
+ }
769
+ }
770
+
873
771
 
874
- const MaskData = require('./maskdata');
772
+ Example2: Mask with custom configs for each/any type of fields
875
773
 
876
- const jwtMaskOptions = {
877
- maskWith: '*',
878
- maxMaskedCharacters: 512,
879
- maskDot: false,
880
- maskHeader: true,
881
- maskPayload: false,
882
- maskSignature: true
774
+ const jsonInput2 = {
775
+ 'credit': '1234-5678-8765-1234',
776
+ 'debit': '0000-1111-2222-3333',
777
+ 'primaryEmail': 'primary@Email.com',
778
+ 'secondaryEmail': 'secondary@Email.com',
779
+ 'password': 'dummyPasswordANDdummyPassword',
780
+ 'homePhone': "+1 1234567890",
781
+ 'workPhone': "+1 9876543210",
782
+ 'addressLine1': "This is my addressline 1. This is my home",
783
+ 'addressLine2': "AddressLine 2",
784
+ 'uuid1': '123e4567-e89b-12d3-a456-426614174000'
883
785
  };
884
786
 
885
- const jwt =
886
- 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ';
887
-
888
- /** In this example,
889
- * Header = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
890
- * Payload = eyJpYXQiOjE1MTYyMzkwMjJ9
891
- * Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
892
- */
893
- const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
894
-
895
- // Output:
896
- ************************************.eyJpYXQiOjE1MTYyMzkwMjJ9.*******************************************
787
+ const jsonMaskConfig2 = {
788
+ // Card
789
+ cardMaskOptions: { maskWith: "X", unmaskedStartDigits: 2,unmaskedEndDigits: 4 },
790
+ cardFields: ['credit', 'debit'],
897
791
 
898
- ```
792
+ // Email
793
+ emailMaskOptions: { maskWith: "*", unmaskedStartCharactersBeforeAt: 2, unmaskedEndCharactersAfterAt: 2, maskAtTheRate: false },
794
+ emailFields: ['primaryEmail', 'secondaryEmail'],
899
795
 
900
- #### Example: Mask all parts except dots and limit the max masked characters in the output to lower value, say 16
796
+ // Password
797
+ passwordMaskOptions: { maskWith: "*", maxMaskedCharacters: 10, unmaskedStartCharacters: 0, unmaskedEndCharacters: 0 },
798
+ passwordFields: ['password'],
901
799
 
902
- ```javascript
800
+ // Phone
801
+ phoneMaskOptions: { maskWith: "*", unmaskedStartDigits: 2, unmaskedEndDigits: 1 },
802
+ phoneFields: ['homePhone', 'workPhone'],
903
803
 
904
- const MaskData = require('./maskdata');
804
+ // String
805
+ stringMaskOptions: { maskWith: "*", maskOnlyFirstOccurance: false, values: [], maskAll: true, maskSpace: false },
806
+ stringFields: ['addressLine1', 'addressLine2'],
905
807
 
906
- const jwtMaskOptions = {
907
- maskWith: '*',
908
- maxMaskedCharacters: 16,
909
- maskDot: false,
910
- maskHeader: true,
911
- maskPayload: true,
912
- maskSignature: true
808
+ // UUID
809
+ uuidMaskOptions: { maskWith: "*", unmaskedStartCharacters: 4, unmaskedEndCharacters: 2 },
810
+ uuidFields: ['uuid1']
913
811
  };
914
812
 
915
- const jwt =
916
- 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ';
917
-
918
- /** In this example,
919
- * Header = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
920
- * Payload = eyJpYXQiOjE1MTYyMzkwMjJ9
921
- * Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
922
- */
923
- const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
813
+ const maskedJsonOutput2 = maskData.maskJSON2(jsonInput2, jsonMaskConfig2);
924
814
 
925
- // Output:
926
- ******.*****.*****
815
+ Output:
927
816
 
817
+ {
818
+ credit: '12XX-XXXX-XXXX-1234',
819
+ debit: '00XX-XXXX-XXXX-3333',
820
+ primaryEmail: 'pr*****@*******om',
821
+ secondaryEmail: 'se*******@*******om',
822
+ password: '**********',
823
+ homePhone: '+1**********0',
824
+ workPhone: '+1**********0',
825
+ addressLine1: '**** ** ** *********** ** **** ** ** ****',
826
+ addressLine2: '*********** *',
827
+ uuid1: '123e****-****-****-****-**********00'
828
+ }
928
829
  ```
929
830
 
930
- ## Get Nested JSON Property
931
- This method returns the value of the nested JSON property if it exists. Otherwise, it returns ```undefined```
932
- ```javascript
933
- const MaskData = require('./maskdata');
934
-
935
- const innerPropety = Maskdata.getInnerProperty(object, field);
831
+ Example3: Mask nested json fields -> Use dot(.) and Array([]) notation to specify the inner fields.
936
832
 
937
- Example:
833
+ ```javascript
938
834
 
939
- const nestedObject = {
940
- level1: {
941
- field1: "field1",
942
- level2: {
943
- field2: "field2",
944
- level3: {
945
- field3: "field3",
946
- field4: [ { Hello: "world" }, { Hello: "Newworld" }, "Just a String" ]
947
- }
948
- }
835
+ const jsonInput2 = {
836
+ cards: {
837
+ creditCards: ['1234-5678-8765-1234', '1111-2222-1111-2222'],
838
+ debitCards: ['0000-1111-2222-3333', '2222-1111-3333-4444']
949
839
  },
950
- value1: "value"
840
+ emails: {
841
+ primaryEmail: 'primary@Email.com',
842
+ secondaryEmail: 'secondary@Email.com'
843
+ },
844
+ passwords: [
845
+ ['dummyPasswordANDdummyPassword', 'dummyPasswordANDdummyPassword']
846
+ ],
847
+ phones: {
848
+ homePhone: "+1 1234567890",
849
+ workPhone: "+1 9876543210",
850
+ },
851
+ address: {
852
+ addressLine1: "This is my addressline 1. This is my home",
853
+ addressLine2: "AddressLine 2"
854
+ },
855
+ uuids: {
856
+ uuid1: '123e4567-e89b-12d3-a456-426614174000'
857
+ },
858
+ jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ'
951
859
  };
952
860
 
953
- const innerPropety = Maskdata.getInnerProperty(nestedObject, 'level1.level2.level3.field4[0].Hello');
861
+ const jsonMaskConfig2 = {
862
+ // Card
863
+ cardMaskOptions: { maskWith: "X", unmaskedStartDigits: 2,unmaskedEndDigits: 4 },
864
+ cardFields: ['cards.creditCards[0]', 'cards.creditCards[1]', 'cards.debitCards[0]', 'cards.debitCards[1]'],
954
865
 
955
- ```
866
+ // Email
867
+ emailMaskOptions: { maskWith: "*", unmaskedStartCharactersBeforeAt: 2, unmaskedEndCharactersAfterAt: 2, maskAtTheRate: false },
868
+ emailFields: ['emails.primaryEmail', 'emails.secondaryEmail'],
956
869
 
870
+ // Password
871
+ passwordMaskOptions: { maskWith: "*", maxMaskedCharacters: 10, unmaskedStartCharacters: 0, unmaskedEndCharacters: 0 },
872
+ passwordFields: ['passwords[0][0]]', 'passwords[0][1]'],
957
873
 
958
- ## Replace the value of a JSON field
959
- To replace a value by keeping the type.
960
- ```javascript
874
+ // Phone
875
+ phoneMaskOptions: { maskWith: "*", unmaskedStartDigits: 2, unmaskedEndDigits: 1 },
876
+ phoneFields: ['phones.homePhone', 'phones.workPhone'],
961
877
 
962
- const input = {
963
- name: "John",
964
- age: 33,
965
- married: true
966
- }
878
+ // String
879
+ stringMaskOptions: { maskWith: "*", maskOnlyFirstOccurance: false, values: [], maskAll: true, maskSpace: false },
880
+ stringFields: ['address.addressLine1', 'address.addressLine2'],
967
881
 
968
- let afterReplacing = MaskData.replaceValue(input, 'age', 99);
969
- afterReplacing = MaskData.replaceValue(input, 'married', false);
882
+ // UUID
883
+ uuidMaskOptions: { maskWith: "*", unmaskedStartCharacters: 4, unmaskedEndCharacters: 2 },
884
+ uuidFields: ['uuids.uuid1']
970
885
 
971
- //Output:
972
- Before replacing: {"name":"John","age":33,"married":true}
973
- After replacing: {"name":"John","age":99,"married":false}
886
+ // JWT
887
+ jwtMaskOptions: { maskWith: '*', maxMaskedCharacters: 512, maskDot: true, maskHeader: true, maskPayload: true, maskSignature: true},
888
+ jwtFields: ['jwt']
889
+ };
974
890
 
975
- Type of age: number
976
- Type of married: boolean
891
+ const maskedJsonOutput2 = MaskData.maskJSON2(jsonInput2, jsonMaskConfig2);
892
+ // OUTPUT:
893
+ {
894
+ cards: {
895
+ creditCards: [ '12XX-XXXX-XXXX-1234', '11XX-XXXX-XXXX-2222' ],
896
+ debitCards: [ '00XX-XXXX-XXXX-3333', '22XX-XXXX-XXXX-4444' ]
897
+ },
898
+ emails: {
899
+ primaryEmail: 'pr*****@*******om',
900
+ secondaryEmail: 'se*******@*******om'
901
+ },
902
+ passwords: [ [ '**********', '**********' ] ],
903
+ phones: { homePhone: '+1**********0', workPhone: '+1**********0' },
904
+ address: {
905
+ addressLine1: '**** ** ** *********** ** **** ** ** ****',
906
+ addressLine2: '*********** *'
907
+ },
908
+ uuids: { uuid1: '123e****-****-****-****-**********00' },
909
+ jwt: '*********************************************************************************************************'
910
+ }
977
911
 
978
912
  ```
979
913
 
@@ -1146,6 +1080,56 @@ const maskedOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
1146
1080
  }
1147
1081
  ```
1148
1082
 
1083
+ ## Get Nested JSON Property
1084
+ This method returns the value of the nested JSON property if it exists. Otherwise, it returns ```undefined```
1085
+ ```javascript
1086
+ const MaskData = require('./maskdata');
1087
+
1088
+ const innerPropety = Maskdata.getInnerProperty(object, field);
1089
+
1090
+ Example:
1091
+
1092
+ const nestedObject = {
1093
+ level1: {
1094
+ field1: "field1",
1095
+ level2: {
1096
+ field2: "field2",
1097
+ level3: {
1098
+ field3: "field3",
1099
+ field4: [ { Hello: "world" }, { Hello: "Newworld" }, "Just a String" ]
1100
+ }
1101
+ }
1102
+ },
1103
+ value1: "value"
1104
+ };
1105
+
1106
+ const innerPropety = Maskdata.getInnerProperty(nestedObject, 'level1.level2.level3.field4[0].Hello');
1107
+
1108
+ ```
1109
+
1110
+
1111
+ ## Replace the value of a JSON field
1112
+ To replace a value by keeping the type.
1113
+ ```javascript
1114
+
1115
+ const input = {
1116
+ name: "John",
1117
+ age: 33,
1118
+ married: true
1119
+ }
1120
+
1121
+ let afterReplacing = MaskData.replaceValue(input, 'age', 99);
1122
+ afterReplacing = MaskData.replaceValue(input, 'married', false);
1123
+
1124
+ //Output:
1125
+ Before replacing: {"name":"John","age":33,"married":true}
1126
+ After replacing: {"name":"John","age":99,"married":false}
1127
+
1128
+ Type of age: number
1129
+ Type of married: boolean
1130
+
1131
+ ```
1132
+
1149
1133
 
1150
1134
  # Report Bugs
1151
1135
  If there is any help needed with the library functionalities or if there is any bug/issue, please raise an issue in GitHub: https://github.com/Sumukha1496/maskdata/issues