Skip to content

Commit

Permalink
Merge pull request #15 from LouLeGrain/str-value-for-dates
Browse files Browse the repository at this point in the history
Human readable unix epoch in JWT decoded payload
  • Loading branch information
yokawasa authored Apr 30, 2024
2 parents e4d4eff + 38f012c commit 1c227a0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ All notable changes to the "jwt-debugger" extension will be documented in this f

## 0.2.0

- Add a new keybind to execute the command `JWT Debugger Decocde`. Now you can execute the command either in `Command Palette` or with keybind `Ctrl+Shift+d` (Mac: `Cmd+Shift+d`)
- Add a new keybind to execute the command `JWT Debugger Decode`. Now you can execute the command either in `Command Palette` or with keybind `Ctrl+Shift+d` (Mac: `Cmd+Shift+d`)

## 0.1.0

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ or find at [Marketplace](https://marketplace.visualstudio.com/)
## Command & Shortcut
| Command | Shortcut | Description |
| --- | --- | --- |
| `JWT Debugger Decocde` | `Ctrl+Shift+d` <br> (Mac: `Cmd+Shift+d`) | Decode selected JWT Token text |
| `JWT Debugger Decode` | `Ctrl+Shift+d` <br> (Mac: `Cmd+Shift+d`) | Decode selected JWT Token text |

> NOTE: The command `JWT Debugger Decocde` and its shortcut key are NOT available when there is no selected text.
> NOTE: The command `JWT Debugger Decode` and its shortcut key are NOT available when there is no selected text.
## Change Log
See [Change Log](CHANGELOG.md)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"commands": [
{
"command": "extension.jwtdebugger.decode",
"title": "JWT Debugger Decocde"
"title": "JWT Debugger Decode"
}
],
"keybindings": [
Expand Down
30 changes: 28 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,20 @@ export function activate(context: vscode.ExtensionContext) {

try {
// jwtDocode return the result of JSON.parse() ( JSON.parse() return any )
const decodedHeader = jwtDecode(encoded_text, { header: true });
const decodedPayload = jwtDecode(encoded_text);
const decodedHeader: any = jwtDecode(encoded_text, { header: true });
const decodedPayload: any = objectMap(
(jwtDecode(encoded_text) as any),
(value: any, key) => {
switch (key) {
case 'nbf':
case 'exp':
case 'iat':
return `${value} (${new Date(value * 1000).toString()})`;
break;
}
return value
});

const panel = vscode.window.createWebviewPanel(
'previewJWTDecoded',
'Preview JWT Decoded Result',
Expand All @@ -50,6 +62,20 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(disposable);
}

/**
* Applies a function to every element of an object, and returns the object
* @param obj Object to iterate on properties
* @param fn Function to apply on each property
* @returns A typed object
*/
export function objectMap<V, R>(obj: { [key: string]: V }, fn: (value: V, key: string, index: number) => R) {
return Object.fromEntries(
Object.entries(obj).map(
([k, v], i) => [k, fn(v, k, i)]
)
);
}

export function deactivate() {}

/**
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"target": "es6",
"outDir": "out",
"lib": [
"es6"
"es2019"
],
"sourceMap": true,
"rootDir": "src",
Expand Down

0 comments on commit 1c227a0

Please sign in to comment.