aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpzread <netfirewall@gmail.com>2013-06-10 16:49:48 +0800
committerpzread <netfirewall@gmail.com>2013-06-10 16:49:48 +0800
commit365cbfa78aeba35035b503b55e9cba5373cc0b1f (patch)
tree295d6cd97692b8f18ec720f9049150835504de8f
parente66d8fe42710430f521bdc7ce007f5cb23a81883 (diff)
downloadtaiwan-online-judge-365cbfa78aeba35035b503b55e9cba5373cc0b1f.tar.gz
taiwan-online-judge-365cbfa78aeba35035b503b55e9cba5373cc0b1f.tar.zst
taiwan-online-judge-365cbfa78aeba35035b503b55e9cba5373cc0b1f.zip
Replace linkid by link. Use idendesc in current auth environment
-rwxr-xr-x[-rw-r--r--]src/py/backend_server.py127
-rwxr-xr-x[-rw-r--r--]src/py/center_server.py126
-rw-r--r--src/py/imc/async.py14
-rw-r--r--src/py/imc/auth.py41
-rwxr-xr-xsrc/py/imc/proxy.py246
-rw-r--r--src/py/netio.py8
-rwxr-xr-x[-rw-r--r--]src/py/tojauth.py27
-rwxr-xr-xsrc/py/user.py31
8 files changed, 303 insertions, 317 deletions
diff --git a/src/py/backend_server.py b/src/py/backend_server.py
index 7535183..8d9d169 100644..100755
--- a/src/py/backend_server.py
+++ b/src/py/backend_server.py
@@ -29,11 +29,11 @@ class BackendWorker(tornado.tcpserver.TCPServer):
self.sock_addr = None
self.ws_port = ws_port
- self._linkid = None
+ self._link = None
self._idendesc = None
- self._pend_mainconn_linkidmap = {}
+ self._pend_mainconn_linkmap = {}
self._pend_filestream_filekeymap = {}
- self._client_linkidmap = {}
+ self._client_linkmap = {}
def start(self):
sock_port = random.randrange(4096,8192)
@@ -61,21 +61,17 @@ class BackendWorker(tornado.tcpserver.TCPServer):
netio.recv_pack(sock_stream,_recv_conn_info)
- def add_client(self,linkid,handler):
- self._client_linkidmap[linkid] = {}
+ def add_client(self,link,handler):
+ self._client_linkmap[link] = {}
- conn = netio.WebSocketConnection('client',linkid,handler)
- conn.add_close_callback(lambda conn : self.del_client(conn.linkid))
+ conn = netio.WebSocketConnection('client',link,handler)
+ conn.add_close_callback(lambda conn : self.del_client(conn.link))
Proxy.instance.add_conn(conn)
- #imc_call_async(self._idendesc,'/center/' + self.center_conn.linkid + '/','add_client',{'backend_linkid':self._linkid,'client_linkid':linkid})
-
return conn
- def del_client(self,linkid):
- del self._client_linkidmap[linkid]
-
- #imc_call_async(self._idendesc,'/center/' + self.center_conn.linkid + '/','del_client',linkid)
+ def del_client(self,link):
+ del self._client_linkmap[link]
def _conn_center(self):
def __retry(conn):
@@ -87,25 +83,23 @@ class BackendWorker(tornado.tcpserver.TCPServer):
def __send_worker_info():
def ___recv_info_cb(data):
info = json.loads(data.decode('utf-8'))
-
pubkey = open('pubkey.pem','r').read()
TOJAuth(pubkey)
self._idendesc = info['idendesc']
- iden = TOJAuth.instance.get_iden('backend',self._linkid,self._idendesc)
- self._linkid = iden['linkid']
- Proxy('backend',self._linkid,TOJAuth.instance,self._idendesc,self._conn_linkid)
+ self._link = info['worker_link']
+ Proxy(self._link,TOJAuth.instance,self._idendesc,self._conn_link)
- self.center_conn = SocketConnection('center',info['center_linkid'],stream,self.center_addr)
+ self.center_conn = SocketConnection(info['center_link'],stream,self.center_addr)
self.center_conn.add_close_callback(__retry)
Proxy.instance.add_conn(self.center_conn)
imc_register_call('','test_dst',self._test_dst)
#imc_register_call('','test_dsta',self._test_dsta)
- #time.sleep(2)
+ time.sleep(1)
- if int(self._linkid) == 2:
- self._test_call('9')
+ #if self._link == '/backend/2/':
+ self._test_call(None)
sock_ip,sock_port = self.sock_addr
netio.send_pack(stream,bytes(json.dumps({
@@ -121,10 +115,10 @@ class BackendWorker(tornado.tcpserver.TCPServer):
stream.set_close_callback(__retry)
stream.connect(self.center_addr,__send_worker_info)
- def _conn_linkid(self,linkid):
+ def _conn_link(self,link):
def __handle_pend(conn):
try:
- retids = self._pend_mainconn_linkidmap.pop(worker_linkid)
+ retids = self._pend_mainconn_linkmap.pop(worker_link)
except KeyError:
return
@@ -133,7 +127,7 @@ class BackendWorker(tornado.tcpserver.TCPServer):
imc.async.ret(retid,conn)
def __conn_cb():
- conn = Proxy.instance.get_conn(worker_linkid)
+ conn = Proxy.instance.get_conn(worker_link)
if conn != None:
__handle_pend(conn)
main_stream.set_close_callback(None)
@@ -143,8 +137,7 @@ class BackendWorker(tornado.tcpserver.TCPServer):
sock_ip,sock_port = self.sock_addr
netio.send_pack(main_stream,bytes(json.dumps({
'conntype':'main',
- 'linkclass':'backend',
- 'linkid':self._linkid,
+ 'link':self._link,
'sock_ip':sock_ip,
'sock_port':sock_port
}),'utf-8'))
@@ -153,7 +146,7 @@ class BackendWorker(tornado.tcpserver.TCPServer):
def __recv_cb(data):
stat = json.loads(data.decode('utf-8'))
if stat == True:
- conn = SocketConnection(worker_linkclass,worker_linkid,main_stream,sock_addr,self._add_pend_filestream)
+ conn = SocketConnection(worker_link,main_stream,sock_addr,self._add_pend_filestream)
Proxy.instance.add_conn(conn)
__handle_pend(conn)
@@ -164,25 +157,25 @@ class BackendWorker(tornado.tcpserver.TCPServer):
if self.center_conn == None:
return None
- stat,ret = imc_call(self._idendesc,'/center/' + self.center_conn.linkid + '/','lookup_linkid',linkid)
+ with TOJAuth.change_current_iden(self._idendesc):
+ stat,ret = Proxy.instance.call(self.center_conn.link,'lookup_link',65536,link)
if stat == False or ret == None:
return None
else:
- worker_linkclass = ret['worker_linkclass']
- worker_linkid = ret['worker_linkid']
+ worker_link = ret['worker_link']
- conn = Proxy.instance.get_conn(worker_linkid)
+ conn = Proxy.instance.get_conn(worker_link)
if conn != None:
return conn
- elif worker_linkid in self._pend_mainconn_linkidmap:
- self._pend_mainconn_linkidmap[worker_linkid].append(imc.async.get_retid())
+ elif worker_link in self._pend_mainconn_linkmap:
+ self._pend_mainconn_linkmap[worker_link].append(imc.async.get_retid())
return imc.async.switch_top()
else:
- self._pend_mainconn_linkidmap[worker_linkid] = [imc.async.get_retid()]
+ self._pend_mainconn_linkmap[worker_link] = [imc.async.get_retid()]
sock_addr = (ret['sock_ip'],ret['sock_port'])
@@ -196,23 +189,22 @@ class BackendWorker(tornado.tcpserver.TCPServer):
self._pend_filestream_filekeymap[filekey] = tornado.stack_context.wrap(callback)
def _handle_mainconn(self,main_stream,addr,info):
- linkclass = info['linkclass']
- linkid = info['linkid']
+ link = info['link']
sock_ip = info['sock_ip']
sock_port = info['sock_port']
- conn = Proxy.instance.get_conn(linkid)
+ conn = Proxy.instance.get_conn(link)
if conn != None:
return
- if (linkid not in self._pend_mainconn_linkidmap) or self._linkid > linkid:
- conn = SocketConnection(linkclass,linkid,main_stream,(sock_ip,sock_port),self._add_pend_filestream)
+ if (link not in self._pend_mainconn_linkmap) or self._link > link:
+ conn = SocketConnection(link,main_stream,(sock_ip,sock_port),self._add_pend_filestream)
Proxy.instance.add_conn(conn)
netio.send_pack(main_stream,bytes(json.dumps(True),'utf-8'))
- if linkid in self._pend_mainconn_linkidmap:
- retids = self._pend_mainconn_linkidmap.pop(linkid)
+ if link in self._pend_mainconn_linkmap:
+ retids = self._pend_mainconn_linkmap.pop(link)
for retid in retids:
imc.async.ret(retid,conn)
@@ -228,18 +220,28 @@ class BackendWorker(tornado.tcpserver.TCPServer):
@imc.async.caller
def _test_call(self,param):
- dst = '/backend/' + '3' + '/'
- ret = imc_call_async(self._idendesc,dst,'test_dst',lambda result : print(result),'test',113)
- print(ret)
+ with TOJAuth.change_current_iden(self._idendesc):
+ for i in range(0,1024):
+ dst = '/backend/' + str((i % 8) + 2) + '/'
+ if dst == self._link:
+ continue
+
+ fileres = Proxy.instance.sendfile(dst,'test.py')
+ ret = imc_call(dst,'test_dst',fileres.filekey)
+ print(fileres.wait())
- ret = imc_call(self._idendesc,'/center/1/','create_iden','client','1234',1221,TOJAuth.ROLETYPE_USER,{'uid':31})
- print(ret)
+ print(self._link)
+
+ #imc_call_async(dst,'test_dst',lambda result : print(result),'test',113)
+
+ #ret = imc_call('/center/1/','create_iden','client','1234',1221,TOJAuth.ROLETYPE_USER,{'uid':31})
+ #print(ret)
return
pend = []
for i in range(0,32):
- if str((i % 16) + 2) == self._linkid:
+ if str((i % 16) + 2) == self._link:
continue
fileres = Proxy.instance.sendfile('/backend/' + str((i % 16) + 2) + '/','Fedora-18-x86_64-DVD.iso')
@@ -250,22 +252,15 @@ class BackendWorker(tornado.tcpserver.TCPServer):
pend.append(fileres)
for p in pend:
- print(self._linkid + ' ' + p.wait())
+ print(self._link + ' ' + p.wait())
- print(self._linkid)
+ print(self._link)
@imc.async.caller
- def _test_dst(self,param,sdfsdf):
- #stat,ret = imc_call(self._idendesc,'/backend/' + self._linkid + '/','test_dsta',param)
- #return ret + ' Too'
-
- print(param)
- print(sdfsdf)
- print(TOJAuth.get_current_iden())
+ def _test_dst(self,filekey):
+ #print(filekey)
- #Proxy.instance.rejectfile(param)
- #print('recv ' + iden['linkid'] + ' > ' + self._linkid)
- #fileres = Proxy.instance.recvfile(param,'data')
+ fileres = Proxy.instance.recvfile(filekey,'data')
#print('recv ' + fileres.wait())
return 'ok'
@@ -287,7 +282,7 @@ class WebSocketConnHandler(tornado.websocket.WebSocketHandler):
else:
try:
info = json.loads(msg)
- self.backend_conn = backend_worker.add_client(info['client_linkid'],self)
+ self.backend_conn = backend_worker.add_client(info['client_link'],self)
except Exception:
self.close()
@@ -316,12 +311,12 @@ if __name__ == '__main__':
worker_list.append(Process(target = start_backend_worker,args = (81, )))
worker_list.append(Process(target = start_backend_worker,args = (82, )))
- #worker_list.append(Process(target = start_backend_worker,args = (181, )))
- #worker_list.append(Process(target = start_backend_worker,args = (182, )))
- #worker_list.append(Process(target = start_backend_worker,args = (183, )))
- #worker_list.append(Process(target = start_backend_worker,args = (184, )))
- #worker_list.append(Process(target = start_backend_worker,args = (185, )))
- #worker_list.append(Process(target = start_backend_worker,args = (186, )))
+ worker_list.append(Process(target = start_backend_worker,args = (181, )))
+ worker_list.append(Process(target = start_backend_worker,args = (182, )))
+ worker_list.append(Process(target = start_backend_worker,args = (183, )))
+ worker_list.append(Process(target = start_backend_worker,args = (184, )))
+ worker_list.append(Process(target = start_backend_worker,args = (185, )))
+ worker_list.append(Process(target = start_backend_worker,args = (186, )))
for proc in worker_list:
proc.start()
diff --git a/src/py/center_server.py b/src/py/center_server.py
index f54a57c..1164b08 100644..100755
--- a/src/py/center_server.py
+++ b/src/py/center_server.py
@@ -18,19 +18,19 @@ from netio import SocketStream,SocketConnection
from tojauth import TOJAuth
class Worker:
- def __init__(self,main_stream,linkclass,linkid,idendesc,worker_info,center_linkid):
+ def __init__(self,main_stream,link,idendesc,worker_info,center_link):
self.main_stream = main_stream
- self.linkclass = linkclass
- self.linkid = linkid
+ self.link = link
self.idendesc = idendesc
self.sock_addr = (worker_info['sock_ip'],worker_info['sock_port'])
netio.send_pack(self.main_stream,bytes(json.dumps({
'idendesc':self.idendesc,
- 'center_linkid':center_linkid
+ 'worker_link':self.link,
+ 'center_link':center_link
}),'utf-8'))
- conn = SocketConnection(self.linkclass,self.linkid,self.main_stream,self.sock_addr)
+ conn = SocketConnection(self.link,self.main_stream,self.sock_addr)
conn.add_close_callback(lambda conn : self.close())
Proxy.instance.add_conn(conn)
@@ -38,10 +38,10 @@ class Worker:
pass
class BackendWorker(Worker):
- def __init__(self,main_stream,linkid,idendesc,worker_info,center_linkid):
+ def __init__(self,main_stream,link,idendesc,worker_info,center_link):
global center_serv
- super().__init__(main_stream,'backend',linkid,idendesc,worker_info,center_linkid)
+ super().__init__(main_stream,link,idendesc,worker_info,center_link)
self.ws_addr = (worker_info['ws_ip'],worker_info['ws_port'])
center_serv.add_backend_worker(self)
@@ -58,7 +58,7 @@ class CenterServer(tornado.tcpserver.TCPServer):
self._ioloop = tornado.ioloop.IOLoop.instance()
self._linkid_usemap = {}
- self._worker_linkidmap = {}
+ self._worker_linkmap = {}
self._backend_clientmap = {}
self._backend_workerlist = []
@@ -66,18 +66,15 @@ class CenterServer(tornado.tcpserver.TCPServer):
privkey = open('privkey.pem','r').read()
TOJAuth(pubkey,privkey)
- self._linkid = self._create_linkid()
+ self._link = self._create_link('center')
- self._idendesc = TOJAuth.instance.create_iden('center',self._linkid,1,TOJAuth.ROLETYPE_TOJ)
- Proxy('center',self._linkid,TOJAuth.instance,self._idendesc)
+ self._idendesc = TOJAuth.instance.create_iden(self._link,1,TOJAuth.ROLETYPE_TOJ)
+ Proxy(self._link,TOJAuth.instance,self._idendesc)
- imc_register_call('','lookup_linkid',self._lookup_linkid)
+ imc_register_call('','lookup_link',self._lookup_link)
imc_register_call('','create_iden',self._create_iden)
- imc_register_call('','add_client',self._add_client)
- imc_register_call('','del_client',self._del_client)
-
- imc_register_call('','test_dst',self._test_dst)
- imc_register_call('','test_dstb',self._test_dstb)
+ #imc_register_call('','add_client',self._add_client)
+ #imc_register_call('','del_client',self._del_client)
def handle_stream(self,stream,addr):
def _recv_worker_info(data):
@@ -85,9 +82,9 @@ class CenterServer(tornado.tcpserver.TCPServer):
linkclass = worker_info['linkclass']
if linkclass == 'backend':
- linkid = self._create_linkid()
- idendesc = TOJAuth.instance.create_iden('backend',linkid,1,TOJAuth.ROLETYPE_TOJ)
- BackendWorker(main_stream,linkid,idendesc,worker_info,self._linkid)
+ link = self._create_link('backend')
+ idendesc = TOJAuth.instance.create_iden(link,1,TOJAuth.ROLETYPE_TOJ)
+ BackendWorker(main_stream,link,idendesc,worker_info,self._link)
fd = stream.fileno()
self._ioloop.remove_handler(fd)
@@ -96,17 +93,17 @@ class CenterServer(tornado.tcpserver.TCPServer):
netio.recv_pack(main_stream,_recv_worker_info)
def add_backend_worker(self,backend):
- backend_linkid = backend.linkid
+ backend_link = backend.link
- self._worker_linkidmap[backend_linkid] = backend
- self._backend_clientmap[backend_linkid] = {}
+ self._worker_linkmap[backend_link] = backend
+ self._backend_clientmap[backend_link] = {}
self._backend_workerlist.append(backend)
def del_backend_worker(self,backend):
- backend_linkid = backend.linkid
+ backend_link = backend.link
- del self._worker_linkidmap[backend_linkid]
- del self._backend_clientmap[backend_linkid]
+ del self._worker_linkmap[backend_link]
+ del self._backend_clientmap[backend_link]
self._backend_workerlist.remove(backend)
def dispatch_client(self):
@@ -114,14 +111,14 @@ class CenterServer(tornado.tcpserver.TCPServer):
if size == 0:
return None
- linkid = self._create_linkid()
- idendesc = TOJAuth.instance.create_iden('client',linkid,2,TOJAuth.ROLETYPE_GUEST)
+ link = self._create_link()
+ idendesc = TOJAuth.instance.create_iden('client',link,2,TOJAuth.ROLETYPE_GUEST)
backend = self._backend_workerlist[random.randrange(size)]
ws_ip,ws_port = backend.ws_addr
- return (idendesc,backend.linkid,ws_ip,ws_port)
+ return (idendesc,backend.link,ws_ip,ws_port)
- def _create_linkid(self):
+ def _create_link(self,linkclass):
linkid = uuid.uuid1()
while linkid in self._linkid_usemap:
linkid = uuid.uuid1()
@@ -129,16 +126,14 @@ class CenterServer(tornado.tcpserver.TCPServer):
linkid = str(linkid)
self._linkid_usemap[linkid] = True
- linkid = str(len(self._linkid_usemap))
-
- return linkid
+ return ''.join(['/',linkclass,'/',str(len(self._linkid_usemap)),'/'])
@imc.async.caller
- def _lookup_linkid(self,linkid):
+ def _lookup_link(self,link):
try:
- worker = self._worker_linkidmap[linkid]
+ worker = self._worker_linkmap[link]
- #a = int(iden['linkid'])
+ #a = int(TOJAuth.get_current_iden()['linkid'])
#b = int(linkid)
#if b > a:
@@ -147,11 +142,11 @@ class CenterServer(tornado.tcpserver.TCPServer):
#else:
# worker = self._worker_linkidmap[str(a - 1)]
- if TOJAuth.get_current_iden()['linkclass'] != 'client':
+ linkclass = TOJAuth.get_current_iden()['link'].split('/',1)
+ if linkclass != 'client':
sock_ip,sock_port = worker.sock_addr
return {
- 'worker_linkclass':worker.linkclass,
- 'worker_linkid':worker.linkid,
+ 'worker_link':worker.link,
'sock_ip':sock_ip,
'sock_port':sock_port
}
@@ -161,47 +156,32 @@ class CenterServer(tornado.tcpserver.TCPServer):
@imc.async.caller
@TOJAuth.check_access(1,TOJAuth.ACCESS_EXECUTE)
- def _create_iden(self,linkclass,linkid,idenid,roletype,payload):
- return TOJAuth.instance.create_iden(linkclass,linkid,idenid,roletype,payload)
+ def _create_iden(self,link,idenid,roletype,payload):
+ return TOJAuth.instance.create_iden(link,idenid,roletype,payload)
- @imc.async.caller
- def _add_client(self,param):
- backend_linkid = iden['linkid']
- client_linkid = param['client_linkid']
+ #@imc.async.caller
+ #def _add_client(self,param):
+ # backend_linkid = iden['linkid']
+ # client_linkid = param['client_linkid']
- self._backend_clientmap[backend_linkid][client_linkid] = True
- conn = Proxy.instance.get_conn(backend_linkid)
- Proxy.instance.link_conn(client_linkid,conn)
+ # self._backend_clientmap[backend_linkid][client_linkid] = True
+ # conn = Proxy.instance.get_conn(backend_linkid)
+ # Proxy.instance.link_conn(client_linkid,conn)
- print(client_linkid);
+ # print(client_linkid);
- @imc.async.caller
- def _del_client(self,param):
- backend_linkid = iden['linkid']
- client_linkid = param
+ #@imc.async.caller
+ #def _del_client(self,param):
+ # backend_linkid = iden['linkid']
+ # client_linkid = param
- del self._backend_clientmap[backend_linkid][client_linkid]
- conn = Proxy.instance.get_conn(client_linkid)
- Proxy.instance.unlink_conn(client_linkid)
+ # del self._backend_clientmap[backend_linkid][client_linkid]
+ # conn = Proxy.instance.get_conn(client_linkid)
+ # Proxy.instance.unlink_conn(client_linkid)
- @imc.async.caller
- def _test_dst(self,param):
- linkidlist = []
- clientmaps = self._backend_clientmap.values()
- for clientmap in clientmaps:
- linkids = clientmap.keys()
- for linkid in linkids:
- linkidlist.append(linkid)
-
- return linkidlist
-
- @imc.async.caller
- def _test_dstb(self,param):
- return param + ' World'
-
class WebConnHandler(tornado.web.RequestHandler):
def set_default_headers(self):
self.set_header('Access-Control-Allow-Origin','*')
@@ -213,10 +193,10 @@ class WebConnHandler(tornado.web.RequestHandler):
if data == None:
self.write('Eno_backend')
else:
- client_idendesc,backend_linkid,ip,port = data
+ client_idendesc,backend_link,ip,port = data
self.write(json.dumps({
'client_idendesc':client_idendesc,
- 'backend_linkid':backend_linkid,
+ 'backend_link':backend_link,
'ip':ip,
'port':port
}))
diff --git a/src/py/imc/async.py b/src/py/imc/async.py
index c0df338..c904d2b 100644
--- a/src/py/imc/async.py
+++ b/src/py/imc/async.py
@@ -17,14 +17,14 @@ def switch_top():
assert greenlet.getcurrent() != gr_main
- old_iden = auth.current_iden
+ old_idendata = auth.current_idendata
old_contexts = tornado.stack_context._state.contexts
- auth.current_iden = None
+ auth.current_idendata = None
result = gr_main.switch(None)
tornado.stack_context._state.contexts = old_contexts
- auth.current_iden = old_iden
+ auth.current_idendata = old_idendata
return result
@@ -48,13 +48,13 @@ def caller(f):
gr = greenlet(_call)
grid = id(gr)
gr_idmap[grid] = set()
- old_iden = auth.current_iden
+ old_idendata = auth.current_idendata
old_contexts = tornado.stack_context._state.contexts
result = gr.switch(*args,**kwargs)
tornado.stack_context._state.contexts = old_contexts
- auth.current_iden = old_iden
+ auth.current_idendata = old_idendata
if result == None:
return (False,None)
@@ -104,7 +104,7 @@ def ret(retid,value = None,err = None):
return
try:
- old_iden = auth.current_iden
+ old_idendata = auth.current_idendata
old_contexts = tornado.stack_context._state.contexts
if err == None:
@@ -114,7 +114,7 @@ def ret(retid,value = None,err = None):
gr.throw(err)
tornado.stack_context._state.contexts = old_contexts
- auth.current_iden = old_iden
+ auth.current_idendata = old_idendata
except TypeError as err:
traceback.print_stack()
diff --git a/src/py/imc/auth.py b/src/py/imc/auth.py
index 03c15dc..2b06ac4 100644
--- a/src/py/imc/auth.py
+++ b/src/py/imc/auth.py
@@ -8,35 +8,49 @@ from Crypto.PublicKey import RSA
from Crypto.Hash import SHA512
from Crypto.Signature import PKCS1_v1_5
-current_iden = None
+current_idendata = (None,None)
class Auth:
def __init__(self):
- global current_iden
+ global current_idendata
self._cache_hashmap = {}
- current_iden = None
+ current_idendata = (None,None)
+
+ Auth.instance = self
@staticmethod
def get_current_iden():
- global current_iden
+ global current_idendata
+
+ iden,idendesc = current_idendata
+ return iden
+
+ @staticmethod
+ def get_current_idendesc():
+ global current_idendata
- return current_iden
+ iden,idendesc = current_idendata
+ return idendesc
@staticmethod
- def change_current_iden(iden):
+ def change_current_iden(idendesc):
@contextlib.contextmanager
def context():
- global current_iden
+ global current_idendata
- old_iden = current_iden
- current_iden = iden
+ iden = Auth.instance.get_iden(idendesc)
+ if iden == None:
+ raise ValueError('Illegal idendesc')
+
+ old_idendata = current_idendata
+ current_idendata = (iden,idendesc)
try:
yield
finally:
- current_iden = old_iden
+ current_idendata = old_idendata
return tornado.stack_context.StackContext(context)
@@ -52,6 +66,13 @@ class Auth:
return json.dumps([data,sign])
+ def verify_iden(self,conn_link,idendesc):
+ pair = json.loads(idendesc)
+ data = pair[0]
+ sign = pair[1]
+
+ return self._verify(bytes(data,'utf-8'),binascii.unhexlify(sign))
+
def get_iden(self,idendesc):
pair = json.loads(idendesc)
data = pair[0]
diff --git a/src/py/imc/proxy.py b/src/py/imc/proxy.py
index 00476f0..c9d57bc 100755
--- a/src/py/imc/proxy.py
+++ b/src/py/imc/proxy.py
@@ -12,10 +12,9 @@ from imc import async
from imc.auth import Auth
class Connection:
- def __init__(self,linkclass,linkid):
- self.linkclass = linkclass
- self.linkid = linkid
- self.link_linkidmap = {}
+ def __init__(self,link):
+ self.link = link
+ self.link_linkmap = {}
self._close_callback = []
self._closed = False
@@ -74,26 +73,22 @@ class FileResult():
return self._result
class Proxy:
- def __init__(self,linkclass,linkid,auth_instance,idendesc,conn_linkid_fn = None):
+ def __init__(self,link,auth_instance,idendesc,conn_link_fn = lambda : None):
self.MSGTYPE_CALL = 'call'
self.MSGTYPE_RET = 'ret'
self.MSGTYPE_SENDFILE = 'sendfile'
self.MSGTYPE_ABORTFILE = 'abortfile'
self._ioloop = tornado.ioloop.IOLoop.instance()
- self._linkclass = linkclass
- self._linkid = linkid
+ self._link = link
self._auth = auth_instance
self._idendesc = idendesc
- if conn_linkid_fn == None:
- self._conn_linkid_fn = lambda : None
- else:
- self._conn_linkid_fn = conn_linkid_fn
+ self._conn_link_fn = conn_link_fn
- self._conn_linkidmap = {}
- self._conn_retidmap = {self._linkid:{}}
- self._conn_filekeymap = {self._linkid:{}}
+ self._conn_linkmap = {}
+ self._conn_retidmap = {self._link:{}}
+ self._conn_filekeymap = {self._link:{}}
self._call_pathmap = {}
self._info_filekeymap = {}
@@ -104,47 +99,47 @@ class Proxy:
self.register_call('imc/','reject_sendfile',self._reject_sendfile)
def add_conn(self,conn):
- assert conn.linkid not in self._conn_linkidmap
+ assert conn.link not in self._conn_linkmap
- self._conn_linkidmap[conn.linkid] = conn
- self._conn_retidmap[conn.linkid] = {}
- self._conn_filekeymap[conn.linkid] = {}
+ self._conn_linkmap[conn.link] = conn
+ self._conn_retidmap[conn.link] = {}
+ self._conn_filekeymap[conn.link] = {}
conn.add_close_callback(self._conn_close_cb)
conn.start_recv(self._recv_dispatch)
- def link_conn(self,linkid,conn):
- assert conn.linkid in self._conn_linkidmap
+ def link_conn(self,link,conn):
+ assert conn.link in self._conn_linkmap
- conn.link_linkidmap[linkid] = True
- self._conn_linkidmap[linkid] = conn
+ conn.link_linkmap[link] = True
+ self._conn_linkmap[link] = conn
- def unlink_conn(self,linkid):
- assert linkid in self._conn_linkidmap
+ def unlink_conn(self,link):
+ assert link in self._conn_linkmap
- conn = self._conn_linkidmap.pop(linkid)
- del conn.link_linkidmap[linkid]
+ conn = self._conn_linkmap.pop(link)
+ del conn.link_linkmap[link]
def del_conn(self,conn):
- waits = list(self._conn_retidmap[conn.linkid].values())
+ waits = list(self._conn_retidmap[conn.link].values())
for wait in waits:
wait['callback']((False,'Eclose'))
- waits = list(self._conn_filekeymap[conn.linkid].values())
+ waits = list(self._conn_filekeymap[conn.link].values())
for wait in waits:
wait['callback']('Eclose')
- linkids = list(conn.link_linkidmap.keys())
- for linkid in linkids:
- self.unlink_conn(linkid)
+ links = list(conn.link_linkmap.keys())
+ for link in links:
+ self.unlink_conn(link)
- del self._conn_linkidmap[conn.linkid]
- del self._conn_retidmap[conn.linkid]
- del self._conn_filekeymap[conn.linkid]
+ del self._conn_linkmap[conn.link]
+ del self._conn_retidmap[conn.link]
+ del self._conn_filekeymap[conn.link]
- def get_conn(self,linkid):
+ def get_conn(self,link):
try:
- return self._conn_linkidmap[linkid]
+ return self._conn_linkmap[link]
except KeyError:
return None
@@ -152,13 +147,14 @@ class Proxy:
def register_call(self,path,func_name,func):
self._call_pathmap[''.join([path,func_name])] = func
- def call(self,idendesc,dst,func_name,timeout,*args):
- return self._route_call(None,async.get_retid(),idendesc,dst,func_name,timeout,list(args))
+ def call(self,dst,func_name,timeout,*args):
+ return self._route_call(None,self._link,async.get_retid(),Auth.get_current_idendesc(),dst,func_name,timeout,list(args))
- def call_async(self,idendesc,dst,func_name,timeout,callback,*args):
+ def call_async(self,dst,func_name,timeout,callback,*args):
@async.caller
def _call():
- result = self._route_call(None,async.get_retid(),idendesc,dst,func_name,timeout,list(args))
+ result = self._route_call(None,self._link,async.get_retid(),Auth.get_current_idendesc(),dst,func_name,timeout,list(args))
+
if callback != None:
callback(result)
@@ -177,7 +173,9 @@ class Proxy:
'timer':self._ioloop.add_timeout(datetime.timedelta(days = 1),lambda : self._ret_sendfile('Etimeout'))
}
- stat,ret = self.call(self._idendesc,dst_link + 'imc/','pend_recvfile',{'filekey':filekey,'filesize':filesize},655360)
+ with Auth.change_current_iden(self._idendesc):
+ stat,ret = self.call(dst_link + 'imc/','pend_recvfile',65536,self._link,filekey,filesize)
+
if stat == False:
raise ConnectionError(ret)
@@ -186,7 +184,7 @@ class Proxy:
def recvfile(self,filekey,filepath):
def _callback(err = None):
try:
- self._del_wait_filekey(in_conn.linkid,filekey)
+ self._del_wait_filekey(in_conn.link,filekey)
except KeyError:
return
@@ -204,14 +202,14 @@ class Proxy:
except KeyError:
return
- src_linkid = info['src_linkid']
+ src_link = info['src_link']
filesize = info['filesize']
- in_conn = self._request_conn(src_linkid)
- self._add_wait_filekey(in_conn.linkid,filekey,filesize,_callback)
+ in_conn = self._request_conn(src_link)
+ self._add_wait_filekey(in_conn.link,filekey,filesize,_callback)
in_conn.recv_file(filekey,filesize,filepath,_callback)
- self._send_msg_sendfile(in_conn,src_linkid,filekey,filesize)
+ self._send_msg_sendfile(in_conn,src_link,filekey,filesize)
return info['fileresult']
@@ -222,104 +220,105 @@ class Proxy:
except KeyError:
return
- dst_link = ''.join(['/',info['src_linkclass'],'/',info['src_linkid'],'/'])
- self.call(self._idendesc,dst_link + 'imc/','reject_sendfile',{'filekey':filekey},65536)
+ with Auth.change_current_iden(self._idendesc):
+ self.call(info['src_link'] + 'imc/','reject_sendfile',65536,filekey)
- def _route_call(self,in_conn,caller_retid,idendesc,dst,func_name,timeout,param):
- def __add_wait_caller(conn_linkid):
- callback = tornado.stack_context.wrap(lambda result : self._ret_call(caller_linkid,caller_retid,result))
- self._conn_retidmap[conn_linkid][caller_retid] = {
+ def _route_call(self,in_conn,caller_link,caller_retid,idendesc,dst,func_name,timeout,param):
+ def __add_wait_caller(conn_link):
+ callback = tornado.stack_context.wrap(lambda result : self._ret_call(caller_link,caller_retid,result))
+ self._conn_retidmap[conn_link][caller_retid] = {
'timer':self._ioloop.add_timeout(datetime.timedelta(milliseconds = timeout),lambda : callback((False,'Etimeout'))),
'callback':callback
}
- def __del_wait_caller(conn_linkid):
- wait = self._conn_retidmap[conn_linkid].pop(caller_retid)
+ def __del_wait_caller(conn_link):
+ wait = self._conn_retidmap[conn_link].pop(caller_retid)
self._ioloop.remove_timeout(wait['timer'])
def __ret(result):
- if caller_linkid == self._linkid:
+ if caller_link == self._link:
return result
else:
- conn = self._request_conn(caller_linkid)
+ conn = self._request_conn(caller_link)
if conn != None:
- self._send_msg_ret(conn,caller_linkid,caller_retid,result)
+ self._send_msg_ret(conn,caller_link,caller_retid,result)
if in_conn != None:
- in_linkclass = in_conn.linkclass
- in_linkid = in_conn.linkid
+ in_link = in_conn.link
else:
- in_linkclass = self._linkclass
- in_linkid = self._linkid
+ in_link = self._link
- iden = self._auth.get_iden(in_linkclass,in_linkid,idendesc)
- if iden == None:
- return __ret(False,'Eilliden')
+ #iden = self._auth.get_iden(in_linkclass,in_linkid,idendesc)
+ #if iden == None:
+ # return __ret((False,'Eilliden'))
try:
dst_part = dst.split('/',3)
- dst_linkid = dst_part[2]
+ dst_link = ''.join(['/',dst_part[1],'/',dst_part[2],'/'])
dst_path = dst_part[3]
except Exception:
- return __ret(False,'Enoexist')
-
- caller_linkid = iden['linkid']
+ return __ret((False,'Enoexist'))
- if dst_linkid == self._linkid:
- __add_wait_caller(self._linkid)
+ if dst_link == self._link:
+ __add_wait_caller(self._link)
try:
- with Auth.change_current_iden(iden):
+ if Auth.get_current_idendesc() == idendesc:
result = self._call_pathmap[''.join([dst_path,func_name])](*param)
+ else:
+ with Auth.change_current_iden(idendesc):
+ result = self._call_pathmap[''.join([dst_path,func_name])](*param)
+
except KeyError:
result = (False,'Enoexist')
- __del_wait_caller(self._linkid)
+ __del_wait_caller(self._link)
return __ret(result)
else:
- conn = self._request_conn(dst_linkid)
+ conn = self._request_conn(dst_link)
if conn == None:
+ print(dst_link)
return __ret((False,'Enoexist'))
else:
- if caller_linkid == self._linkid:
- __add_wait_caller(conn.linkid)
- self._send_msg_call(conn,caller_retid,idendesc,dst,func_name,timeout,param)
+ if caller_link == self._link:
+ __add_wait_caller(conn.link)
+ self._send_msg_call(conn,caller_link,caller_retid,idendesc,dst,func_name,timeout,param)
result = async.switch_top()
- __del_wait_caller(conn.linkid)
+ __del_wait_caller(conn.link)
return __ret(result)
else:
- self._send_msg_call(conn,caller_retid,idendesc,dst,func_name,timeout,param)
+ self._send_msg_call(conn,caller_link,caller_retid,idendesc,dst,func_name,timeout,param)
return
- def _ret_call(self,caller_linkid,caller_retid,result):
+ def _ret_call(self,caller_link,caller_retid,result):
@async.caller
def __ret_remote():
- conn = self._request_conn(caller_linkid)
+ conn = self._request_conn(caller_link)
if conn != None:
- self._send_msg_ret(conn,caller_linkid,caller_retid,result)
+ self._send_msg_ret(conn,caller_link,caller_retid,result)
- if caller_linkid == self._linkid:
+ if caller_link == self._link:
async.ret(caller_retid,result)
else:
__ret_remote()
- def _route_sendfile(self,out_conn,src_linkid,filekey,filesize):
+ def _route_sendfile(self,out_conn,src_link,filekey,filesize):
def __send_cb(err = None):
try:
- self._del_wait_filekey(out_conn.linkid,filekey)
+ self._del_wait_filekey(out_conn.link,filekey)
except KeyError:
return
@@ -333,7 +332,7 @@ class Proxy:
def __bridge_cb(err = None):
try:
- self._del_wait_filekey(in_conn,filekey)
+ self._del_wait_filekey(in_conn.link,filekey)
if err != None:
if not in_conn.closed():
@@ -344,7 +343,7 @@ class Proxy:
pass
try:
- self._del_wait_filekey(out_conn,filekey)
+ self._del_wait_filekey(out_conn.link,filekey)
if err != None:
if not out_conn.closed():
@@ -354,7 +353,7 @@ class Proxy:
except KeyError:
pass
- if src_linkid == self._linkid:
+ if src_link == self._link:
try:
info = self._info_filekeymap[filekey]
if info['filesize'] != filesize:
@@ -364,28 +363,28 @@ class Proxy:
self._ioloop.add_callback(self._ret_sendfile,filekey,'Enoexist')
return
- self._add_wait_filekey(out_conn.linkid,filekey,filesize,__send_cb)
+ self._add_wait_filekey(out_conn.link,filekey,filesize,__send_cb)
out_conn.send_file(filekey,info['filepath'],__send_cb)
else:
- in_conn = self._request_conn(src_linkid)
- self._add_wait_filekey(in_conn.linkid,filekey,filesize,__bridge_cb)
- self._add_wait_filekey(out_conn.linkid,filekey,filesize,__bridge_cb)
+ in_conn = self._request_conn(src_link)
+ self._add_wait_filekey(in_conn.link,filekey,filesize,__bridge_cb)
+ self._add_wait_filekey(out_conn.link,filekey,filesize,__bridge_cb)
send_fn = out_conn.send_filedata(filekey,filesize,__bridge_cb)
in_conn.recv_filedata(filekey,filesize,send_fn)
- self._send_msg_sendfile(in_conn,src_linkid,filekey,filesize)
+ self._send_msg_sendfile(in_conn,src_link,filekey,filesize)
- def _add_wait_filekey(self,conn_linkid,filekey,filesize,callback):
+ def _add_wait_filekey(self,conn_link,filekey,filesize,callback):
callback = tornado.stack_context.wrap(callback)
- self._conn_filekeymap[conn_linkid][filekey] = {
- 'timer':self._ioloop.add_timeout(datetime.timedelta(milliseconds = min(filesize,1000)),lambda : callback('Etimeout')),
+ self._conn_filekeymap[conn_link][filekey] = {
+ 'timer':self._ioloop.add_timeout(datetime.timedelta(milliseconds = max(filesize,10000)),lambda : callback('Etimeout')),
'callback':callback
}
- def _del_wait_filekey(self,conn_linkid,filekey):
- wait = self._conn_filekeymap[conn_linkid].pop(filekey)
+ def _del_wait_filekey(self,conn_link,filekey):
+ wait = self._conn_filekeymap[conn_link].pop(filekey)
self._ioloop.remove_timeout(wait['timer'])
def _ret_sendfile(self,filekey,err = None):
@@ -404,15 +403,15 @@ class Proxy:
else:
fileresult.ret_result(err)
- def _request_conn(self,linkid):
+ def _request_conn(self,link):
try:
- return self._conn_linkidmap[linkid]
+ return self._conn_linkmap[link]
except KeyError:
- conn = self._conn_linkid_fn(linkid)
+ conn = self._conn_link_fn(link)
- if conn != None and conn.linkid != linkid:
- self.link_conn(linkid,conn)
+ if conn != None and conn.link != link:
+ self.link_conn(link,conn)
return conn
@@ -441,9 +440,10 @@ class Proxy:
elif msg_type == self.MSGTYPE_ABORTFILE:
self._recv_msg_abortfile(conn,msg)
- def _send_msg_call(self,conn,caller_retid,idendesc,dst,func_name,timeout,param):
+ def _send_msg_call(self,conn,caller_link,caller_retid,idendesc,dst,func_name,timeout,param):
msg = {
'type':self.MSGTYPE_CALL,
+ 'caller_link':caller_link,
'caller_retid':caller_retid,
'idendesc':idendesc,
'dst':dst,
@@ -457,8 +457,9 @@ class Proxy:
def _recv_msg_call(self,conn,msg):
@async.caller
def __call():
- self._route_call(conn,caller_retid,idendesc,dst,func_name,timeout,param)
+ self._route_call(conn,caller_link,caller_retid,idendesc,dst,func_name,timeout,param)
+ caller_link = msg['caller_link']
caller_retid = msg['caller_retid']
idendesc = msg['idendesc']
dst = msg['dst']
@@ -468,11 +469,11 @@ class Proxy:
__call()
- def _send_msg_ret(self,conn,caller_linkid,caller_retid,result):
+ def _send_msg_ret(self,conn,caller_link,caller_retid,result):
stat,data = result
msg = {
'type':self.MSGTYPE_RET,
- 'caller_linkid':caller_linkid,
+ 'caller_link':caller_link,
'caller_retid':caller_retid,
'result':{'stat':stat,'data':data}
}
@@ -480,17 +481,17 @@ class Proxy:
conn.send_msg(bytes(json.dumps(msg),'utf-8'))
def _recv_msg_ret(self,conn,msg):
- caller_linkid = msg['caller_linkid']
+ caller_link = msg['caller_link']
caller_retid = msg['caller_retid']
data = msg['result']
result = (data['stat'],data['data'])
- self._ret_call(caller_linkid,caller_retid,result)
+ self._ret_call(caller_link,caller_retid,result)
- def _send_msg_sendfile(self,conn,src_linkid,filekey,filesize):
+ def _send_msg_sendfile(self,conn,src_link,filekey,filesize):
msg = {
'type':self.MSGTYPE_SENDFILE,
- 'src_linkid':src_linkid,
+ 'src_link':src_link,
'filekey':filekey,
'filesize':filesize
}
@@ -500,9 +501,9 @@ class Proxy:
def _recv_msg_sendfile(self,conn,msg):
@async.caller
def __call():
- self._route_sendfile(conn,src_linkid,filekey,filesize)
+ self._route_sendfile(conn,src_link,filekey,filesize)
- src_linkid = msg['src_linkid']
+ src_link = msg['src_link']
filekey = msg['filekey']
filesize = msg['filesize']
@@ -521,7 +522,7 @@ class Proxy:
@async.caller
def __call():
try:
- self._conn_filekeymap[conn.linkid][filekey]['callback'](err)
+ self._conn_filekeymap[conn.link][filekey]['callback'](err)
except KeyError:
pass
@@ -532,28 +533,23 @@ class Proxy:
__call()
@async.caller
- def _pend_recvfile(self,iden,param):
- filekey = param['filekey']
- filesize = param['filesize']
-
+ def _pend_recvfile(self,src_link,filekey,filesize):
self._info_filekeymap[filekey] = {
- 'src_linkclass':iden['linkclass'],
- 'src_linkid':iden['linkid'],
+ 'src_link':src_link,
'filesize':filesize,
'fileresult':FileResult(filekey),
'timer':self._ioloop.add_timeout(datetime.timedelta(days = 1),lambda : self._ret_sendfile('Etimeout'))
}
@async.caller
- def _reject_sendfile(self,iden,param):
- filekey = param['filekey']
+ def _reject_sendfile(self,filekey):
self._ioloop.add_callback(self._ret_sendfile,filekey,'Ereject')
-def imc_call(idendesc,dst,func_name,*args):
- return Proxy.instance.call(idendesc,dst,func_name,65536,*args)
+def imc_call(dst,func_name,*args):
+ return Proxy.instance.call(dst,func_name,65536,*args)
-def imc_call_async(idendesc,dst,func_name,callback,*args):
- Proxy.instance.call_async(idendesc,dst,func_name,65536,callback,*args)
+def imc_call_async(dst,func_name,callback,*args):
+ Proxy.instance.call_async(dst,func_name,65536,callback,*args)
def imc_register_call(path,func_name,func):
Proxy.instance.register_call(path,func_name,func)
diff --git a/src/py/netio.py b/src/py/netio.py
index b71ab0b..aa2f9b2 100644
--- a/src/py/netio.py
+++ b/src/py/netio.py
@@ -294,8 +294,8 @@ class SocketStream:
self._ioloop.update_handler(fd,stat)
class SocketConnection(Connection):
- def __init__(self,linkclass,linkid,main_stream,file_addr,add_pend_filestream_fn = None):
- super().__init__(linkclass,linkid)
+ def __init__(self,link,main_stream,file_addr,add_pend_filestream_fn = None):
+ super().__init__(link)
self._ioloop = tornado.ioloop.IOLoop.current()
self._sendfile_filekeymap = {}
@@ -489,8 +489,8 @@ class SocketConnection(Connection):
del self._sendfile_filekeymap[filekey]
class WebSocketConnection(Connection):
- def __init__(self,linkclass,linkid,handler):
- super().__init__(linkclass,linkid)
+ def __init__(self,link,handler):
+ super().__init__(link)
self._ioloop = tornado.ioloop.IOLoop.current()
self.handler = handler
diff --git a/src/py/tojauth.py b/src/py/tojauth.py
index 701095b..0f775a6 100644..100755
--- a/src/py/tojauth.py
+++ b/src/py/tojauth.py
@@ -17,7 +17,7 @@ class TOJAuth(Auth):
ROLETYPE_GROUP = 5
ROLETYPE_GUEST = 6
- auth_accessid = 1
+ _accessid = 1
def __init__(self, pubkey, privkey = None):
super().__init__()
@@ -30,26 +30,25 @@ class TOJAuth(Auth):
TOJAuth.db = AsyncDB(config.CORE_DBNAME, config.CORE_DBUSER,
config.CORE_DBPASSWORD)
- def create_iden(self, linkclass, linkid, idenid, roletype, payload = {}):
+ def create_iden(self, link, idenid, roletype, payload = {}):
iden = payload
iden.update({
- 'linkclass' : linkclass,
- 'linkid' : linkid,
+ 'link' : link,
'idenid' : idenid,
'roletype' : roletype
})
return self.sign_iden(iden)
- def get_iden(self, conn_linkclass, conn_linkid, idendesc):
- iden = super().get_iden(idendesc)
+ def verify_iden(self, conn_link, idendesc):
+ iden = self.get_iden(idendesc)
if iden == None:
- return None
+ return False
- if conn_linkclass == 'client' and conn_linkid != iden['linkid']:
- return None
+ if conn_link != iden['link']:
+ return False
- return iden
+ return True
@staticmethod
def check_access(accessid, access_mask):
@@ -95,7 +94,7 @@ class TOJAuth(Auth):
def create_access(self, owner_idenid):
self.check_access(
- self.auth_accessid, self.ACCESS_EXECUTE)(lambda x:x)(0)
+ self._accessid, self.ACCESS_EXECUTE)(lambda x:x)(0)
cur = self.db.cursor()
sqlstr = ('INSERT INTO "ACCESS" ("owner_idenid") VALUES (%s) '
@@ -132,7 +131,7 @@ class TOJAuth(Auth):
def create_role(self, rolename, roletype):
self.check_access(
- self.auth_accessid, self.ACCESS_EXECUTE)(lambda x:x)(0)
+ self._accessid, self.ACCESS_EXECUTE)(lambda x:x)(0)
cur = self.db.cursor()
sqlstr = ('INSERT INTO "ROLE" ("rolename", "roletype") VALUES (%s, %s)'
@@ -149,7 +148,7 @@ class TOJAuth(Auth):
def set_role_relation(self, idenid, roleid):
self.check_access(
- self.auth_accessid, self.ACCESS_EXECUTE)(lambda x:x)(0)
+ self._accessid, self.ACCESS_EXECUTE)(lambda x:x)(0)
cur = self.db.cursor()
table = 'IDEN_ROLE'
@@ -161,7 +160,7 @@ class TOJAuth(Auth):
def del_role_relation(self, idenid, roleid):
self.check_access(
- self.auth_accessid, self.ACCESS_EXECUTE)(lambda x:x)(0)
+ self._accessid, self.ACCESS_EXECUTE)(lambda x:x)(0)
cur = self.db.cursor()
sqlstr = ('DELETE FROM "IDEN_ROLE" WHERE "idenid"=%s '
diff --git a/src/py/user.py b/src/py/user.py
index 45e195a..4d1c2fb 100755
--- a/src/py/user.py
+++ b/src/py/user.py
@@ -7,7 +7,7 @@ import imc.proxy
import config
class User:
- auth_accessid = 2
+ _accessid = 2
USERNAME_LEN_MIN = 5
USERNAME_LEN_MAX = 50
@@ -22,11 +22,12 @@ class User:
ABOUTME_LEN_MIN = 0
ABOUTME_LEN_MAX = 1000
- def __init__(self, mod_iden):
+ def __init__(self, mod_idendesc, get_link):
User.instance = self
User.db = AsyncDB(config.CORE_DBNAME, config.CORE_DBUSER,
config.CORE_DBPASSWORD)
- User.mod_iden = mod_iden
+ User._idendesc = mod_idendesc
+ self.get_link = get_link
@imc.async.caller
def register(self, username, password, nickname, email, avatar, aboutme):
@@ -67,7 +68,7 @@ class User:
passhash = self._password_hash(password)
- with TOJAuth.change_current_iden(self.mod_iden):
+ with TOJAuth.change_current_iden(self._idendesc):
try:
uid = self._create_user(
username, passhash, nickname, email, avatar, aboutme)
@@ -76,7 +77,7 @@ class User:
return {'uid' : uid}
- @TOJAuth.check_access(auth_accessid, TOJAuth.ACCESS_EXECUTE)
+ @TOJAuth.check_access(_accessid, TOJAuth.ACCESS_EXECUTE)
def _create_user(self, username, passhash, nickname, email, avatar,
aboutme):
roleid = TOJAuth.instance.create_role(username, TOJAuth.ROLETYPE_USER)
@@ -118,13 +119,10 @@ class User:
if idenid == None:
return 'Ewrong_password'
-
- linkclass = TOJAuth.get_current_iden()['linkclass']
- linkid = TOJAuth.get_current_iden()['linkid']
-
- with TOJAuth.change_current_iden(self.mod_iden):
+
+ with TOJAuth.change_current_iden(self._idendesc):
idendesc = TOJAuth.instance.create_iden(
- linkclass, linkid, idenid, TOJAuth.ROLETYPE_USER, {'uid' : uid}
+ TOJAuth.get_current_iden()['link'], idenid, TOJAuth.ROLETYPE_USER, {'uid' : uid}
)
ret = {
@@ -161,12 +159,9 @@ class User:
if real_uphash != uphash:
return 'Ewrong_uphash'
- linkclass = TOJAuth.get_current_iden()['linkclass']
- linkid = TOJAuth.get_current_iden()['linkid']
-
- with TOJAuth.change_current_iden(self.mod_iden):
+ with TOJAuth.change_current_iden(self._idendesc):
idendesc = TOJAuth.instance.create_iden(
- linkclass, linkid, idenid, TOJAuth.ROLETYPE_USER, {'uid' : uid}
+ TOJAuth.get_current_iden()['link'], idenid, TOJAuth.ROLETYPE_USER, {'uid' : uid}
)
ret = {
@@ -224,7 +219,7 @@ class User:
if idenid != TOJAuth.get_current_iden()['idenid']:
TOJAuth.check_access(
- self.auth_accessid, TOJAuth.ACCESS_EXECUTE)(lambda x:x)(0)
+ self._accessid, TOJAuth.ACCESS_EXECUTE)(lambda x:x)(0)
cur = self.db.cursor()
sqlstr = ('UPDATE "USER" SET "nickname" = %s, "email" = %s, '
@@ -252,7 +247,7 @@ class User:
if idenid != TOJAuth.get_current_iden()['idenid']:
TOJAuth.check_access(
- self.auth_accessid, TOJAuth.ACCESS_EXECUTE)(lambda x:x)(0)
+ self._accessid, TOJAuth.ACCESS_EXECUTE)(lambda x:x)(0)
old_passhash = self._password_hash(old_password)