Skip to content

Commit 33d3305

Browse files
committed
Support Bearer API key auth on all API endpoints
1 parent a68338d commit 33d3305

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

app/auth.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,19 @@ def decode_session_token(token: str) -> dict[str, Any] | None:
4141

4242

4343
def get_current_user(request: Request) -> dict[str, Any] | None:
44-
"""Extract user info from session cookie. Returns None if not authenticated."""
44+
"""Extract user info from Bearer API key or session cookie.
45+
46+
Checks Authorization header first (for programmatic access like Home Assistant),
47+
then falls back to session cookie (for browser sessions).
48+
"""
49+
# Check Bearer token against CASHPILOT_API_KEY
50+
api_key = os.getenv("CASHPILOT_API_KEY", "")
51+
if api_key:
52+
auth_header = request.headers.get("Authorization", "")
53+
if auth_header == f"Bearer {api_key}":
54+
return {"uid": 0, "u": "api", "r": "owner"}
55+
56+
# Fall back to session cookie
4557
token = request.cookies.get(SESSION_COOKIE)
4658
if not token:
4759
return None

0 commit comments

Comments
 (0)