Created
March 31, 2012 21:56
-
-
Save johnsheehan/2268946 to your computer and use it in GitHub Desktop.
@ssl_required decorator for Flask on Heroku
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
from flask import request, redirect | |
import os | |
def https_required(f): | |
@wraps(f) | |
def decorated_function(*args, **kwargs): | |
scheme = 'http' | |
if request.headers.get('x-forwarded-proto', None): | |
scheme = request.headers.get('x-forwarded-proto') | |
if scheme == 'http' and os.environ.get('REALM', '') == 'prod': # set REALM env variable from CLI | |
newurl = request.url.replace('http://', 'https://') | |
return redirect(newurl, 301) | |
return f(*args, **kwargs) | |
return decorated_function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment