Files
google_api_gateway_test/api_server/api_server/local_generate_long_lived_token.py
2024-10-16 08:51:49 -04:00

41 lines
1.1 KiB
Python

import jwt
import subprocess
from pathlib import Path
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
import time
def main():
gateway_address = get_terraform_output("gateway_address")
client_id = get_terraform_output("client_id")
jwt_private_key = get_terraform_output("jwt_private_key")
private_key = serialization.load_pem_private_key(
jwt_private_key.encode("utf-8"), password=None, backend=default_backend()
)
encoded = jwt.encode(
{
"iss": "issuer of the token",
"sub": "Alice",
"aud": client_id,
"iat": int(time.time()),
"exp": int(time.time()) + 30 * 60 * 60 * 24,
},
private_key,
algorithm="RS256",
)
print(encoded)
def get_terraform_output(name: str) -> str:
terraform_folder = Path(__file__).parent / "../../terraform"
result = subprocess.run(
["terraform", f"-chdir={terraform_folder}", "output", "-raw", name],
stdout=subprocess.PIPE,
)
return result.stdout.decode("utf-8")
if __name__ == "__main__":
main()