From 643a6b2eb7ec1f374ffac057165ac3a5e70c9682 Mon Sep 17 00:00:00 2001 From: Brian Wiborg Date: Sat, 11 Oct 2025 01:07:55 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=92=EF=B8=8F=20Add=20optionally=5Fauth?= =?UTF-8?q?enticated=20permission?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ohmyapi/builtin/auth/routes.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/ohmyapi/builtin/auth/routes.py b/src/ohmyapi/builtin/auth/routes.py index 8a81389..2f1cb2a 100644 --- a/src/ohmyapi/builtin/auth/routes.py +++ b/src/ohmyapi/builtin/auth/routes.py @@ -1,6 +1,6 @@ import time from enum import Enum -from typing import Any, Dict, List +from typing import Any, Dict, List, Optional import jwt import settings @@ -124,6 +124,12 @@ async def get_current_user(token: str = Depends(oauth2_scheme)) -> User: return user +async def optionally_authenticated(token: Optional[str] = Depends(oauth2_scheme)) -> Optional[User]: + if token is None: + return None + return await get_current_user(token) + + async def require_authenticated(current_user: User = Depends(get_current_user)) -> User: """Ensure the current user is an admin.""" if not current_user: