Merge pull request #120 from alphagov/set-secure-cookie

Set secure cookie
This commit is contained in:
NIcholas Staples
2016-01-28 15:42:25 +00:00
2 changed files with 9 additions and 4 deletions

View File

@@ -1,13 +1,15 @@
from datetime import timedelta, datetime
from werkzeug.datastructures import CallbackDict from werkzeug.datastructures import CallbackDict
from flask.sessions import SessionInterface, SessionMixin from flask.sessions import SessionInterface, SessionMixin
from itsdangerous import URLSafeTimedSerializer, BadSignature from itsdangerous import URLSafeTimedSerializer, BadSignature
class ItsdangerousSession(CallbackDict, SessionMixin): class ItsdangerousSession(CallbackDict, SessionMixin):
def __init__(self, initial=None): def __init__(self, initial=None):
def on_update(self): def on_update(self):
self.modified = True self.modified = True
CallbackDict.__init__(self, initial, on_update) CallbackDict.__init__(self, initial, on_update)
self.modified = False self.modified = False
@@ -43,8 +45,9 @@ class ItsdangerousSessionInterface(SessionInterface):
response.delete_cookie(app.session_cookie_name, response.delete_cookie(app.session_cookie_name,
domain=domain) domain=domain)
return return
expires = self.get_expiration_time(app, session) session.permanent = True
expires = datetime.utcnow() + timedelta(app.config.get('PERMANENT_SESSION_LIFETIME'))
val = self.get_serializer(app).dumps(dict(session)) val = self.get_serializer(app).dumps(dict(session))
response.set_cookie(app.session_cookie_name, val, response.set_cookie(app.session_cookie_name, val,
expires=expires, httponly=True, expires=expires, httponly=True,
domain=domain) domain=domain, secure=app.config.get('SESSION_COOKIE_SECURE'))

View File

@@ -20,7 +20,7 @@ class Config(object):
SESSION_COOKIE_NAME = 'notify_admin_session' SESSION_COOKIE_NAME = 'notify_admin_session'
SESSION_COOKIE_PATH = '/admin' SESSION_COOKIE_PATH = '/admin'
SESSION_COOKIE_HTTPONLY = True SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SECURE = True SESSION_COOKIE_SECURE = False
PERMANENT_SESSION_LIFETIME = 3600 # seconds PERMANENT_SESSION_LIFETIME = 3600 # seconds
API_HOST_NAME = os.getenv('API_HOST_NAME') API_HOST_NAME = os.getenv('API_HOST_NAME')
@@ -56,6 +56,8 @@ class Test(Development):
class Live(Config): class Live(Config):
DEBUG = False DEBUG = False
HTTP_PROTOCOL = 'https' HTTP_PROTOCOL = 'https'
SESSION_COOKIE_SECURE = True
configs = { configs = {
'live': Live, 'live': Live,