2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-03 23:15:33 +00:00
Comrad/komrade/backend/people.py

111 lines
3.1 KiB
Python
Raw Normal View History

2020-09-08 11:23:41 +00:00
import os,sys; sys.path.append(os.path.abspath(os.path.join(os.path.abspath(os.path.join(os.path.dirname(__file__),'..')),'..')))
from komrade import *
from komrade.backend import *
2020-09-09 17:34:19 +00:00
class Persona(Caller):
2020-09-08 11:23:41 +00:00
2020-09-09 17:34:19 +00:00
def __init__(self, name, passphrase=DEBUG_DEFAULT_PASSPHRASE):
super().__init__(name=name,passphrase=passphrase)
2020-09-09 18:31:36 +00:00
# self.boot(create=False)
2020-09-08 12:11:13 +00:00
2020-09-09 18:31:36 +00:00
# def boot(self,create=False):
# # Do I already have my keys?
# # yes? -- login
2020-09-08 12:11:13 +00:00
2020-09-09 18:31:36 +00:00
# keys = self.keychain()
# if keys.get('pubkey') and keys.get('privkey'):
# self.log('booted!')
# return True
2020-09-09 14:38:37 +00:00
2020-09-09 18:31:36 +00:00
# # If not, forge them -- only once!
# if not have_keys and create:
# self.get_new_keys()
2020-09-09 14:38:37 +00:00
2020-09-09 17:34:19 +00:00
def exists_locally_as_contact(self):
return self.pubkey and not self.privkey
def exists_locally_as_persona(self):
return self.pubkey and self.privkey
def exists_on_server(self):
2020-09-09 22:38:57 +00:00
answer = self.ring_ring({
2020-09-09 18:31:36 +00:00
'_route':'does_username_exist',
'name':self.name
})
2020-09-09 22:38:57 +00:00
self.log('answer??',answer)
2020-09-10 08:08:50 +00:00
return answer
2020-09-08 11:23:41 +00:00
2020-09-09 17:34:19 +00:00
# login?
2020-09-09 17:36:07 +00:00
# def login(self):
# if keys.get('pubkey') and keys.get('privkey')
2020-09-09 17:34:19 +00:00
2020-09-08 11:23:41 +00:00
2020-09-09 15:30:34 +00:00
def register(self, name = None, passphrase = DEBUG_DEFAULT_PASSPHRASE, is_group=None):
2020-09-08 11:23:41 +00:00
# get needed metadata
if not name: name=self.name
if name is None:
name = input('\nWhat is the name for this account? ')
if passphrase is None:
passphrase = getpass.getpass('\nEnter a memborable password: ')
# if is_group is None:
# is_group = input('\nIs this a group account? [y/N]').strip().lower() == 'y'
2020-09-09 15:30:34 +00:00
# form request
msg_to_op = {
2020-09-09 18:31:36 +00:00
'_route':'forge_new_keys',
2020-09-09 15:30:34 +00:00
'name':name,
'passphrase':hashish(passphrase.encode())
}
2020-09-08 11:23:41 +00:00
2020-09-09 15:30:34 +00:00
# get message back
msg_obj = self.phone.ring_ring(msg_to_op)
2020-09-09 15:35:38 +00:00
phone_res = msg_obj.msg
2020-09-09 15:30:34 +00:00
2020-09-08 11:23:41 +00:00
# URI id
uri_id = phone_res.get('uri_id')
returned_keys = phone_res.get('_keychain')
self.log('got URI from Op:',uri_id)
self.log('got returnd keys from Op:',returned_keys)
# better have the right keys
assert set(KEYMAKER_DEFAULT_KEYS_TO_SAVE_ON_CLIENT) == set(returned_keys.keys())
# now save these keys!
saved_keys = self.save_keychain(name,returned_keys,uri_id=uri_id)
self.log('saved keys!',saved_keys)
# success!
self.log('yay!!!!')
return saved_keys
2020-09-08 15:14:48 +00:00
2020-09-09 17:34:19 +00:00
def ring_ring(self,msg):
return super().ring_ring(msg)
2020-09-09 15:30:34 +00:00
2020-09-09 17:34:19 +00:00
def send_msg_to(self,msg,to_whom):
msg = self.compose_msg_to(msg,to_whom)
2020-09-09 22:26:48 +00:00
msg.encrypt()
2020-09-09 15:30:34 +00:00
2020-09-09 18:31:36 +00:00
{'_route':'deliver_to', 'msg':msg}
2020-09-09 15:30:34 +00:00
2020-09-09 17:34:19 +00:00
return self.ring_ring(msg)
2020-09-09 15:30:34 +00:00
2020-09-09 17:34:19 +00:00
2020-09-09 15:30:34 +00:00
2020-09-08 15:14:48 +00:00
if __name__=='__main__':
2020-09-09 17:34:19 +00:00
marx = Persona('marx')
elon = Persona('elon')
2020-09-08 15:14:48 +00:00
2020-09-09 18:31:36 +00:00
# marx.register()
2020-09-10 08:24:50 +00:00
elon.register()
2020-09-09 15:41:33 +00:00
# person.register()
# print(person.pubkey)
2020-09-09 15:54:54 +00:00
# elon.send_msg_to('youre dumb',marx)
2020-09-09 18:31:36 +00:00
#Caller('elon').ring_ring({'_route':'say_hello','_msg':'my dumb message to operator'})
2020-09-09 17:39:30 +00:00
2020-09-10 08:24:50 +00:00
# print(marx.exists_on_server())