-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add login authentication and rate limiting features #24
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
base: master
Are you sure you want to change the base?
Changes from all commits
feb8780
8d47ee7
62ae56b
5e184d0
251c68d
3f30780
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 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -56,6 +56,9 @@ def jwt_algorithm(self) -> str: | |||||||||||||
| class Authentication: | ||||||||||||||
| """Authentication configuration for a RestEndpoint.""" | ||||||||||||||
|
|
||||||||||||||
| # Standard JWT reserved claims that cannot be used as extra claims | ||||||||||||||
| RESERVED_CLAIMS = {"exp", "iat", "nbf", "iss", "sub", "aud", "jti"} | ||||||||||||||
|
Comment on lines
+59
to
+60
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. Make Ruff is correctly flagging this mutable class attribute ( Suggested fix-from typing import Any
+from typing import Any, ClassVar
@@
- RESERVED_CLAIMS = {"exp", "iat", "nbf", "iss", "sub", "aud", "jti"}
+ RESERVED_CLAIMS: ClassVar[frozenset[str]] = frozenset(
+ {"exp", "iat", "nbf", "iss", "sub", "aud", "jti"}
+ )📝 Committable suggestion
Suggested change
🧰 Tools🪛 Ruff (0.15.5)[warning] 60-60: Mutable default value for class attribute (RUF012) 🤖 Prompt for AI Agents |
||||||||||||||
|
|
||||||||||||||
| def __init__( | ||||||||||||||
| self, | ||||||||||||||
| backend: type | None = None, | ||||||||||||||
|
|
@@ -74,16 +77,16 @@ def __init__( | |||||||||||||
|
|
||||||||||||||
| # Validate jwt_extra_claims - reject reserved claims | ||||||||||||||
| if jwt_extra_claims: | ||||||||||||||
| RESERVED_CLAIMS = {"exp", "iat", "nbf", "iss", "sub", "aud", "jti"} | ||||||||||||||
| reserved_found = [] | ||||||||||||||
| for claim in jwt_extra_claims: | ||||||||||||||
| if claim in RESERVED_CLAIMS: | ||||||||||||||
| if claim in self.RESERVED_CLAIMS: | ||||||||||||||
| reserved_found.append(claim) | ||||||||||||||
|
|
||||||||||||||
| if reserved_found: | ||||||||||||||
| raise ConfigurationError( | ||||||||||||||
| f"JWT extra claims cannot include reserved claims: " | ||||||||||||||
| f"{reserved_found}. Reserved claims are: {sorted(RESERVED_CLAIMS)}" | ||||||||||||||
| f"{reserved_found}. Reserved claims are: " | ||||||||||||||
| f"{sorted(self.RESERVED_CLAIMS)}" | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| self.jwt_extra_claims = jwt_extra_claims | ||||||||||||||
|
|
||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -112,9 +112,16 @@ def _resolve_callable(dotted_path: str) -> Any: | |
|
|
||
| try: | ||
| sig = inspect.signature(fn) | ||
| except ValueError: | ||
| # Some callables (e.g., builtins) don't have inspectable signatures | ||
| return fn | ||
| except (ValueError, TypeError) as exc: | ||
| # Only allow specific cases where signature inspection legitimately fails | ||
| if hasattr(fn, "__name__") and fn.__name__ in ("<lambda>",): | ||
| # Lambdas can't be properly inspected in some Python versions | ||
| return fn | ||
| # For other cases, raise a clear error about the validation function | ||
| raise ValueError( | ||
| f"Login validation function {fn!r} cannot be inspected: {exc}. " | ||
| f"Ensure it's a regular Python function with inspectable signature." | ||
| ) from exc | ||
|
Comment on lines
+115
to
+124
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. Reject uninspectable login validators instead of letting them through.
Suggested fix- except (ValueError, TypeError) as exc:
- # Only allow specific cases where signature inspection legitimately fails
- if hasattr(fn, "__name__") and fn.__name__ in ("<lambda>",):
- # Lambdas can't be properly inspected in some Python versions
- return fn
- # For other cases, raise a clear error about the validation function
- raise ValueError(
+ except (ValueError, TypeError) as exc:
+ raise ConfigurationError(
f"Login validation function {fn!r} cannot be inspected: {exc}. "
f"Ensure it's a regular Python function with inspectable signature."
) from exc🧰 Tools🪛 Ruff (0.15.5)[warning] 121-124: Avoid specifying long messages outside the exception class (TRY003) 🤖 Prompt for AI Agents |
||
|
|
||
| # Count required positional parameters | ||
| required_params = 0 | ||
|
|
||
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.
Keep the frozen auth classes untouched.
This PR adds behavior directly to
BaseAuthenticationand rewritesJWTAuthentication, but both class bodies are explicitly frozen in this repo. Please move the new auth flow into helpers/new backends instead of editing these classes in place.As per coding guidelines, "
lightapi/auth.py: Do not modify lightapi/auth.py: JWTAuthentication and BaseAuthentication class bodies are FROZEN`."Also applies to: 46-131
🧰 Tools
🪛 Ruff (0.15.5)
[warning] 33-33: Unused method argument:
request(ARG002)
🤖 Prompt for AI Agents