mirror of
https://mirror.skon.top/github.com/langgenius/dify.git
synced 2026-04-20 15:20:15 +08:00
refactor(api): flatten nested conditionals and clean up token helpers (#34835)
Some checks are pending
autofix.ci / autofix (push) Waiting to run
Build and Push API & Web / build (api, {{defaultContext}}:api, Dockerfile, DIFY_API_IMAGE_NAME, linux/amd64, ubuntu-latest, build-api-amd64) (push) Waiting to run
Build and Push API & Web / build (api, {{defaultContext}}:api, Dockerfile, DIFY_API_IMAGE_NAME, linux/arm64, ubuntu-24.04-arm, build-api-arm64) (push) Waiting to run
Build and Push API & Web / build (web, {{defaultContext}}, web/Dockerfile, DIFY_WEB_IMAGE_NAME, linux/amd64, ubuntu-latest, build-web-amd64) (push) Waiting to run
Build and Push API & Web / build (web, {{defaultContext}}, web/Dockerfile, DIFY_WEB_IMAGE_NAME, linux/arm64, ubuntu-24.04-arm, build-web-arm64) (push) Waiting to run
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Blocked by required conditions
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Blocked by required conditions
Main CI Pipeline / Skip Duplicate Checks (push) Waiting to run
Main CI Pipeline / Check Changed Files (push) Blocked by required conditions
Main CI Pipeline / Run API Tests (push) Blocked by required conditions
Main CI Pipeline / Skip API Tests (push) Blocked by required conditions
Main CI Pipeline / API Tests (push) Blocked by required conditions
Main CI Pipeline / Run Web Tests (push) Blocked by required conditions
Main CI Pipeline / Skip Web Tests (push) Blocked by required conditions
Main CI Pipeline / Web Tests (push) Blocked by required conditions
Main CI Pipeline / Run Web Full-Stack E2E (push) Blocked by required conditions
Main CI Pipeline / Skip Web Full-Stack E2E (push) Blocked by required conditions
Main CI Pipeline / Web Full-Stack E2E (push) Blocked by required conditions
Main CI Pipeline / Style Check (push) Blocked by required conditions
Main CI Pipeline / Run VDB Tests (push) Blocked by required conditions
Main CI Pipeline / Skip VDB Tests (push) Blocked by required conditions
Main CI Pipeline / VDB Tests (push) Blocked by required conditions
Main CI Pipeline / Run DB Migration Test (push) Blocked by required conditions
Main CI Pipeline / Skip DB Migration Test (push) Blocked by required conditions
Main CI Pipeline / DB Migration Test (push) Blocked by required conditions
Trigger i18n Sync on Push / trigger (push) Waiting to run
Some checks are pending
autofix.ci / autofix (push) Waiting to run
Build and Push API & Web / build (api, {{defaultContext}}:api, Dockerfile, DIFY_API_IMAGE_NAME, linux/amd64, ubuntu-latest, build-api-amd64) (push) Waiting to run
Build and Push API & Web / build (api, {{defaultContext}}:api, Dockerfile, DIFY_API_IMAGE_NAME, linux/arm64, ubuntu-24.04-arm, build-api-arm64) (push) Waiting to run
Build and Push API & Web / build (web, {{defaultContext}}, web/Dockerfile, DIFY_WEB_IMAGE_NAME, linux/amd64, ubuntu-latest, build-web-amd64) (push) Waiting to run
Build and Push API & Web / build (web, {{defaultContext}}, web/Dockerfile, DIFY_WEB_IMAGE_NAME, linux/arm64, ubuntu-24.04-arm, build-web-arm64) (push) Waiting to run
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Blocked by required conditions
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Blocked by required conditions
Main CI Pipeline / Skip Duplicate Checks (push) Waiting to run
Main CI Pipeline / Check Changed Files (push) Blocked by required conditions
Main CI Pipeline / Run API Tests (push) Blocked by required conditions
Main CI Pipeline / Skip API Tests (push) Blocked by required conditions
Main CI Pipeline / API Tests (push) Blocked by required conditions
Main CI Pipeline / Run Web Tests (push) Blocked by required conditions
Main CI Pipeline / Skip Web Tests (push) Blocked by required conditions
Main CI Pipeline / Web Tests (push) Blocked by required conditions
Main CI Pipeline / Run Web Full-Stack E2E (push) Blocked by required conditions
Main CI Pipeline / Skip Web Full-Stack E2E (push) Blocked by required conditions
Main CI Pipeline / Web Full-Stack E2E (push) Blocked by required conditions
Main CI Pipeline / Style Check (push) Blocked by required conditions
Main CI Pipeline / Run VDB Tests (push) Blocked by required conditions
Main CI Pipeline / Skip VDB Tests (push) Blocked by required conditions
Main CI Pipeline / VDB Tests (push) Blocked by required conditions
Main CI Pipeline / Run DB Migration Test (push) Blocked by required conditions
Main CI Pipeline / Skip DB Migration Test (push) Blocked by required conditions
Main CI Pipeline / DB Migration Test (push) Blocked by required conditions
Trigger i18n Sync on Push / trigger (push) Waiting to run
This commit is contained in:
@@ -47,23 +47,17 @@ def _cookie_domain() -> str | None:
|
|||||||
def _real_cookie_name(cookie_name: str) -> str:
|
def _real_cookie_name(cookie_name: str) -> str:
|
||||||
if is_secure() and _cookie_domain() is None:
|
if is_secure() and _cookie_domain() is None:
|
||||||
return "__Host-" + cookie_name
|
return "__Host-" + cookie_name
|
||||||
else:
|
return cookie_name
|
||||||
return cookie_name
|
|
||||||
|
|
||||||
|
|
||||||
def _try_extract_from_header(request: Request) -> str | None:
|
def _try_extract_from_header(request: Request) -> str | None:
|
||||||
auth_header = request.headers.get("Authorization")
|
auth_header = request.headers.get("Authorization")
|
||||||
if auth_header:
|
if not auth_header or " " not in auth_header:
|
||||||
if " " not in auth_header:
|
return None
|
||||||
return None
|
auth_scheme, auth_token = auth_header.split(None, 1)
|
||||||
else:
|
if auth_scheme.lower() != "bearer":
|
||||||
auth_scheme, auth_token = auth_header.split(None, 1)
|
return None
|
||||||
auth_scheme = auth_scheme.lower()
|
return auth_token
|
||||||
if auth_scheme != "bearer":
|
|
||||||
return None
|
|
||||||
else:
|
|
||||||
return auth_token
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def extract_refresh_token(request: Request) -> str | None:
|
def extract_refresh_token(request: Request) -> str | None:
|
||||||
@@ -90,14 +84,9 @@ def extract_webapp_access_token(request: Request) -> str | None:
|
|||||||
|
|
||||||
|
|
||||||
def extract_webapp_passport(app_code: str, request: Request) -> str | None:
|
def extract_webapp_passport(app_code: str, request: Request) -> str | None:
|
||||||
def _try_extract_passport_token_from_cookie(request: Request) -> str | None:
|
return request.cookies.get(_real_cookie_name(COOKIE_NAME_PASSPORT + "-" + app_code)) or request.headers.get(
|
||||||
return request.cookies.get(_real_cookie_name(COOKIE_NAME_PASSPORT + "-" + app_code))
|
HEADER_NAME_PASSPORT
|
||||||
|
)
|
||||||
def _try_extract_passport_token_from_header(request: Request) -> str | None:
|
|
||||||
return request.headers.get(HEADER_NAME_PASSPORT)
|
|
||||||
|
|
||||||
ret = _try_extract_passport_token_from_cookie(request) or _try_extract_passport_token_from_header(request)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
|
|
||||||
def set_access_token_to_cookie(request: Request, response: Response, token: str, samesite: str = "Lax"):
|
def set_access_token_to_cookie(request: Request, response: Response, token: str, samesite: str = "Lax"):
|
||||||
@@ -209,22 +198,18 @@ def check_csrf_token(request: Request, user_id: str):
|
|||||||
|
|
||||||
if not csrf_token:
|
if not csrf_token:
|
||||||
_unauthorized()
|
_unauthorized()
|
||||||
verified = {}
|
|
||||||
try:
|
try:
|
||||||
verified = PassportService().verify(csrf_token)
|
verified = PassportService().verify(csrf_token)
|
||||||
except:
|
except Exception:
|
||||||
_unauthorized()
|
_unauthorized()
|
||||||
|
raise # unreachable, but helps the type checker see verified is always bound
|
||||||
|
|
||||||
if verified.get("sub") != user_id:
|
if verified.get("sub") != user_id:
|
||||||
_unauthorized()
|
_unauthorized()
|
||||||
|
|
||||||
exp: int | None = verified.get("exp")
|
exp: int | None = verified.get("exp")
|
||||||
if not exp:
|
if not exp or exp < int(datetime.now(UTC).timestamp()):
|
||||||
_unauthorized()
|
_unauthorized()
|
||||||
else:
|
|
||||||
time_now = int(datetime.now().timestamp())
|
|
||||||
if exp < time_now:
|
|
||||||
_unauthorized()
|
|
||||||
|
|
||||||
|
|
||||||
def generate_csrf_token(user_id: str) -> str:
|
def generate_csrf_token(user_id: str) -> str:
|
||||||
|
|||||||
Reference in New Issue
Block a user