Skip to content

Instantly share code, notes, and snippets.

@raveenb
Last active January 20, 2021 16:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raveenb/ab9485c80abe491da98b9ddd43d72fad to your computer and use it in GitHub Desktop.
Save raveenb/ab9485c80abe491da98b9ddd43d72fad to your computer and use it in GitHub Desktop.
Fix Oauth Issue for Flask+Traefik

If you are using Oauth login/register on a Flask server and its is behind Traefik. You will need to do two things to fix the https and redirect issues. You may be getting errors indicating that the OAuth callback is not valid or is not https enabled. And the intersting part to note is that it will work fine when running on local host and straight up https, but fail when the app is behing a proxy/edege router/traffic multiplexer like Traefik or NGinx.

  1. Do the Proxy fix to the wsgi app inside of the flask app. This is from the flask documentation itself https://flask.palletsprojects.com/en/1.1.x/deploying/wsgi-standalone/#deploying-proxy-setups
# assuming app is the Flask app

from werkzeug.middleware.proxy_fix import ProxyFix

app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)
  1. Add the PREFERRED_URL_SCHEME in Flask config to https
class FlaskConfig:
  SECRET_KEY = "..."  # <<< Standard stuff
  SQLALCHEMY_DATABASE_URI = os.getenv("DATABASE_URL")
  SQLALCHEMY_TRACK_MODIFICATIONS = False
  
  PREFERRED_URL_SCHEME = "https"  # <<< Note here
  

this is shamelessly copied in parts from https://flask-dance.readthedocs.io/en/latest/proxies.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment