generated from martinthomson/internet-draft-template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutil.py
66 lines (49 loc) · 1.56 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import json
import subprocess
from textwrap import fill
from typing import Dict
from jwcrypto import jwk, jwt
example = {
"kty": "EC",
"d": "xzUEdsyLosZF0acZGRAjTKImb0lQvAvssDK5XIZELd0",
"use": "sig",
"crv": "P-256",
"x": "I3HWm_0Ds1dPMI-IWmf4mBmH-YaeAVbPVu7vB27CxXo",
"y": "6N_d5Elj9bs1htgV3okJKIdbHEpkgTmAluYKJemzn1M",
"kid": "12",
"alg": "ES256",
}
EXAMPLE_KEY = jwk.JWK(**example)
MAX_LENGTH = 68
def formatToken(input: str, key: jwk.JWK) -> str:
token = jwt.JWT(jwt=input, key=key, expected_type="JWS")
header = printJson(token.header)
claims = printJson(token.claims)
return f"""{header}
.
{claims}"""
def printJson(input: str) -> str:
return json.dumps(json.loads(input), sort_keys=True, indent=2, ensure_ascii=False)
def printObject(input: Dict) -> str:
return printJson(json.dumps(input))
def printLongJsonObject(input: Dict) -> str:
text = printJson(json.dumps(input))
return fill(
text,
width=MAX_LENGTH,
break_on_hyphens=False,
replace_whitespace=False,
subsequent_indent=" ",
)
def printText(input: str) -> str:
return fill(input, width=MAX_LENGTH, break_on_hyphens=False)
# TODO: find a better way to do create CBOR Diagnostics output
# this is still too wide
def printCBORDiagnostics(input: bytes) -> str:
diag = subprocess.check_output(
"cborg hex2diag --width 65 " + input.hex(), shell=True
).decode("utf8")
return diag
def outputFile(file_name: str, input: str):
with open(file_name, "w") as file:
file.write(input)