-
-
Save ncouture/05e16d2b551393de373b5ec20494cbed to your computer and use it in GitHub Desktop.
SSLOnlyMiddleware
This file contains hidden or 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
setup( | |
entry_points={ | |
'paste.filter_app_factory': [ | |
'ssl_only = myapp.middlewares.ssl_only:make_filter', | |
], | |
} | |
) |
This file contains hidden or 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
[app:main] | |
use = egg:myapp | |
filter-with = egg:myapp#ssl_only |
This file contains hidden or 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 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