A couple of projects I have created are required to run only over HTTPS. I have noticed that I am using this more and more and that I have seen a few questions in the user forum about this same thing. This also came about after running a PCI compliance test where SSL was being used, but secure.session() was not being called.
The way I currently make this happen is to put this at the bottom of my db.py file:
############ FORCED SSL #############
from gluon.settings import global_settings
if global_settings.cronjob:
print 'Running as shell script.'
elif not request.is_https:
session.secure()
redirect('https://%s/%s' % (request.env.http_host, request.application))
#####################################
The benefit of this is that if you access the application over HTTP, it redirects the browser to HTTPS, and secures the session. This also make sure that cron jobs aren't affected.
Should we include something like this as a setting somewhere to make it easy for people to force their applications to only use SSL? Something like:
request.force_ssl = True
Thoughts?