lenapi 0.1.4.5__tar.gz → 0.1.4.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/PKG-INFO +15 -13
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/README.md +14 -12
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/__init__.py +1 -1
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/auth.py +2 -12
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/cli.py +0 -2
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi.egg-info/PKG-INFO +15 -13
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/pyproject.toml +1 -1
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/tests/test_security_patch.py +42 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/LICENSE +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/admin.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/application.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/authentication.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/authorization.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/background.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/concurrency.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/database.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/dependencies.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/docs_guard.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/logging.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/middleware.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/migration_render.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/model.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/models.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/mongodb.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/openapi.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/parameters.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/project.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/request.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/response.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/security.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/security_store.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/sentinels.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/service.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/throttling.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi/transaction.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi.egg-info/SOURCES.txt +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi.egg-info/dependency_links.txt +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi.egg-info/entry_points.txt +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi.egg-info/requires.txt +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi.egg-info/top_level.txt +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/lenapi_bootstrap.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/setup.cfg +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/tests/test_admin_console.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/tests/test_cli_settings_recovery.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/tests/test_docs_guard.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/tests/test_framework_features.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/tests/test_media_upload.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/tests/test_migration_render.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/tests/test_mongodb.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/tests/test_platform_services.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/tests/test_project_isolation.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/tests/test_sentinels.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/tests/test_throttle.py +0 -0
- {lenapi-0.1.4.5 → lenapi-0.1.4.6}/tests/test_user_model_extension.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: lenapi
|
|
3
|
-
Version: 0.1.4.
|
|
3
|
+
Version: 0.1.4.6
|
|
4
4
|
Summary: A modern ASGI framework with Pydantic validation, sync and async database access, WebSockets, authentication, and integrated API documentation.
|
|
5
5
|
Author: Maekandex Communication Limitation
|
|
6
6
|
Author-email: "Ifeanyi M. Onovo" <ifeanyi@maekandex.com.ng>
|
|
@@ -1474,7 +1474,6 @@ def oidc_profile(identity=Depends(provider_auth)):
|
|
|
1474
1474
|
# settings.py — replace with the exact origin serving your frontend.
|
|
1475
1475
|
SOCIAL_AUTH_ALLOWED_ORIGINS = ["https://myapp.com"]
|
|
1476
1476
|
SOCIAL_AUTH_SUCCESS_CALLBACK = "https://api.example.com/auth/social/success"
|
|
1477
|
-
FRONTEND_REDIRECT_URI = "https://app.example.com/login/success"
|
|
1478
1477
|
|
|
1479
1478
|
# main.py
|
|
1480
1479
|
from lenapi import LENAPI
|
|
@@ -1500,12 +1499,14 @@ This mounts routes such as `/auth/google/login` and `/auth/google/callback` only
|
|
|
1500
1499
|
|
|
1501
1500
|
The provider's OAuth access token is used internally and is never returned. A successful social login requires `SOCIAL_AUTH_SUCCESS_CALLBACK`; without it the callback returns a generic `503` and no identity data. The browser callback never returns provider identity or a LenAPI token. `jwt_enabled` remains accepted for compatibility but credential issuance belongs to your backend callback. Google and LinkedIn use OIDC identity endpoints in this integration; GitHub uses OAuth 2.0 and its profile/email APIs, not OIDC.
|
|
1502
1501
|
|
|
1503
|
-
To keep provider identity out of the browser response and let your backend own login/session handling, configure `SOCIAL_AUTH_SUCCESS_CALLBACK`. LenAPI validates the provider callback and POSTs the verified identity as JSON to your backend.
|
|
1502
|
+
To keep provider identity out of the browser response and let your backend own login/session handling, configure `SOCIAL_AUTH_SUCCESS_CALLBACK`. LenAPI validates the provider callback and POSTs the verified identity as JSON to your backend. The webhook must return JSON containing `redirect_url`; LenAPI redirects the browser to that URL and forwards any `Set-Cookie` headers from the webhook response. Redirect URLs must use HTTPS; HTTP is accepted only for localhost development. Query parameters are allowed so the webhook can include a short-lived, single-use exchange code; URL fragments are rejected. Do not put a bearer token or provider access token in the redirect URL. When callback mode is enabled, your backend owns JWT/session issuance; `jwt_enabled` does not cause LenAPI to create or return a JWT.
|
|
1504
1503
|
|
|
1505
1504
|
```python
|
|
1506
1505
|
# Developer backend endpoint (the SOCIAL_AUTH_SUCCESS_CALLBACK URL)
|
|
1507
1506
|
from lenapi import JSONResponse, LENAPI, Request
|
|
1508
|
-
import
|
|
1507
|
+
import hashlib
|
|
1508
|
+
import secrets
|
|
1509
|
+
from urllib.parse import urlencode
|
|
1509
1510
|
|
|
1510
1511
|
app = LENAPI()
|
|
1511
1512
|
|
|
@@ -1515,19 +1516,20 @@ async def social_success(request: Request):
|
|
|
1515
1516
|
identity = data["identity"]
|
|
1516
1517
|
user_id = data.get("user_id")
|
|
1517
1518
|
|
|
1518
|
-
#
|
|
1519
|
+
# Apply your own account rules, then store a short-lived, single-use code.
|
|
1519
1520
|
user = await find_or_create_user(identity, user_id)
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
"session_id", session_id, max_age=3600, path="/",
|
|
1525
|
-
httponly=True, secure=True, samesite="lax",
|
|
1521
|
+
code = secrets.token_urlsafe(32)
|
|
1522
|
+
code_hash = hashlib.sha256(code.encode("utf-8")).hexdigest()
|
|
1523
|
+
await app.cache.set(
|
|
1524
|
+
f"social_login:{code_hash}", {"user_id": str(user.id)}, ttl=400
|
|
1526
1525
|
)
|
|
1527
|
-
|
|
1526
|
+
redirect_url = "https://app.example.com/login/success?" + urlencode({"code": code})
|
|
1527
|
+
return JSONResponse({"redirect_url": redirect_url})
|
|
1528
1528
|
```
|
|
1529
1529
|
|
|
1530
|
-
The
|
|
1530
|
+
The frontend should immediately POST the code to a separate backend exchange route. That route hashes the submitted code, retrieves and deletes its cache entry, and only then issues your bearer token or session. Do not place an access token in the redirect URL.
|
|
1531
|
+
|
|
1532
|
+
The webhook body is `{"provider": "google", "identity": {...}, "user_id": "..."}`; `user_id` is omitted when auto-registration is disabled. This is a plain HTTPS server-to-server POST; the receiving application is responsible for protecting its endpoint. The backend's `Set-Cookie` is forwarded to the browser with LenAPI's redirect response, so choose cookie domain and frontend/API domains accordingly. The redirect destination comes from the webhook's `redirect_url`, never a browser-supplied URL. The helper functions in the sample are application code, not LenAPI built-ins.
|
|
1531
1533
|
|
|
1532
1534
|
`allowed_origins` can also be passed directly to `SocialAuth`; otherwise it reads `SOCIAL_AUTH_ALLOWED_ORIGINS` from `settings.py`. When a browser sends an `Origin` header to a provider login route, LenApi requires an exact match in this list and returns `403` otherwise. Replace `https://myapp.com` with the exact origin serving your frontend. Top-level navigations can omit `Origin`; those still rely on the required, random, browser-cookie-bound OAuth `state` check on callback. Do not treat the origin allowlist as a replacement for OAuth state validation, HTTPS, or correct provider callback URL registration. The provider callback itself is not checked against the browser `Origin` header because providers may omit it or send a different origin.
|
|
1533
1535
|
|
|
@@ -1422,7 +1422,6 @@ def oidc_profile(identity=Depends(provider_auth)):
|
|
|
1422
1422
|
# settings.py — replace with the exact origin serving your frontend.
|
|
1423
1423
|
SOCIAL_AUTH_ALLOWED_ORIGINS = ["https://myapp.com"]
|
|
1424
1424
|
SOCIAL_AUTH_SUCCESS_CALLBACK = "https://api.example.com/auth/social/success"
|
|
1425
|
-
FRONTEND_REDIRECT_URI = "https://app.example.com/login/success"
|
|
1426
1425
|
|
|
1427
1426
|
# main.py
|
|
1428
1427
|
from lenapi import LENAPI
|
|
@@ -1448,12 +1447,14 @@ This mounts routes such as `/auth/google/login` and `/auth/google/callback` only
|
|
|
1448
1447
|
|
|
1449
1448
|
The provider's OAuth access token is used internally and is never returned. A successful social login requires `SOCIAL_AUTH_SUCCESS_CALLBACK`; without it the callback returns a generic `503` and no identity data. The browser callback never returns provider identity or a LenAPI token. `jwt_enabled` remains accepted for compatibility but credential issuance belongs to your backend callback. Google and LinkedIn use OIDC identity endpoints in this integration; GitHub uses OAuth 2.0 and its profile/email APIs, not OIDC.
|
|
1450
1449
|
|
|
1451
|
-
To keep provider identity out of the browser response and let your backend own login/session handling, configure `SOCIAL_AUTH_SUCCESS_CALLBACK`. LenAPI validates the provider callback and POSTs the verified identity as JSON to your backend.
|
|
1450
|
+
To keep provider identity out of the browser response and let your backend own login/session handling, configure `SOCIAL_AUTH_SUCCESS_CALLBACK`. LenAPI validates the provider callback and POSTs the verified identity as JSON to your backend. The webhook must return JSON containing `redirect_url`; LenAPI redirects the browser to that URL and forwards any `Set-Cookie` headers from the webhook response. Redirect URLs must use HTTPS; HTTP is accepted only for localhost development. Query parameters are allowed so the webhook can include a short-lived, single-use exchange code; URL fragments are rejected. Do not put a bearer token or provider access token in the redirect URL. When callback mode is enabled, your backend owns JWT/session issuance; `jwt_enabled` does not cause LenAPI to create or return a JWT.
|
|
1452
1451
|
|
|
1453
1452
|
```python
|
|
1454
1453
|
# Developer backend endpoint (the SOCIAL_AUTH_SUCCESS_CALLBACK URL)
|
|
1455
1454
|
from lenapi import JSONResponse, LENAPI, Request
|
|
1456
|
-
import
|
|
1455
|
+
import hashlib
|
|
1456
|
+
import secrets
|
|
1457
|
+
from urllib.parse import urlencode
|
|
1457
1458
|
|
|
1458
1459
|
app = LENAPI()
|
|
1459
1460
|
|
|
@@ -1463,19 +1464,20 @@ async def social_success(request: Request):
|
|
|
1463
1464
|
identity = data["identity"]
|
|
1464
1465
|
user_id = data.get("user_id")
|
|
1465
1466
|
|
|
1466
|
-
#
|
|
1467
|
+
# Apply your own account rules, then store a short-lived, single-use code.
|
|
1467
1468
|
user = await find_or_create_user(identity, user_id)
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
"session_id", session_id, max_age=3600, path="/",
|
|
1473
|
-
httponly=True, secure=True, samesite="lax",
|
|
1469
|
+
code = secrets.token_urlsafe(32)
|
|
1470
|
+
code_hash = hashlib.sha256(code.encode("utf-8")).hexdigest()
|
|
1471
|
+
await app.cache.set(
|
|
1472
|
+
f"social_login:{code_hash}", {"user_id": str(user.id)}, ttl=400
|
|
1474
1473
|
)
|
|
1475
|
-
|
|
1474
|
+
redirect_url = "https://app.example.com/login/success?" + urlencode({"code": code})
|
|
1475
|
+
return JSONResponse({"redirect_url": redirect_url})
|
|
1476
1476
|
```
|
|
1477
1477
|
|
|
1478
|
-
The
|
|
1478
|
+
The frontend should immediately POST the code to a separate backend exchange route. That route hashes the submitted code, retrieves and deletes its cache entry, and only then issues your bearer token or session. Do not place an access token in the redirect URL.
|
|
1479
|
+
|
|
1480
|
+
The webhook body is `{"provider": "google", "identity": {...}, "user_id": "..."}`; `user_id` is omitted when auto-registration is disabled. This is a plain HTTPS server-to-server POST; the receiving application is responsible for protecting its endpoint. The backend's `Set-Cookie` is forwarded to the browser with LenAPI's redirect response, so choose cookie domain and frontend/API domains accordingly. The redirect destination comes from the webhook's `redirect_url`, never a browser-supplied URL. The helper functions in the sample are application code, not LenAPI built-ins.
|
|
1479
1481
|
|
|
1480
1482
|
`allowed_origins` can also be passed directly to `SocialAuth`; otherwise it reads `SOCIAL_AUTH_ALLOWED_ORIGINS` from `settings.py`. When a browser sends an `Origin` header to a provider login route, LenApi requires an exact match in this list and returns `403` otherwise. Replace `https://myapp.com` with the exact origin serving your frontend. Top-level navigations can omit `Origin`; those still rely on the required, random, browser-cookie-bound OAuth `state` check on callback. Do not treat the origin allowlist as a replacement for OAuth state validation, HTTPS, or correct provider callback URL registration. The provider callback itself is not checked against the browser `Origin` header because providers may omit it or send a different origin.
|
|
1481
1483
|
|
|
@@ -43,7 +43,6 @@ class SocialAuth:
|
|
|
43
43
|
user_model=None,
|
|
44
44
|
allowed_origins=None,
|
|
45
45
|
success_callback: str | None = None,
|
|
46
|
-
frontend_redirect_uri: str | None = None,
|
|
47
46
|
) -> None:
|
|
48
47
|
if isinstance(providers, str):
|
|
49
48
|
raise TypeError("providers must be a list or tuple of provider names")
|
|
@@ -74,13 +73,6 @@ class SocialAuth:
|
|
|
74
73
|
if success_callback is None:
|
|
75
74
|
success_callback = getattr(self.settings, "SOCIAL_AUTH_SUCCESS_CALLBACK", "") or None
|
|
76
75
|
self.success_callback = self._validate_success_callback(success_callback)
|
|
77
|
-
if frontend_redirect_uri is None:
|
|
78
|
-
frontend_redirect_uri = getattr(self.settings, "FRONTEND_REDIRECT_URI", "") or None
|
|
79
|
-
self.frontend_redirect_uri = (
|
|
80
|
-
self._validate_success_redirect(frontend_redirect_uri)
|
|
81
|
-
if frontend_redirect_uri
|
|
82
|
-
else None
|
|
83
|
-
)
|
|
84
76
|
if allowed_origins is None:
|
|
85
77
|
allowed_origins = getattr(self.settings, "SOCIAL_AUTH_ALLOWED_ORIGINS", ())
|
|
86
78
|
self.allowed_origins = self._normalize_origins(allowed_origins)
|
|
@@ -118,7 +110,6 @@ class SocialAuth:
|
|
|
118
110
|
or not parsed.hostname
|
|
119
111
|
or parsed.username is not None
|
|
120
112
|
or parsed.password is not None
|
|
121
|
-
or parsed.query
|
|
122
113
|
or parsed.fragment
|
|
123
114
|
):
|
|
124
115
|
raise ValueError("Social login success callback returned an invalid redirect URL")
|
|
@@ -360,11 +351,10 @@ class SocialAuth:
|
|
|
360
351
|
parsed_result = {}
|
|
361
352
|
if isinstance(parsed_result, dict):
|
|
362
353
|
result = parsed_result
|
|
363
|
-
redirect_url = result.get("redirect_url")
|
|
354
|
+
redirect_url = result.get("redirect_url")
|
|
364
355
|
if not redirect_url:
|
|
365
356
|
raise ValueError(
|
|
366
|
-
"Social login
|
|
367
|
-
"or FRONTEND_REDIRECT_URI setting"
|
|
357
|
+
"Social login success callback must return a redirect_url"
|
|
368
358
|
)
|
|
369
359
|
redirect_url = self._validate_success_redirect(redirect_url)
|
|
370
360
|
cookies = response.headers.get_list("set-cookie")
|
|
@@ -104,8 +104,6 @@ SOCIAL_AUTH_ALLOWED_ORIGINS = []
|
|
|
104
104
|
# Optional server-to-server endpoint for application-owned social login/session handling.
|
|
105
105
|
# Use HTTPS when enabling it.
|
|
106
106
|
SOCIAL_AUTH_SUCCESS_CALLBACK = ""
|
|
107
|
-
# Browser destination after the social-auth webhook succeeds.
|
|
108
|
-
FRONTEND_REDIRECT_URI = ""
|
|
109
107
|
# Keep this generated key private. Rotate it to invalidate existing tokens.
|
|
110
108
|
SECRET_KEY = "__LENAPI_GENERATED_SECRET_KEY__"
|
|
111
109
|
TOKEN = {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: lenapi
|
|
3
|
-
Version: 0.1.4.
|
|
3
|
+
Version: 0.1.4.6
|
|
4
4
|
Summary: A modern ASGI framework with Pydantic validation, sync and async database access, WebSockets, authentication, and integrated API documentation.
|
|
5
5
|
Author: Maekandex Communication Limitation
|
|
6
6
|
Author-email: "Ifeanyi M. Onovo" <ifeanyi@maekandex.com.ng>
|
|
@@ -1474,7 +1474,6 @@ def oidc_profile(identity=Depends(provider_auth)):
|
|
|
1474
1474
|
# settings.py — replace with the exact origin serving your frontend.
|
|
1475
1475
|
SOCIAL_AUTH_ALLOWED_ORIGINS = ["https://myapp.com"]
|
|
1476
1476
|
SOCIAL_AUTH_SUCCESS_CALLBACK = "https://api.example.com/auth/social/success"
|
|
1477
|
-
FRONTEND_REDIRECT_URI = "https://app.example.com/login/success"
|
|
1478
1477
|
|
|
1479
1478
|
# main.py
|
|
1480
1479
|
from lenapi import LENAPI
|
|
@@ -1500,12 +1499,14 @@ This mounts routes such as `/auth/google/login` and `/auth/google/callback` only
|
|
|
1500
1499
|
|
|
1501
1500
|
The provider's OAuth access token is used internally and is never returned. A successful social login requires `SOCIAL_AUTH_SUCCESS_CALLBACK`; without it the callback returns a generic `503` and no identity data. The browser callback never returns provider identity or a LenAPI token. `jwt_enabled` remains accepted for compatibility but credential issuance belongs to your backend callback. Google and LinkedIn use OIDC identity endpoints in this integration; GitHub uses OAuth 2.0 and its profile/email APIs, not OIDC.
|
|
1502
1501
|
|
|
1503
|
-
To keep provider identity out of the browser response and let your backend own login/session handling, configure `SOCIAL_AUTH_SUCCESS_CALLBACK`. LenAPI validates the provider callback and POSTs the verified identity as JSON to your backend.
|
|
1502
|
+
To keep provider identity out of the browser response and let your backend own login/session handling, configure `SOCIAL_AUTH_SUCCESS_CALLBACK`. LenAPI validates the provider callback and POSTs the verified identity as JSON to your backend. The webhook must return JSON containing `redirect_url`; LenAPI redirects the browser to that URL and forwards any `Set-Cookie` headers from the webhook response. Redirect URLs must use HTTPS; HTTP is accepted only for localhost development. Query parameters are allowed so the webhook can include a short-lived, single-use exchange code; URL fragments are rejected. Do not put a bearer token or provider access token in the redirect URL. When callback mode is enabled, your backend owns JWT/session issuance; `jwt_enabled` does not cause LenAPI to create or return a JWT.
|
|
1504
1503
|
|
|
1505
1504
|
```python
|
|
1506
1505
|
# Developer backend endpoint (the SOCIAL_AUTH_SUCCESS_CALLBACK URL)
|
|
1507
1506
|
from lenapi import JSONResponse, LENAPI, Request
|
|
1508
|
-
import
|
|
1507
|
+
import hashlib
|
|
1508
|
+
import secrets
|
|
1509
|
+
from urllib.parse import urlencode
|
|
1509
1510
|
|
|
1510
1511
|
app = LENAPI()
|
|
1511
1512
|
|
|
@@ -1515,19 +1516,20 @@ async def social_success(request: Request):
|
|
|
1515
1516
|
identity = data["identity"]
|
|
1516
1517
|
user_id = data.get("user_id")
|
|
1517
1518
|
|
|
1518
|
-
#
|
|
1519
|
+
# Apply your own account rules, then store a short-lived, single-use code.
|
|
1519
1520
|
user = await find_or_create_user(identity, user_id)
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
"session_id", session_id, max_age=3600, path="/",
|
|
1525
|
-
httponly=True, secure=True, samesite="lax",
|
|
1521
|
+
code = secrets.token_urlsafe(32)
|
|
1522
|
+
code_hash = hashlib.sha256(code.encode("utf-8")).hexdigest()
|
|
1523
|
+
await app.cache.set(
|
|
1524
|
+
f"social_login:{code_hash}", {"user_id": str(user.id)}, ttl=400
|
|
1526
1525
|
)
|
|
1527
|
-
|
|
1526
|
+
redirect_url = "https://app.example.com/login/success?" + urlencode({"code": code})
|
|
1527
|
+
return JSONResponse({"redirect_url": redirect_url})
|
|
1528
1528
|
```
|
|
1529
1529
|
|
|
1530
|
-
The
|
|
1530
|
+
The frontend should immediately POST the code to a separate backend exchange route. That route hashes the submitted code, retrieves and deletes its cache entry, and only then issues your bearer token or session. Do not place an access token in the redirect URL.
|
|
1531
|
+
|
|
1532
|
+
The webhook body is `{"provider": "google", "identity": {...}, "user_id": "..."}`; `user_id` is omitted when auto-registration is disabled. This is a plain HTTPS server-to-server POST; the receiving application is responsible for protecting its endpoint. The backend's `Set-Cookie` is forwarded to the browser with LenAPI's redirect response, so choose cookie domain and frontend/API domains accordingly. The redirect destination comes from the webhook's `redirect_url`, never a browser-supplied URL. The helper functions in the sample are application code, not LenAPI built-ins.
|
|
1531
1533
|
|
|
1532
1534
|
`allowed_origins` can also be passed directly to `SocialAuth`; otherwise it reads `SOCIAL_AUTH_ALLOWED_ORIGINS` from `settings.py`. When a browser sends an `Origin` header to a provider login route, LenApi requires an exact match in this list and returns `403` otherwise. Replace `https://myapp.com` with the exact origin serving your frontend. Top-level navigations can omit `Origin`; those still rely on the required, random, browser-cookie-bound OAuth `state` check on callback. Do not treat the origin allowlist as a replacement for OAuth state validation, HTTPS, or correct provider callback URL registration. The provider callback itself is not checked against the browser `Origin` header because providers may omit it or send a different origin.
|
|
1533
1535
|
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "lenapi"
|
|
7
|
-
version = "0.1.4.
|
|
7
|
+
version = "0.1.4.6"
|
|
8
8
|
description = "A modern ASGI framework with Pydantic validation, sync and async database access, WebSockets, authentication, and integrated API documentation."
|
|
9
9
|
authors = [
|
|
10
10
|
{name = "Ifeanyi M. Onovo", email = "ifeanyi@maekandex.com.ng"},
|
|
@@ -208,6 +208,48 @@ def test_social_auth_success_callback_redirects_without_exposing_identity():
|
|
|
208
208
|
assert delivered["identity"]["sub"] == "provider-user-1"
|
|
209
209
|
|
|
210
210
|
|
|
211
|
+
def test_social_auth_uses_webhook_redirect_with_query_parameters(monkeypatch):
|
|
212
|
+
import httpx
|
|
213
|
+
|
|
214
|
+
settings = SimpleNamespace(
|
|
215
|
+
SOCIAL_AUTH_SUCCESS_CALLBACK="https://backend.example/auth/social/success",
|
|
216
|
+
)
|
|
217
|
+
auth = SocialAuth(["google"], settings=settings)
|
|
218
|
+
|
|
219
|
+
class CallbackResponse:
|
|
220
|
+
is_success = True
|
|
221
|
+
content = b'{"redirect_url": "https://frontend.example/login/success?code=one-time-code"}'
|
|
222
|
+
headers = httpx.Headers([
|
|
223
|
+
("set-cookie", "session_id=opaque-session; Path=/; HttpOnly; Secure; SameSite=None")
|
|
224
|
+
])
|
|
225
|
+
|
|
226
|
+
@staticmethod
|
|
227
|
+
def json():
|
|
228
|
+
return {"redirect_url": "https://frontend.example/login/success?code=one-time-code"}
|
|
229
|
+
|
|
230
|
+
class CallbackClient:
|
|
231
|
+
def __init__(self, **kwargs):
|
|
232
|
+
pass
|
|
233
|
+
|
|
234
|
+
async def __aenter__(self):
|
|
235
|
+
return self
|
|
236
|
+
|
|
237
|
+
async def __aexit__(self, *_args):
|
|
238
|
+
return False
|
|
239
|
+
|
|
240
|
+
async def post(self, url, **kwargs):
|
|
241
|
+
assert url == settings.SOCIAL_AUTH_SUCCESS_CALLBACK
|
|
242
|
+
return CallbackResponse()
|
|
243
|
+
|
|
244
|
+
monkeypatch.setattr(httpx, "AsyncClient", CallbackClient)
|
|
245
|
+
redirect_url, cookies = asyncio.run(
|
|
246
|
+
auth._deliver_success("google", {"sub": "provider-user"}, None)
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
assert redirect_url == "https://frontend.example/login/success?code=one-time-code"
|
|
250
|
+
assert cookies == [
|
|
251
|
+
"session_id=opaque-session; Path=/; HttpOnly; Secure; SameSite=None"
|
|
252
|
+
]
|
|
211
253
|
def test_social_auth_origin_configuration_is_normalized_and_validated():
|
|
212
254
|
auth = SocialAuth(
|
|
213
255
|
["google"],
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|