maskdata 1.3.2 → 1.3.3

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,42 @@ 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.3 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.3
36
+ - Better documentation
37
+ - Bug fix and enabling strings with regex characters in the string mask functionality
38
+ - https://github.com/Sumukha1496/maskdata/issues/49
39
+ - Deprecate stringmask v1
40
+
48
41
  ### Version 1.3.2
49
42
  - Recursive masking feature: [Details](#recursive-masking)
50
43
  - https://github.com/Sumukha1496/maskdata/issues/45
@@ -59,18 +52,18 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
59
52
  - https://github.com/Sumukha1496/maskdata/issues/42
60
53
  - Supporting generic string masking(string mask V2) as part of maskJson2: [generic String masking in a JSON](#json-mask-examples)
61
54
 
62
- # How to Use
63
- ```javascript
64
- const MaskData = require('./maskdata');
65
- ```
66
-
67
55
  <br/>
68
56
 
69
57
  ## Maskdata for Typescript
70
58
  > Follow this document for more details:
71
59
  [Maskdata for typescript](./MASKDATA_FOR_TYPESCRIPT.md)
72
60
 
73
- <br/>
61
+ <br/><br/>
62
+
63
+ # How to Mask
64
+ ```javascript
65
+ const MaskData = require('./maskdata');
66
+ ```
74
67
 
75
68
  ## Mask card number
76
69
  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 +95,20 @@ const cardAfterMasking = MaskData.maskCard(cardNumber, maskCardOptions);
102
95
 
103
96
 
104
97
  ## Mask Email id
105
- Use this method instead of maskEmail(). To mask with the default options, don't pass the configurations.
98
+ Function to mask email id. Input must have a '@' in it.
106
99
  ```javascript
107
100
  const MaskData = require('./maskdata');
108
101
 
109
102
  const emailMask2Options = {
103
+ // Character to mask the data. The default value is '*'
110
104
  maskWith: "*",
105
+ // If we want to keep the starting 'n' characters before the '@' unmasked.
106
+ // Default value is 3
111
107
  unmaskedStartCharactersBeforeAt: 3,
108
+ // If we want to keep the last 'n' characters AFTER the '@' unmasked.
109
+ // Default value is 2
112
110
  unmaskedEndCharactersAfterAt: 2,
111
+ // Flag to mask or show '@'. Default value is false means do not mask
113
112
  maskAtTheRate: false
114
113
  };
115
114
 
@@ -130,380 +129,25 @@ Here,
130
129
  2. **unmaskedEndCharactersAfterAt** --> number of characters not to be masked starting from the end till @.
131
130
  * If unmaskedEndCharactersAfterAt > number of characters after @, then it will not mask the characters after @
132
131
 
133
- ### Mask only the characters before '@'
132
+ ### Mask only the characters before '@' / Show only the domain
134
133
  ```javascript
135
134
  const MaskData = require('./maskdata');
136
135
 
137
136
  const emailMask2Options = {
138
137
  maskWith: "*",
139
- unmaskedStartCharactersBeforeAt: 0,
140
- unmaskedEndCharactersAfterAt: 257, // Give a number which is more than the characters after @
138
+ unmaskedStartCharactersBeforeAt: 0, // Set this to 0 to mask the first part
139
+ unmaskedEndCharactersAfterAt: 257, // Enter a large number which is always more than the characters after @
141
140
  maskAtTheRate: false
142
141
  };
143
142
 
144
143
  const email = "abcd@email.com";
145
144
 
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
- };
145
+ const maskedEmail = MaskData.maskEmail2(email, emailMask2Options);
484
146
 
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
- }
147
+ //Output: ****@email.com
505
148
 
506
149
  ```
150
+
507
151
  ## Mask Password
508
152
  ```javascript
509
153
  const MaskData = require('./maskdata');
@@ -869,111 +513,399 @@ eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.******************
869
513
 
870
514
  #### Example: Mask header and the signature part
871
515
 
872
- ```javascript
516
+ ```javascript
517
+
518
+ const MaskData = require('./maskdata');
519
+
520
+ const jwtMaskOptions = {
521
+ maskWith: '*',
522
+ maxMaskedCharacters: 512,
523
+ maskDot: false,
524
+ maskHeader: true,
525
+ maskPayload: false,
526
+ maskSignature: true
527
+ };
528
+
529
+ const jwt =
530
+ 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ';
531
+
532
+ /** In this example,
533
+ * Header = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
534
+ * Payload = eyJpYXQiOjE1MTYyMzkwMjJ9
535
+ * Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
536
+ */
537
+ const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
538
+
539
+ // Output:
540
+ ************************************.eyJpYXQiOjE1MTYyMzkwMjJ9.*******************************************
541
+
542
+ ```
543
+
544
+ #### Example: Mask all parts except dots and limit the max masked characters in the output to lower value, say 16
545
+
546
+ ```javascript
547
+
548
+ const MaskData = require('./maskdata');
549
+
550
+ const jwtMaskOptions = {
551
+ maskWith: '*',
552
+ maxMaskedCharacters: 16,
553
+ maskDot: false,
554
+ maskHeader: true,
555
+ maskPayload: true,
556
+ maskSignature: true
557
+ };
558
+
559
+ const jwt =
560
+ 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ';
561
+
562
+ /** In this example,
563
+ * Header = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
564
+ * Payload = eyJpYXQiOjE1MTYyMzkwMjJ9
565
+ * Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
566
+ */
567
+ const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
568
+
569
+ // Output:
570
+ ******.*****.*****
571
+
572
+ ```
573
+
574
+ ## Mask JSON
575
+ 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.
576
+
577
+ ```javascript
578
+ const MaskData = require('./maskdata');
579
+
580
+ // Default configs are as below. If specific masking changes are needed, use the corresponding configs for each type of field.
581
+ const defaultjsonMask2Configs = {
582
+ cardMaskOptions: defaultCardMaskOptions, // Optional
583
+ cardFields: [], // List of card fields to be masked
584
+
585
+ emailMaskOptions: defaultEmailMask2Options, // Optional
586
+ emailFields: [], // List of email fields to be masked
587
+
588
+ passwordMaskOptions: defaultPasswordMaskOptions, // Optional
589
+ passwordFields: [], // List of password fields to be masked
590
+
591
+ phoneMaskOptions: defaultPhoneMaskOptions, // Optional
592
+ phoneFields: [], // List of phone fields to be masked
593
+
594
+ stringMaskOptions: defaultStringMaskOptions, // Mandatory if stringFields are given. Otherwise, stringFields won't be masked
595
+ stringFields: [], // List of String fields to be masked
596
+
597
+ uuidMaskOptions: defaultUuidMaskOptions, // Optional
598
+ uuidFields: [], // List of UUID fields to be masked
599
+
600
+ jwtMaskOptions: defaultJwtMaskOptions, // Optional
601
+ jwtFields: [], // List of JWT fields to be masked
602
+
603
+ genericStrings: [ // This list can have multiple entries of { config: defaultStringMaskV2Options, fields: [] }
604
+ {
605
+ config: defaultStringMaskV2Options,
606
+ fields: []
607
+ }
608
+ ]
609
+ };
610
+ ```
611
+ > 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)
612
+
613
+ ```javascript
614
+ const defaultjsonMask2Configs = {
615
+ cardMaskOptions: {
616
+ maskWith: "*",
617
+ unmaskedStartDigits: 4,
618
+ unmaskedEndDigits: 1
619
+ },
620
+ cardFields: [],
621
+
622
+ emailMaskOptions: {
623
+ maskWith: "*",
624
+ unmaskedStartCharactersBeforeAt: 3,
625
+ unmaskedEndCharactersAfterAt: 2,
626
+ maskAtTheRate: false
627
+ },
628
+ emailFields: [],
629
+
630
+ passwordMaskOptions: {
631
+ maskWith: "*",
632
+ maxMaskedCharacters: 16,
633
+ fixedOutputLength: undefined,
634
+ unmaskedStartCharacters: 0,
635
+ unmaskedEndCharacters: 0
636
+ },
637
+ passwordFields: [],
638
+
639
+ phoneMaskOptions: {
640
+ maskWith: "*",
641
+ unmaskedStartDigits: 4,
642
+ unmaskedEndDigits: 1
643
+ },
644
+ phoneFields: [],
645
+
646
+ stringMaskOptions: {
647
+ maskWith: "*",
648
+ maskOnlyFirstOccurance: false,
649
+ values: [],
650
+ maskAll: false,
651
+ maskSpace: true
652
+ },
653
+ stringFields: [],
654
+
655
+ uuidMaskOptions: {
656
+ maskWith: "*",
657
+ unmaskedStartCharacters: 0,
658
+ unmaskedEndCharacters: 0
659
+ },
660
+ uuidFields: [],
661
+
662
+ jwtMaskOptions: {
663
+ maskWith: '*',
664
+ maxMaskedCharacters: 512,
665
+ maskDot: true,
666
+ maskHeader: true,
667
+ maskPayload: true,
668
+ maskSignature: true
669
+ },
670
+ jwtFields: [],
671
+ // To extend the mask function to generic strings.
672
+ genericStrings: [
673
+ {
674
+ config: {
675
+ maskWith: "*",
676
+ maxMaskedCharacters: 256,
677
+ fixedOutputLength: undefined,
678
+ unmaskedStartDigits: 0,
679
+ unmaskedEndDigits: 0
680
+ },
681
+ fields: []
682
+ }
683
+ ]
684
+ };
685
+
686
+ ```
687
+
688
+ ### JSON mask examples
689
+ Example1:
690
+ ```javascript
691
+ const jsonInput = {
692
+ 'credit': '1234-5678-8765-1234',
693
+ 'debit': '0000-1111-2222-3333',
694
+ 'primaryEmail': 'primary@Email.com',
695
+ 'secondaryEmail': 'secondary@Email.com',
696
+ 'password': 'dummyPassword',
697
+ 'homePhone': "+1 1234567890",
698
+ 'workPhone': "+1 9876543210",
699
+ 'addressLine1': "This is my addressline 1. This is my home",
700
+ 'addressLine2': "AddressLine 2",
701
+ 'uuid1': '123e4567-e89b-12d3-a456-426614174000',
702
+ 'randomStrings': {
703
+ 'row1': 'This is row 1 random string',
704
+ 'row2': ['Entry1', 'Entry2', 'Entry3'],
705
+ 'row3': {
706
+ 'key1': 'Row3 Object 1',
707
+ 'key2': 'Row3 Object 2',
708
+ 'key3': ['Entry1', 'Entry2', 'Entry3']
709
+ }
710
+ }
711
+ };
712
+
713
+ const jsonMaskConfig = {
714
+ cardFields: ['credit', 'debit'],
715
+ emailFields: ['primaryEmail', 'secondaryEmail'],
716
+ passwordFields: ['password'],
717
+ phoneFields: ['homePhone', 'workPhone'],
718
+ stringMaskOptions: {
719
+ maskWith: "*",
720
+ maskOnlyFirstOccurance: false,
721
+ values: ["This"]
722
+ },
723
+ stringFields: ['addressLine1', 'addressLine2'],
724
+ uuidFields: ['uuid1'],
725
+ genericStrings: [
726
+ {
727
+ fields: ['randomStrings.row1'],
728
+ config: {
729
+ maskWith: '*',
730
+ unmaskedStartCharacters: 2,
731
+ unmaskedEndCharacters: 3,
732
+ maxMaskedCharacters: 8
733
+ }
734
+ },
735
+ { fields: ['randomStrings.row2.*'], config: { maskWith: 'X', unmaskedEndCharacters: 1 } },
736
+ { fields: ['randomStrings.row3.key1'] },
737
+ {
738
+ fields: ['randomStrings.row3.key3.*'],
739
+ config: { maskWith: '@', unmaskedEndCharacters: 1 }
740
+ }
741
+ ]
742
+ };
743
+
744
+ const maskedJsonOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
745
+
746
+ Output:
747
+ {
748
+ credit: '1234-****-****-***4',
749
+ debit: '0000-****-****-***3',
750
+ primaryEmail: 'pri****@*******om',
751
+ secondaryEmail: 'sec******@*******om',
752
+ password: '*************',
753
+ homePhone: '+1 1********0',
754
+ workPhone: '+1 9********0',
755
+ addressLine1: '**** is my addressline 1. **** is my home',
756
+ addressLine2: 'AddressLine 2',
757
+ uuid1: '********-****-****-****-************',
758
+ randomStrings: {
759
+ row1: 'Th***ing',
760
+ row2: ['XXXXX1', 'XXXXX2', 'XXXXX3'],
761
+ row3: {
762
+ key1: '*************',
763
+ key2: 'Row3 Object 2',
764
+ key3: ['@@@@@1', '@@@@@2', '@@@@@3']
765
+ }
766
+ }
767
+ }
768
+
873
769
 
874
- const MaskData = require('./maskdata');
770
+ Example2: Mask with custom configs for each/any type of fields
875
771
 
876
- const jwtMaskOptions = {
877
- maskWith: '*',
878
- maxMaskedCharacters: 512,
879
- maskDot: false,
880
- maskHeader: true,
881
- maskPayload: false,
882
- maskSignature: true
772
+ const jsonInput2 = {
773
+ 'credit': '1234-5678-8765-1234',
774
+ 'debit': '0000-1111-2222-3333',
775
+ 'primaryEmail': 'primary@Email.com',
776
+ 'secondaryEmail': 'secondary@Email.com',
777
+ 'password': 'dummyPasswordANDdummyPassword',
778
+ 'homePhone': "+1 1234567890",
779
+ 'workPhone': "+1 9876543210",
780
+ 'addressLine1': "This is my addressline 1. This is my home",
781
+ 'addressLine2': "AddressLine 2",
782
+ 'uuid1': '123e4567-e89b-12d3-a456-426614174000'
883
783
  };
884
784
 
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.*******************************************
785
+ const jsonMaskConfig2 = {
786
+ // Card
787
+ cardMaskOptions: { maskWith: "X", unmaskedStartDigits: 2,unmaskedEndDigits: 4 },
788
+ cardFields: ['credit', 'debit'],
897
789
 
898
- ```
790
+ // Email
791
+ emailMaskOptions: { maskWith: "*", unmaskedStartCharactersBeforeAt: 2, unmaskedEndCharactersAfterAt: 2, maskAtTheRate: false },
792
+ emailFields: ['primaryEmail', 'secondaryEmail'],
899
793
 
900
- #### Example: Mask all parts except dots and limit the max masked characters in the output to lower value, say 16
794
+ // Password
795
+ passwordMaskOptions: { maskWith: "*", maxMaskedCharacters: 10, unmaskedStartCharacters: 0, unmaskedEndCharacters: 0 },
796
+ passwordFields: ['password'],
901
797
 
902
- ```javascript
798
+ // Phone
799
+ phoneMaskOptions: { maskWith: "*", unmaskedStartDigits: 2, unmaskedEndDigits: 1 },
800
+ phoneFields: ['homePhone', 'workPhone'],
903
801
 
904
- const MaskData = require('./maskdata');
802
+ // String
803
+ stringMaskOptions: { maskWith: "*", maskOnlyFirstOccurance: false, values: [], maskAll: true, maskSpace: false },
804
+ stringFields: ['addressLine1', 'addressLine2'],
905
805
 
906
- const jwtMaskOptions = {
907
- maskWith: '*',
908
- maxMaskedCharacters: 16,
909
- maskDot: false,
910
- maskHeader: true,
911
- maskPayload: true,
912
- maskSignature: true
806
+ // UUID
807
+ uuidMaskOptions: { maskWith: "*", unmaskedStartCharacters: 4, unmaskedEndCharacters: 2 },
808
+ uuidFields: ['uuid1']
913
809
  };
914
810
 
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);
811
+ const maskedJsonOutput2 = maskData.maskJSON2(jsonInput2, jsonMaskConfig2);
924
812
 
925
- // Output:
926
- ******.*****.*****
813
+ Output:
927
814
 
815
+ {
816
+ credit: '12XX-XXXX-XXXX-1234',
817
+ debit: '00XX-XXXX-XXXX-3333',
818
+ primaryEmail: 'pr*****@*******om',
819
+ secondaryEmail: 'se*******@*******om',
820
+ password: '**********',
821
+ homePhone: '+1**********0',
822
+ workPhone: '+1**********0',
823
+ addressLine1: '**** ** ** *********** ** **** ** ** ****',
824
+ addressLine2: '*********** *',
825
+ uuid1: '123e****-****-****-****-**********00'
826
+ }
928
827
  ```
929
828
 
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);
829
+ Example3: Mask nested json fields -> Use dot(.) and Array([]) notation to specify the inner fields.
936
830
 
937
- Example:
831
+ ```javascript
938
832
 
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
- }
833
+ const jsonInput2 = {
834
+ cards: {
835
+ creditCards: ['1234-5678-8765-1234', '1111-2222-1111-2222'],
836
+ debitCards: ['0000-1111-2222-3333', '2222-1111-3333-4444']
949
837
  },
950
- value1: "value"
838
+ emails: {
839
+ primaryEmail: 'primary@Email.com',
840
+ secondaryEmail: 'secondary@Email.com'
841
+ },
842
+ passwords: [
843
+ ['dummyPasswordANDdummyPassword', 'dummyPasswordANDdummyPassword']
844
+ ],
845
+ phones: {
846
+ homePhone: "+1 1234567890",
847
+ workPhone: "+1 9876543210",
848
+ },
849
+ address: {
850
+ addressLine1: "This is my addressline 1. This is my home",
851
+ addressLine2: "AddressLine 2"
852
+ },
853
+ uuids: {
854
+ uuid1: '123e4567-e89b-12d3-a456-426614174000'
855
+ },
856
+ jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ'
951
857
  };
952
858
 
953
- const innerPropety = Maskdata.getInnerProperty(nestedObject, 'level1.level2.level3.field4[0].Hello');
859
+ const jsonMaskConfig2 = {
860
+ // Card
861
+ cardMaskOptions: { maskWith: "X", unmaskedStartDigits: 2,unmaskedEndDigits: 4 },
862
+ cardFields: ['cards.creditCards[0]', 'cards.creditCards[1]', 'cards.debitCards[0]', 'cards.debitCards[1]'],
954
863
 
955
- ```
864
+ // Email
865
+ emailMaskOptions: { maskWith: "*", unmaskedStartCharactersBeforeAt: 2, unmaskedEndCharactersAfterAt: 2, maskAtTheRate: false },
866
+ emailFields: ['emails.primaryEmail', 'emails.secondaryEmail'],
956
867
 
868
+ // Password
869
+ passwordMaskOptions: { maskWith: "*", maxMaskedCharacters: 10, unmaskedStartCharacters: 0, unmaskedEndCharacters: 0 },
870
+ passwordFields: ['passwords[0][0]]', 'passwords[0][1]'],
957
871
 
958
- ## Replace the value of a JSON field
959
- To replace a value by keeping the type.
960
- ```javascript
872
+ // Phone
873
+ phoneMaskOptions: { maskWith: "*", unmaskedStartDigits: 2, unmaskedEndDigits: 1 },
874
+ phoneFields: ['phones.homePhone', 'phones.workPhone'],
961
875
 
962
- const input = {
963
- name: "John",
964
- age: 33,
965
- married: true
966
- }
876
+ // String
877
+ stringMaskOptions: { maskWith: "*", maskOnlyFirstOccurance: false, values: [], maskAll: true, maskSpace: false },
878
+ stringFields: ['address.addressLine1', 'address.addressLine2'],
967
879
 
968
- let afterReplacing = MaskData.replaceValue(input, 'age', 99);
969
- afterReplacing = MaskData.replaceValue(input, 'married', false);
880
+ // UUID
881
+ uuidMaskOptions: { maskWith: "*", unmaskedStartCharacters: 4, unmaskedEndCharacters: 2 },
882
+ uuidFields: ['uuids.uuid1']
970
883
 
971
- //Output:
972
- Before replacing: {"name":"John","age":33,"married":true}
973
- After replacing: {"name":"John","age":99,"married":false}
884
+ // JWT
885
+ jwtMaskOptions: { maskWith: '*', maxMaskedCharacters: 512, maskDot: true, maskHeader: true, maskPayload: true, maskSignature: true},
886
+ jwtFields: ['jwt']
887
+ };
974
888
 
975
- Type of age: number
976
- Type of married: boolean
889
+ const maskedJsonOutput2 = MaskData.maskJSON2(jsonInput2, jsonMaskConfig2);
890
+ // OUTPUT:
891
+ {
892
+ cards: {
893
+ creditCards: [ '12XX-XXXX-XXXX-1234', '11XX-XXXX-XXXX-2222' ],
894
+ debitCards: [ '00XX-XXXX-XXXX-3333', '22XX-XXXX-XXXX-4444' ]
895
+ },
896
+ emails: {
897
+ primaryEmail: 'pr*****@*******om',
898
+ secondaryEmail: 'se*******@*******om'
899
+ },
900
+ passwords: [ [ '**********', '**********' ] ],
901
+ phones: { homePhone: '+1**********0', workPhone: '+1**********0' },
902
+ address: {
903
+ addressLine1: '**** ** ** *********** ** **** ** ** ****',
904
+ addressLine2: '*********** *'
905
+ },
906
+ uuids: { uuid1: '123e****-****-****-****-**********00' },
907
+ jwt: '*********************************************************************************************************'
908
+ }
977
909
 
978
910
  ```
979
911
 
@@ -1146,6 +1078,56 @@ const maskedOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
1146
1078
  }
1147
1079
  ```
1148
1080
 
1081
+ ## Get Nested JSON Property
1082
+ This method returns the value of the nested JSON property if it exists. Otherwise, it returns ```undefined```
1083
+ ```javascript
1084
+ const MaskData = require('./maskdata');
1085
+
1086
+ const innerPropety = Maskdata.getInnerProperty(object, field);
1087
+
1088
+ Example:
1089
+
1090
+ const nestedObject = {
1091
+ level1: {
1092
+ field1: "field1",
1093
+ level2: {
1094
+ field2: "field2",
1095
+ level3: {
1096
+ field3: "field3",
1097
+ field4: [ { Hello: "world" }, { Hello: "Newworld" }, "Just a String" ]
1098
+ }
1099
+ }
1100
+ },
1101
+ value1: "value"
1102
+ };
1103
+
1104
+ const innerPropety = Maskdata.getInnerProperty(nestedObject, 'level1.level2.level3.field4[0].Hello');
1105
+
1106
+ ```
1107
+
1108
+
1109
+ ## Replace the value of a JSON field
1110
+ To replace a value by keeping the type.
1111
+ ```javascript
1112
+
1113
+ const input = {
1114
+ name: "John",
1115
+ age: 33,
1116
+ married: true
1117
+ }
1118
+
1119
+ let afterReplacing = MaskData.replaceValue(input, 'age', 99);
1120
+ afterReplacing = MaskData.replaceValue(input, 'married', false);
1121
+
1122
+ //Output:
1123
+ Before replacing: {"name":"John","age":33,"married":true}
1124
+ After replacing: {"name":"John","age":99,"married":false}
1125
+
1126
+ Type of age: number
1127
+ Type of married: boolean
1128
+
1129
+ ```
1130
+
1149
1131
 
1150
1132
  # Report Bugs
1151
1133
  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