fix: use base_url config for OAuth redirect URI to handle reverse proxy

This commit is contained in:
2026-05-08 14:11:09 -04:00
parent 39f1b5de30
commit 05045bafa2
+6 -4
View File
@@ -914,13 +914,16 @@ async def start(
) )
return web.Response(text=body, content_type="text/html") return web.Response(text=body, content_type="text/html")
def _oauth_redirect_uri(request) -> str:
base = config.get("base_url", "").rstrip("/") or str(request.url.origin())
return f"{base}/login/oauth/gitea/callback"
async def oauth_gitea_redirect(request): async def oauth_gitea_redirect(request):
"""GET /login/oauth/gitea — kick off the Gitea OAuth2 flow.""" """GET /login/oauth/gitea — kick off the Gitea OAuth2 flow."""
if not oauth_mod.is_enabled(config): if not oauth_mod.is_enabled(config):
return web.Response(status=404, text="OAuth not configured") return web.Response(status=404, text="OAuth not configured")
state = oauth_mod.make_state() state = oauth_mod.make_state()
redirect_uri = f"{request.url.origin()}/login/oauth/gitea/callback" raise web.HTTPFound(oauth_mod.authorization_url(config, state, _oauth_redirect_uri(request)))
raise web.HTTPFound(oauth_mod.authorization_url(config, state, redirect_uri))
async def oauth_gitea_callback(request): async def oauth_gitea_callback(request):
"""GET /login/oauth/gitea/callback — handle Gitea's redirect back.""" """GET /login/oauth/gitea/callback — handle Gitea's redirect back."""
@@ -933,9 +936,8 @@ async def start(
if not oauth_mod.validate_state(state): if not oauth_mod.validate_state(state):
logger.warning("OAuth: invalid or expired state token from %s", request.remote) logger.warning("OAuth: invalid or expired state token from %s", request.remote)
raise web.HTTPFound("/login?error=1") raise web.HTTPFound("/login?error=1")
redirect_uri = f"{request.url.origin()}/login/oauth/gitea/callback"
try: try:
token = await oauth_mod.exchange_code(config, code, redirect_uri) token = await oauth_mod.exchange_code(config, code, _oauth_redirect_uri(request))
profile = await oauth_mod.fetch_user(config, token) profile = await oauth_mod.fetch_user(config, token)
except oauth_mod.OAuthError as exc: except oauth_mod.OAuthError as exc:
logger.warning("OAuth error: %s", exc) logger.warning("OAuth error: %s", exc)