Skip to content

Commit 0c52ec4

Browse files
gregriffGreg Griffin
and
Greg Griffin
authored
fix: Add optional Nonce parameter to the authorization URL requests (#606)
* feat: add optional nonce parameter to the authorization URL requests * fix: shorten docstring to be below max line length --------- Co-authored-by: Greg Griffin <greg@lapetussolutions.com>
1 parent 7cfad72 commit 0c52ec4

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/keycloak/keycloak_openid.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def well_known(self):
257257
data_raw = self.connection.raw_get(URL_WELL_KNOWN.format(**params_path))
258258
return raise_error_from_response(data_raw, KeycloakGetError)
259259

260-
def auth_url(self, redirect_uri, scope="email", state=""):
260+
def auth_url(self, redirect_uri, scope="email", state="", nonce=""):
261261
"""Get authorization URL endpoint.
262262
263263
:param redirect_uri: Redirect url to receive oauth code
@@ -266,6 +266,8 @@ def auth_url(self, redirect_uri, scope="email", state=""):
266266
:type scope: str
267267
:param state: State will be returned to the redirect_uri
268268
:type state: str
269+
:param nonce: Associates a Client session with an ID Token to mitigate replay attacks
270+
:type nonce: str
269271
:returns: Authorization URL Full Build
270272
:rtype: str
271273
"""
@@ -275,6 +277,7 @@ def auth_url(self, redirect_uri, scope="email", state=""):
275277
"redirect-uri": redirect_uri,
276278
"scope": scope,
277279
"state": state,
280+
"nonce": nonce,
278281
}
279282
return URL_AUTH.format(**params_path)
280283

@@ -903,7 +906,7 @@ async def a_well_known(self):
903906
data_raw = await self.connection.a_raw_get(URL_WELL_KNOWN.format(**params_path))
904907
return raise_error_from_response(data_raw, KeycloakGetError)
905908

906-
async def a_auth_url(self, redirect_uri, scope="email", state=""):
909+
async def a_auth_url(self, redirect_uri, scope="email", state="", nonce=""):
907910
"""Get authorization URL endpoint asynchronously.
908911
909912
:param redirect_uri: Redirect url to receive oauth code
@@ -912,6 +915,8 @@ async def a_auth_url(self, redirect_uri, scope="email", state=""):
912915
:type scope: str
913916
:param state: State will be returned to the redirect_uri
914917
:type state: str
918+
:param nonce: Associates a Client session with an ID Token to mitigate replay attacks
919+
:type nonce: str
915920
:returns: Authorization URL Full Build
916921
:rtype: str
917922
"""
@@ -921,6 +926,7 @@ async def a_auth_url(self, redirect_uri, scope="email", state=""):
921926
"redirect-uri": redirect_uri,
922927
"scope": scope,
923928
"state": state,
929+
"nonce": nonce,
924930
}
925931
return URL_AUTH.format(**params_path)
926932

src/keycloak/urls_patterns.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
URL_ENTITLEMENT = "realms/{realm-name}/authz/entitlement/{resource-server-id}"
3636
URL_AUTH = (
3737
"{authorization-endpoint}?client_id={client-id}&response_type=code&redirect_uri={redirect-uri}"
38-
"&scope={scope}&state={state}"
38+
"&scope={scope}&state={state}&nonce={nonce}"
3939
)
4040
URL_DEVICE = "realms/{realm-name}/protocol/openid-connect/auth/device"
4141

tests/test_keycloak_openid.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def test_auth_url(env, oid: KeycloakOpenID):
121121
res
122122
== f"http://{env.KEYCLOAK_HOST}:{env.KEYCLOAK_PORT}/realms/{oid.realm_name}"
123123
+ f"/protocol/openid-connect/auth?client_id={oid.client_id}&response_type=code"
124-
+ "&redirect_uri=http://test.test/*&scope=email&state="
124+
+ "&redirect_uri=http://test.test/*&scope=email&state=&nonce="
125125
)
126126

127127

@@ -575,7 +575,7 @@ async def test_a_auth_url(env, oid: KeycloakOpenID):
575575
res
576576
== f"http://{env.KEYCLOAK_HOST}:{env.KEYCLOAK_PORT}/realms/{oid.realm_name}"
577577
+ f"/protocol/openid-connect/auth?client_id={oid.client_id}&response_type=code"
578-
+ "&redirect_uri=http://test.test/*&scope=email&state="
578+
+ "&redirect_uri=http://test.test/*&scope=email&state=&nonce="
579579
)
580580

581581

0 commit comments

Comments
 (0)