Skip to content

Commit a97ca4e

Browse files
committed
fix(keycloak): add automatic retry for token retrieval on 5xx
1 parent 91e8c19 commit a97ca4e

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "cardo-python-utils"
7-
version = "0.5.7"
7+
version = "0.5.8"
88
description = "Python library enhanced with a wide range of functions for different scenarios."
99
readme = "README.rst"
1010
requires-python = ">=3.8"

python_utils/django/oidc_settings.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import json
88
import os
9+
import time
910
import requests
1011

1112
from django.conf import settings
@@ -125,10 +126,17 @@ def get_oidc_confidential_client_token(**kwargs) -> dict:
125126
data["client_assertion_type"] = KEYCLOAK_CLIENT_ASSERTION_TYPE
126127
data["client_assertion"] = get_confidential_client_service_account_token()
127128

128-
response = requests.post(
129-
get_oidc_op_token_endpoint(),
130-
data=data,
131-
)
129+
max_retries = 3
130+
for attempt in range(max_retries + 1):
131+
response = requests.post(
132+
get_oidc_op_token_endpoint(),
133+
data=data,
134+
)
135+
# Retry on server errors (5xx), it might be a temporary issue with Keycloak.
136+
if response.status_code < 500 or attempt == max_retries:
137+
break
138+
time.sleep(2**attempt) # 1, 2, 4 seconds
139+
132140
response.raise_for_status()
133141

134142
return response.json()

0 commit comments

Comments
 (0)