2013-03-15 02:38:12 +00:00
|
|
|
import collections
|
2019-10-21 23:55:34 +00:00
|
|
|
import hashlib
|
2013-03-15 02:38:12 +00:00
|
|
|
import threading
|
2020-07-29 20:52:44 +00:00
|
|
|
|
|
|
|
from hydrus.core import HydrusExceptions
|
2020-04-22 21:00:35 +00:00
|
|
|
from hydrus.core import HydrusData
|
|
|
|
from hydrus.core import HydrusGlobals as HG
|
2013-03-15 02:38:12 +00:00
|
|
|
|
2013-12-04 22:44:16 +00:00
|
|
|
HYDRUS_SESSION_LIFETIME = 30 * 86400
|
2017-03-02 02:14:56 +00:00
|
|
|
|
2014-07-23 21:21:37 +00:00
|
|
|
class HydrusSessionManagerServer( object ):
|
2013-03-15 02:38:12 +00:00
|
|
|
|
|
|
|
def __init__( self ):
|
|
|
|
|
|
|
|
self._lock = threading.Lock()
|
|
|
|
|
2014-08-06 20:29:17 +00:00
|
|
|
self.RefreshAllAccounts()
|
|
|
|
|
2017-05-10 21:33:58 +00:00
|
|
|
HG.controller.sub( self, 'RefreshAccounts', 'update_session_accounts' )
|
|
|
|
HG.controller.sub( self, 'RefreshAllAccounts', 'update_all_session_accounts' )
|
2013-11-06 18:22:07 +00:00
|
|
|
|
2013-03-15 02:38:12 +00:00
|
|
|
|
2019-10-21 23:55:34 +00:00
|
|
|
def _GetAccountFromAccountKey( self, service_key, account_key ):
|
2013-10-30 22:28:06 +00:00
|
|
|
|
2019-10-21 23:55:34 +00:00
|
|
|
account_keys_to_accounts = self._service_keys_to_account_keys_to_accounts[ service_key ]
|
|
|
|
|
|
|
|
if account_key not in account_keys_to_accounts:
|
2013-11-06 18:22:07 +00:00
|
|
|
|
2019-10-21 23:55:34 +00:00
|
|
|
if HG.server_busy.locked():
|
|
|
|
|
|
|
|
raise HydrusExceptions.ServerBusyException( 'Sorry, server is busy and cannot fetch account data right now!' )
|
|
|
|
|
2013-10-30 22:28:06 +00:00
|
|
|
|
2019-10-21 23:55:34 +00:00
|
|
|
account = HG.controller.Read( 'account', service_key, account_key )
|
2016-07-27 21:53:34 +00:00
|
|
|
|
2019-10-21 23:55:34 +00:00
|
|
|
account_keys_to_accounts[ account_key ] = account
|
|
|
|
|
|
|
|
|
|
|
|
account = account_keys_to_accounts[ account_key ]
|
|
|
|
|
|
|
|
return account
|
|
|
|
|
|
|
|
|
|
|
|
def _GetAccountKeyFromAccessKey( self, service_key, access_key ):
|
|
|
|
|
|
|
|
hashed_access_key = hashlib.sha256( access_key ).digest()
|
|
|
|
|
|
|
|
if hashed_access_key not in self._service_keys_to_hashed_access_keys_to_account_keys[ service_key ]:
|
|
|
|
|
|
|
|
if HG.server_busy.locked():
|
2014-07-23 21:21:37 +00:00
|
|
|
|
2019-10-21 23:55:34 +00:00
|
|
|
raise HydrusExceptions.ServerBusyException( 'Sorry, server is busy and cannot fetch account key data right now!' )
|
2014-07-23 21:21:37 +00:00
|
|
|
|
|
|
|
|
2019-10-21 23:55:34 +00:00
|
|
|
account_key = HG.controller.Read( 'account_key_from_access_key', service_key, access_key )
|
|
|
|
|
|
|
|
self._service_keys_to_hashed_access_keys_to_account_keys[ service_key ][ hashed_access_key ] = account_key
|
|
|
|
|
|
|
|
|
|
|
|
account_key = self._service_keys_to_hashed_access_keys_to_account_keys[ service_key ][ hashed_access_key ]
|
|
|
|
|
|
|
|
return account_key
|
|
|
|
|
|
|
|
|
|
|
|
def AddSession( self, service_key, access_key ):
|
|
|
|
|
|
|
|
with self._lock:
|
|
|
|
|
|
|
|
account_key = self._GetAccountKeyFromAccessKey( service_key, access_key )
|
|
|
|
|
|
|
|
account = self._GetAccountFromAccountKey( service_key, account_key )
|
|
|
|
|
2015-07-01 22:02:07 +00:00
|
|
|
session_key = HydrusData.GenerateKey()
|
2013-10-30 22:28:06 +00:00
|
|
|
|
2015-03-25 22:04:19 +00:00
|
|
|
now = HydrusData.GetNow()
|
2013-11-06 18:22:07 +00:00
|
|
|
|
2014-10-01 22:58:32 +00:00
|
|
|
expires = now + HYDRUS_SESSION_LIFETIME
|
2013-11-06 18:22:07 +00:00
|
|
|
|
2017-05-10 21:33:58 +00:00
|
|
|
HG.controller.Write( 'session', session_key, service_key, account_key, expires )
|
2016-07-27 21:53:34 +00:00
|
|
|
|
|
|
|
self._service_keys_to_session_keys_to_sessions[ service_key ][ session_key ] = ( account_key, expires )
|
2013-11-06 18:22:07 +00:00
|
|
|
|
2017-05-31 21:50:53 +00:00
|
|
|
return ( session_key, expires )
|
|
|
|
|
2013-10-02 22:06:06 +00:00
|
|
|
|
|
|
|
|
2014-08-27 22:15:22 +00:00
|
|
|
def GetAccount( self, service_key, session_key ):
|
2013-03-15 02:38:12 +00:00
|
|
|
|
2013-10-02 22:06:06 +00:00
|
|
|
with self._lock:
|
|
|
|
|
2016-07-27 21:53:34 +00:00
|
|
|
session_keys_to_sessions = self._service_keys_to_session_keys_to_sessions[ service_key ]
|
2014-05-28 21:03:24 +00:00
|
|
|
|
2016-07-27 21:53:34 +00:00
|
|
|
if session_key in session_keys_to_sessions:
|
2013-11-06 18:22:07 +00:00
|
|
|
|
2016-07-27 21:53:34 +00:00
|
|
|
( account_key, expires ) = session_keys_to_sessions[ session_key ]
|
2013-11-06 18:22:07 +00:00
|
|
|
|
2016-07-27 21:53:34 +00:00
|
|
|
if HydrusData.TimeHasPassed( expires ):
|
|
|
|
|
|
|
|
del session_keys_to_sessions[ session_key ]
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
account = self._service_keys_to_account_keys_to_accounts[ service_key ][ account_key ]
|
|
|
|
|
|
|
|
return account
|
|
|
|
|
2013-11-06 18:22:07 +00:00
|
|
|
|
|
|
|
|
2015-05-06 20:26:18 +00:00
|
|
|
raise HydrusExceptions.SessionException( 'Did not find that session! Try again!' )
|
2013-10-02 22:06:06 +00:00
|
|
|
|
|
|
|
|
2013-11-06 18:22:07 +00:00
|
|
|
|
2019-09-25 21:34:18 +00:00
|
|
|
def GetAccountFromAccessKey( self, service_key, access_key ):
|
|
|
|
|
|
|
|
with self._lock:
|
|
|
|
|
2019-10-21 23:55:34 +00:00
|
|
|
account_key = self._GetAccountKeyFromAccessKey( service_key, access_key )
|
2019-09-25 21:34:18 +00:00
|
|
|
|
2019-10-21 23:55:34 +00:00
|
|
|
account = self._GetAccountFromAccountKey( service_key, account_key )
|
2019-09-25 21:34:18 +00:00
|
|
|
|
|
|
|
return account
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-03-02 02:14:56 +00:00
|
|
|
def GetDirtyAccounts( self ):
|
|
|
|
|
|
|
|
with self._lock:
|
|
|
|
|
|
|
|
service_keys_to_dirty_accounts = {}
|
|
|
|
|
2019-10-21 23:55:34 +00:00
|
|
|
for ( service_key, account_keys_to_accounts ) in self._service_keys_to_account_keys_to_accounts.items():
|
2017-03-02 02:14:56 +00:00
|
|
|
|
2019-10-21 23:55:34 +00:00
|
|
|
dirty_accounts = [ account_key for account_key in account_keys_to_accounts.values() if account_key.IsDirty() ]
|
2017-03-02 02:14:56 +00:00
|
|
|
|
|
|
|
if len( dirty_accounts ) > 0:
|
|
|
|
|
|
|
|
service_keys_to_dirty_accounts[ service_key ] = dirty_accounts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return service_keys_to_dirty_accounts
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-27 21:53:34 +00:00
|
|
|
def RefreshAccounts( self, service_key, account_keys = None ):
|
2013-11-06 18:22:07 +00:00
|
|
|
|
|
|
|
with self._lock:
|
|
|
|
|
2016-07-27 21:53:34 +00:00
|
|
|
account_keys_to_accounts = self._service_keys_to_account_keys_to_accounts[ service_key ]
|
|
|
|
|
|
|
|
if account_keys is None:
|
|
|
|
|
2019-10-21 23:55:34 +00:00
|
|
|
account_keys = list( account_keys_to_accounts.keys() )
|
2016-07-27 21:53:34 +00:00
|
|
|
|
|
|
|
|
2014-10-01 22:58:32 +00:00
|
|
|
for account_key in account_keys:
|
2013-11-06 18:22:07 +00:00
|
|
|
|
2017-05-10 21:33:58 +00:00
|
|
|
account = HG.controller.Read( 'account', service_key, account_key )
|
2013-11-06 18:22:07 +00:00
|
|
|
|
2016-07-27 21:53:34 +00:00
|
|
|
account_keys_to_accounts[ account_key ] = account
|
2013-11-06 18:22:07 +00:00
|
|
|
|
|
|
|
|
2013-03-15 02:38:12 +00:00
|
|
|
|
|
|
|
|
2017-03-02 02:14:56 +00:00
|
|
|
def RefreshAllAccounts( self, service_key = None ):
|
2013-11-06 18:22:07 +00:00
|
|
|
|
2014-08-06 20:29:17 +00:00
|
|
|
with self._lock:
|
2013-11-06 18:22:07 +00:00
|
|
|
|
2017-03-02 02:14:56 +00:00
|
|
|
if service_key is None:
|
|
|
|
|
|
|
|
self._service_keys_to_session_keys_to_sessions = collections.defaultdict( dict )
|
|
|
|
|
|
|
|
self._service_keys_to_account_keys_to_accounts = collections.defaultdict( dict )
|
|
|
|
|
2019-10-21 23:55:34 +00:00
|
|
|
self._service_keys_to_hashed_access_keys_to_account_keys = collections.defaultdict( dict )
|
|
|
|
|
2017-05-10 21:33:58 +00:00
|
|
|
existing_sessions = HG.controller.Read( 'sessions' )
|
2017-03-02 02:14:56 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
del self._service_keys_to_session_keys_to_sessions[ service_key ]
|
|
|
|
|
|
|
|
del self._service_keys_to_account_keys_to_accounts[ service_key ]
|
|
|
|
|
2019-10-21 23:55:34 +00:00
|
|
|
del self._service_keys_to_hashed_access_keys_to_account_keys[ service_key ]
|
|
|
|
|
2017-05-10 21:33:58 +00:00
|
|
|
existing_sessions = HG.controller.Read( 'sessions', service_key )
|
2017-03-02 02:14:56 +00:00
|
|
|
|
2014-09-10 22:37:38 +00:00
|
|
|
|
2019-10-21 23:55:34 +00:00
|
|
|
for ( session_key, service_key, account, hashed_access_key, expires ) in existing_sessions:
|
2014-10-01 22:58:32 +00:00
|
|
|
|
|
|
|
account_key = account.GetAccountKey()
|
2014-08-06 20:29:17 +00:00
|
|
|
|
2016-07-27 21:53:34 +00:00
|
|
|
self._service_keys_to_session_keys_to_sessions[ service_key ][ session_key ] = ( account_key, expires )
|
2014-08-06 20:29:17 +00:00
|
|
|
|
2019-10-21 23:55:34 +00:00
|
|
|
if account_key not in self._service_keys_to_account_keys_to_accounts:
|
|
|
|
|
|
|
|
self._service_keys_to_account_keys_to_accounts[ service_key ][ account_key ] = account
|
|
|
|
|
|
|
|
|
|
|
|
if hashed_access_key not in self._service_keys_to_hashed_access_keys_to_account_keys:
|
|
|
|
|
|
|
|
self._service_keys_to_hashed_access_keys_to_account_keys[ service_key ][ hashed_access_key ] = account_key
|
|
|
|
|
2014-08-06 20:29:17 +00:00
|
|
|
|
2013-11-06 18:22:07 +00:00
|
|
|
|
|
|
|
|
2017-03-02 02:14:56 +00:00
|
|
|
|
|
|
|
def UpdateAccounts( self, service_key, accounts ):
|
|
|
|
|
|
|
|
with self._lock:
|
|
|
|
|
|
|
|
account_keys_to_accounts = self._service_keys_to_account_keys_to_accounts[ service_key ]
|
|
|
|
|
|
|
|
for account in accounts:
|
|
|
|
|
|
|
|
account_keys_to_accounts[ account.GetAccountKey() ] = account
|
|
|
|
|
|
|
|
|
|
|
|
|