aboutsummaryrefslogtreecommitdiffstats
path: root/src/py/imc/auth.py
diff options
context:
space:
mode:
authorpzread <netfirewall@gmail.com>2013-06-09 13:34:55 +0800
committerpzread <netfirewall@gmail.com>2013-06-09 13:34:55 +0800
commit77dd822815744579b05da117efb14f43b7088038 (patch)
treeea2388767586a740d06bde30c4e29c940f9a91c4 /src/py/imc/auth.py
parent872567a4cf3bff7d9d310f5e66f465f5523d58d9 (diff)
parent8cf636373548c8e3484a137268ddd041d12bbe4a (diff)
downloadtaiwan-online-judge-77dd822815744579b05da117efb14f43b7088038.tar.gz
taiwan-online-judge-77dd822815744579b05da117efb14f43b7088038.tar.zst
taiwan-online-judge-77dd822815744579b05da117efb14f43b7088038.zip
Merge branch '2.0'
Conflicts: README.md
Diffstat (limited to 'src/py/imc/auth.py')
-rw-r--r--src/py/imc/auth.py79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/py/imc/auth.py b/src/py/imc/auth.py
new file mode 100644
index 0000000..03c15dc
--- /dev/null
+++ b/src/py/imc/auth.py
@@ -0,0 +1,79 @@
+import time
+import json
+import binascii
+import contextlib
+
+import tornado.stack_context
+from Crypto.PublicKey import RSA
+from Crypto.Hash import SHA512
+from Crypto.Signature import PKCS1_v1_5
+
+current_iden = None
+
+class Auth:
+ def __init__(self):
+ global current_iden
+
+ self._cache_hashmap = {}
+ current_iden = None
+
+ @staticmethod
+ def get_current_iden():
+ global current_iden
+
+ return current_iden
+
+ @staticmethod
+ def change_current_iden(iden):
+ @contextlib.contextmanager
+ def context():
+ global current_iden
+
+ old_iden = current_iden
+ current_iden = iden
+
+ try:
+ yield
+
+ finally:
+ current_iden = old_iden
+
+ return tornado.stack_context.StackContext(context)
+
+ def set_signkey(self,key):
+ self._signer = PKCS1_v1_5.new(RSA.importKey(key))
+
+ def set_verifykey(self,key):
+ self._verifier = PKCS1_v1_5.new(RSA.importKey(key))
+
+ def sign_iden(self,iden):
+ data = json.dumps(iden)
+ sign = binascii.hexlify(self._sign(bytes(data,'utf-8'))).decode('utf-8')
+
+ return json.dumps([data,sign])
+
+ def get_iden(self,idendesc):
+ pair = json.loads(idendesc)
+ data = pair[0]
+ sign = pair[1]
+
+ if self._verify(bytes(data,'utf-8'),binascii.unhexlify(sign)):
+ return json.loads(data)
+
+ else:
+ return None
+
+ def _sign(self,data):
+ return self._signer.sign(SHA512.new(data))
+
+ def _verify(self,data,sig):
+ h = SHA512.new(data)
+ if h in self._cache_hashmap:
+ return True
+
+ if self._verifier.verify(h,sig) == True:
+ self._cache_hashmap[h] = True
+
+ return True
+ else:
+ return False