|
|
|
@@ -9,9 +9,17 @@ from template import render |
|
|
|
from functools import partial |
|
|
|
|
|
|
|
## WSGIAuth |
|
|
|
import threading |
|
|
|
import re |
|
|
|
import Cookie |
|
|
|
import hashlib |
|
|
|
import hmac |
|
|
|
import os |
|
|
|
import random |
|
|
|
import time |
|
|
|
import cPickle |
|
|
|
|
|
|
|
|
|
|
|
md5 = lambda x : hashlib.md5( x ).hexdigest() |
|
|
|
sha1 = lambda key,value: hmac.new( key, value, hashlib.sha1 ).hexdigest() |
|
|
|
|
|
|
|
@@ -87,7 +95,6 @@ class WSGIMySQL( object ): |
|
|
|
|
|
|
|
self.__dict_cursor = MySQLdb.cursors.DictCursor |
|
|
|
|
|
|
|
|
|
|
|
def __newconn( self, alias ): |
|
|
|
import MySQLdb |
|
|
|
|
|
|
|
@@ -141,25 +148,103 @@ class WSGIMySQL( object ): |
|
|
|
class WSGISimpleAuth( object ): |
|
|
|
__metaclass__ = Singleton |
|
|
|
|
|
|
|
def __init__( self, secret_key, login_url=None, forbidden_url=None ): |
|
|
|
self.__secret_key = secret_key |
|
|
|
def __init__( self, timeout=900, auth_dir = 'auth_files', login_url=None, forbidden_url=None ): |
|
|
|
import os |
|
|
|
self.__authdir = os.path.normpath( os.path.join( os.path.split(__file__)[0], auth_dir ) ) |
|
|
|
self.__timeout = timeout |
|
|
|
self._lock = threading.RLock() |
|
|
|
|
|
|
|
def uuid( self ): |
|
|
|
"""Generate a unique session ID""" |
|
|
|
return hashlib.sha256( |
|
|
|
str(os.getpid()) |
|
|
|
+ str(time()) |
|
|
|
+ str(random.random()) |
|
|
|
).hexdigest() |
|
|
|
|
|
|
|
def auth( self, permission='', group='', p_g_mode='AND', p_mode='OR', g_mode='OR' ): |
|
|
|
def acquire_lock(self): |
|
|
|
self._lock.acquire() |
|
|
|
|
|
|
|
def release_lock(self): |
|
|
|
self._lock.release() |
|
|
|
|
|
|
|
|
|
|
|
def require( self, permission='', group='', p_g_mode='AND', p_mode='OR', g_mode='OR' ): |
|
|
|
def real_decorator( wsgi_application ): |
|
|
|
def wrapper( environ, start_response ): |
|
|
|
try: |
|
|
|
uuid = Cookie.SimpleCookie(environ["HTTP_COOKIE"])["uuid"].value |
|
|
|
except: |
|
|
|
uuid = None |
|
|
|
|
|
|
|
environ['auth.uuid'] = uuid |
|
|
|
#-------------------------------------------------------------- |
|
|
|
|
|
|
|
def my_start_response( status, response_headers ): |
|
|
|
# |
|
|
|
# aggiunge il cookie all'header |
|
|
|
# |
|
|
|
cookie = Cookie.SimpleCookie() |
|
|
|
cookie["uuid"] = uuid |
|
|
|
response_headers.append( ('Set-Cookie',cookie.OutputString()) ) |
|
|
|
response_headers.append( ('Set-Cookie',cookie.output()) ) |
|
|
|
|
|
|
|
# |
|
|
|
# salva le informazioni legate al cookie |
|
|
|
# |
|
|
|
storage['epoch_write'] = time.time() |
|
|
|
|
|
|
|
self.acquire_lock() ## LOCK |
|
|
|
|
|
|
|
f = open( path, 'w' ) |
|
|
|
try: |
|
|
|
cPickle.dump(storage, f) |
|
|
|
finally: |
|
|
|
f.close() |
|
|
|
|
|
|
|
self.release_lock() ## RELEASE |
|
|
|
|
|
|
|
# |
|
|
|
# start response originale |
|
|
|
# |
|
|
|
start_response( status, response_headers ); |
|
|
|
|
|
|
|
#-------------------------------------------------------------- |
|
|
|
# |
|
|
|
# recupera UUID dal cookie |
|
|
|
# |
|
|
|
try: |
|
|
|
uuid = Cookie.SimpleCookie(environ["HTTP_COOKIE"])["uuid"].value |
|
|
|
except: |
|
|
|
uuid = self.uuid() |
|
|
|
|
|
|
|
# |
|
|
|
# utilizza lo UUID per recuperare le informazioni (locali) ad esso legate |
|
|
|
# |
|
|
|
path = os.path.join( self.__authdir, uuid ) |
|
|
|
|
|
|
|
self.acquire_lock() ## LOCK |
|
|
|
|
|
|
|
f = open( path, 'r' ) |
|
|
|
|
|
|
|
try: |
|
|
|
storage = cPickle.load( f ) |
|
|
|
except: |
|
|
|
# UUID assente, crea una nuova struttura dati |
|
|
|
storage = { |
|
|
|
'epoch_created': time.time(), |
|
|
|
'permissions': [], |
|
|
|
'groups': [], |
|
|
|
} |
|
|
|
|
|
|
|
f.close() |
|
|
|
|
|
|
|
self.release_lock() ## RELEASE |
|
|
|
|
|
|
|
storage['epoch_read'] = time.time() |
|
|
|
|
|
|
|
# |
|
|
|
# popola environ |
|
|
|
# |
|
|
|
environ['auth.uuid'] = uuid |
|
|
|
environ['auth.storage'] = storage |
|
|
|
|
|
|
|
# |
|
|
|
# output dei contenuti generati |
|
|
|
# |
|
|
|
for item in wsgi_application( environ, my_start_response ): |
|
|
|
yield item |
|
|
|
|
|
|
|
@@ -168,4 +253,22 @@ class WSGISimpleAuth( object ): |
|
|
|
return real_decorator |
|
|
|
|
|
|
|
|
|
|
|
# EOF |
|
|
|
# EOF |
|
|
|
|
|
|
|
""" |
|
|
|
{ |
|
|
|
uuid: jkfghkjgdhfgkjlsk, |
|
|
|
permission=[], |
|
|
|
groups=[], |
|
|
|
timeout=3600 |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{ |
|
|
|
uuid: jkfghkjgdhfgkjlsk, |
|
|
|
permission=[], |
|
|
|
groups=[], |
|
|
|
timeout=3600 |
|
|
|
} |
|
|
|
""" |