/usr/share/pyshared/pesto/session/base.py is in python-pesto 25-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 | # Copyright (c) 2007-2010 Oliver Cope. All rights reserved.
# See LICENSE.txt for terms of redistribution and use.
"""
Web session management.
"""
__docformat__ = 'restructuredtext en'
__all__ = ['session_middleware']
import logging
import os
import random
import re
import threading
from time import sleep, time
from pesto.request import Request
from pesto.response import Response
from pesto.cookie import Cookie
from pesto.wsgiutils import ClosingIterator
def get_session_id_from_querystring(environ):
"""
Return the session from the query string or None if no session can be read.
"""
pattern = re.escape(environ['pesto.sessionmanager'].COOKIE_NAME) + '=([0-9a-z]{%d})' % ID_LENGTH
try:
return re.search(pattern, environ.get("QUERY_STRING", "")).group(1)
except AttributeError:
return None
def get_session_id_from_cookie(environ):
"""
Return the session from a cookie or None if no session can be read
"""
cookie = Request(environ).cookies.get(environ['pesto.sessionmanager'].COOKIE_NAME)
if cookie and is_valid_id(cookie.value):
return cookie.value
return None
try:
import hashlib
ID_LENGTH = hashlib.sha256().digest_size * 2
def generate_id():
"""Generate a unique session ID"""
return hashlib.sha256(
str(os.getpid())
+ str(time())
+ str(random.random())
).hexdigest()
except ImportError:
import sha
ID_LENGTH = 40
def generate_id():
"""Generate a unique session ID"""
return sha.new(
str(os.getpid())
+ str(time())
+ str(random.random())
).hexdigest()
def is_valid_id(session_id, pattern=re.compile('^[a-f0-9]{%d}$' % ID_LENGTH)):
"""
Return True if ``session_id`` is a well formed session id. This must be
a hex string as produced by hashlib objects' ``hexdigest`` method.
Synopsis::
>>> is_valid_id('a' * ID_LENGTH)
True
>>> is_valid_id('z' * ID_LENGTH)
False
>>> is_valid_id('a' * (ID_LENGTH - 1))
False
"""
try:
return pattern.match(session_id) is not None
except TypeError:
return False
class Session(object):
"""
Session objects store information about the http sessions
"""
# Indicates whether the session is newly created (ie within the current request)
is_new = True
def __init__(self, session_manager, session_id, is_new, data=None):
"""
Create a new session object within the given session manager.
"""
self.session_manager = session_manager
self._changed = False
self.session_id = session_id
self.is_new = is_new
self.data = {}
if data is not None:
self.data.update(data)
def save_if_changed(self):
"""
Save the session in the underlying storage mechanism if the session is
new or if it has been changed since being loaded.
Note that this will only detect changes to the session object itself.
If you store a mutable object within the session and change that
then you must explicity call ``request.session.save`` to ensure
your change is saved.
Return
``True`` if the session was saved, or ``False`` if it
was not necessary to save the session.
"""
if self._changed or self.is_new:
self.save()
self._changed = False
self.is_new = False
return True
return False
def save(self):
"""
Saves the session to the underlying storage mechanism.
"""
self.session_manager.store(self)
def setdefault(self, key, value=None):
self._changed = True
return self.data.setdefault(key, value)
def pop(self, key, default):
self._changed = True
return self.data.pop(key, default)
def popitem(self):
self._changed = True
return self.data.popitem()
def clear(self):
self._changed = True
return self.data.clear()
def has_key(self, key):
return self.data.has_key(key)
def items(self):
return self.data.items()
def iteritems(self):
return self.data.iteritems()
def iterkeys(self):
return self.data.iterkeys()
def itervalues(self):
return self.data.itervalues()
def update(self, other, **kwargs):
self._changed = True
return self.data.update(other, **kwargs)
def values(self):
return self.data.values()
def get(self, key, default=None):
return self.data.get(key, default)
def __getitem__(self, key):
return self.data[key]
def __iter__(self):
return self.data.__iter__()
def invalidate(self):
"""
invalidate and remove this session from the sessionmanager
"""
self.session_manager.remove(self.session_id)
self.session_id = None
def __setitem__(self, key, val):
self._changed = True
return self.data.__setitem__(key, val)
def __delitem__(self, key):
self._changed = True
return self.data.__delitem__(key)
def text(self):
"""
Return a useful text representation of the session
"""
import pprint
return "<%s id=%s, is_new=%s\n%s\n>" % (
self.__class__.__name__, self.session_id, self.is_new,
pprint.pformat(self)
)
class SessionManagerBase(object):
"""
Manages Session objects using an ObjectStore to persist the
sessions.
"""
# Which version of the pickling protocol to select.
PICKLE_PROTOCOL = -1
# Key to use in HTTP cookies
COOKIE_NAME = "pesto_session"
def load(self, session_id):
"""
Load a session object from this sessionmanager.
Note that if ``session_id`` cannot be found in the underlying storage,
a new session id will be created.
"""
self.acquire_lock(session_id)
try:
data = self._get_session_data(session_id)
if data is None:
# Generate a fresh session with a new id
session = Session(self, generate_id(), is_new=True, data=data)
else:
session = Session(self, session_id, is_new=False, data=data)
self.update_access_time(session.session_id)
return session
finally:
self.release_lock(session_id)
def update_access_time(self, session_id):
raise NotImplementedError
def get_access_time(self, session_id):
raise NotImplementedError
def acquire_lock(self, session_id=None):
"""
Acquire a lock for the given session_id.
If session_id is none, then the whole storage should be locked.
"""
raise NotImplementedError
def release_lock(self, session_id=None):
raise NotImplementedError
def read_session(self, session_id):
"""
Return a session object from the given ``session_id``. If
``session_id`` is None a new session will be generated.
Synopsis::
>>> from pesto.session.memorysessionmanager import MemorySessionManager
>>> sm = MemorySessionManager()
>>> sm.read_session(None) #doctest: +ELLIPSIS
<pesto.session.base.Session object at 0x...>
"""
if session_id is not None:
session = self.load(session_id)
else:
session = Session(self, generate_id(), is_new=True)
self.update_access_time(session.session_id)
return session
def __contains__(self, session_id):
"""
Return true if the given session id exists in this sessionmanager
"""
raise NotImplementedError
def store(self, session):
"""
Save the given session object in this sessionmanager.
"""
self.acquire_lock(session.session_id)
try:
self._store(session)
finally:
self.release_lock(session.session_id)
def _store(self, session):
"""
Write session data to the underlying storage.
Subclasses must implement this method
"""
def remove(self, session_id):
"""
Remove the specified session from the session manager.
"""
self.acquire_lock(session_id)
try:
self._remove(session_id)
finally:
self.release_lock(session_id)
def _remove(self, session_id):
"""
Remove the specified session from the underlying storage.
Subclasses must implement this method
"""
raise NotImplementedError
def _get_session_data(self, session_id):
"""
Return a dict of the session data from the underlying storage, or
``None`` if the session does not exist.
"""
raise NotImplementedError
def close(self):
"""
Close the persistent store cleanly.
"""
self.acquire_lock()
try:
self._close()
finally:
self.release_lock()
def _close(self):
"""
Default implementation: do nothing
"""
def purge(self, olderthan=1800):
for session_id in self._purge_candidates(olderthan):
self.remove(session_id)
def _purge_candidates(self, olderthan=1800):
"""
Return a list of session ids ready to be purged from the session
manager.
"""
raise NotImplementedError
class ThreadsafeSessionManagerBase(SessionManagerBase):
"""
Base class for sessioning to run in a threaded environment.
DOES NOT GUARANTEE PROCESS-LEVEL SAFETY!
"""
def __init__(self):
super(ThreadsafeSessionManagerBase, self).__init__()
self._access_times = {}
self._lock = threading.RLock()
def _purge_candidates(self, olderthan=1800):
"""
Purge all sessions older than ``olderthan`` seconds.
"""
# Re-importing time fixes exception raised on interpreter shutdown
from time import time
expiry = time() - olderthan
self.acquire_lock()
try:
return [
id for id, access_time in self._access_times.iteritems() if access_time < expiry
]
finally:
self.release_lock()
def acquire_lock(self, session_id=None):
self._lock.acquire()
def release_lock(self, session_id=None):
self._lock.release()
def update_access_time(self, session_id):
self.acquire_lock()
try:
self._access_times[session_id] = time()
finally:
self.release_lock()
def get_access_time(self, session_id):
"""
Return the time the given session_id was last accessed
"""
return self._access_times[session_id]
def _remove(self, session_id):
"""
Subclasses should call this implementation to ensure the access_times
dictionary is kept up to date.
"""
try:
del self._access_times[session_id]
except KeyError:
logging.warn("tried to remove non-existant session id %r", session_id)
def __contains__(self, session_id):
return session_id in self._access_times
def start_thread_purger(sessionmanager, howoften=60, olderthan=1800, lock=threading.Lock()):
"""
Start a thread to purge sessions older than ``olderthan`` seconds every
``howoften`` seconds.
"""
def _purge():
while True:
sleep(howoften)
sessionmanager.purge(olderthan)
lock.acquire()
try:
if hasattr(sessionmanager, '_purge_thread'):
# Don't start the thread twice
return
sessionmanager._purge_thread = threading.Thread(target=_purge)
sessionmanager._purge_thread.setDaemon(True)
sessionmanager._purge_thread.start()
finally:
lock.release()
def session_middleware(
session_manager,
auto_purge_every=60,
auto_purge_olderthan=1800,
persist='cookie',
cookie_path=None,
cookie_domain=None
):
"""
WSGI middleware application for sessioning.
Synopsis::
>>> from pesto.session.memorysessionmanager import MemorySessionManager
>>> def my_wsgi_app(environ, start_response):
... session = environ['pesto.session']
...
>>> app = session_middleware(MemorySessionManager())(my_wsgi_app)
>>>
session_manager
An implementation of ``pesto.session.base.SessionManagerBase``
auto_purge_every
If non-zero, a separate thread will be launched which will purge
expired sessions every ``auto_purge_every`` seconds. In a CGI
environment (or equivalent, detected via, ``environ['wsgi.run_once']``)
the session manager will be purged after every request.
auto_purge_olderthan
Auto purge sessions older than ``auto_purge_olderthan`` seconds.
persist
Either ``cookie`` or ``querystring``. If set to ``cookie`` then
sessions will be automatically persisted via a session cookie.
If ``querystring`` then the session-id will be read from the
querystring. However it is up to the underlying application to ensure
that the session-id is embedded into all links generated by the
application.
cookie_path
The path to use when setting cookies. If ``None`` this will be taken
from the ``SCRIPT_NAME`` variable.
cookie_domain
The domain to use when setting cookies. If ``None`` this will not be
set and the browser will default to the domain used for the request.
"""
get_session_id = {
'cookie': get_session_id_from_cookie,
'querystring': get_session_id_from_querystring,
}[persist]
def middleware(app):
def sessionmanager_middleware(environ, start_response):
if environ['wsgi.run_once'] and auto_purge_every > 0:
session_manager.purge(auto_purge_olderthan)
environ['pesto.sessionmanager'] = session_manager
session = session_manager.read_session(get_session_id(environ))
environ['pesto.session'] = session
my_start_response = start_response
if persist == 'cookie' and session.is_new:
def my_start_response(status, headers, exc_info=None):
_cookie_path = cookie_path
if _cookie_path is None:
_cookie_path = environ.get('SCRIPT_NAME')
if not _cookie_path:
_cookie_path = '/'
cookie = Cookie(
session_manager.COOKIE_NAME,
session.session_id,
path=_cookie_path,
domain=cookie_domain,
http_only=True
)
return start_response(
status,
list(headers) + [("Set-Cookie", str(cookie))],
exc_info
)
elif persist == 'querystring':
request = Request(environ)
if session.is_new and environ['REQUEST_METHOD'] == 'GET':
query_session_id = request.query.get(session_manager.COOKIE_NAME)
if query_session_id != session.session_id:
new_query = [
item for item in request.query.iterallitems()
if item[0] != session_manager.COOKIE_NAME
]
new_query.append((session_manager.COOKIE_NAME, session.session_id))
return ClosingIterator(
Response.redirect(
request.make_uri(query=new_query)
)(environ, my_start_response),
session.save_if_changed
)
return ClosingIterator(app(environ, my_start_response), session.save_if_changed)
if auto_purge_every > 0:
start_thread_purger(
session_manager,
howoften = auto_purge_every,
olderthan = auto_purge_olderthan
)
return sessionmanager_middleware
return middleware
|