Skip to content

Commit a892b5f

Browse files
authored
Add -d/--decode flag to decode the JWT (#162)
* Add -d/--decode flag to decode the JWT * Update README.md * Fix inconsistency between Python versions * Just print strings, and no signature
1 parent 9e7dffd commit a892b5f

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Top-level:
3232

3333
<!-- @begin-id-help@ -->
3434
```
35-
usage: id [-h] [-V] [-v] audience
35+
usage: id [-h] [-V] [-v] [-d] audience
3636
3737
a tool for generating OIDC identities
3838
@@ -44,6 +44,7 @@ optional arguments:
4444
-V, --version show program's version number and exit
4545
-v, --verbose run with additional debug logging; supply multiple times to
4646
increase verbosity (default: 0)
47+
-d, --decode decode the OIDC token into JSON (default: False)
4748
```
4849
<!-- @end-id-help@ -->
4950

id/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from __future__ import annotations
2020

21+
import base64
2122
from typing import Callable
2223

2324
__version__ = "1.2.1"
@@ -77,3 +78,14 @@ def detect_credential(audience: str) -> str | None:
7778
if credential is not None:
7879
return credential
7980
return None
81+
82+
83+
def decode_oidc_token(token: str) -> tuple[str, str, str]:
84+
# Split the token into its three parts: header, payload, and signature
85+
header, payload, signature = token.split(".")
86+
87+
# Decode base64-encoded header and payload
88+
decoded_header = base64.urlsafe_b64decode(header + "==").decode("utf-8")
89+
decoded_payload = base64.urlsafe_b64decode(payload + "==").decode("utf-8")
90+
91+
return decoded_header, decoded_payload, signature

id/__main__.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ def _parser() -> argparse.ArgumentParser:
4444
default=0,
4545
help="run with additional debug logging; supply multiple times to increase verbosity",
4646
)
47+
parser.add_argument(
48+
"-d",
49+
"--decode",
50+
action="store_true",
51+
help="decode the OIDC token into JSON",
52+
)
4753
parser.add_argument(
4854
"audience",
4955
type=str,
@@ -66,9 +72,15 @@ def main() -> None:
6672

6773
logger.debug(f"parsed arguments {args}")
6874

69-
from . import detect_credential
75+
from . import decode_oidc_token, detect_credential
7076

71-
print(detect_credential(args.audience))
77+
token = detect_credential(args.audience)
78+
if token and args.decode:
79+
header, payload, signature = decode_oidc_token(token)
80+
print(header)
81+
print(payload)
82+
else:
83+
print(token)
7284

7385

7486
if __name__ == "__main__": # pragma: no cover

0 commit comments

Comments
 (0)