Skip to content

Commit

Permalink
Set expiries to the cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
csy113 committed Apr 29, 2014
1 parent 198859d commit 9235136
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 7 additions & 3 deletions session.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from google.appengine.ext import ndb
from random import randrange
from google.appengine.api import users
from time_func import getTimeFromNow
import logging

class CSRFToken(ndb.Model):
createTime = ndb.DateTimeProperty(auto_now_add=True)
token = ndb.StringProperty()

def checkTokenValid(webapp2ReqHandle):
csrfToken = None
try:
sessionID = int(webapp2ReqHandle.request.cookies.get('sessionid'))
except TypeError:
Expand All @@ -26,8 +28,8 @@ def checkTokenValid(webapp2ReqHandle):
return ret

def _setCookie(webapp2ReqHandle, sessionID):
#TODO add expire time
webapp2ReqHandle.response.set_cookie('sessionid', '%d' % sessionID, httponly=True)
webapp2ReqHandle.response.set_cookie('sessionid', '%d' % sessionID,
httponly=True, expires=getTimeFromNow(1))

def _createCSRFToken():
token = '%s' % randrange(1000, 99999999)
Expand All @@ -51,7 +53,9 @@ def getOrInsertCSRFToken(webapp2ReqHandle):
if csrfToken is None:
csrfToken = _createCSRFToken()
_setCookie(webapp2ReqHandle, csrfToken.key.id())
logging.info('Token retrived: %s' % csrfToken)
logging.info('Token created : %s' % csrfToken)
else:
logging.info('Token retrived: %s' % csrfToken)
return csrfToken

def deleteCSRFToken(webapp2ReqHandle):
Expand Down
5 changes: 4 additions & 1 deletion time_func.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timedelta
import logging

timeformat="%Y-%m-%d %H:%M:%S"
Expand All @@ -23,3 +23,6 @@ def isToday(inputdate):

def getTimeNow():
return datetime.now()

def getTimeFromNow(hour):
return datetime.now() + timedelta(hours=hour)

0 comments on commit 9235136

Please sign in to comment.