|
|
@@ -4,6 +4,7 @@ import time
|
|
|
import datetime
|
|
|
import logging
|
|
|
from aiohttp import ClientSession
|
|
|
+import jwt
|
|
|
|
|
|
from open_webui.models.auths import (
|
|
|
AddUserForm,
|
|
|
@@ -24,6 +25,7 @@ from open_webui.constants import ERROR_MESSAGES, WEBHOOK_MESSAGES
|
|
|
from open_webui.env import (
|
|
|
WEBUI_AUTH,
|
|
|
WEBUI_AUTH_TRUSTED_EMAIL_HEADER,
|
|
|
+ WEBUI_AUTH_TRUSTED_TOKEN_HEADER,
|
|
|
WEBUI_AUTH_TRUSTED_NAME_HEADER,
|
|
|
WEBUI_AUTH_COOKIE_SAME_SITE,
|
|
|
WEBUI_AUTH_COOKIE_SECURE,
|
|
|
@@ -327,7 +329,35 @@ async def ldap_auth(request: Request, response: Response, form_data: LdapForm):
|
|
|
|
|
|
@router.post("/signin", response_model=SessionUserResponse)
|
|
|
async def signin(request: Request, response: Response, form_data: SigninForm):
|
|
|
- if WEBUI_AUTH_TRUSTED_EMAIL_HEADER:
|
|
|
+ if WEBUI_AUTH_TRUSTED_TOKEN_HEADER:
|
|
|
+ if WEBUI_AUTH_TRUSTED_TOKEN_HEADER not in request.headers:
|
|
|
+ raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_TRUSTED_HEADER)
|
|
|
+
|
|
|
+ trusted_token = request.headers[WEBUI_AUTH_TRUSTED_TOKEN_HEADER]
|
|
|
+ # 讲token解密,获取用户名称和邮箱
|
|
|
+ try:
|
|
|
+ payload = jwt.decode(trusted_token, "WEBUI_SECRET_KEY", algorithms=["HS256"])
|
|
|
+ except jwt.ExpiredSignatureError:
|
|
|
+ raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_TRUSTED_HEADER)
|
|
|
+ except jwt.InvalidTokenError:
|
|
|
+ raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_TRUSTED_HEADER)
|
|
|
+ trusted_email = payload.get("email")
|
|
|
+ trusted_name = payload.get("name")
|
|
|
+
|
|
|
+ if not trusted_email or not trusted_name:
|
|
|
+ raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_TRUSTED_HEADER)
|
|
|
+
|
|
|
+ # 验证用户是否存在
|
|
|
+ if not Users.get_user_by_email(trusted_email.lower()):
|
|
|
+ await signup(
|
|
|
+ request,
|
|
|
+ response,
|
|
|
+ SignupForm(email=trusted_email, password=str(uuid.uuid4()), name=trusted_name),
|
|
|
+ )
|
|
|
+ user = Auths.authenticate_user_by_trusted_header(trusted_email)
|
|
|
+
|
|
|
+
|
|
|
+ elif WEBUI_AUTH_TRUSTED_EMAIL_HEADER:
|
|
|
if WEBUI_AUTH_TRUSTED_EMAIL_HEADER not in request.headers:
|
|
|
raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_TRUSTED_HEADER)
|
|
|
|