Skip to content

Commit 2d8e38c

Browse files
Syed Raza AbbasSyed Raza Abbas
authored andcommitted
yaml made optional and register,login code refactored
1 parent 60db770 commit 2d8e38c

File tree

2 files changed

+118
-40
lines changed

2 files changed

+118
-40
lines changed

kinde_sdk/auth/oauth.py

Lines changed: 117 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def __init__(
3333
token_url: Optional[str] = None,
3434
logout_url: Optional[str] = None,
3535
userinfo_url: Optional[str] = None,
36-
config_file: str = "config.yaml", # Path to the configuration file
36+
config_file: Optional[str] = None, # config_file optional
37+
storage_config: Optional[Dict[str, Any]] = None, # Add storage_config parameter
3738
audience: Optional[str] = None,
3839
host: Optional[str] = None,
3940
state: Optional[str] = None,
@@ -68,8 +69,11 @@ def __init__(
6869
raise KindeConfigurationException("Userinfo URL is required.")
6970

7071
# Load configuration and create the appropriate storage backend
71-
config = load_config(config_file)
72-
storage_config = config.get("storage", {"type": "memory"}) # Default to "memory"
72+
if config_file:
73+
config = load_config(config_file)
74+
storage_config = config.get("storage", {"type": "memory"}) # Default to "memory"
75+
elif storage_config is None:
76+
storage_config = {"type": "memory"} # Default to in-memory storage if no config is provided
7377
storage = StorageFactory.create_storage(storage_config)
7478

7579
self.session_manager = UserSession(storage=storage)
@@ -83,7 +87,7 @@ def __init__(
8387
self.proxy = None
8488
self.proxy_headers = None
8589

86-
def authenticate_user(self, user_id: str, auth_code: str) -> None:
90+
def code_exchange(self, user_id: str, auth_code: str) -> None:
8791
"""Exchange authorization code for tokens and store in session."""
8892
data = {
8993
"grant_type": "authorization_code",
@@ -231,16 +235,88 @@ def get_tokens_for_core(self, user_id: str) -> Optional[Dict[str, str]]:
231235

232236
return tokens
233237

234-
def login(self, params: Optional[Dict[str, Any]] = None) -> str:
238+
# def login(self, params: Optional[Dict[str, Any]] = None) -> str:
239+
# """
240+
# Generate the login URL for user authentication.
241+
242+
# Args:
243+
# params (Optional[Dict[str, Any]]): A dictionary of query parameters to include in the login URL.
244+
# Supported keys: org_code, org_name, is_create_org, auth_url_params.
245+
246+
# Returns:
247+
# str: The login URL.
248+
# """
249+
# # Default parameters
250+
# default_params = {
251+
# "client_id": self.client_id,
252+
# "response_type": "code",
253+
# "redirect_uri": self.redirect_uri,
254+
# "scope": "openid profile email", # Default scope
255+
# }
256+
257+
# # Merge default parameters with user-provided parameters
258+
# if params:
259+
# # Handle organization-specific parameters
260+
# if "org_code" in params:
261+
# default_params["org_code"] = params["org_code"]
262+
# if "org_name" in params:
263+
# default_params["org_name"] = params["org_name"]
264+
# if "is_create_org" in params:
265+
# default_params["is_create_org"] = "true" if params["is_create_org"] else "false"
266+
267+
# # Handle additional auth URL parameters
268+
# if "auth_url_params" in params and isinstance(params["auth_url_params"], dict):
269+
# default_params.update(params["auth_url_params"])
270+
271+
# return f"{self.auth_url}?{urlencode(default_params)}"
272+
273+
# def register(self, params: Optional[Dict[str, Any]] = None) -> str:
274+
# """
275+
# Generate the registration URL for user sign-up.
276+
277+
# Args:
278+
# params (Optional[Dict[str, Any]]): A dictionary of query parameters to include in the registration URL.
279+
# Supported keys: org_code, org_name, is_create_org, auth_url_params.
280+
281+
# Returns:
282+
# str: The registration URL.
283+
# """
284+
# # Default parameters
285+
# default_params = {
286+
# "client_id": self.client_id,
287+
# "response_type": "code",
288+
# "redirect_uri": self.redirect_uri,
289+
# "scope": "openid profile email", # Default scope
290+
# }
291+
292+
# # Merge default parameters with user-provided parameters
293+
# if params:
294+
# # Handle organization-specific parameters
295+
# if "org_code" in params:
296+
# default_params["org_code"] = params["org_code"]
297+
# if "org_name" in params:
298+
# default_params["org_name"] = params["org_name"]
299+
# if "is_create_org" in params:
300+
# default_params["is_create_org"] = "true" if params["is_create_org"] else "false"
301+
302+
# # Handle additional auth URL parameters
303+
# if "auth_url_params" in params and isinstance(params["auth_url_params"], dict):
304+
# default_params.update(params["auth_url_params"])
305+
306+
# return f"{self.auth_url}/register?{urlencode(default_params)}"
307+
308+
309+
def _get_auth_url(self, prompt: Optional[str] = None, params: Optional[Dict[str, Any]] = None) -> str:
235310
"""
236-
Generate the login URL for user authentication.
311+
Helper method to generate the authentication URL for login or registration.
237312
238313
Args:
239-
params (Optional[Dict[str, Any]]): A dictionary of query parameters to include in the login URL.
240-
Supported keys: org_code, org_name, is_create_org, auth_url_params.
314+
prompt (Optional[str]): Set to "create" for registration.
315+
params (Optional[Dict[str, Any]]): A dictionary of query parameters to include in the URL.
316+
Supported keys: org_code, org_name, is_create_org, auth_url_params.
241317
242318
Returns:
243-
str: The login URL.
319+
str: The authentication URL.
244320
"""
245321
# Default parameters
246322
default_params = {
@@ -250,6 +326,10 @@ def login(self, params: Optional[Dict[str, Any]] = None) -> str:
250326
"scope": "openid profile email", # Default scope
251327
}
252328

329+
# Add prompt if provided (used for registration)
330+
if prompt:
331+
default_params["prompt"] = prompt
332+
253333
# Merge default parameters with user-provided parameters
254334
if params:
255335
# Handle organization-specific parameters
@@ -266,37 +346,41 @@ def login(self, params: Optional[Dict[str, Any]] = None) -> str:
266346

267347
return f"{self.auth_url}?{urlencode(default_params)}"
268348

349+
def login(self, params: Optional[Dict[str, Any]] = None) -> str:
350+
"""
351+
Generate the login URL for user authentication.
352+
353+
Args:
354+
params (Optional[Dict[str, Any]]): A dictionary of query parameters to include in the login URL.
355+
Supported keys: org_code, org_name, is_create_org, auth_url_params.
356+
357+
Returns:
358+
str: The login URL.
359+
"""
360+
return self._get_auth_url(params=params)
361+
269362
def register(self, params: Optional[Dict[str, Any]] = None) -> str:
270363
"""
271364
Generate the registration URL for user sign-up.
272365
273366
Args:
274367
params (Optional[Dict[str, Any]]): A dictionary of query parameters to include in the registration URL.
275-
Supported keys: org_code, org_name, is_create_org, auth_url_params.
368+
Supported keys: org_code, org_name, is_create_org, auth_url_params.
276369
277370
Returns:
278371
str: The registration URL.
279372
"""
280-
# Default parameters
281-
default_params = {
282-
"client_id": self.client_id,
283-
"response_type": "code",
284-
"redirect_uri": self.redirect_uri,
285-
"scope": "openid profile email", # Default scope
286-
}
287-
288-
# Merge default parameters with user-provided parameters
289-
if params:
290-
# Handle organization-specific parameters
291-
if "org_code" in params:
292-
default_params["org_code"] = params["org_code"]
293-
if "org_name" in params:
294-
default_params["org_name"] = params["org_name"]
295-
if "is_create_org" in params:
296-
default_params["is_create_org"] = "true" if params["is_create_org"] else "false"
297-
298-
# Handle additional auth URL parameters
299-
if "auth_url_params" in params and isinstance(params["auth_url_params"], dict):
300-
default_params.update(params["auth_url_params"])
301-
302-
return f"{self.auth_url}/register?{urlencode(default_params)}"
373+
return self._get_auth_url(prompt="create", params=params)
374+
375+
376+
"""
377+
Example usage for login and register.
378+
# Login URL
379+
login_url = oauth_client.login(params={"org_code": "12345"})
380+
print(login_url) # Output: https://auth.kinde.com?client_id=...&response_type=code&...
381+
382+
# Registration URL
383+
register_url = oauth_client.register(params={"org_code": "12345"})
384+
print(register_url) # Output: https://auth.kinde.com?client_id=...&response_type=code&...&prompt=create
385+
386+
"""

kinde_sdk/auth/storage_factory.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ def create_storage(config: Dict[str, Any]) -> "StorageInterface":
2626
return MemoryStorage()
2727
elif storage_type == "local_storage":
2828
return LocalStorage()
29-
# elif storage_type == "postgres":
30-
# return PostgresStorage(**options)
31-
# Add more storage types here
32-
# elif storage_type == "postgres":
33-
# return PostgresStorage(**options)
34-
# elif storage_type == "dynamo":
35-
# return DynamoStorage(**options)
29+
# Add more storage types here as needed
3630
else:
3731
raise ValueError(f"Unsupported storage type: {storage_type}")

0 commit comments

Comments
 (0)