|
@@ -1,5 +1,5 @@
|
|
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
|
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
|
-from fastapi import HTTPException, status, Depends
|
|
|
|
|
|
+from fastapi import HTTPException, status, Depends, Request
|
|
|
|
|
|
from apps.webui.models.users import Users
|
|
from apps.webui.models.users import Users
|
|
|
|
|
|
@@ -24,7 +24,8 @@ ALGORITHM = "HS256"
|
|
# Auth Utils
|
|
# Auth Utils
|
|
##############
|
|
##############
|
|
|
|
|
|
-bearer_security = HTTPBearer()
|
|
|
|
|
|
+bearer_security = HTTPBearer(auto_error=False)
|
|
|
|
+
|
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
|
|
|
|
|
|
|
|
@@ -75,13 +76,24 @@ def get_http_authorization_cred(auth_header: str):
|
|
|
|
|
|
|
|
|
|
def get_current_user(
|
|
def get_current_user(
|
|
|
|
+ request: Request,
|
|
auth_token: HTTPAuthorizationCredentials = Depends(bearer_security),
|
|
auth_token: HTTPAuthorizationCredentials = Depends(bearer_security),
|
|
):
|
|
):
|
|
|
|
+ # get token from cookie
|
|
|
|
+ token = request.cookies.get("token")
|
|
|
|
+
|
|
|
|
+ if auth_token is None and token is None:
|
|
|
|
+ raise HTTPException(status_code=403, detail="Not authenticated")
|
|
|
|
+
|
|
|
|
+ if auth_token is not None:
|
|
|
|
+ token = auth_token.credentials
|
|
|
|
+
|
|
# auth by api key
|
|
# auth by api key
|
|
- if auth_token.credentials.startswith("sk-"):
|
|
|
|
- return get_current_user_by_api_key(auth_token.credentials)
|
|
|
|
|
|
+ if token.startswith("sk-"):
|
|
|
|
+ return get_current_user_by_api_key(token)
|
|
|
|
+
|
|
# auth by jwt token
|
|
# auth by jwt token
|
|
- data = decode_token(auth_token.credentials)
|
|
|
|
|
|
+ data = decode_token(token)
|
|
if data != None and "id" in data:
|
|
if data != None and "id" in data:
|
|
user = Users.get_user_by_id(data["id"])
|
|
user = Users.get_user_by_id(data["id"])
|
|
if user is None:
|
|
if user is None:
|