hydrus/include/HydrusSessions.py

111 lines
3.4 KiB
Python
Raw Normal View History

2013-03-15 02:38:12 +00:00
import collections
import httplib
import HydrusConstants as HC
2013-07-24 20:26:00 +00:00
import HydrusExceptions
2013-03-15 02:38:12 +00:00
import os
import Queue
import re
import sqlite3
import sys
import threading
import time
import traceback
import wx
import yaml
class HydrusSessionManagerClient():
def __init__( self ):
2013-07-10 20:25:57 +00:00
existing_sessions = HC.app.Read( 'hydrus_sessions' )
2013-03-15 02:38:12 +00:00
self._sessions = { service_identifier : ( session_key, expiry ) for ( service_identifier, session_key, expiry ) in existing_sessions }
self._lock = threading.Lock()
def DeleteSessionKey( self, service_identifier ):
with self._lock:
2013-07-10 20:25:57 +00:00
HC.app.Write( 'delete_hydrus_session_key', service_identifier )
2013-03-15 02:38:12 +00:00
del self._sessions[ service_identifier ]
def GetSessionKey( self, service_identifier ):
2013-07-31 21:26:38 +00:00
now = HC.GetNow()
2013-03-15 02:38:12 +00:00
with self._lock:
if service_identifier in self._sessions:
( session_key, expiry ) = self._sessions[ service_identifier ]
if now + 600 > expiry: del self._sessions[ service_identifier ]
else: return session_key
# session key expired or not found
2013-07-10 20:25:57 +00:00
service = HC.app.Read( 'service', service_identifier )
2013-03-15 02:38:12 +00:00
connection = service.GetConnection()
connection.Get( 'session_key' )
cookies = connection.GetCookies()
try: session_key = cookies[ 'session_key' ].decode( 'hex' )
except: raise Exception( 'Service did not return a session key!' )
expiry = now + 30 * 86400
self._sessions[ service_identifier ] = ( session_key, expiry )
2013-07-10 20:25:57 +00:00
HC.app.Write( 'hydrus_session', service_identifier, session_key, expiry )
2013-03-15 02:38:12 +00:00
return session_key
class HydrusSessionManagerServer():
def __init__( self ):
2013-07-10 20:25:57 +00:00
existing_sessions = HC.app.GetDB().Read( 'sessions', HC.HIGH_PRIORITY )
2013-03-15 02:38:12 +00:00
self._sessions = { ( session_key, service_identifier ) : ( account_identifier, expiry ) for ( session_key, service_identifier, account_identifier, expiry ) in existing_sessions }
self._lock = threading.Lock()
def AddSession( self, session_key, service_identifier, account_identifier, expiry ):
2013-07-10 20:25:57 +00:00
HC.app.GetDB().Write( 'session', HC.HIGH_PRIORITY, session_key, service_identifier, account_identifier, expiry )
2013-03-15 02:38:12 +00:00
self._sessions[ ( session_key, service_identifier ) ] = ( account_identifier, expiry )
def GetAccountIdentifier( self, session_key, service_identifier ):
2013-07-31 21:26:38 +00:00
now = HC.GetNow()
2013-03-15 02:38:12 +00:00
with self._lock:
if ( session_key, service_identifier ) in self._sessions:
( account_identifier, expiry ) = self._sessions[ ( session_key, service_identifier ) ]
if now > expiry: del self._sessions[ ( session_key, service_identifier ) ]
else: return account_identifier
# session not found, or expired
2013-07-24 20:26:00 +00:00
raise HydrusExceptions.SessionException()
2013-03-15 02:38:12 +00:00