-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
11 adapt to reference implementation #12
base: main
Are you sure you want to change the base?
Changes from 10 commits
a5e0ac1
541d81b
15d5b4a
e9709c0
4472ab8
c8b0c68
8f8d625
0e3e17c
338ede3
96372a4
112dc08
58000a8
826c295
21caa9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.1.3 | ||
2.0.0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from pydantic import BaseModel, ConfigDict, StringConstraints, field_validator | ||
from typing_extensions import Annotated, Optional | ||
|
||
from corbado_python_sdk.utils import validators | ||
from corbado_python_sdk.utils import DEFAULT_SESSION_TOKEN_COOKIE_NAME, validators | ||
|
||
|
||
class Config(BaseModel): | ||
|
@@ -16,8 +16,9 @@ class Config(BaseModel): | |
Attributes: | ||
project_id (str): The unique identifier for the project. | ||
api_secret (str): The secret key used to authenticate API requests. | ||
backend_api (str): The base URL for the backend API. Defaults to "https://backendapi.cloud.corbado.io/v2". | ||
short_session_cookie_name (str): The name of the cookie for short session management. Defaults to "cbo_short_session". | ||
backend_api (str): The base URL for the backend API. | ||
frontend_api (str): The base URL for the frontend API. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. frontend api before backend api There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
session_token_cookie_name (str): The name of the cookie for short session management. Defaults to "cbo_session_token". | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can remove this, we should not need that anymore There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||
""" | ||
|
||
# Make sure that field assignments are also validated, use "set_assignment_validation(False)" | ||
|
@@ -28,36 +29,49 @@ class Config(BaseModel): | |
project_id: str | ||
api_secret: str | ||
|
||
backend_api: str = "https://backendapi.cloud.corbado.io/v2" | ||
short_session_cookie_name: str = "cbo_short_session" | ||
session_token_cookie_name: str = DEFAULT_SESSION_TOKEN_COOKIE_NAME | ||
cname: Optional[Annotated[str, StringConstraints(strip_whitespace=True, min_length=1)]] = None | ||
|
||
_issuer: Optional[Annotated[str, StringConstraints(strip_whitespace=True, min_length=1)]] = None | ||
_frontend_api: Optional[str] = None | ||
frontend_api: str | ||
backend_api: str | ||
|
||
@field_validator("backend_api") | ||
@field_validator( | ||
"backend_api", | ||
) | ||
@classmethod | ||
def validate_backend_api(cls, backend_api: str) -> str: | ||
"""Validate the backend API URL and ensure it ends with '/v2'. | ||
|
||
Args: | ||
backend_api (str): Backend API URL to validate. | ||
|
||
Raises: | ||
ValueError: _description_ | ||
|
||
Returns: | ||
str: Validated backend API URL ending with '/v2'. | ||
""" | ||
if not validators.url_validator(backend_api): | ||
raise ValueError(f'Invalid URL "{backend_api}" provided for backend API.') | ||
backend_api = validators.url_validator(url=backend_api) | ||
|
||
# Append '/v2' if not already present | ||
if not backend_api.endswith("/v2"): | ||
return backend_api.rstrip("/") + "/v2" | ||
|
||
return backend_api | ||
|
||
@field_validator( | ||
"frontend_api", | ||
) | ||
@classmethod | ||
def validate_frontend_api(cls, frontend_api: str) -> str: | ||
"""Validate the frontend API URL. | ||
|
||
Args: | ||
frontend_api (str): Frontend API URL to validate. | ||
|
||
Returns: | ||
str: Validated frontend API. | ||
""" | ||
return validators.url_validator(url=frontend_api) | ||
|
||
@field_validator("project_id") | ||
@classmethod | ||
def project_id_validator(cls, project_id: str) -> str: | ||
|
@@ -121,32 +135,3 @@ def issuer(self, issuer: str) -> None: | |
issuer (str): issuer to set. | ||
""" | ||
self._issuer = issuer | ||
|
||
@property | ||
def frontend_api(self) -> str: | ||
"""Get Frontend API. | ||
|
||
Returns: | ||
str: Frontend API | ||
""" | ||
if not self._frontend_api: | ||
self._frontend_api = "https://" + self.project_id + ".frontendapi.corbado.io" | ||
return self._frontend_api | ||
|
||
@frontend_api.setter | ||
def frontend_api(self, frontend_api: str) -> None: | ||
"""Set Frontend API. Use it to override default value. | ||
|
||
Args: | ||
frontend_api (str): Frontend API to set. | ||
""" | ||
self._frontend_api = validators.url_validator(url=frontend_api) # validate url | ||
|
||
# ------- Internal --------------# | ||
def set_assignment_validation(self, validate: bool) -> None: | ||
"""Only use it if you know what you do. Sets assignment validation. | ||
|
||
Args: | ||
validate (bool): Enable/disable validation | ||
""" | ||
self.model_config["validate_assignment"] = validate |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,9 +70,10 @@ def sessions(self) -> SessionService: | |
""" | ||
if not self._sessions: | ||
self._sessions = SessionService( | ||
short_session_cookie_name=self.config.short_session_cookie_name, | ||
session_token_cookie_name=self.config.session_token_cookie_name, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||
issuer=self.config.issuer, | ||
jwks_uri=self.config.frontend_api + "/.well-known/jwks", | ||
project_id=self.config.project_id, | ||
) | ||
|
||
return self._sessions | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .session_validation_result import SessionValidationResult | ||
from corbado_python_sdk.generated import UserStatus | ||
|
||
from .user_entity import UserEntity as UserEntity | ||
|
||
__all__ = ["UserEntity", "SessionValidationResult"] | ||
__all__ = ["UserEntity", "UserStatus"] |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .server_exception import ServerException as ServerException | ||
from .standard_exception import StandardException as StandardException | ||
from .token_validation_exception import TokenValidationException, ValidationErrorType | ||
|
||
__all__ = ["ServerException", "StandardException"] | ||
__all__ = ["ServerException", "StandardException", "TokenValidationException", "ValidationErrorType"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from enum import Enum | ||
|
||
from typing_extensions import Optional | ||
|
||
|
||
class ValidationErrorType(Enum): | ||
""" | ||
Enum representing types of validation errors. | ||
|
||
This enum categorizes various validation errors that may occur during | ||
token validation processes. | ||
|
||
Attributes: | ||
INVALID_TOKEN (str): Indicates that the token is invalid. More information in 'original_exception'. | ||
SIGNING_KEY_ERROR (str): Indicates that the signing key could not be retrieved. More information in 'original_exception'. | ||
EMPTY_SESSION_TOKEN (str): Indicates that the session token is empty. | ||
EMPTY_ISSUER (str): Indicates that the issuer is empty. | ||
ISSUER_MISSMATCH (str): Indicates that the token issuer does not match the expected issuer. | ||
""" | ||
|
||
INVALID_TOKEN = "Invalid token" # noqa s105 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldnot we have the following codes like in go: CodeJWTGeneral Code = iota There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, but due to different JWT implementation in Java and Python and error handling not all abovementioned error may get caught. |
||
SIGNING_KEY_ERROR = "Could not retrieve signing key" | ||
EMPTY_SESSION_TOKEN = "Session token is empty" # noqa s105 | ||
EMPTY_ISSUER = "Issuer is empty" | ||
ISSUER_MISSMATCH = "Token issuer does not match" | ||
|
||
|
||
class TokenValidationException(Exception): | ||
"""Custom exception class for handling validation errors. | ||
|
||
This exception wraps around other exceptions to provide additional context | ||
regarding validation failures. | ||
|
||
Attributes: | ||
message (str): The custom error message describing the validation error. | ||
error_type (ValidationErrorType): Enum value indicating the type of validation error. | ||
original_exception (Optional[Exception]): The original exception that caused this error, if any. | ||
""" | ||
|
||
def __init__(self, message: str, error_type: ValidationErrorType, original_exception: Optional[Exception] = None): | ||
"""Initialize ValidationError with message, error type, and optional original exception. | ||
|
||
Args: | ||
message (str): A description of the error. | ||
error_type (ValidationErrorType): The specific type of validation error. | ||
original_exception (Optional[Exception], optional): The original exception that caused | ||
this error, if available. Defaults to None. | ||
""" | ||
super().__init__(message) | ||
self.message: str = message | ||
self.error_type: ValidationErrorType = error_type | ||
self.original_exception: Exception | None = original_exception | ||
|
||
def __str__(self) -> str: | ||
"""Return a string representation of the validation error. | ||
|
||
Includes the error type, custom message, and details of the original exception | ||
if it is available. | ||
|
||
Returns: | ||
str: Formatted string containing error type, message, and any original exception details. | ||
""" | ||
base_message: str = f"[{self.error_type.value}] {self.message}" | ||
if self.original_exception: | ||
return f"{base_message} | Caused by: {repr(self.original_exception)}" | ||
return base_message |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please the following order: project id, api secret, frontend api, backend api
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done