Skip to content

Commit 6f3edc4

Browse files
authored
JWT Decode Script (#354)
Create JwtDecode.js Signed-off-by: 0mgfriday <[email protected]>
1 parent 64575a8 commit 6f3edc4

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66
## [Unreleased]
77
### Added
88
- targeted/SQLMapCommandGenerator.js - it will generate and copy sqlmap command based on the request
9+
- encode-decode/JwtDecode.js - Decodes JWTs
910

1011
### Changed
1112
- Update minimum ZAP version to 2.12.0:

encode-decode/JwtDecode.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// JWT Decode by 0mgfriday
2+
var Base64 = Java.type("java.util.Base64")
3+
var String = Java.type("java.lang.String")
4+
var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");
5+
6+
/**
7+
* Decode JWT into a text representation
8+
*
9+
* @param {EncodeDecodeScriptHelper} helper - A helper object with various utility methods.
10+
* For more details see https://github.com/zaproxy/zap-extensions/tree/main/addOns/encoder/src/main/java/org/zaproxy/addon/encoder/processors/script/EncodeDecodeScriptHelper.java
11+
* @param {String} value - JWT to decode
12+
* @returns {EncodeDecodeResult} - Decoded JWT (JSON)
13+
*/
14+
function process(helper, value){
15+
var parts = value.split('.')
16+
17+
if (parts.length == 2 || parts.length == 3) {
18+
try {
19+
var result = formatJson(b64decode(parts[0])) + '\n' + formatJson(b64decode(parts[1]))
20+
21+
if (parts.length == 3 && parts[2] != '') {
22+
result += '\n{SIGNATURE}'
23+
}
24+
25+
return helper.newResult(result);
26+
} catch (err) {
27+
return helper.newError("Invalid JWT: Unable to decode");
28+
}
29+
}
30+
31+
return helper.newError("Invalid JWT");
32+
}
33+
34+
function b64decode(s) {
35+
var bytes = Base64.getUrlDecoder().decode(s)
36+
return new String(bytes, StandardCharsets.UTF_8)
37+
}
38+
39+
function formatJson(json) {
40+
return JSON.stringify(JSON.parse(json),null,2)
41+
}

0 commit comments

Comments
 (0)