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/MASKDATA_FOR_TYPESCRIPT.md +18 -14
- package/README.md +460 -478
- package/documentation/get_nested_json_property.md +27 -0
- package/documentation/main.md +117 -0
- package/documentation/mask_cards.md +0 -0
- package/documentation/mask_email_id.md +47 -0
- package/documentation/mask_email_id_with_the_default_configuration.md +355 -0
- package/documentation/mask_jwt_token.md +132 -0
- package/documentation/mask_password.md +31 -0
- package/documentation/mask_password_with_the_default_configuration.md +43 -0
- package/documentation/mask_phone_number.md +26 -0
- package/documentation/mask_phone_number_with_the_default_configuration.md +155 -0
- package/documentation/mask_uuid.md +30 -0
- package/documentation/replace_the_value_of_a_json_field.md +220 -0
- package/index.d.ts +6 -0
- package/index.js +5 -0
- package/lib/helpers/Constants.js +2 -1
- package/lib/helpers/MaskHelper.js +12 -1
- package/lib/maskService/StringMask.js +3 -1
- package/package.json +1 -1
- package/tests/testString.js +16 -4
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
## Get Nested JSON Property
|
|
2
|
+
This method returns the value of the nested JSON property if it exists. Otherwise, it returns ```undefined```
|
|
3
|
+
```javascript
|
|
4
|
+
const MaskData = require('./maskdata');
|
|
5
|
+
|
|
6
|
+
const innerPropety = Maskdata.getInnerProperty(object, field);
|
|
7
|
+
|
|
8
|
+
Example:
|
|
9
|
+
|
|
10
|
+
const nestedObject = {
|
|
11
|
+
level1: {
|
|
12
|
+
field1: "field1",
|
|
13
|
+
level2: {
|
|
14
|
+
field2: "field2",
|
|
15
|
+
level3: {
|
|
16
|
+
field3: "field3",
|
|
17
|
+
field4: [ { Hello: "world" }, { Hello: "Newworld" }, "Just a String" ]
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
value1: "value"
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const innerPropety = Maskdata.getInnerProperty(nestedObject, 'level1.level2.level3.field4[0].Hello');
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
maskdata is a Node.js module to mask various kinds of data. With the help of maskdata, you can mask email, phone number, card number, password, jwt tokens etc... Either mask them separately or mask all these types of data in a json together.
|
|
2
|
+
<br/><br/>Also, it provides utility functions to get a field, or replace a field from any complex/nested JSON.
|
|
3
|
+
|
|
4
|
+
# Table of Contents
|
|
5
|
+
- [Supported Features](#features)
|
|
6
|
+
- [Install maskdata](#install-maskdata)
|
|
7
|
+
- [Version 1.3.3 Features](#release-features)
|
|
8
|
+
- [Maskdata for Typescript](#maskdata-for-typescript)
|
|
9
|
+
- [How to Use](#how-to-use)
|
|
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)
|
|
27
|
+
- [Report Bugs](#report-bugs)
|
|
28
|
+
- [Give a Star](#give-a-star)
|
|
29
|
+
- [LICENSE - "MIT"](#license---mit)
|
|
30
|
+
|
|
31
|
+
# Features
|
|
32
|
+
* Mask Card numbers
|
|
33
|
+
* Mask Email ids
|
|
34
|
+
* Mask Passwords
|
|
35
|
+
* Mask Phone numbers
|
|
36
|
+
* Mask given words/substrings in a String
|
|
37
|
+
* Mask generic Strings
|
|
38
|
+
* Mask UUIDs
|
|
39
|
+
* Mask JWT tokens
|
|
40
|
+
* Mask JSON - JSON can contain cards, emails, passwords, phones, strings, jwt tokens, UUIDs etc... Mask all of them with a single call using - [Mask JSON fields](#mask-json)
|
|
41
|
+
* Get nested field from JSON
|
|
42
|
+
* Set/Replace nested field values from JSON
|
|
43
|
+
* [Maskdata for typescript](#maskdata-for-typescript)
|
|
44
|
+
|
|
45
|
+
# Install maskdata
|
|
46
|
+
> npm i maskdata
|
|
47
|
+
|
|
48
|
+
# Release Features
|
|
49
|
+
### Version 1.3.3
|
|
50
|
+
- Improved documentation
|
|
51
|
+
### Version 1.3.2
|
|
52
|
+
- Recursive masking feature: [Details](#recursive-masking)
|
|
53
|
+
- https://github.com/Sumukha1496/maskdata/issues/45
|
|
54
|
+
- https://github.com/Sumukha1496/maskdata/issues/47
|
|
55
|
+
- Fixed output length masking: [Mask Password](#mask-password) and [Mask String](#generic-string-masking)
|
|
56
|
+
- https://github.com/Sumukha1496/maskdata/issues/46
|
|
57
|
+
### Version 1.3.1
|
|
58
|
+
- Removal of maskJSONFields function: https://www.npmjs.com/package/maskdata/v/1.1.10#mask-fields-in-a-json
|
|
59
|
+
- String mask version2 for generic string masking in response to the below issues: [Details](#generic-string-masking)
|
|
60
|
+
- https://github.com/Sumukha1496/maskdata/issues/17
|
|
61
|
+
- https://github.com/Sumukha1496/maskdata/issues/40
|
|
62
|
+
- https://github.com/Sumukha1496/maskdata/issues/42
|
|
63
|
+
- Supporting generic string masking(string mask V2) as part of maskJson2: [generic String masking in a JSON](#json-mask-examples)
|
|
64
|
+
|
|
65
|
+
<br/>
|
|
66
|
+
|
|
67
|
+
## Maskdata for Typescript
|
|
68
|
+
> Follow this document for more details:
|
|
69
|
+
[Maskdata for typescript](documentation/MASKDATA_FOR_TYPESCRIPT.md)
|
|
70
|
+
|
|
71
|
+
<br/>
|
|
72
|
+
|
|
73
|
+
# How to Use
|
|
74
|
+
```javascript
|
|
75
|
+
const MaskData = require('./maskdata');
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Mask card number
|
|
79
|
+
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
|
|
80
|
+
```javascript
|
|
81
|
+
const MaskData = require('./maskdata');
|
|
82
|
+
|
|
83
|
+
const maskCardOptions = {
|
|
84
|
+
// Character to mask the data. The default value is '*'
|
|
85
|
+
maskWith: "*",
|
|
86
|
+
|
|
87
|
+
// Should be positive Integer
|
|
88
|
+
// If the starting 'n' digits need to be visible/unmasked
|
|
89
|
+
// Default value is 4
|
|
90
|
+
unmaskedStartDigits: 4,
|
|
91
|
+
|
|
92
|
+
// Should be positive Integer
|
|
93
|
+
// If the ending 'n' digits need to be visible/unmasked
|
|
94
|
+
// Default value is 1.
|
|
95
|
+
unmaskedEndDigits: 1
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const cardNumber = "1234-5678-1234-5678";
|
|
99
|
+
|
|
100
|
+
const cardAfterMasking = MaskData.maskCard(cardNumber, maskCardOptions);
|
|
101
|
+
|
|
102
|
+
//Output: 1234-****-****-***8
|
|
103
|
+
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
For more examples:
|
|
107
|
+
|
|
108
|
+
- [Mask Email id](documentation/mask_email_id.md)
|
|
109
|
+
- [Mask Email id with the default configuration](documentation/mask_email_id_with_the_default_configuration.md)
|
|
110
|
+
- [Mask Password](documentation/mask_password.md)
|
|
111
|
+
- [Mask Password with the default configuration](documentation/mask_password_with_the_default_configuration.md)
|
|
112
|
+
- [Mask Phone Number](documentation/mask_phone_number.md)
|
|
113
|
+
- [Mask Phone Number with the default configuration](documentation/mask_phone_number_with_the_default_configuration.md)
|
|
114
|
+
- [Mask UUID](documentation/mask_uuid.md)
|
|
115
|
+
- [Mask JWT Token](documentation/mask_jwt_token.md)
|
|
116
|
+
- [Get Nested JSON Property](documentation/get_nested_json_property.md)
|
|
117
|
+
- [Replace the value of a JSON field](documentation/replace_the_value_of_a_json_field.md)
|
|
File without changes
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
## Mask Email id
|
|
2
|
+
Use this method instead of maskEmail(). To mask with the default options, don't pass the configurations.
|
|
3
|
+
```javascript
|
|
4
|
+
const MaskData = require('./maskdata');
|
|
5
|
+
|
|
6
|
+
const emailMask2Options = {
|
|
7
|
+
maskWith: "*",
|
|
8
|
+
unmaskedStartCharactersBeforeAt: 3,
|
|
9
|
+
unmaskedEndCharactersAfterAt: 2,
|
|
10
|
+
maskAtTheRate: false
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const email = "my.test.email@testEmail.com";
|
|
14
|
+
|
|
15
|
+
const maskedEmail = MaskData.maskEmail2(email, emailMask2Options);
|
|
16
|
+
|
|
17
|
+
//Output: my.********@**********om
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
### Consider an email: abcd@email.com
|
|
21
|
+
Here,
|
|
22
|
+
- number of characters before @ -> 4
|
|
23
|
+
- number of characters after @ --> 9
|
|
24
|
+
|
|
25
|
+
1. **unmaskedStartCharactersBeforeAt** --> number of starting characters (before @)not to be masked.
|
|
26
|
+
* If unmaskedStartCharactersBeforeAt > number of characters before @, then it will not mask the characters before @
|
|
27
|
+
2. **unmaskedEndCharactersAfterAt** --> number of characters not to be masked starting from the end till @.
|
|
28
|
+
* If unmaskedEndCharactersAfterAt > number of characters after @, then it will not mask the characters after @
|
|
29
|
+
|
|
30
|
+
### Mask only the characters before '@'
|
|
31
|
+
```javascript
|
|
32
|
+
const MaskData = require('./maskdata');
|
|
33
|
+
|
|
34
|
+
const emailMask2Options = {
|
|
35
|
+
maskWith: "*",
|
|
36
|
+
unmaskedStartCharactersBeforeAt: 0,
|
|
37
|
+
unmaskedEndCharactersAfterAt: 257, // Give a number which is more than the characters after @
|
|
38
|
+
maskAtTheRate: false
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const email = "abcd@email.com";
|
|
42
|
+
|
|
43
|
+
const maskedEmail = MaskData.maskEmail2(email, emailMask2Options);
|
|
44
|
+
|
|
45
|
+
//Output: ****@email.com
|
|
46
|
+
|
|
47
|
+
```
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
## Mask Email id with the default configuration
|
|
2
|
+
To mask with the default options, don't pass the configurations.
|
|
3
|
+
```javascript
|
|
4
|
+
const MaskData = require('./maskdata');
|
|
5
|
+
|
|
6
|
+
/** Default Options
|
|
7
|
+
maskWith: "*",
|
|
8
|
+
unmaskedStartCharactersBeforeAt: 3,
|
|
9
|
+
unmaskedEndCharactersAfterAt: 2,
|
|
10
|
+
maskAtTheRate: false
|
|
11
|
+
**/
|
|
12
|
+
const email = "my.test.email@testEmail.com";
|
|
13
|
+
|
|
14
|
+
const maskedEmail = MaskData.maskEmail2(email);
|
|
15
|
+
|
|
16
|
+
//Output: my.********@**********om
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Mask JSON
|
|
21
|
+
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.
|
|
22
|
+
|
|
23
|
+
```javascript
|
|
24
|
+
const MaskData = require('./maskdata');
|
|
25
|
+
|
|
26
|
+
// Default configs are as below. If specific masking changes are needed, use the corresponding configs for each type of field.
|
|
27
|
+
const defaultjsonMask2Configs = {
|
|
28
|
+
cardMaskOptions: defaultCardMaskOptions, // Optional
|
|
29
|
+
cardFields: [], // List of card fields to be masked
|
|
30
|
+
|
|
31
|
+
emailMaskOptions: defaultEmailMask2Options, // Optional
|
|
32
|
+
emailFields: [], // List of email fields to be masked
|
|
33
|
+
|
|
34
|
+
// For passwords, tokens, etc...
|
|
35
|
+
passwordMaskOptions: defaultPasswordMaskOptions, // Optional
|
|
36
|
+
passwordFields: [], // List of password fields to be masked
|
|
37
|
+
|
|
38
|
+
phoneMaskOptions: defaultPhoneMaskOptions, // Optional
|
|
39
|
+
phoneFields: [], // List of phone fields to be masked
|
|
40
|
+
|
|
41
|
+
stringMaskOptions: defaultStringMaskOptions, // Mandatory if stringFields are given. Otherwise, stringFields won't be masked
|
|
42
|
+
stringFields: [], // List of String fields to be masked
|
|
43
|
+
|
|
44
|
+
uuidMaskOptions: defaultUuidMaskOptions, // Optional
|
|
45
|
+
uuidFields: [], // List of UUID fields to be masked
|
|
46
|
+
|
|
47
|
+
jwtMaskOptions: defaultJwtMaskOptions, // Optional
|
|
48
|
+
jwtFields: [], // List of JWT fields to be masked
|
|
49
|
+
|
|
50
|
+
genericStrings: [
|
|
51
|
+
{
|
|
52
|
+
config: defaultStringMaskV2Options,
|
|
53
|
+
fields: []
|
|
54
|
+
}
|
|
55
|
+
]
|
|
56
|
+
};
|
|
57
|
+
```
|
|
58
|
+
> 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)
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
const defaultjsonMask2Configs = {
|
|
62
|
+
cardMaskOptions: {
|
|
63
|
+
maskWith: "*",
|
|
64
|
+
unmaskedStartDigits: 4,
|
|
65
|
+
unmaskedEndDigits: 1
|
|
66
|
+
},
|
|
67
|
+
cardFields: [],
|
|
68
|
+
|
|
69
|
+
emailMaskOptions: {
|
|
70
|
+
maskWith: "*",
|
|
71
|
+
unmaskedStartCharactersBeforeAt: 3,
|
|
72
|
+
unmaskedEndCharactersAfterAt: 2,
|
|
73
|
+
maskAtTheRate: false
|
|
74
|
+
},
|
|
75
|
+
emailFields: [],
|
|
76
|
+
|
|
77
|
+
passwordMaskOptions: {
|
|
78
|
+
maskWith: "*",
|
|
79
|
+
maxMaskedCharacters: 16,
|
|
80
|
+
unmaskedStartCharacters: 0,
|
|
81
|
+
unmaskedEndCharacters: 0
|
|
82
|
+
},
|
|
83
|
+
passwordFields: [],
|
|
84
|
+
|
|
85
|
+
phoneMaskOptions: {
|
|
86
|
+
maskWith: "*",
|
|
87
|
+
unmaskedStartDigits: 4,
|
|
88
|
+
unmaskedEndDigits: 1
|
|
89
|
+
},
|
|
90
|
+
phoneFields: [],
|
|
91
|
+
|
|
92
|
+
stringMaskOptions: {
|
|
93
|
+
maskWith: "*",
|
|
94
|
+
maskOnlyFirstOccurance: false,
|
|
95
|
+
values: [],
|
|
96
|
+
maskAll: false,
|
|
97
|
+
maskSpace: true
|
|
98
|
+
},
|
|
99
|
+
stringFields: [],
|
|
100
|
+
|
|
101
|
+
uuidMaskOptions: {
|
|
102
|
+
maskWith: "*",
|
|
103
|
+
unmaskedStartCharacters: 0,
|
|
104
|
+
unmaskedEndCharacters: 0
|
|
105
|
+
},
|
|
106
|
+
uuidFields: [],
|
|
107
|
+
|
|
108
|
+
jwtMaskOptions: {
|
|
109
|
+
maskWith: '*',
|
|
110
|
+
maxMaskedCharacters: 512,
|
|
111
|
+
maskDot: true,
|
|
112
|
+
maskHeader: true,
|
|
113
|
+
maskPayload: true,
|
|
114
|
+
maskSignature: true
|
|
115
|
+
},
|
|
116
|
+
jwtFields: [],
|
|
117
|
+
// To extend the mask function to other types of data.
|
|
118
|
+
genericStrings: [
|
|
119
|
+
{
|
|
120
|
+
config: {
|
|
121
|
+
maskWith: "*",
|
|
122
|
+
maxMaskedCharacters: 256,
|
|
123
|
+
unmaskedStartDigits: 0,
|
|
124
|
+
unmaskedEndDigits: 0
|
|
125
|
+
},
|
|
126
|
+
fields: []
|
|
127
|
+
}
|
|
128
|
+
]
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### JSON mask examples
|
|
134
|
+
Example1:
|
|
135
|
+
```javascript
|
|
136
|
+
const jsonInput = {
|
|
137
|
+
'credit': '1234-5678-8765-1234',
|
|
138
|
+
'debit': '0000-1111-2222-3333',
|
|
139
|
+
'primaryEmail': 'primary@Email.com',
|
|
140
|
+
'secondaryEmail': 'secondary@Email.com',
|
|
141
|
+
'password': 'dummyPassword',
|
|
142
|
+
'homePhone': "+1 1234567890",
|
|
143
|
+
'workPhone': "+1 9876543210",
|
|
144
|
+
'addressLine1': "This is my addressline 1. This is my home",
|
|
145
|
+
'addressLine2': "AddressLine 2",
|
|
146
|
+
'uuid1': '123e4567-e89b-12d3-a456-426614174000',
|
|
147
|
+
'randomStrings': {
|
|
148
|
+
'row1': 'This is row 1 random string',
|
|
149
|
+
'row2': ['Entry1', 'Entry2', 'Entry3'],
|
|
150
|
+
'row3': {
|
|
151
|
+
'key1': 'Row3 Object 1',
|
|
152
|
+
'key2': 'Row3 Object 2',
|
|
153
|
+
'key3': ['Entry1', 'Entry2', 'Entry3']
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const jsonMaskConfig = {
|
|
159
|
+
cardFields: ['credit', 'debit'],
|
|
160
|
+
emailFields: ['primaryEmail', 'secondaryEmail'],
|
|
161
|
+
passwordFields: ['password'],
|
|
162
|
+
phoneFields: ['homePhone', 'workPhone'],
|
|
163
|
+
stringMaskOptions: {
|
|
164
|
+
maskWith: "*",
|
|
165
|
+
maskOnlyFirstOccurance: false,
|
|
166
|
+
values: ["This"]
|
|
167
|
+
},
|
|
168
|
+
stringFields: ['addressLine1', 'addressLine2'],
|
|
169
|
+
uuidFields: ['uuid1'],
|
|
170
|
+
genericStrings: [
|
|
171
|
+
{
|
|
172
|
+
fields: ['randomStrings.row1'],
|
|
173
|
+
config: {
|
|
174
|
+
maskWith: '*',
|
|
175
|
+
unmaskedStartCharacters: 2,
|
|
176
|
+
unmaskedEndCharacters: 3,
|
|
177
|
+
maxMaskedCharacters: 8
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
{ fields: ['randomStrings.row2.*'], config: { maskWith: 'X', unmaskedEndCharacters: 1 } },
|
|
181
|
+
{ fields: ['randomStrings.row3.key1'] },
|
|
182
|
+
{
|
|
183
|
+
fields: ['randomStrings.row3.key3.*'],
|
|
184
|
+
config: { maskWith: '@', unmaskedEndCharacters: 1 }
|
|
185
|
+
}
|
|
186
|
+
]
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const maskedJsonOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
|
|
190
|
+
|
|
191
|
+
Output:
|
|
192
|
+
{
|
|
193
|
+
credit: '1234-****-****-***4',
|
|
194
|
+
debit: '0000-****-****-***3',
|
|
195
|
+
primaryEmail: 'pri****@*******om',
|
|
196
|
+
secondaryEmail: 'sec******@*******om',
|
|
197
|
+
password: '*************',
|
|
198
|
+
homePhone: '+1 1********0',
|
|
199
|
+
workPhone: '+1 9********0',
|
|
200
|
+
addressLine1: '**** is my addressline 1. **** is my home',
|
|
201
|
+
addressLine2: 'AddressLine 2',
|
|
202
|
+
uuid1: '********-****-****-****-************',
|
|
203
|
+
randomStrings: {
|
|
204
|
+
row1: 'Th***ing',
|
|
205
|
+
row2: ['XXXXX1', 'XXXXX2', 'XXXXX3'],
|
|
206
|
+
row3: {
|
|
207
|
+
key1: '*************',
|
|
208
|
+
key2: 'Row3 Object 2',
|
|
209
|
+
key3: ['@@@@@1', '@@@@@2', '@@@@@3']
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
Example2: Mask with custom configs for each/any type of fields
|
|
216
|
+
|
|
217
|
+
const jsonInput2 = {
|
|
218
|
+
'credit': '1234-5678-8765-1234',
|
|
219
|
+
'debit': '0000-1111-2222-3333',
|
|
220
|
+
'primaryEmail': 'primary@Email.com',
|
|
221
|
+
'secondaryEmail': 'secondary@Email.com',
|
|
222
|
+
'password': 'dummyPasswordANDdummyPassword',
|
|
223
|
+
'homePhone': "+1 1234567890",
|
|
224
|
+
'workPhone': "+1 9876543210",
|
|
225
|
+
'addressLine1': "This is my addressline 1. This is my home",
|
|
226
|
+
'addressLine2': "AddressLine 2",
|
|
227
|
+
'uuid1': '123e4567-e89b-12d3-a456-426614174000'
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const jsonMaskConfig2 = {
|
|
231
|
+
// Card
|
|
232
|
+
cardMaskOptions: { maskWith: "X", unmaskedStartDigits: 2,unmaskedEndDigits: 4 },
|
|
233
|
+
cardFields: ['credit', 'debit'],
|
|
234
|
+
|
|
235
|
+
// Email
|
|
236
|
+
emailMaskOptions: { maskWith: "*", unmaskedStartCharactersBeforeAt: 2, unmaskedEndCharactersAfterAt: 2, maskAtTheRate: false },
|
|
237
|
+
emailFields: ['primaryEmail', 'secondaryEmail'],
|
|
238
|
+
|
|
239
|
+
// Password
|
|
240
|
+
passwordMaskOptions: { maskWith: "*", maxMaskedCharacters: 10, unmaskedStartCharacters: 0, unmaskedEndCharacters: 0 },
|
|
241
|
+
passwordFields: ['password'],
|
|
242
|
+
|
|
243
|
+
// Phone
|
|
244
|
+
phoneMaskOptions: { maskWith: "*", unmaskedStartDigits: 2, unmaskedEndDigits: 1 },
|
|
245
|
+
phoneFields: ['homePhone', 'workPhone'],
|
|
246
|
+
|
|
247
|
+
// String
|
|
248
|
+
stringMaskOptions: { maskWith: "*", maskOnlyFirstOccurance: false, values: [], maskAll: true, maskSpace: false },
|
|
249
|
+
stringFields: ['addressLine1', 'addressLine2'],
|
|
250
|
+
|
|
251
|
+
// UUID
|
|
252
|
+
uuidMaskOptions: { maskWith: "*", unmaskedStartCharacters: 4, unmaskedEndCharacters: 2 },
|
|
253
|
+
uuidFields: ['uuid1']
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
const maskedJsonOutput2 = maskData.maskJSON2(jsonInput2, jsonMaskConfig2);
|
|
257
|
+
|
|
258
|
+
Output:
|
|
259
|
+
|
|
260
|
+
{
|
|
261
|
+
credit: '12XX-XXXX-XXXX-1234',
|
|
262
|
+
debit: '00XX-XXXX-XXXX-3333',
|
|
263
|
+
primaryEmail: 'pr*****@*******om',
|
|
264
|
+
secondaryEmail: 'se*******@*******om',
|
|
265
|
+
password: '**********',
|
|
266
|
+
homePhone: '+1**********0',
|
|
267
|
+
workPhone: '+1**********0',
|
|
268
|
+
addressLine1: '**** ** ** *********** ** **** ** ** ****',
|
|
269
|
+
addressLine2: '*********** *',
|
|
270
|
+
uuid1: '123e****-****-****-****-**********00'
|
|
271
|
+
}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
Example3: Mask nested json fields -> Use dot(.) and Array([]) notation to specify the inner fields.
|
|
275
|
+
|
|
276
|
+
```javascript
|
|
277
|
+
|
|
278
|
+
const jsonInput2 = {
|
|
279
|
+
cards: {
|
|
280
|
+
creditCards: ['1234-5678-8765-1234', '1111-2222-1111-2222'],
|
|
281
|
+
debitCards: ['0000-1111-2222-3333', '2222-1111-3333-4444']
|
|
282
|
+
},
|
|
283
|
+
emails: {
|
|
284
|
+
primaryEmail: 'primary@Email.com',
|
|
285
|
+
secondaryEmail: 'secondary@Email.com'
|
|
286
|
+
},
|
|
287
|
+
passwords: [
|
|
288
|
+
['dummyPasswordANDdummyPassword', 'dummyPasswordANDdummyPassword']
|
|
289
|
+
],
|
|
290
|
+
phones: {
|
|
291
|
+
homePhone: "+1 1234567890",
|
|
292
|
+
workPhone: "+1 9876543210",
|
|
293
|
+
},
|
|
294
|
+
address: {
|
|
295
|
+
addressLine1: "This is my addressline 1. This is my home",
|
|
296
|
+
addressLine2: "AddressLine 2"
|
|
297
|
+
},
|
|
298
|
+
uuids: {
|
|
299
|
+
uuid1: '123e4567-e89b-12d3-a456-426614174000'
|
|
300
|
+
},
|
|
301
|
+
jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ'
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
const jsonMaskConfig2 = {
|
|
305
|
+
// Card
|
|
306
|
+
cardMaskOptions: { maskWith: "X", unmaskedStartDigits: 2,unmaskedEndDigits: 4 },
|
|
307
|
+
cardFields: ['cards.creditCards[0]', 'cards.creditCards[1]', 'cards.debitCards[0]', 'cards.debitCards[1]'],
|
|
308
|
+
|
|
309
|
+
// Email
|
|
310
|
+
emailMaskOptions: { maskWith: "*", unmaskedStartCharactersBeforeAt: 2, unmaskedEndCharactersAfterAt: 2, maskAtTheRate: false },
|
|
311
|
+
emailFields: ['emails.primaryEmail', 'emails.secondaryEmail'],
|
|
312
|
+
|
|
313
|
+
// Password
|
|
314
|
+
passwordMaskOptions: { maskWith: "*", maxMaskedCharacters: 10, unmaskedStartCharacters: 0, unmaskedEndCharacters: 0 },
|
|
315
|
+
passwordFields: ['passwords[0][0]]', 'passwords[0][1]'],
|
|
316
|
+
|
|
317
|
+
// Phone
|
|
318
|
+
phoneMaskOptions: { maskWith: "*", unmaskedStartDigits: 2, unmaskedEndDigits: 1 },
|
|
319
|
+
phoneFields: ['phones.homePhone', 'phones.workPhone'],
|
|
320
|
+
|
|
321
|
+
// String
|
|
322
|
+
stringMaskOptions: { maskWith: "*", maskOnlyFirstOccurance: false, values: [], maskAll: true, maskSpace: false },
|
|
323
|
+
stringFields: ['address.addressLine1', 'address.addressLine2'],
|
|
324
|
+
|
|
325
|
+
// UUID
|
|
326
|
+
uuidMaskOptions: { maskWith: "*", unmaskedStartCharacters: 4, unmaskedEndCharacters: 2 },
|
|
327
|
+
uuidFields: ['uuids.uuid1']
|
|
328
|
+
|
|
329
|
+
// JWT
|
|
330
|
+
jwtMaskOptions: { maskWith: '*', maxMaskedCharacters: 512, maskDot: true, maskHeader: true, maskPayload: true, maskSignature: true},
|
|
331
|
+
jwtFields: ['jwt']
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
const maskedJsonOutput2 = MaskData.maskJSON2(jsonInput2, jsonMaskConfig2);
|
|
335
|
+
// OUTPUT:
|
|
336
|
+
{
|
|
337
|
+
cards: {
|
|
338
|
+
creditCards: [ '12XX-XXXX-XXXX-1234', '11XX-XXXX-XXXX-2222' ],
|
|
339
|
+
debitCards: [ '00XX-XXXX-XXXX-3333', '22XX-XXXX-XXXX-4444' ]
|
|
340
|
+
},
|
|
341
|
+
emails: {
|
|
342
|
+
primaryEmail: 'pr*****@*******om',
|
|
343
|
+
secondaryEmail: 'se*******@*******om'
|
|
344
|
+
},
|
|
345
|
+
passwords: [ [ '**********', '**********' ] ],
|
|
346
|
+
phones: { homePhone: '+1**********0', workPhone: '+1**********0' },
|
|
347
|
+
address: {
|
|
348
|
+
addressLine1: '**** ** ** *********** ** **** ** ** ****',
|
|
349
|
+
addressLine2: '*********** *'
|
|
350
|
+
},
|
|
351
|
+
uuids: { uuid1: '123e****-****-****-****-**********00' },
|
|
352
|
+
jwt: '*********************************************************************************************************'
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
```
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
## Mask JWT Token
|
|
2
|
+
This function returns the masked JWT tokens. A JWT token consists of 3 parts separated by 2 dots. i.e, `{header}.{payload}.{signature}`. Based on the usecase, we will have to mask/keep a part/s of jwt token. With maskdata you can mask JWT tokens with all possible combinations.
|
|
3
|
+
If the input is null, undefined, non-string, string with length < 5, doesn't contain 2 dots(invalid JWT format), the function will return the input as it is without masking.
|
|
4
|
+
|
|
5
|
+
```javascript
|
|
6
|
+
|
|
7
|
+
const MaskData = require('./maskdata');
|
|
8
|
+
|
|
9
|
+
const jwtMaskOptions = {
|
|
10
|
+
// Character to mask the data. The default value is '*'
|
|
11
|
+
maskWith: '*',
|
|
12
|
+
|
|
13
|
+
// Max masked characters in the output(EXCLUDING the unmasked characters). Default value is 512
|
|
14
|
+
maxMaskedCharacters: 512,
|
|
15
|
+
|
|
16
|
+
// Config to mask OR keep the dots(.). Default value is true, i.e, mask dots
|
|
17
|
+
maskDot: true,
|
|
18
|
+
|
|
19
|
+
// Config to mask OR keep the first part of the JWT. i.e, the header part. Default value is true, i.e, mask the header part
|
|
20
|
+
maskHeader: true,
|
|
21
|
+
|
|
22
|
+
// Config to mask OR keep the second part of the JWT. i.e, the payload part. Default value is true, i.e, mask the payload part
|
|
23
|
+
maskPayload: true,
|
|
24
|
+
|
|
25
|
+
// Config to mask OR keep the third part of the JWT. i.e, the signature part. Default value is true, i.e, mask the signature part
|
|
26
|
+
maskSignature: true
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const jwt =
|
|
30
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ';
|
|
31
|
+
|
|
32
|
+
/** In this example,
|
|
33
|
+
* Header = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
|
|
34
|
+
* Payload = eyJpYXQiOjE1MTYyMzkwMjJ9
|
|
35
|
+
* Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
|
|
36
|
+
*/
|
|
37
|
+
const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
|
|
38
|
+
|
|
39
|
+
// Output:
|
|
40
|
+
*********************************************************************************************************
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
#### Example: Mask only the signature part
|
|
45
|
+
|
|
46
|
+
```javascript
|
|
47
|
+
|
|
48
|
+
const MaskData = require('./maskdata');
|
|
49
|
+
|
|
50
|
+
const jwtMaskOptions = {
|
|
51
|
+
maskWith: '*',
|
|
52
|
+
maxMaskedCharacters: 512,
|
|
53
|
+
maskDot: false,
|
|
54
|
+
maskHeader: false,
|
|
55
|
+
maskPayload: false,
|
|
56
|
+
maskSignature: true
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const jwt =
|
|
60
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ';
|
|
61
|
+
|
|
62
|
+
/** In this example,
|
|
63
|
+
* Header = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
|
|
64
|
+
* Payload = eyJpYXQiOjE1MTYyMzkwMjJ9
|
|
65
|
+
* Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
|
|
66
|
+
*/
|
|
67
|
+
const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
|
|
68
|
+
|
|
69
|
+
// Output:
|
|
70
|
+
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.*******************************************
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
#### Example: Mask header and the signature part
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
|
|
78
|
+
const MaskData = require('./maskdata');
|
|
79
|
+
|
|
80
|
+
const jwtMaskOptions = {
|
|
81
|
+
maskWith: '*',
|
|
82
|
+
maxMaskedCharacters: 512,
|
|
83
|
+
maskDot: false,
|
|
84
|
+
maskHeader: true,
|
|
85
|
+
maskPayload: false,
|
|
86
|
+
maskSignature: true
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const jwt =
|
|
90
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ';
|
|
91
|
+
|
|
92
|
+
/** In this example,
|
|
93
|
+
* Header = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
|
|
94
|
+
* Payload = eyJpYXQiOjE1MTYyMzkwMjJ9
|
|
95
|
+
* Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
|
|
96
|
+
*/
|
|
97
|
+
const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
|
|
98
|
+
|
|
99
|
+
// Output:
|
|
100
|
+
************************************.eyJpYXQiOjE1MTYyMzkwMjJ9.*******************************************
|
|
101
|
+
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
#### Example: Mask all parts except dots and limit the max masked characters in the output to lower value, say 16
|
|
105
|
+
|
|
106
|
+
```javascript
|
|
107
|
+
|
|
108
|
+
const MaskData = require('./maskdata');
|
|
109
|
+
|
|
110
|
+
const jwtMaskOptions = {
|
|
111
|
+
maskWith: '*',
|
|
112
|
+
maxMaskedCharacters: 16,
|
|
113
|
+
maskDot: false,
|
|
114
|
+
maskHeader: true,
|
|
115
|
+
maskPayload: true,
|
|
116
|
+
maskSignature: true
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const jwt =
|
|
120
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ';
|
|
121
|
+
|
|
122
|
+
/** In this example,
|
|
123
|
+
* Header = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
|
|
124
|
+
* Payload = eyJpYXQiOjE1MTYyMzkwMjJ9
|
|
125
|
+
* Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
|
|
126
|
+
*/
|
|
127
|
+
const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
|
|
128
|
+
|
|
129
|
+
// Output:
|
|
130
|
+
******.*****.*****
|
|
131
|
+
|
|
132
|
+
```
|