File tree Expand file tree Collapse file tree 3 files changed +28
-3
lines changed
Expand file tree Collapse file tree 3 files changed +28
-3
lines changed Original file line number Diff line number Diff 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
3737a 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
Original file line number Diff line number Diff line change 1818
1919from __future__ import annotations
2020
21+ import base64
2122from 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
Original file line number Diff line number Diff 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
7486if __name__ == "__main__" : # pragma: no cover
You can’t perform that action at this time.
0 commit comments