mirror of
https://github.com/yweber/lodel2.git
synced 2025-11-02 04:20:55 +01:00
Small RAM session handler implementation
This commit is contained in:
parent
51feb0654c
commit
60a1176728
2 changed files with 57 additions and 0 deletions
15
plugins/ram_sessions/__init__.py
Normal file
15
plugins/ram_sessions/__init__.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
from lodel.settings.validator import SettingValidator
|
||||
|
||||
__plugin_name__ = 'ram_session'
|
||||
__version__ = [0,0,1]
|
||||
__type__ = 'session_handler'
|
||||
__loader__ = 'main.py'
|
||||
__author__ = "Lodel2 dev team"
|
||||
__fullname__ = "RAM Session Store Plugin"
|
||||
|
||||
CONFSPEC = {
|
||||
'lodel2.sessions':{
|
||||
'expiration': (900, SettingValidator('int')),
|
||||
'token_size': (512, SettingValidator('int')),
|
||||
}
|
||||
}
|
||||
42
plugins/ram_sessions/main.py
Normal file
42
plugins/ram_sessions/main.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#-*- coding: utf-8 -*-
|
||||
import os
|
||||
import copy
|
||||
|
||||
from lodel.settings import Settings
|
||||
from lodel.auth.exceptions import *
|
||||
|
||||
__sessions = dict()
|
||||
|
||||
def _check_token(token):
|
||||
if len(token) != Settings.sessions.token_size:
|
||||
raise ClientAuthenticationFailure("Malformed session token")
|
||||
if token not in __sessions:
|
||||
raise ClientAuthenticationFailure("No session with this token")
|
||||
|
||||
def start_session():
|
||||
token = os.urandom(Settings.sessions.token_size)
|
||||
__sessions[token] = dict()
|
||||
return token
|
||||
|
||||
def destroy_session(token):
|
||||
_check_token(token)
|
||||
del(__sessions[token])
|
||||
|
||||
def restore_session(token):
|
||||
_check_token(token)
|
||||
return __sessions[token]
|
||||
|
||||
def save_session(token, datas):
|
||||
_check_token(token)
|
||||
__sessions[token] = copy.copy(datas)
|
||||
|
||||
|
||||
def get_value(token, name):
|
||||
_check_token(token)
|
||||
return __sessions[token][name]
|
||||
|
||||
def del_value(token, name):
|
||||
_check_token(token)
|
||||
if name in __sessions[token]:
|
||||
del(__sessions[token][name])
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue