What changed, and why it matters
This commit fixes a bug in LNbits' single-sign-on (SSO) login code. The login endpoint required callers to supply a 'user_id' value, but in normal use it should be optional. Without the fix, users trying to log in via an external provider such as Keycloak would hit an error because the parameter was missing. The change makes the parameter optional by giving it a default value of None, and adds a test that confirms the redirect to the SSO provider still works when no user_id is supplied.
Treat as a normal bug-fix commit. Reviewers should confirm that downstream SSO callback/token handling safely handles user_id=None and that no other endpoints have similar unintended required parameters.
Security signals we found
Required parameter made optional to restore intended SSO login flow
New regression test added for missing user_id scenario
No authorization bypass or privilege escalation visible in diff
Evidence from the diff
In lnbits/core/views/auth_api.py the login_with_sso_provider route declared user_id as str | None without a default. FastAPI therefore treated it as a required query parameter. The patch adds = None so the parameter becomes optional. A new test verifies that a GET to /api/v1/auth/{provider} (no user_id) returns HTTP 307 and redirects to the provider’s login URL. The fix is small and targeted; it does not appear to change authorization logic, only restores the intended API contract.
Changed components
lnbits/core/views/auth_api.pytests/api/test_auth_api.pyInspect captured patch +19 / −1
diff --git a/lnbits/core/views/auth_api.py b/lnbits/core/views/auth_api.py
index 5d5c08d..5a88c25 100644
--- a/lnbits/core/views/auth_api.py
+++ b/lnbits/core/views/auth_api.py
@@ -326,7 +326,7 @@ async def api_delete_user_api_token(
async def login_with_sso_provider(
request: Request,
provider: str,
- user_id: str | None,
+ user_id: str | None = None,
auth_user_id: str | None = Depends(optional_user_id),
):
provider_sso = _new_sso(provider)
diff --git a/tests/api/test_auth_api.py b/tests/api/test_auth_api.py
index 8993cd8..83ea8dc 100644
--- a/tests/api/test_auth_api.py
+++ b/tests/api/test_auth_api.py
@@ -58,6 +58,24 @@ async def test_auth_api_logout_and_update_ui_customization(
assert "cookie_access_token=" in logout.headers["set-cookie"]
+@pytest.mark.anyio
+async def test_auth_api_keycloak_login_without_user_id_redirects_to_provider(
+ http_client: AsyncClient, mocker
+):
+ provider = "keycloak"
+ login_sso = _FakeSSO()
+ mocker.patch("lnbits.core.views.auth_api._new_sso", return_value=login_sso)
+
+ response = await http_client.get(f"/api/v1/auth/{provider}")
+
+ assert response.status_code == 307
+ assert response.headers["location"] == "https://example.com/sso/login"
+ assert login_sso.redirect_uri == (
+ f"{http_client.base_url}/api/v1/auth/{provider}/token"
+ )
+ assert login_sso.state is None
+
+
@pytest.mark.anyio
async def test_auth_api_sso_login_and_callback(http_client: AsyncClient, mocker):
user = await create_user_account(
Why this scored 35/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.