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

136 lines
4.0 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-11 14:35:47 +00:00
from komrade.backend.keymaker import *
2020-09-08 11:23:41 +00:00
2020-09-09 17:34:19 +00:00
class Persona(Caller):
2020-09-08 11:23:41 +00:00
2020-09-10 17:52:07 +00:00
def __init__(self, name=None, passphrase=DEBUG_DEFAULT_PASSPHRASE):
2020-09-09 17:34:19 +00:00
super().__init__(name=name,passphrase=passphrase)
2020-09-11 14:35:47 +00:00
if SHOW_STATUS:
from komrade.cli import CLI
self.cli = CLI()
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-10 21:32:59 +00:00
answer = self.phone.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-11 14:35:47 +00:00
def register(self, name = None, passphrase = None, is_group=None, show_intro=0,show_body=True):
2020-09-10 17:52:07 +00:00
# defaults
if name and not self.name: self.name=name
if not name and self.name: name=self.name
if not name and not self.name: name=''
2020-09-11 14:35:47 +00:00
clear_screen()
# intro narration?
if SHOW_STATUS and show_intro:
name = self.cli.status_keymaker_intro(name)
2020-09-11 17:17:21 +00:00
# 1) forge public/private keys
2020-09-11 14:35:47 +00:00
keypair = KomradeAsymmetricKey()
pubkey,privkey = keypair.pubkey_obj,keypair.privkey_obj
2020-09-11 17:17:21 +00:00
# 2) make sure we have passphrase
passphrase=self.crypt_keys.hash(b'boogywoogy')
if SHOW_STATUS and not passphrase:
2020-09-11 14:35:47 +00:00
passphrase = self.cli.status_keymaker_body(
name,
passphrase,
pubkey,
privkey,
2020-09-11 17:17:21 +00:00
self.crypt_keys.hash,
self
2020-09-11 14:35:47 +00:00
)
else:
if not passphrase: passphrase=getpass('Enter a memorable password to encrypt your private key with: ')
2020-09-10 21:32:59 +00:00
2020-09-11 17:17:21 +00:00
# 3) form an encryption key
privkey_decr = KomradeSymmetricKeyWithPassphrase(passphrase)
privkey_encr = privkey_decr.encrypt(privkey.data)
2020-09-08 11:23:41 +00:00
2020-09-11 17:17:21 +00:00
self.cli.status_keymaker_part3(privkey,privkey_decr,privkey_encr,passphrase)
exit()
2020-09-10 09:00:11 +00:00
# save the ones we should on server
2020-09-10 10:59:37 +00:00
data = {
2020-09-10 13:53:21 +00:00
**{'name':name, 'passphrase':self.crypt_keys.hash(passphrase.encode()), ROUTE_KEYNAME:'register_new_user'},
2020-09-10 10:59:37 +00:00
**keys_returned
}
2020-09-10 13:52:23 +00:00
self.log('sending to server:',dict_format(data,tab=2))
2020-09-10 09:31:32 +00:00
# msg_to_op = self.compose_msg_to(data, self.op)
2020-09-10 21:32:59 +00:00
2020-09-10 09:31:32 +00:00
# ring operator
2020-09-10 09:34:34 +00:00
# call from phone since I don't have pubkey on record on Op yet
resp_msg_obj = self.phone.ring_ring(data)
2020-09-10 13:52:23 +00:00
self.log('register got back from op:',dict_format(resp_msg_obj,tab=2))
2020-09-10 09:34:34 +00:00
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-10 14:00:55 +00:00
def test_register():
import random
num = random.choice(list(range(0,1000)))
2020-09-10 21:32:59 +00:00
botname=f'marx{str(num).zfill(3)}'
marxbot = Persona(botname)
# marxbot=Persona()
2020-09-10 14:00:55 +00:00
marxbot.register()
2020-09-09 15:30:34 +00:00
2020-09-08 15:14:48 +00:00
if __name__=='__main__':
2020-09-11 14:35:47 +00:00
test_register()
# marx = Persona('marx')
# elon = Persona('elon')
2020-09-08 15:14:48 +00:00
2020-09-11 14:35:47 +00:00
# marx.register()
# # elon.register()
# # person.register()
# # print(person.pubkey)
2020-09-09 15:41:33 +00:00
2020-09-11 14:35:47 +00:00
# # elon.send_msg_to('youre dumb',marx)
# #Caller('elon').ring_ring({'_route':'say_hello','_msg':'my dumb message to operator'})
2020-09-09 17:39:30 +00:00
2020-09-11 14:35:47 +00:00
# # print(marx.exists_on_server())