Skip to content

Instantly share code, notes, and snippets.

@johnsheehan
Created March 31, 2012 21:56
Show Gist options
  • Save johnsheehan/2268946 to your computer and use it in GitHub Desktop.
Save johnsheehan/2268946 to your computer and use it in GitHub Desktop.
@ssl_required decorator for Flask on Heroku
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