fix(oauth): force https on Google redirect_uri for public hosts
Proxy-header trust alone wasn't enough in prod (still built http://). Build the redirect_uri and force https for non-localhost hosts so it matches the registered Google URI regardless of how the reverse proxy forwards the scheme.
This commit is contained in:
parent
5118fd6989
commit
1f7ec835db
1 changed files with 8 additions and 0 deletions
|
|
@ -48,6 +48,14 @@ async def google_login(request: Request):
|
||||||
if oauth is None:
|
if oauth is None:
|
||||||
raise HTTPException(status_code=404, detail="Google login not configured")
|
raise HTTPException(status_code=404, detail="Google login not configured")
|
||||||
redirect_uri = str(request.url_for("google_callback"))
|
redirect_uri = str(request.url_for("google_callback"))
|
||||||
|
# Behind a TLS-terminating reverse proxy the app sees the request as http, so
|
||||||
|
# url_for builds an http:// redirect_uri — but Google requires the exact
|
||||||
|
# registered https URI for public hosts (→ redirect_uri_mismatch otherwise).
|
||||||
|
# Force https except on localhost (where there's no proxy and http is used).
|
||||||
|
if redirect_uri.startswith("http://") and not any(
|
||||||
|
h in redirect_uri for h in ("localhost", "127.0.0.1")
|
||||||
|
):
|
||||||
|
redirect_uri = "https://" + redirect_uri[len("http://"):]
|
||||||
return await oauth.google.authorize_redirect(request, redirect_uri)
|
return await oauth.google.authorize_redirect(request, redirect_uri)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue