maskdata 1.2.3 → 1.2.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 +155 -4
- package/index.d.ts +10 -0
- package/index.js +5 -0
- package/lib/helpers/Constants.js +11 -1
- package/lib/helpers/MaskHelper.js +37 -0
- package/lib/maskService/JsonMask.js +4 -1
- package/lib/maskService/JwtMask.js +92 -0
- package/package.json +6 -2
- package/tests/scribble.js +85 -0
- package/tests/testJsonFields.js +192 -383
- package/tests/testJwt.js +262 -0
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
|
|
|
4
4
|
# Table of Contents
|
|
5
5
|
- [Features](#features)
|
|
6
6
|
- [Install maskdata](#install-maskdata)
|
|
7
|
-
- [Version 1.2.
|
|
7
|
+
- [Version 1.2.4 Features](#release-features)
|
|
8
8
|
- [How to Use](#how-to-use)
|
|
9
9
|
- [Maskdata for Typescript](#maskdata-for-typescript)
|
|
10
10
|
- [Mask Card number](#mask-card-number)
|
|
@@ -19,6 +19,7 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
|
|
|
19
19
|
- [Mask Phone Number with the default configuration](#mask-phone-number-with-the-default-configuration)
|
|
20
20
|
- [Mask words/characters in a string](#mask-the-characters-or-words-in-the-string)
|
|
21
21
|
- [Mask UUID](#mask-uuid)
|
|
22
|
+
- [Mask JWT Token](#mask-jwt-token)
|
|
22
23
|
- [Get Nested JSON Property](#get-nested-json-property)
|
|
23
24
|
- [Replace the value of a JSON field](#replace-the-value-of-a-json-field)
|
|
24
25
|
- [Report Bugs](#report-bugs)
|
|
@@ -32,6 +33,7 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
|
|
|
32
33
|
* Mask Phone numbers
|
|
33
34
|
* Mask the given words/substrings from throughout a String
|
|
34
35
|
* Mask UUIDs
|
|
36
|
+
* Mask JWT tokens
|
|
35
37
|
* 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)
|
|
36
38
|
* Get nested field from JSON
|
|
37
39
|
* Set/Replace nested field values from JSON
|
|
@@ -41,6 +43,11 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
|
|
|
41
43
|
> npm i maskdata
|
|
42
44
|
|
|
43
45
|
# Release Features
|
|
46
|
+
### Version 1.2.4
|
|
47
|
+
- JWT token masking: Mask JWT tokens with configs to mask as per your need. More details: [Mask JWT Token](#mask-jwt-token)
|
|
48
|
+
- Mask Json now supports JWT token masking also. More details: [Mask JWT in a JSON](#mask-json)
|
|
49
|
+
- Bug fix in maskJson2 where it was not checking for the empty/null/undefined fields and was resulting in error `"TypeError: validatedConfig[typeToFunctionMap[key][1]] is not iterable"`
|
|
50
|
+
- Better test coverage to the module with the addition of new test cases
|
|
44
51
|
### Version 1.2.3
|
|
45
52
|
- Bug fix for masking a list of elements in the nested json. More details: [Mask multiple fields](#mask-multiple-fields)
|
|
46
53
|
- Deprecated *maskJsonFields*(For documentation on the maskJsonFields, check previous version README.md files) function and will be removed in the subsequent versions. Use *maksJson2* instead: https://www.npmjs.com/package/maskdata#mask-json
|
|
@@ -189,6 +196,9 @@ const defaultjsonMask2Configs = {
|
|
|
189
196
|
|
|
190
197
|
uuidMaskOptions: defaultUuidMaskOptions, // Optional
|
|
191
198
|
uuidFields: [] // List of UUID fields to be masked
|
|
199
|
+
|
|
200
|
+
jwtMaskOptions: defaultJwtMaskOptions, // Optional
|
|
201
|
+
jwtFields: [] // List of JWT fields to be masked
|
|
192
202
|
};
|
|
193
203
|
```
|
|
194
204
|
<b>NOTE: For defaultCardMaskOptions, defaultEmailMask2Options, defaultPasswordMaskOptions, defaultPhoneMaskOptions, defaultStringMaskOptions and defaultUuidMaskOptions refer the corresponding masking features. </b>
|
|
@@ -654,6 +664,143 @@ const uuidAfterMasking = MaskData.maskUuid(uuidInput, maskUuidOptions);
|
|
|
654
664
|
|
|
655
665
|
```
|
|
656
666
|
|
|
667
|
+
## Mask JWT Token
|
|
668
|
+
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.
|
|
669
|
+
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.
|
|
670
|
+
|
|
671
|
+
```javascript
|
|
672
|
+
|
|
673
|
+
const MaskData = require('./maskdata');
|
|
674
|
+
|
|
675
|
+
const jwtMaskOptions = {
|
|
676
|
+
// Character to mask the data. The default value is '*'
|
|
677
|
+
maskWith: '*',
|
|
678
|
+
|
|
679
|
+
// Max masked characters in the output(EXCLUDING the unmasked characters). Default value is 512
|
|
680
|
+
maxMaskedCharacters: 512,
|
|
681
|
+
|
|
682
|
+
// Config to mask OR keep the dots(.). Default value is true, i.e, mask dots
|
|
683
|
+
maskDot: true,
|
|
684
|
+
|
|
685
|
+
// 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
|
|
686
|
+
maskHeader: true,
|
|
687
|
+
|
|
688
|
+
// 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
|
|
689
|
+
maskPayload: true,
|
|
690
|
+
|
|
691
|
+
// 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
|
|
692
|
+
maskSignature: true
|
|
693
|
+
};
|
|
694
|
+
|
|
695
|
+
const jwt =
|
|
696
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ';
|
|
697
|
+
|
|
698
|
+
/** In this example,
|
|
699
|
+
* Header = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
|
|
700
|
+
* Payload = eyJpYXQiOjE1MTYyMzkwMjJ9
|
|
701
|
+
* Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
|
|
702
|
+
*/
|
|
703
|
+
const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
|
|
704
|
+
console.log(maskedJwt);
|
|
705
|
+
|
|
706
|
+
// Output:
|
|
707
|
+
*********************************************************************************************************
|
|
708
|
+
|
|
709
|
+
```
|
|
710
|
+
|
|
711
|
+
#### Example: Mask only the signature part
|
|
712
|
+
|
|
713
|
+
```javascript
|
|
714
|
+
|
|
715
|
+
const MaskData = require('./maskdata');
|
|
716
|
+
|
|
717
|
+
const jwtMaskOptions = {
|
|
718
|
+
maskWith: '*',
|
|
719
|
+
maxMaskedCharacters: 512,
|
|
720
|
+
maskDot: false,
|
|
721
|
+
maskHeader: false,
|
|
722
|
+
maskPayload: false,
|
|
723
|
+
maskSignature: true
|
|
724
|
+
};
|
|
725
|
+
|
|
726
|
+
const jwt =
|
|
727
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ';
|
|
728
|
+
|
|
729
|
+
/** In this example,
|
|
730
|
+
* Header = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
|
|
731
|
+
* Payload = eyJpYXQiOjE1MTYyMzkwMjJ9
|
|
732
|
+
* Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
|
|
733
|
+
*/
|
|
734
|
+
const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
|
|
735
|
+
console.log(maskedJwt);
|
|
736
|
+
|
|
737
|
+
// Output:
|
|
738
|
+
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.*******************************************
|
|
739
|
+
|
|
740
|
+
```
|
|
741
|
+
|
|
742
|
+
#### Example: Mask header and the signature part
|
|
743
|
+
|
|
744
|
+
```javascript
|
|
745
|
+
|
|
746
|
+
const MaskData = require('./maskdata');
|
|
747
|
+
|
|
748
|
+
const jwtMaskOptions = {
|
|
749
|
+
maskWith: '*',
|
|
750
|
+
maxMaskedCharacters: 512,
|
|
751
|
+
maskDot: false,
|
|
752
|
+
maskHeader: true,
|
|
753
|
+
maskPayload: false,
|
|
754
|
+
maskSignature: true
|
|
755
|
+
};
|
|
756
|
+
|
|
757
|
+
const jwt =
|
|
758
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ';
|
|
759
|
+
|
|
760
|
+
/** In this example,
|
|
761
|
+
* Header = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
|
|
762
|
+
* Payload = eyJpYXQiOjE1MTYyMzkwMjJ9
|
|
763
|
+
* Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
|
|
764
|
+
*/
|
|
765
|
+
const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
|
|
766
|
+
console.log(maskedJwt);
|
|
767
|
+
|
|
768
|
+
// Output:
|
|
769
|
+
************************************.eyJpYXQiOjE1MTYyMzkwMjJ9.*******************************************
|
|
770
|
+
|
|
771
|
+
```
|
|
772
|
+
|
|
773
|
+
#### Example: Mask all parts except dots and limit the max masked characters in the output to lower value, say 16
|
|
774
|
+
|
|
775
|
+
```javascript
|
|
776
|
+
|
|
777
|
+
const MaskData = require('./maskdata');
|
|
778
|
+
|
|
779
|
+
const jwtMaskOptions = {
|
|
780
|
+
maskWith: '*',
|
|
781
|
+
maxMaskedCharacters: 16,
|
|
782
|
+
maskDot: false,
|
|
783
|
+
maskHeader: true,
|
|
784
|
+
maskPayload: true,
|
|
785
|
+
maskSignature: true
|
|
786
|
+
};
|
|
787
|
+
|
|
788
|
+
const jwt =
|
|
789
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjJ9.tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ';
|
|
790
|
+
|
|
791
|
+
/** In this example,
|
|
792
|
+
* Header = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
|
|
793
|
+
* Payload = eyJpYXQiOjE1MTYyMzkwMjJ9
|
|
794
|
+
* Signature = tbDepxpstvGdW8TC3G8zg4B6rUYAOvfzdceoH48wgRQ
|
|
795
|
+
*/
|
|
796
|
+
const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
|
|
797
|
+
console.log(maskedJwt);
|
|
798
|
+
|
|
799
|
+
// Output:
|
|
800
|
+
******.*****.*****
|
|
801
|
+
|
|
802
|
+
```
|
|
803
|
+
|
|
657
804
|
## Get Nested JSON Property
|
|
658
805
|
This method returns the value of the nested JSON property if it exists. Otherwise, it returns ```undefined```
|
|
659
806
|
```javascript
|
|
@@ -726,7 +873,8 @@ const jsonInput = {
|
|
|
726
873
|
secondaryEmail: 'secondary@Email.com',
|
|
727
874
|
moreEmails: ["email1@email.com", "email2@email.com", "email3@email.com", {childEmail: "child@child.com", secondChild: {nestedkid: "hello@hello.com"}}]
|
|
728
875
|
},
|
|
729
|
-
array: ["element1", "element22", "element333"]
|
|
876
|
+
array: ["element1", "element22", "element333"],
|
|
877
|
+
jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJsb2wiLCJuYW1lIjoiVGVzdCIsImlhdCI6ImxvbCJ9.XNDxZcBWWEKYkCiu6XFGmAeuPF7iFnI7Sdv91gVZJMU'
|
|
730
878
|
};
|
|
731
879
|
|
|
732
880
|
const jsonMaskConfig = {
|
|
@@ -736,10 +884,12 @@ const jsonMaskConfig = {
|
|
|
736
884
|
unmaskedEndCharactersAfterAt: 0, maskAtTheRate: false },
|
|
737
885
|
|
|
738
886
|
stringMaskOptions: { maskWith: "?", maskOnlyFirstOccurance: false, values: [], maskAll: true, maskSpace: false },
|
|
887
|
+
jwtMaskOptions: { maskWith: '*', maxMaskedCharacters: 32, maskDot: false, maskHeader: true, maskPayload: true, maskSignature: true},
|
|
739
888
|
|
|
740
889
|
cardFields: ['cards[*].number'],
|
|
741
890
|
emailFields: ['emails.*'],
|
|
742
|
-
stringFields: ['array.*']
|
|
891
|
+
stringFields: ['array.*'],
|
|
892
|
+
jwtFields: ['jwt']
|
|
743
893
|
}
|
|
744
894
|
|
|
745
895
|
const maskedOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
|
|
@@ -775,7 +925,8 @@ const maskedOutput = maskData.maskJSON2(jsonInput, jsonMaskConfig);
|
|
|
775
925
|
}
|
|
776
926
|
]
|
|
777
927
|
},
|
|
778
|
-
"array": ["????????", "?????????", "??????????"]
|
|
928
|
+
"array": ["????????", "?????????", "??????????"],
|
|
929
|
+
"jwt": "************.**********.**********"
|
|
779
930
|
}
|
|
780
931
|
|
|
781
932
|
```
|
package/index.d.ts
CHANGED
|
@@ -45,6 +45,15 @@ declare module 'maskdata' {
|
|
|
45
45
|
unmaskedEndCharacters?: number;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
export interface JwtMaskOptions {
|
|
49
|
+
maskWith?: string;
|
|
50
|
+
maxMaskedCharacters?: number;
|
|
51
|
+
maskDot?: boolean;
|
|
52
|
+
maskHeader?: boolean;
|
|
53
|
+
maskPayload?: boolean;
|
|
54
|
+
maskSignature?: boolean;
|
|
55
|
+
}
|
|
56
|
+
|
|
48
57
|
export interface JsonMask2Configs {
|
|
49
58
|
cardMaskOptions?: CardMaskOptions;
|
|
50
59
|
cardFields?: string[];
|
|
@@ -72,6 +81,7 @@ declare module 'maskdata' {
|
|
|
72
81
|
export function maskCard(cardNumber?: string, options?: CardMaskOptions): string;
|
|
73
82
|
export function maskString(str?: string, options?: StringMaskOptions): string;
|
|
74
83
|
export function maskUuid(uuid?: string, options?: UuidMaskOptions): string;
|
|
84
|
+
export function maskJwt(jwt?: string, options?: JwtMaskOptions): string;
|
|
75
85
|
export function getInnerProperty(object: object, field: string): any;
|
|
76
86
|
export function replaceValue(object: object, field: string, value: any): object;
|
|
77
87
|
}
|
package/index.js
CHANGED
|
@@ -7,6 +7,7 @@ const MaskString = require('./lib/maskService/StringMask');
|
|
|
7
7
|
const MaskPassword = require('./lib/maskService/PasswordMask');
|
|
8
8
|
const MaskUuid = require('./lib/maskService/UuidMask');
|
|
9
9
|
const JsonMask = require('./lib/maskService/JsonMask');
|
|
10
|
+
const JwtMask = require('./lib/maskService/JwtMask');
|
|
10
11
|
const JsonGetSet = require('./lib/helpers/jsonGetSet');
|
|
11
12
|
|
|
12
13
|
class MaskData {
|
|
@@ -47,6 +48,10 @@ class MaskData {
|
|
|
47
48
|
return MaskUuid.maskUuid(uuid, options);
|
|
48
49
|
}
|
|
49
50
|
|
|
51
|
+
static maskJwt(jwt, options) {
|
|
52
|
+
return JwtMask.maskJwt(jwt, options);
|
|
53
|
+
}
|
|
54
|
+
|
|
50
55
|
static getInnerProperty(object, field) {
|
|
51
56
|
return JsonGetSet.get(object, field);
|
|
52
57
|
}
|
package/lib/helpers/Constants.js
CHANGED
|
@@ -11,6 +11,15 @@ const defaultEmailMask2Options = {
|
|
|
11
11
|
maskAtTheRate: false
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
+
const defaultJwtMaskOptions = {
|
|
15
|
+
maskWith: '*',
|
|
16
|
+
maxMaskedCharacters: 512,
|
|
17
|
+
maskDot: true,
|
|
18
|
+
maskHeader: true,
|
|
19
|
+
maskPayload: true,
|
|
20
|
+
maskSignature: true
|
|
21
|
+
};
|
|
22
|
+
|
|
14
23
|
const defaultJsonMaskOptions = {
|
|
15
24
|
maskWith: '*',
|
|
16
25
|
fields: [],
|
|
@@ -67,5 +76,6 @@ module.exports = {
|
|
|
67
76
|
defaultPasswordMaskOptions,
|
|
68
77
|
defaultStringMaskOptions,
|
|
69
78
|
defaultUuidMaskOptions,
|
|
70
|
-
defaultjsonMask2Configs
|
|
79
|
+
defaultjsonMask2Configs,
|
|
80
|
+
defaultJwtMaskOptions
|
|
71
81
|
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const { isBoolean } = require('lodash');
|
|
1
2
|
const BadOption = require('../ExceptionsHandler/BadOption');
|
|
2
3
|
const Constants = require('./Constants');
|
|
3
4
|
|
|
@@ -231,6 +232,42 @@ class MaskHelper {
|
|
|
231
232
|
}
|
|
232
233
|
}
|
|
233
234
|
|
|
235
|
+
static validateJwtMaskOptions(options) {
|
|
236
|
+
let reasons = [];
|
|
237
|
+
try {
|
|
238
|
+
options.maxMaskedCharacters = parseInt(options.maxMaskedCharacters);
|
|
239
|
+
if (isNaN(options.maxMaskedCharacters)) {
|
|
240
|
+
throw Error();
|
|
241
|
+
}
|
|
242
|
+
} catch (err) {
|
|
243
|
+
reasons.push('maxMaskedCharacters must be a positive integera');
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (options.maskDot === null || options.maskDot === undefined) {
|
|
247
|
+
options.maskDot = true;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (options.maskHeader === null || options.maskHeader === undefined) {
|
|
251
|
+
options.maskHeader = true;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (options.maskPayload === null || options.maskPayload === undefined) {
|
|
255
|
+
options.maskPayload = true;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (options.maskSignature === null || options.maskSignature === undefined) {
|
|
259
|
+
options.maskSignature = true;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (!options.maskWith || options.maskWith.toString().length === 0) {
|
|
263
|
+
options.maskWith = '*';
|
|
264
|
+
}
|
|
265
|
+
if (reasons.length > 0) {
|
|
266
|
+
const badOptions = new BadOption('Jwt mask configuration: ', reasons);
|
|
267
|
+
badOptions.throwBadOptionException();
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
234
271
|
static validateCardMaskOptions(options, cardLength) {
|
|
235
272
|
let reasons = [];
|
|
236
273
|
try {
|
|
@@ -9,6 +9,7 @@ const PasswordMask = require('./PasswordMask');
|
|
|
9
9
|
const PhoneMask = require('./PhoneMask');
|
|
10
10
|
const StringMask = require('./StringMask');
|
|
11
11
|
const UuidMask = require('./UuidMask');
|
|
12
|
+
const JwtMask = require('./JwtMask');
|
|
12
13
|
const JsonGetSet = require('../helpers/jsonGetSet');
|
|
13
14
|
|
|
14
15
|
const typeToFunctionMap = {
|
|
@@ -17,7 +18,8 @@ const typeToFunctionMap = {
|
|
|
17
18
|
password: [PasswordMask.maskPassword, 'passwordFields', 'passwordMaskOptions'],
|
|
18
19
|
phone: [PhoneMask.maskPhone, 'phoneFields', 'phoneMaskOptions'],
|
|
19
20
|
string: [StringMask.maskString, 'stringFields', 'stringMaskOptions'],
|
|
20
|
-
uuid: [UuidMask.maskUuid, 'uuidFields', 'uuidMaskOptions']
|
|
21
|
+
uuid: [UuidMask.maskUuid, 'uuidFields', 'uuidMaskOptions'],
|
|
22
|
+
jwt: [JwtMask.maskJwt, 'jwtFields', 'jwtMaskOptions']
|
|
21
23
|
};
|
|
22
24
|
|
|
23
25
|
class JsonMask {
|
|
@@ -127,6 +129,7 @@ class JsonMask {
|
|
|
127
129
|
}
|
|
128
130
|
|
|
129
131
|
for (const key of Object.keys(typeToFunctionMap)) {
|
|
132
|
+
if (!validatedConfig[typeToFunctionMap[key][1]]) continue;
|
|
130
133
|
for (const field of validatedConfig[typeToFunctionMap[key][1]]) {
|
|
131
134
|
this.mask(maskedObj, field, key, validatedConfig[typeToFunctionMap[key][2]]);
|
|
132
135
|
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const MaskHelper = require('../helpers/MaskHelper');
|
|
3
|
+
const Constants = require('../helpers/Constants');
|
|
4
|
+
|
|
5
|
+
class JwtMask {
|
|
6
|
+
static maskJwt(jwt, options) {
|
|
7
|
+
if (!jwt || typeof jwt !== 'string' || jwt.length < 5) return jwt;
|
|
8
|
+
const countOfDots = jwt.split('.').length - 1;
|
|
9
|
+
if (countOfDots != 2) return jwt;
|
|
10
|
+
let maskedJwt = '';
|
|
11
|
+
options = options
|
|
12
|
+
? MaskHelper.mapWithDefaultValues(options, Constants.defaultJwtMaskOptions)
|
|
13
|
+
: Constants.defaultJwtMaskOptions;
|
|
14
|
+
MaskHelper.validateJwtMaskOptions(options);
|
|
15
|
+
const indexOfFirstDot = jwt.indexOf('.');
|
|
16
|
+
const indexOfSecondDot = jwt.indexOf('.', indexOfFirstDot + 1);
|
|
17
|
+
|
|
18
|
+
if (!indexOfFirstDot || !indexOfSecondDot || indexOfFirstDot <= 0 || indexOfSecondDot <= 0) {
|
|
19
|
+
return jwt;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const part1 = jwt.substring(0, indexOfFirstDot);
|
|
23
|
+
const part2 = jwt.substring(indexOfFirstDot + 1, indexOfSecondDot);
|
|
24
|
+
const part3 = jwt.substring(indexOfSecondDot + 1);
|
|
25
|
+
|
|
26
|
+
const maxMasked =
|
|
27
|
+
options.maxMaskedCharacters > 2
|
|
28
|
+
? options.maxMaskedCharacters - (options.maskDot === true ? 2 : 0)
|
|
29
|
+
: options.maxMaskedCharacters;
|
|
30
|
+
|
|
31
|
+
let maskpartcnt = 0;
|
|
32
|
+
let maskLength = 0;
|
|
33
|
+
if (options.maskHeader === true) {
|
|
34
|
+
maskpartcnt++;
|
|
35
|
+
maskLength += part1.length;
|
|
36
|
+
}
|
|
37
|
+
if (options.maskPayload === true) {
|
|
38
|
+
maskpartcnt++;
|
|
39
|
+
maskLength += part2.length;
|
|
40
|
+
}
|
|
41
|
+
if (options.maskSignature === true) {
|
|
42
|
+
maskpartcnt++;
|
|
43
|
+
maskLength += part3.length;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let residual = 0;
|
|
47
|
+
let eachMask = 0;
|
|
48
|
+
|
|
49
|
+
const isJwtLengthGreater = maskLength > maxMasked;
|
|
50
|
+
|
|
51
|
+
if (isJwtLengthGreater) {
|
|
52
|
+
residual = maxMasked % maskpartcnt;
|
|
53
|
+
eachMask = maxMasked / maskpartcnt;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let maskedCharCount = 0;
|
|
57
|
+
|
|
58
|
+
if (options.maskHeader === false) {
|
|
59
|
+
maskedJwt += part1;
|
|
60
|
+
maskedJwt += options.maskDot === true ? options.maskWith : '.';
|
|
61
|
+
} else {
|
|
62
|
+
maskedJwt += isJwtLengthGreater
|
|
63
|
+
? options.maskWith.repeat(residual + eachMask)
|
|
64
|
+
: options.maskWith.repeat(part1.length);
|
|
65
|
+
residual = 0;
|
|
66
|
+
maskedCharCount += part1.length;
|
|
67
|
+
maskedJwt += options.maskDot === true ? options.maskWith : '.';
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (options.maskPayload === false) {
|
|
71
|
+
maskedJwt += part2;
|
|
72
|
+
maskedJwt += options.maskDot === true ? options.maskWith : '.';
|
|
73
|
+
} else {
|
|
74
|
+
maskedJwt += isJwtLengthGreater
|
|
75
|
+
? options.maskWith.repeat(residual + eachMask)
|
|
76
|
+
: options.maskWith.repeat(part2.length);
|
|
77
|
+
residual = 0;
|
|
78
|
+
maskedJwt += options.maskDot === true ? options.maskWith : '.';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (options.maskSignature === false) {
|
|
82
|
+
maskedJwt += part3;
|
|
83
|
+
} else {
|
|
84
|
+
maskedJwt += isJwtLengthGreater
|
|
85
|
+
? options.maskWith.repeat(residual + eachMask)
|
|
86
|
+
: options.maskWith.repeat(part3.length);
|
|
87
|
+
}
|
|
88
|
+
return maskedJwt;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
module.exports = JwtMask;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "maskdata",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"description": "Node module to mask various kinds of data in the required formats",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -33,7 +33,11 @@
|
|
|
33
33
|
"phone",
|
|
34
34
|
"uuid",
|
|
35
35
|
"string",
|
|
36
|
-
"sensitive"
|
|
36
|
+
"sensitive",
|
|
37
|
+
"JWT",
|
|
38
|
+
"jwt mask",
|
|
39
|
+
"jwt token",
|
|
40
|
+
"token"
|
|
37
41
|
],
|
|
38
42
|
"author": "sumukha.hs<sumukha.hs369@gmail.com>",
|
|
39
43
|
"homepage": "https://www.npmjs.com/package/maskdata?activeTab=readme",
|
package/tests/scribble.js
CHANGED
|
@@ -458,3 +458,88 @@ const jsonMaskConfig2 = {
|
|
|
458
458
|
|
|
459
459
|
const maskedJsonOutput2 = MaskData.maskJSON2(jsonInput2, jsonMaskConfig2);
|
|
460
460
|
console.log(JSON.stringify(maskedJsonOutput2));
|
|
461
|
+
|
|
462
|
+
const jwtMaskOptions = {
|
|
463
|
+
maskWith: '*',
|
|
464
|
+
maxMaskedCharacters: 512,
|
|
465
|
+
maskDot: true,
|
|
466
|
+
maskHeader: true,
|
|
467
|
+
maskPayload: true,
|
|
468
|
+
maskSignature: true
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
const jwt =
|
|
472
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJsb2wiLCJuYW1lIjoiVGVzdCIsImlhdCI6ImxvbCJ9.XNDxZcBWWEKYkCiu6XFGmAeuPF7iFnI7Sdv91gVZJMU';
|
|
473
|
+
console.log(jwt);
|
|
474
|
+
const maskedJwt = MaskData.maskJwt(jwt, jwtMaskOptions);
|
|
475
|
+
console.log(maskedJwt);
|
|
476
|
+
|
|
477
|
+
const jsonInput3 = {
|
|
478
|
+
cards: [
|
|
479
|
+
{
|
|
480
|
+
number: '1234-5678-8765-1234'
|
|
481
|
+
},
|
|
482
|
+
{
|
|
483
|
+
number: '1111-2222-1111-2222'
|
|
484
|
+
},
|
|
485
|
+
{
|
|
486
|
+
number: '0000-1111-2222-3333'
|
|
487
|
+
},
|
|
488
|
+
{
|
|
489
|
+
name: 'No card number here'
|
|
490
|
+
}
|
|
491
|
+
],
|
|
492
|
+
emails: {
|
|
493
|
+
primaryEmail: 'primary@Email.com',
|
|
494
|
+
secondaryEmail: 'secondary@Email.com',
|
|
495
|
+
moreEmails: [
|
|
496
|
+
'email1@email.com',
|
|
497
|
+
'email2@email.com',
|
|
498
|
+
'email3@email.com',
|
|
499
|
+
{ childEmail: 'child@child.com', secondChild: { nestedkid: 'hello@hello.com' } }
|
|
500
|
+
]
|
|
501
|
+
},
|
|
502
|
+
array: ['element1', 'element22', 'element333'],
|
|
503
|
+
jwts: [
|
|
504
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJsb2wiLCJuYW1lIjoiVGVzdCIsImlhdCI6ImxvbCJ9.XNDxZcBWWEKYkCiu6XFGmAeuPF7iFnI7Sdv91gVZJMU',
|
|
505
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJsb2wiLCJuYW1lIjoiVGVzdCIsImlhdCI6ImxvbCJ9.XNDxZcBWWEKYkCiu6XFGmAeuPF7iFnI7Sdv91gVZJMU',
|
|
506
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJsb2wiLCJuYW1lIjoiVGVzdCIsImlhdCI6ImxvbCJ9.XNDxZcBWWEKYkCiu6XFGmAeuPF7iFnI7Sdv91gVZJMU'
|
|
507
|
+
]
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
const jsonMaskConfig3 = {
|
|
511
|
+
cardMaskOptions: { maskWith: 'X', unmaskedStartDigits: 0, unmaskedEndDigits: 0 },
|
|
512
|
+
|
|
513
|
+
emailMaskOptions: {
|
|
514
|
+
maskWith: '*',
|
|
515
|
+
unmaskedStartCharactersBeforeAt: 0,
|
|
516
|
+
unmaskedEndCharactersAfterAt: 0,
|
|
517
|
+
maskAtTheRate: false
|
|
518
|
+
},
|
|
519
|
+
|
|
520
|
+
stringMaskOptions: {
|
|
521
|
+
maskWith: '?',
|
|
522
|
+
maskOnlyFirstOccurance: false,
|
|
523
|
+
values: [],
|
|
524
|
+
maskAll: true,
|
|
525
|
+
maskSpace: false
|
|
526
|
+
},
|
|
527
|
+
jwtMaskOptions: {
|
|
528
|
+
maskWith: '*',
|
|
529
|
+
maxMaskedCharacters: 32,
|
|
530
|
+
maskDot: false,
|
|
531
|
+
maskHeader: true,
|
|
532
|
+
maskPayload: true,
|
|
533
|
+
maskSignature: true
|
|
534
|
+
},
|
|
535
|
+
|
|
536
|
+
cardFields: ['cards[*].number'],
|
|
537
|
+
emailFields: ['emails.*'],
|
|
538
|
+
stringFields: ['array.*'],
|
|
539
|
+
jwtFields: ['jwts.*']
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
const maskedOutput = MaskData.maskJSON2(jsonInput3, jsonMaskConfig3);
|
|
543
|
+
|
|
544
|
+
console.log(maskedOutput);
|
|
545
|
+
console.log(maskedOutput['emails'].moreEmails[3].secondChild.nestedkid);
|