asma-helpers 0.33.3 → 0.33.5
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseJwt.d.ts","sourceRoot":"","sources":["../../src/helpers/parseJwt.ts"],"names":[],"mappings":"AAEA,wBAAgB,QAAQ,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,iBA2B3C"}
|
|
1
|
+
{"version":3,"file":"parseJwt.d.ts","sourceRoot":"","sources":["../../src/helpers/parseJwt.ts"],"names":[],"mappings":"AAEA,wBAAgB,QAAQ,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,iBA2B3C;AAQD,wBAAgB,sBAAsB,CAAC,YAAY,EAAE,MAAM,UA2C1D"}
|
package/lib/helpers/parseJwt.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { realWindow } from '..';
|
|
2
2
|
export function parseJwt(jwtToken) {
|
|
3
|
-
|
|
3
|
+
let base64Url = jwtToken?.split('.')[1];
|
|
4
4
|
if (!base64Url) {
|
|
5
5
|
return;
|
|
6
6
|
}
|
|
@@ -8,12 +8,12 @@ export function parseJwt(jwtToken) {
|
|
|
8
8
|
return JSON.parse(decodeURIComponent(escape(realWindow.atob(base64Url))));
|
|
9
9
|
}
|
|
10
10
|
catch (e) {
|
|
11
|
-
console.error('Error parsing jwt, JSON.parse(decodeURIComponent(escape(realWindow.atob(base64Url)))): ', e
|
|
11
|
+
console.error('tried to decode base64Url: ', base64Url, 'Error parsing jwt, JSON.parse(decodeURIComponent(escape(realWindow.atob(base64Url)))): ', e);
|
|
12
12
|
try {
|
|
13
13
|
return JSON.parse(realWindow.atob(base64Url));
|
|
14
14
|
}
|
|
15
15
|
catch (e) {
|
|
16
|
-
console.error('Error parsing jwt second step JSON.parse(realWindow.atob(base64Url)): ', e
|
|
16
|
+
console.error('tried to decode base64Url: ', base64Url, 'Error parsing jwt second step JSON.parse(realWindow.atob(base64Url)): ', e);
|
|
17
17
|
return;
|
|
18
18
|
}
|
|
19
19
|
}
|
|
@@ -23,4 +23,41 @@ function escape(str) {
|
|
|
23
23
|
return '%' + c.charCodeAt(0).toString(16);
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
|
+
export function splitAndValidateBase64(base64String) {
|
|
27
|
+
// Define the allowed Base64 character set including padding '='
|
|
28
|
+
const base64Charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
|
29
|
+
// Function to validate if a chunk is a valid Base64 segment
|
|
30
|
+
const isValidBase64Chunk = (chunk) => {
|
|
31
|
+
if (chunk.length !== 4) {
|
|
32
|
+
console.error(`chunk.length !== 4', chunk:${chunk}`);
|
|
33
|
+
return false; // Chunks must be 4 characters long
|
|
34
|
+
}
|
|
35
|
+
// Check each character in the chunk
|
|
36
|
+
for (let char of chunk) {
|
|
37
|
+
if (!base64Charset.includes(char)) {
|
|
38
|
+
console.error(`chunk: ${chunk} !base64Charset.includes(char), char:${char}`);
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
// Validate padding (if present)
|
|
43
|
+
const paddingCount = (chunk.match(/=/g) || []).length;
|
|
44
|
+
if (paddingCount > 2) {
|
|
45
|
+
console.error(`chunk: ${chunk}, paddingCount > 2, paddingCount:${paddingCount}`);
|
|
46
|
+
return false;
|
|
47
|
+
} // There can be at most 2 padding characters
|
|
48
|
+
// Padding should be at the end of the chunk
|
|
49
|
+
if (paddingCount > 0 && !chunk.endsWith('=')) {
|
|
50
|
+
console.error(`chunk: ${chunk},paddingCount:${paddingCount}, paddingCount > 0 && !chunk.endsWith('=')`);
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
};
|
|
55
|
+
// Split the string into 4-character chunks
|
|
56
|
+
const chunks = base64String.match(/.{1,4}/g) || [];
|
|
57
|
+
// Filter out invalid chunks
|
|
58
|
+
const validChunks = chunks.filter(isValidBase64Chunk);
|
|
59
|
+
// Join the valid chunks back into a single string
|
|
60
|
+
const validBase64String = validChunks.join('');
|
|
61
|
+
return validBase64String;
|
|
62
|
+
}
|
|
26
63
|
//# sourceMappingURL=parseJwt.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseJwt.js","sourceRoot":"","sources":["../../src/helpers/parseJwt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,IAAI,CAAA;AAE/B,MAAM,UAAU,QAAQ,CAAI,QAAgB;IACxC,
|
|
1
|
+
{"version":3,"file":"parseJwt.js","sourceRoot":"","sources":["../../src/helpers/parseJwt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,IAAI,CAAA;AAE/B,MAAM,UAAU,QAAQ,CAAI,QAAgB;IACxC,IAAI,SAAS,GAAG,QAAQ,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IAEvC,IAAI,CAAC,SAAS,EAAE,CAAC;QACb,OAAM;IACV,CAAC;IACD,IAAI,CAAC;QACD,OAAO,IAAI,CAAC,KAAK,CAAC,kBAAkB,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAM,CAAA;IAClF,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACT,OAAO,CAAC,KAAK,CACT,6BAA6B,EAC7B,SAAS,EACT,yFAAyF,EACzF,CAAC,CACJ,CAAA;QACD,IAAI,CAAC;YACD,OAAO,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,CAAM,CAAA;QACtD,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACT,OAAO,CAAC,KAAK,CACT,6BAA6B,EAC7B,SAAS,EACT,wEAAwE,EACxE,CAAC,CACJ,CAAA;YACD,OAAM;QACV,CAAC;IACL,CAAC;AACL,CAAC;AAED,SAAS,M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,YAAY,4CAA4C,CAAC,CAAA;YACvG,OAAO,KAAK,CAAA;QAChB,CAAC;QACD,OAAO,IAAI,CAAA;IACf,CAAC,CAAA;IAED,2CAA2C;IAC3C,MAAM,MAAM,GAAG,YAAY,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,EAAE,CAAA;IAElD,4BAA4B;IAC5B,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,CAAC,kBAAkB,CAAC,CAAA;IAErD,kDAAkD;IAClD,MAAM,iBAAiB,GAAG,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IAE9C,OAAO,iBAAiB,CAAA;AAC5B,CAAC"}
|
package/package.json
CHANGED
package/src/helpers/parseJwt.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { realWindow } from '..'
|
|
2
2
|
|
|
3
3
|
export function parseJwt<R>(jwtToken: string) {
|
|
4
|
-
|
|
4
|
+
let base64Url = jwtToken?.split('.')[1]
|
|
5
5
|
|
|
6
6
|
if (!base64Url) {
|
|
7
7
|
return
|
|
@@ -10,19 +10,19 @@ export function parseJwt<R>(jwtToken: string) {
|
|
|
10
10
|
return JSON.parse(decodeURIComponent(escape(realWindow.atob(base64Url)))) as R
|
|
11
11
|
} catch (e) {
|
|
12
12
|
console.error(
|
|
13
|
-
'Error parsing jwt, JSON.parse(decodeURIComponent(escape(realWindow.atob(base64Url)))): ',
|
|
14
|
-
e,
|
|
15
13
|
'tried to decode base64Url: ',
|
|
16
14
|
base64Url,
|
|
15
|
+
'Error parsing jwt, JSON.parse(decodeURIComponent(escape(realWindow.atob(base64Url)))): ',
|
|
16
|
+
e,
|
|
17
17
|
)
|
|
18
18
|
try {
|
|
19
19
|
return JSON.parse(realWindow.atob(base64Url)) as R
|
|
20
20
|
} catch (e) {
|
|
21
21
|
console.error(
|
|
22
|
-
'Error parsing jwt second step JSON.parse(realWindow.atob(base64Url)): ',
|
|
23
|
-
e,
|
|
24
22
|
'tried to decode base64Url: ',
|
|
25
23
|
base64Url,
|
|
24
|
+
'Error parsing jwt second step JSON.parse(realWindow.atob(base64Url)): ',
|
|
25
|
+
e,
|
|
26
26
|
)
|
|
27
27
|
return
|
|
28
28
|
}
|
|
@@ -34,3 +34,48 @@ function escape(str: string) {
|
|
|
34
34
|
return '%' + c.charCodeAt(0).toString(16)
|
|
35
35
|
})
|
|
36
36
|
}
|
|
37
|
+
|
|
38
|
+
export function splitAndValidateBase64(base64String: string) {
|
|
39
|
+
// Define the allowed Base64 character set including padding '='
|
|
40
|
+
const base64Charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
|
|
41
|
+
|
|
42
|
+
// Function to validate if a chunk is a valid Base64 segment
|
|
43
|
+
const isValidBase64Chunk = (chunk: string) => {
|
|
44
|
+
if (chunk.length !== 4) {
|
|
45
|
+
console.error(`chunk.length !== 4', chunk:${chunk}`)
|
|
46
|
+
return false // Chunks must be 4 characters long
|
|
47
|
+
}
|
|
48
|
+
// Check each character in the chunk
|
|
49
|
+
for (let char of chunk) {
|
|
50
|
+
if (!base64Charset.includes(char)) {
|
|
51
|
+
console.error(`chunk: ${chunk} !base64Charset.includes(char), char:${char}`)
|
|
52
|
+
return false
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Validate padding (if present)
|
|
57
|
+
const paddingCount = (chunk.match(/=/g) || []).length
|
|
58
|
+
if (paddingCount > 2) {
|
|
59
|
+
console.error(`chunk: ${chunk}, paddingCount > 2, paddingCount:${paddingCount}`)
|
|
60
|
+
return false
|
|
61
|
+
} // There can be at most 2 padding characters
|
|
62
|
+
|
|
63
|
+
// Padding should be at the end of the chunk
|
|
64
|
+
if (paddingCount > 0 && !chunk.endsWith('=')) {
|
|
65
|
+
console.error(`chunk: ${chunk},paddingCount:${paddingCount}, paddingCount > 0 && !chunk.endsWith('=')`)
|
|
66
|
+
return false
|
|
67
|
+
}
|
|
68
|
+
return true
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Split the string into 4-character chunks
|
|
72
|
+
const chunks = base64String.match(/.{1,4}/g) || []
|
|
73
|
+
|
|
74
|
+
// Filter out invalid chunks
|
|
75
|
+
const validChunks = chunks.filter(isValidBase64Chunk)
|
|
76
|
+
|
|
77
|
+
// Join the valid chunks back into a single string
|
|
78
|
+
const validBase64String = validChunks.join('')
|
|
79
|
+
|
|
80
|
+
return validBase64String
|
|
81
|
+
}
|