|
|
|
@ -10,8 +10,10 @@
|
|
|
|
|
import os,time
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from flask_api import status
|
|
|
|
|
from .p2p import connect
|
|
|
|
|
import asyncio
|
|
|
|
|
from .crypto import *
|
|
|
|
|
from main import log
|
|
|
|
|
from .p2p import *
|
|
|
|
|
|
|
|
|
|
# works better with tor?
|
|
|
|
|
import json
|
|
|
|
@ -22,10 +24,125 @@ jsonify = json.dumps
|
|
|
|
|
DEBUG = True
|
|
|
|
|
UPLOAD_DIR = 'uploads/'
|
|
|
|
|
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
|
|
|
|
|
NODES_PRIME = [("128.232.229.63",8468), ("68.66.241.111",8468)]
|
|
|
|
|
PORT_LISTEN = 8469
|
|
|
|
|
|
|
|
|
|
# Api Functions
|
|
|
|
|
|
|
|
|
|
NODE = connect()
|
|
|
|
|
class Api(object):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self,app_storage):
|
|
|
|
|
#self.connect()
|
|
|
|
|
self.app_storage = app_storage
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
# def connect(self):
|
|
|
|
|
#from .p2p import connect
|
|
|
|
|
#self.node = connect()
|
|
|
|
|
|
|
|
|
|
def get(self,key):
|
|
|
|
|
async def _get():
|
|
|
|
|
node = Server(storage=HalfForgetfulStorage())
|
|
|
|
|
await node.listen(PORT_LISTEN)
|
|
|
|
|
await node.bootstrap(NODES_PRIME)
|
|
|
|
|
return await node.get(key)
|
|
|
|
|
return asyncio.run(_get())
|
|
|
|
|
|
|
|
|
|
def set(self,key,value):
|
|
|
|
|
async def _set():
|
|
|
|
|
node = Server(storage=HalfForgetfulStorage())
|
|
|
|
|
await node.listen(PORT_LISTEN)
|
|
|
|
|
await node.bootstrap(NODES_PRIME)
|
|
|
|
|
return await node.set(key,value)
|
|
|
|
|
return asyncio.run(_set())
|
|
|
|
|
|
|
|
|
|
def has(self,key):
|
|
|
|
|
return self.get(key) is not None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## PERSONS
|
|
|
|
|
def get_person(self,username):
|
|
|
|
|
person = self.get('/person/'+username)
|
|
|
|
|
return None if person is None else json.loads(person)
|
|
|
|
|
|
|
|
|
|
def set_person(self,username,public_key):
|
|
|
|
|
pem_public_key = save_public_key(public_key,return_instead=True)
|
|
|
|
|
obj = {'name':username, 'public_key':pem_public_key.decode()}
|
|
|
|
|
obj_str = jsonify(obj)
|
|
|
|
|
self.set('/person/'+username,obj_str)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Register
|
|
|
|
|
def register(self,name,passkey):
|
|
|
|
|
|
|
|
|
|
if not (name and passkey):
|
|
|
|
|
error('name and passkey not set')
|
|
|
|
|
return {'error':'Register failed'},status.HTTP_400_BAD_REQUEST
|
|
|
|
|
|
|
|
|
|
person = self.get_person(name)
|
|
|
|
|
if person is not None:
|
|
|
|
|
log('error! person exists')
|
|
|
|
|
return {'error':'Register failed'}
|
|
|
|
|
|
|
|
|
|
private_key,public_key = new_keys(password=passkey,save=False)
|
|
|
|
|
pem_private_key = save_private_key(private_key,password=passkey,return_instead=True)
|
|
|
|
|
pem_public_key = save_public_key(public_key,return_instead=True)
|
|
|
|
|
|
|
|
|
|
self.app_storage.put('_keys',
|
|
|
|
|
private=str(pem_private_key.decode()),
|
|
|
|
|
public=str(pem_public_key.decode())) #(private_key,password=passkey)
|
|
|
|
|
self.set_person(name,public_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log('success! Account created')
|
|
|
|
|
return {'success':'Account created', 'username':name}
|
|
|
|
|
|
|
|
|
|
def load_private_key(self,password):
|
|
|
|
|
if not self.app_storage.exists('_keys'): return None
|
|
|
|
|
pem_private_key=self.app_storage.get('_keys').get('private')
|
|
|
|
|
try:
|
|
|
|
|
return load_private_key(pem_private_key.encode(),password)
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
log('!!',e)
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## LOGIN
|
|
|
|
|
def login(self,name,passkey):
|
|
|
|
|
# verify input
|
|
|
|
|
if not (name and passkey):
|
|
|
|
|
return {'error':'Name and password required'}
|
|
|
|
|
|
|
|
|
|
# try to load private key
|
|
|
|
|
private_key = self.load_private_key(passkey)
|
|
|
|
|
if private_key is None:
|
|
|
|
|
return {'error':'You have never registered on this device'}
|
|
|
|
|
|
|
|
|
|
# see if user exists
|
|
|
|
|
person = self.get_person(name)
|
|
|
|
|
log(person)
|
|
|
|
|
if person is None:
|
|
|
|
|
return {'error':'Login failed'}
|
|
|
|
|
|
|
|
|
|
# verify keys
|
|
|
|
|
person_public_key_pem = person['public_key']
|
|
|
|
|
public_key = load_public_key(person_public_key_pem)
|
|
|
|
|
real_public_key = private_key.public_key()
|
|
|
|
|
|
|
|
|
|
#log('PUBLIC',public_key.public_numbers())
|
|
|
|
|
#log('REAL PUBLIC',real_public_key.public_numbers())
|
|
|
|
|
|
|
|
|
|
if public_key.public_numbers() != real_public_key.public_numbers():
|
|
|
|
|
return {'error':'keys do not match!'}
|
|
|
|
|
return {'success':'Login successful', 'username':name}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## LOGIN
|
|
|
|
@ -45,11 +162,6 @@ def login(data):
|
|
|
|
|
|
|
|
|
|
return {'success':'Login success'},status.HTTP_200_OK
|
|
|
|
|
|
|
|
|
|
def get_person(username):
|
|
|
|
|
async def go():
|
|
|
|
|
return await NODE.get('/person/'+username)
|
|
|
|
|
|
|
|
|
|
return asyncio.run(run())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def register(name,passkey):
|
|
|
|
@ -73,8 +185,6 @@ def register(name,passkey):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
## CREATE
|
|
|
|
|
|
|
|
|
|
def allowed_file(filename):
|
|
|
|
|