Skip to content

Instantly share code, notes, and snippets.

@ncouture
Forked from mmerickel/setup.py
Created May 5, 2016 20:10
Show Gist options
  • Save ncouture/05e16d2b551393de373b5ec20494cbed to your computer and use it in GitHub Desktop.
Save ncouture/05e16d2b551393de373b5ec20494cbed to your computer and use it in GitHub Desktop.
SSLOnlyMiddleware
setup(
entry_points={
'paste.filter_app_factory': [
'ssl_only = myapp.middlewares.ssl_only:make_filter',
],
}
)
[app:main]
use = egg:myapp
filter-with = egg:myapp#ssl_only
from webob.exc import HTTPMovedPermanently
from webob.dec import wsgify
@wsgify.middleware
def SSLOnlyMiddleware(request, app):
# check that the incoming request was using https
if request.headers['X-Forwarded-Proto'] != 'https':
# redirect the user back using https
https_url = request.url.replace('http://', 'https://')
return HTTPMovedPermanently(location=https_url)
# override the request scheme to tell the app that we are using https
request.scheme = 'https'
return app
def make_filter(app, global_conf, **filter_settings):
return SSLOnlyMiddleware(app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment