Package buildbot :: Package status :: Package web :: Module session
[frames] | no frames]

Source Code for Module buildbot.status.web.session

  1  # This file is part of Buildbot.  Buildbot is free software: you can 
  2  # redistribute it and/or modify it under the terms of the GNU General Public 
  3  # License as published by the Free Software Foundation, version 2. 
  4  # 
  5  # This program is distributed in the hope that it will be useful, but WITHOUT 
  6  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
  8  # details. 
  9  # 
 10  # You should have received a copy of the GNU General Public License along with 
 11  # this program; if not, write to the Free Software Foundation, Inc., 51 
 12  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 13  # 
 14  # Copyright Buildbot Team Members 
 15  # 
 16  # Insipration, and some code, from: 
 17  #    :copyright: (c) 2011 by the Werkzeug Team, see Werkzeug's AUTHORS for more 
 18  #    details. 
 19   
 20  try: 
 21      from hashlib import sha1 
 22      sha1 = sha1 # make pyflakes happy 
 23  except ImportError: 
 24      from sha import new as sha1 
 25  from time import time 
 26  from random import random 
 27  from datetime import datetime, timedelta 
 28  import os 
29 -def _urandom():
30 if hasattr(os, 'urandom'): 31 return os.urandom(30) 32 return random()
33 36 37
38 -class Session(object):
39 """I'm a user's session. Contains information about a user's session 40 a user can have several session 41 a session is associated with a cookie 42 """ 43 user = "" 44 infos = {}
45 - def __init__(self, user, infos):
46 self.user = user 47 self.infos = infos 48 self.renew()
49
50 - def renew(self):
51 # one day expiration. hardcoded for now... 52 self.expiration = datetime.now()+ timedelta(1) 53 return self.expiration
54
55 - def expired(self):
56 return datetime.now() > self.expiration
57
58 - def userInfosHTML(self):
59 return ('%(fullName)s [<a href="mailto:%(email)s">%(email)s</a>]' % 60 (self.infos))
61
62 - def getExpiration(self):
63 delim = '-' 64 d = self.expiration.utctimetuple() 65 return '%s, %02d%s%s%s%s %02d:%02d:%02d GMT' % ( 66 ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')[d.tm_wday], 67 d.tm_mday, delim, 68 ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 69 'Oct', 'Nov', 'Dec')[d.tm_mon - 1], 70 delim, str(d.tm_year), d.tm_hour, d.tm_min, d.tm_sec 71 )
72
73 -class SessionManager(object):
74 """I'm the session manager. Holding the current sessions 75 managing cookies, and their expiration 76 77 KISS version for the moment: 78 79 The sessions are stored in RAM so that you have to relogin after buildbot 80 reboot 81 82 Old sessions are searched at every connection, which is not very good for 83 scaling 84 85 """ 86 87 # borg pattern (similar to singleton) not too loose sessions with reconfig 88 __shared_state = dict(sessions={},users={}) 89
90 - def __init__(self):
91 self.__dict__ = self.__shared_state
92
93 - def new(self, user, infos):
94 cookie = generate_cookie() 95 user = infos["userName"] 96 self.users[user] = self.sessions[cookie] = s = Session(user, infos) 97 return cookie, s
98
99 - def gc(self):
100 """remove old cookies""" 101 expired = [] 102 for cookie in self.sessions: 103 s = self.sessions[cookie] 104 if s.expired(): 105 expired.append(cookie) 106 for cookie in expired: 107 del self.sessions[cookie]
108
109 - def get(self, cookie):
110 self.gc() 111 if cookie in self.sessions: 112 return self.sessions[cookie] 113 return None
114
115 - def remove(self, cookie):
116 if cookie in self.sessions: 117 del self.sessions[cookie]
118
119 - def getUser(self, user):
120 return self.users.get(user)
121