Proxy handling #131

This commit is contained in:
Daniel Grams 2021-03-02 11:52:47 +01:00
parent 48cbc4e8cd
commit f77b7de90c
2 changed files with 22 additions and 0 deletions

View File

@ -29,6 +29,14 @@ app.config["SECURITY_EMAIL_SENDER"] = os.getenv("MAIL_DEFAULT_SENDER")
app.config["LANGUAGES"] = ["en", "de"]
app.config["SERVER_NAME"] = os.getenv("SERVER_NAME")
# Proxy handling
if os.getenv("PREFERRED_URL_SCHEME"): # pragma: no cover
app.config["PREFERRED_URL_SCHEME"] = os.getenv("PREFERRED_URL_SCHEME")
from project.reverse_proxied import ReverseProxied
app.wsgi_app = ReverseProxied(app.wsgi_app)
# Generate a nice key using secrets.token_urlsafe()
app.config["SECRET_KEY"] = os.environ.get(
"SECRET_KEY", "pf9Wkove4IKEAXvy-cQkeDPhv9Cb3Ag-wyJILbq_dFw"

View File

@ -0,0 +1,14 @@
from project import app
class ReverseProxied(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
# if one of x_forwarded or preferred_url is https, prefer it.
forwarded_scheme = environ.get("HTTP_X_FORWARDED_PROTO", None)
preferred_scheme = app.config.get("PREFERRED_URL_SCHEME", None)
if "https" in [forwarded_scheme, preferred_scheme]: # pragma: no cover
environ["wsgi.url_scheme"] = "https"
return self.app(environ, start_response)