fix(sso): AuthGuard 가 ASGI scope.path 를 사용하도록 변경
request.url.path 는 uvicorn --root-path 가 prepend 된 형태(/corm/health/db)라 _AUTH_EXEMPT_PREFIXES 의 /health prefix 와 매칭 안 됐다. scope.path 는 root_path 가 제외된 형태(/health/db)라 정확히 매칭된다. next_path 계산도 같이 정리 (이중 prefix 방지).
This commit is contained in:
@@ -71,7 +71,9 @@ class AuthGuardMiddleware(BaseHTTPMiddleware):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
async def dispatch(self, request: Request, call_next):
|
async def dispatch(self, request: Request, call_next):
|
||||||
path = request.url.path # ASGI 에서 root_path 가 빠진 경로
|
# ASGI scope 의 raw path 는 root_path 가 제외된 형태.
|
||||||
|
# (request.url.path 는 root_path 가 prepend 된 형태라 prefix 매칭이 어긋남)
|
||||||
|
path = request.scope.get("path", "")
|
||||||
for prefix in _AUTH_EXEMPT_PREFIXES:
|
for prefix in _AUTH_EXEMPT_PREFIXES:
|
||||||
if path == prefix or path.startswith(prefix):
|
if path == prefix or path.startswith(prefix):
|
||||||
return await call_next(request)
|
return await call_next(request)
|
||||||
@@ -85,8 +87,8 @@ class AuthGuardMiddleware(BaseHTTPMiddleware):
|
|||||||
wants_html = "text/html" in accept or not path.startswith("/api/")
|
wants_html = "text/html" in accept or not path.startswith("/api/")
|
||||||
if wants_html:
|
if wants_html:
|
||||||
next_path = f"{APP_ROOT_PATH}{path}" if APP_ROOT_PATH else path
|
next_path = f"{APP_ROOT_PATH}{path}" if APP_ROOT_PATH else path
|
||||||
if request.url.query:
|
if request.scope.get("query_string"):
|
||||||
next_path = f"{next_path}?{request.url.query}"
|
next_path = f"{next_path}?{request.scope['query_string'].decode('latin-1')}"
|
||||||
sep = "&" if "?" in MAIN_LOGIN_URL else "?"
|
sep = "&" if "?" in MAIN_LOGIN_URL else "?"
|
||||||
target = f"{MAIN_LOGIN_URL}{sep}next={quote(next_path, safe='/')}"
|
target = f"{MAIN_LOGIN_URL}{sep}next={quote(next_path, safe='/')}"
|
||||||
return RedirectResponse(url=target, status_code=302)
|
return RedirectResponse(url=target, status_code=302)
|
||||||
|
|||||||
Reference in New Issue
Block a user