docs(guides/integrate/service-users/private-key-jwt): (#7677)

* docs(guides/integrate/service-users/private-key-jwt): adjust incomplete, outdated and incorrect parts of the python example.

* wrong variable name for kid in the header

---------

Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
This commit is contained in:
Alex Rimlin 2024-04-03 18:51:13 +03:00 committed by GitHub
parent 25ef3da9d5
commit fa9635eb93
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -118,21 +118,27 @@ import datetime
# Replace with your service user ID and private key # Replace with your service user ID and private key
service_user_id = "your_service_user_id" service_user_id = "your_service_user_id"
private_key = "-----BEGIN PRIVATE KEY-----\nYOUR_PRIVATE_KEY\n-----END PRIVATE KEY-----" private_key = "-----BEGIN PRIVATE KEY-----\nYOUR_PRIVATE_KEY\n-----END PRIVATE KEY-----"
key_id = "your_key_id"
# ZITADEL API URL (replace if needed) # ZITADEL API URL (replace if needed)
api_url = "your_custom_domain" api_url = "your_custom_domain"
# Generate JWT claims # Generate JWT claims
payload = { payload = {
"iss": "your_zitadel_instance_id", "iss": service_user_id,
"sub": service_user_id, "sub": service_user_id,
"aud": api_url, "aud": api_url,
"exp": datetime.utcnow() + datetime.timedelta(minutes=5), "exp": datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(minutes=5),
"iat": datetime.utcnow() "iat": datetime.datetime.now(datetime.timezone.utc)
}
header = {
"alg": "RS256",
"kid": key_id
} }
# Sign the JWT using RS256 algorithm # Sign the JWT using RS256 algorithm
encoded_jwt = jwt.encode(payload, private_key, algorithm="RS256") encoded_jwt = jwt.encode(payload, private_key, algorithm="RS256", headers=header)
print(f"Generated JWT: {encoded_jwt}") print(f"Generated JWT: {encoded_jwt}")
``` ```