encode/decode
This commit is contained in:
parent
26d3f1f8fb
commit
1e09e70574
7 changed files with 22 additions and 16 deletions
|
@ -55,8 +55,8 @@ class Changelog(db.Model):
|
||||||
c.revision = cls.query.filter_by(user_id=user.id).count()
|
c.revision = cls.query.filter_by(user_id=user.id).count()
|
||||||
c.data = json.dumps([action] + list(args))
|
c.data = json.dumps([action] + list(args))
|
||||||
_data = str(c.revision) + str(c.timestamp) + c.data
|
_data = str(c.revision) + str(c.timestamp) + c.data
|
||||||
_data = _data.encode('utf-8')
|
_data = _data.encode()
|
||||||
c.sig = settings.sk.sign(_data, encoding='base64')
|
c.sig = settings.sk.sign(_data, encoding='base64').decode()
|
||||||
state.db.session.add(c)
|
state.db.session.add(c)
|
||||||
state.db.session.commit()
|
state.db.session.commit()
|
||||||
if state.nodes:
|
if state.nodes:
|
||||||
|
@ -80,9 +80,9 @@ class Changelog(db.Model):
|
||||||
next_revision = last.revision + 1 if last else 0
|
next_revision = last.revision + 1 if last else 0
|
||||||
if revision == next_revision:
|
if revision == next_revision:
|
||||||
_data = str(revision) + str(timestamp) + data
|
_data = str(revision) + str(timestamp) + data
|
||||||
_data = _data.encode('utf-8')
|
_data = _data.encode()
|
||||||
if rebuild:
|
if rebuild:
|
||||||
sig = settings.sk.sign(_data, encoding='base64')
|
sig = settings.sk.sign(_data, encoding='base64').decode()
|
||||||
if valid(user.id, _data, sig):
|
if valid(user.id, _data, sig):
|
||||||
c = cls()
|
c = cls()
|
||||||
c.created = datetime.utcnow()
|
c.created = datetime.utcnow()
|
||||||
|
@ -112,14 +112,14 @@ class Changelog(db.Model):
|
||||||
|
|
||||||
def verify(self):
|
def verify(self):
|
||||||
_data = str(self.revision) + str(self.timestamp) + self.data
|
_data = str(self.revision) + str(self.timestamp) + self.data
|
||||||
_data = _data.encode('utf-8')
|
_data = _data.encode()
|
||||||
return valid(self.user_id, _data, self.sig)
|
return valid(self.user_id, _data, self.sig.encode())
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _rebuild(cls):
|
def _rebuild(cls):
|
||||||
for c in cls.query.filter_by(user_id=settings.USER_ID):
|
for c in cls.query.filter_by(user_id=settings.USER_ID):
|
||||||
_data = str(c.revision) + str(c.timestamp) + c.data
|
_data = str(c.revision) + str(c.timestamp) + c.data
|
||||||
_data = _data.encode('utf-8')
|
_data = _data.encode()
|
||||||
c.sig = settings.sk.sign(_data, encoding='base64')
|
c.sig = settings.sk.sign(_data, encoding='base64')
|
||||||
state.db.session.add(c)
|
state.db.session.add(c)
|
||||||
state.db.session.commit()
|
state.db.session.commit()
|
||||||
|
|
|
@ -37,7 +37,7 @@ def get(vk):
|
||||||
|
|
||||||
def put(sk, data):
|
def put(sk, data):
|
||||||
id = sk.get_verifying_key().to_ascii(encoding='base64').decode()
|
id = sk.get_verifying_key().to_ascii(encoding='base64').decode()
|
||||||
data = json.dumps(data).encode('utf-8')
|
data = json.dumps(data).encode()
|
||||||
sig = sk.sign(data, encoding='base64')
|
sig = sk.sign(data, encoding='base64')
|
||||||
url ='%s/%s' % (base, id)
|
url ='%s/%s' % (base, id)
|
||||||
headers = {
|
headers = {
|
||||||
|
|
|
@ -8,4 +8,4 @@ def loads(*args, **kargs):
|
||||||
|
|
||||||
def dumps(*args, **kargs):
|
def dumps(*args, **kargs):
|
||||||
#print('dumps', args, kargs)
|
#print('dumps', args, kargs)
|
||||||
return json.dumps(*args, **kargs).encode('utf-8')
|
return json.dumps(*args, **kargs).encode()
|
||||||
|
|
|
@ -57,8 +57,8 @@ class LocalNodesBase(Thread):
|
||||||
'port': server['node_port'],
|
'port': server['node_port'],
|
||||||
'cert': server['cert']
|
'cert': server['cert']
|
||||||
})
|
})
|
||||||
sig = sk.sign(message.encode('utf-8'), encoding='base64').decode('utf-8')
|
sig = sk.sign(message.encode(), encoding='base64').decode()
|
||||||
packet = json.dumps([sig, USER_ID, message]).encode('utf-8')
|
packet = json.dumps([sig, USER_ID, message]).encode()
|
||||||
else:
|
else:
|
||||||
packet = None
|
packet = None
|
||||||
return packet
|
return packet
|
||||||
|
|
|
@ -125,7 +125,7 @@ class Node(Thread):
|
||||||
logger.debug('unable to find host %s', self.user_id)
|
logger.debug('unable to find host %s', self.user_id)
|
||||||
self.online = False
|
self.online = False
|
||||||
return None
|
return None
|
||||||
content = json.dumps([action, args]).encode('utf-8')
|
content = json.dumps([action, args]).encode()
|
||||||
sig = settings.sk.sign(content, encoding=ENCODING).decode()
|
sig = settings.sk.sign(content, encoding=ENCODING).decode()
|
||||||
headers = {
|
headers = {
|
||||||
'User-Agent': settings.USER_AGENT,
|
'User-Agent': settings.USER_AGENT,
|
||||||
|
@ -185,7 +185,7 @@ class Node(Thread):
|
||||||
|
|
||||||
def _valid(self, data, sig):
|
def _valid(self, data, sig):
|
||||||
if isinstance(data, str):
|
if isinstance(data, str):
|
||||||
data = data.encode('utf-8')
|
data = data.encode()
|
||||||
try:
|
try:
|
||||||
self.vk.verify(sig, data, encoding=ENCODING)
|
self.vk.verify(sig, data, encoding=ENCODING)
|
||||||
#except ed25519.BadSignatureError:
|
#except ed25519.BadSignatureError:
|
||||||
|
|
|
@ -34,7 +34,7 @@ def _to_json(python_object):
|
||||||
|
|
||||||
def json_dumps(obj):
|
def json_dumps(obj):
|
||||||
indent = 2
|
indent = 2
|
||||||
return json.dumps(obj, indent=indent, default=_to_json, ensure_ascii=False).encode('utf-8')
|
return json.dumps(obj, indent=indent, default=_to_json, ensure_ascii=False).encode()
|
||||||
|
|
||||||
def run_async(func):
|
def run_async(func):
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
|
|
10
oml/utils.py
10
oml/utils.py
|
@ -114,9 +114,15 @@ def valid(key, value, sig):
|
||||||
'''
|
'''
|
||||||
validate that value was signed by key
|
validate that value was signed by key
|
||||||
'''
|
'''
|
||||||
vk = ed25519.VerifyingKey(str(key), encoding=ENCODING)
|
if isinstance(sig, str):
|
||||||
|
sig = sig.encode()
|
||||||
|
if isinstance(value, str):
|
||||||
|
value = value.encode()
|
||||||
|
if isinstance(key, str):
|
||||||
|
key = key.encode()
|
||||||
|
vk = ed25519.VerifyingKey(key, encoding=ENCODING)
|
||||||
try:
|
try:
|
||||||
vk.verify(str(sig), str(value), encoding=ENCODING)
|
vk.verify(sig, value, encoding=ENCODING)
|
||||||
#except ed25519.BadSignatureError:
|
#except ed25519.BadSignatureError:
|
||||||
except:
|
except:
|
||||||
return False
|
return False
|
||||||
|
|
Loading…
Reference in a new issue