Skip to content

Commit

Permalink
Add slash validation on Settings URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
diogosilva30 committed Oct 29, 2022
1 parent 871a08e commit 87db494
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/django_keycloak/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,39 @@ class Settings:
# Derived setting of the SERVER/INTERNAL_URL and BASE_PATH
KEYCLOAK_URL: str = field(init=False)

def __force_starting_and_ending_slash(self, string: str) -> str:
"""
Forces a given string to start and end with a slash "/"
Parameters
----------
string: str
A string to force the starting and ending slash.
Returns
-------
str
The transformed string starting and ending with a slash.
"""
if not string.endswith("/"):
string += "/"
if not string.startswith("/"):
string = "/" + string
return string

def __post_init__(self) -> None:
# Decide URL (internal url overrides serverl url)

# Make sure "BASE_PATH" starts and ends with a slash
self.BASE_PATH = self.__force_starting_and_ending_slash(self.BASE_PATH)
# Make sure both "SERVER_URL" and "INTERNAL_URL" don't contain any
# trailing slash
self.SERVER_URL = self.SERVER_URL.rstrip("/")
self.INTERNAL_URL = self.INTERNAL_URL.rstrip("/")

# Decide URL (internal url overrides server url)
URL = self.INTERNAL_URL if self.INTERNAL_URL else self.SERVER_URL
self.KEYCLOAK_URL = f"{URL}/{self.BASE_PATH}/"

self.KEYCLOAK_URL = f"{URL}{self.BASE_PATH}"


# Get keycloak configs from django
Expand Down

0 comments on commit 87db494

Please sign in to comment.