python-keycloak icon indicating copy to clipboard operation
python-keycloak copied to clipboard

Encode url path segment on GET requests

Open ebessah opened this issue 3 years ago • 2 comments

The URL path segment for GET calls in this library is not encoded/escaped and I've encounted an issue where creating a client role with a role name like arn:aws:iam::*:role/keycloak,arn:aws:iam::*:saml-provider/keycloak and setting the skip_exists flag to True fails with error: 'location'

keycloak_admin.create_client_role(
    client_id=saml_client_id,
    payload={
        "name": "arn:aws:iam::*:role/keycloak,arn:aws:iam::*:saml-provider/keycloak",
    },
    skip_exists=True,
)

The line in the code which causes the failure is https://github.com/marcospereirampj/python-keycloak/blob/f4eda508304f251df02b08b85c32bf154dcc29b8/src/keycloak/keycloak_admin.py#L1641

This failure should be fixed when the URL path for GET requests is escaped or encoded.

ebessah avatar Sep 22 '22 12:09 ebessah

Same applies to the auth_url method

auth_url = keycloak_openid.auth_url("http://127.0.0.1:8080/oidc_callback", scope="openid", state={"session_id": "1234"})
print(auth_url)
"http://mykeycloak:9080/realms/master/protocol/openid-connect/auth?client_id=myclient&response_type=code&redirect_uri=http://127.0.0.1:8080/oidc_callback&scope=openid&state={'session_id': '1234'}"

params should be encoded with urllib.parse.urlencode (or a similar tool)

from urllib.parse import urlencode

auth_endpoint_url = "http://mykeycloak:9080/realms/master/protocol/openid-connect/auth"
params = dict(
    client_id="myclient", 
    response_type="code", 
    redirect_uri="http://127.0.0.1:8080/oidc_callback", 
    scope="openid", 
    state={'session_id': '1234'}
)
url_params = urlencode(params)
print(f"{auth_endpoint_url}?{url_params}")

"http://mykeycloak:9080/realms/master/protocol/openid-connect/auth?client_id=myclient&response_type=code&redirect_uri=http%3A%2F%2F127.0.0.1%3A8080%2Foidc_callback&scope=openid&state=%7B%27session_id%27%3A+%271234%27%7D"

tonthon avatar Mar 14 '23 13:03 tonthon