2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-09 13:10:44 +00:00
Comrad/p2p/keyserver.py

81 lines
2.4 KiB
Python
Raw Normal View History

2020-09-02 10:05:33 +00:00
"""
Run primitive keyserver
Only on node prime!
"""
import os
from flask import Flask
2020-09-02 12:24:50 +00:00
from flask import request
import asyncio
from pythemis.skeygen import KEY_PAIR_TYPE, GenerateKeyPair
from pythemis.smessage import SMessage, ssign, sverify
from pythemis.exception import ThemisError
from base64 import b64encode,b64decode
2020-09-02 15:14:40 +00:00
BSEP=b'||||||||||'
BSEP2=b'@@@@@@@@@@'
BSEP3=b'##########'
2020-09-02 10:05:33 +00:00
keyhome = os.path.join(os.path.expanduser('~'),'.komrade','.keyserver')
2020-09-02 12:24:50 +00:00
if not os.path.exists(keyhome): os.makedirs(keyhome)
2020-09-02 09:57:07 +00:00
2020-09-02 10:05:33 +00:00
keyserver = 'komrade.app'
keyserver_port = 5566
app = Flask(__name__)
async def init():
2020-09-02 12:24:50 +00:00
from api import Api
api = Api()
# keyserver = await api.personate('keyserver')
2020-09-02 10:05:33 +00:00
2020-09-02 12:24:50 +00:00
keypair = GenerateKeyPair(KEY_PAIR_TYPE.EC)
privkey = keypair.export_private_key()
pubkey = keypair.export_public_key()
2020-09-02 10:05:33 +00:00
2020-09-02 12:24:50 +00:00
print('pubkey:',pubkey)
with open('.keyserver.loc','wb') as of: of.write(b64encode(pubkey))
with open(os.path.join(keyhome,'.keyserver.key'),'wb') as of: of.write(b64encode(privkey))
2020-09-02 10:05:33 +00:00
2020-09-02 12:24:50 +00:00
## load pubkey
PATH_PUBKEY = os.path.join(os.path.dirname(__file__),'.keyserver.loc')
PATH_PRIVKEY = os.path.join(keyhome,'.keyserver.key')
if not os.path.exists(PATH_PRIVKEY) or not os.path.exists(PATH_PUBKEY):
asyncio.run(init())
with open(PATH_PUBKEY) as f:
PUBKEY_b64 = f.read()
PUBKEY = b64decode(PUBKEY_b64)
with open(PATH_PRIVKEY) as f:
PRIVKEY_b64 = f.read()
PRIVKEY = b64decode(PRIVKEY_b64)
@app.route('/pub')
def pubkey():
2020-09-02 15:14:40 +00:00
return PUBKEY_b64
2020-09-02 12:24:50 +00:00
@app.route('/add/<name>',methods=['POST'])
def add(name):
key_fn = os.path.join(keyhome,name+'.loc')
if not os.path.exists(key_fn):
2020-09-02 10:05:33 +00:00
with open(key_fn,'wb') as of:
2020-09-02 15:14:40 +00:00
pubkey,signed_pubkey=request.data.split(BSEP)
server_signed_pubkey = b64encode(ssign(PRIVKEY,pubkey))
package = pubkey + BSEP + signed_pubkey + BSEP + server_signed_pubkey
package_b64 = b64encode(package)
print('add package -->',package)
print('add package_b64 -->',package_b64)
of.write(package_b64)
return package_b64
2020-09-02 10:05:33 +00:00
return None
2020-09-02 12:24:50 +00:00
@app.route('/get/<name>')
def get(name):
key_fn = os.path.join(keyhome,name+'.loc')
if os.path.exists(key_fn):
with open(key_fn,'rb') as f:
signed_key=f.read()
return signed_key
return b''
2020-09-02 10:05:33 +00:00
if __name__ == '__main__':
2020-09-02 12:24:50 +00:00
app.run(host='0.0.0.0',port=keyserver_port)
# asyncio.run(init())