From 2b57544d7fe37998dab6f54612d5dd10ff3efb8d Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 1 Oct 2013 23:16:29 +0200 Subject: Added options for persistent login. --- fbin.py | 35 +++++++++++++++++++++++++++++------ templates/login.tmpl | 3 +++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/fbin.py b/fbin.py index 4b7f2a7..4a94e97 100755 --- a/fbin.py +++ b/fbin.py @@ -372,19 +372,29 @@ class Application(object): self.update_user_login(user) + rememberme = 'rememberme' in form + forever = 'forever' in form + c = Cookie.SimpleCookie() c['uid'] = user.id c['identifier'] = hashlib.sha1(str(user.id) + password).hexdigest() dt = datetime.datetime.utcnow() + datetime.timedelta(days = 30) expires = dt.strftime('%a, %d-%b-%y %H:%M:%S GMT') - c['uid']['expires'] = expires - c['identifier']['expires'] = expires - - start_response('302 Found', [ + if rememberme: + c['uid']['expires'] = expires + c['identifier']['expires'] = expires + if forever: + c['forever'] = 1 + c['forever']['expires'] = expires + + headers = [ ('Location', next if next else (settings.virtual_root + 'u')), ('Set-Cookie', c['uid'].OutputString()), - ('Set-Cookie', c['identifier'].OutputString())]) + ('Set-Cookie', c['identifier'].OutputString())] + if 'forever' in c: + headers.append(('Set-Cookie', c['forever'].OutputString())) + start_response('302 Found', headers) return [] def register(self, environ, start_response, path): @@ -432,9 +442,12 @@ class Application(object): c['uid']['expires'] = expires c['identifier'] = '' c['identifier']['expires'] = expires + c['forever'] = 0 + c['forever']['expires'] = expires start_response('302 Found', [ ('Set-Cookie', c['uid'].OutputString()), ('Set-Cookie', c['identifier'].OutputString()), + ('Set-Cookie', c['forever'].OutputString()), ('Location', settings.virtual_root)]) return [] @@ -631,11 +644,21 @@ class Application(object): a = api def __call__(self, environ, start_response): + def never_forget_wrapper(status, headers, exc_info = None): + c = Cookie.SimpleCookie(environ['HTTP_COOKIE'] if 'HTTP_COOKIE' in environ else None) + if 'forever' in c and c['forever'].value == '1' and not any(x[0] == 'Set-Cookie' for x in headers): + dt = datetime.datetime.utcnow() + datetime.timedelta(days = 30) + expires = dt.strftime('%a, %d-%b-%y %H:%M:%S GMT') + for k in c.keys(): + c[k]['path'] = settings.virtual_root + c[k]['expires'] = expires + headers.append(('Set-Cookie', c[k].OutputString())) + return start_response(status, headers, exc_info) try: path = environ['PATH_INFO'].split('/')[1:] module = path[0] if len(path) else '' if len(module) and module in 'fulshmitorcda': - return getattr(self, module)(environ, start_response, path) + return getattr(self, module)(environ, never_forget_wrapper, path) elif path == ['favicon.ico']: return self.static(environ, start_response, ['s'] + path) else: diff --git a/templates/login.tmpl b/templates/login.tmpl index e55600d..af2f955 100644 --- a/templates/login.tmpl +++ b/templates/login.tmpl @@ -12,6 +12,9 @@

password

+

+

+

#end def -- cgit v1.2.3