ID
DG-3049
Type
Guide
Version
1.0
Status
Active
Created date
Updated date
This is the current version.
The following code samples are written in Python and provided as samples only.
Use of the samples are for illustrative purpose only.
create_jwk.py
# create_jwk.py
# !pip3 install python_jwt
import python_jwt as jwt
import jwcrypto.jwk as jwk
import datetime
print("\n Create a JWK with 2048bit RSA keypair\n")
key = jwk.JWK.generate(kty='RSA', size=2048, kid='eee9f17a3b598fd86417a980b591fbe6')
pub_key = key.export_public()
priv_key = key.export_private()
print("\n Public key\n", pub_key)
print("\n Private key\n", priv_key)
create_jwt.py
# create_jwt.py
# !pip3 install python_jwt
import jwcrypto.jwt as jwt
import jwcrypto.jwk as jwk
import datetime
from pathlib import Path
header = {
"alg": "RS256",
"kid": "eee9f17a3b598fd86417a980b591fbe6",
"typ": "JWT"
}
claims = {
"iss": "8b0914e0-09b4-47d7-9fc9-eb3ddaf2f7aa",
"sub": "8b0914e0-09b4-47d7-9fc9-eb3ddaf2f7aa",
"aud": "https://pca-svt.digitalhealth.gov.au/PcaAuthApi/v2/auth/token",
"exp": "1352660008",
"jti": "rand0m-n0n-reusable-jwt-1d-123"
}
def get_signing_key(filename):
with open(filename) as priv_file:
key = jwk.JWK.from_json(priv_file.read())
return key
print("\n# Encoded JWT with RS256 Signature\n")
file_path = (Path(__file__).parent / "../json/my-private-key.json").resolve()
rsa_signing_jwk = get_signing_key(file_path)
Token = jwt.JWT(header, claims)
Token.make_signed_token(rsa_signing_jwk)
print(Token.serialize())
Back: API reference | Home