You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Comrad/komrade/backend/people.py

164 lines
5.8 KiB
Python

4 years ago
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 *
4 years ago
class Persona(Caller):
4 years ago
4 years ago
def __init__(self, name=None, passphrase=DEBUG_DEFAULT_PASSPHRASE):
4 years ago
super().__init__(name=name,passphrase=passphrase)
4 years ago
# self.boot(create=False)
4 years ago
4 years ago
# def boot(self,create=False):
# # Do I already have my keys?
# # yes? -- login
4 years ago
4 years ago
# keys = self.keychain()
# if keys.get('pubkey') and keys.get('privkey'):
# self.log('booted!')
# return True
4 years ago
4 years ago
# # If not, forge them -- only once!
# if not have_keys and create:
# self.get_new_keys()
4 years ago
4 years ago
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):
4 years ago
answer = self.ring_ring({
4 years ago
'_route':'does_username_exist',
'name':self.name
})
4 years ago
self.log('answer??',answer)
4 years ago
return answer
4 years ago
4 years ago
# login?
4 years ago
# def login(self):
# if keys.get('pubkey') and keys.get('privkey')
4 years ago
4 years ago
def register(self, name = None, passphrase = None, is_group=None):
# 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=''
# ring ring!
ticks = ['*ring*','*ring ring*','*ring*']
self.status(None,ART_OLDPHONE3,3,pause=False,ticks = ticks)
# hello, op?
self.status(f'\n\n@{name if name else "?"}: Uh yes hello, Operator? I would like to join Komrade, the socialist network. Could you patch me through?',clear=True,pause=True)
while not name:
name=self.status(('name','@TheOperator: Of course, Komrade ...?\n@'),clear=False).get('vals',{}).get('name')
print()
self.name=name
self.status(f"@TheOperator: Of course, Komrade @{name}. A fine name. First, though, let me see if by an awkward accident someone has already taken this name.",clear=False)
# does username exist
res = self.exists_on_server()
self.status('I got this result',res,clear=False)
stop
exit()
4 years ago
self.status([
4 years ago
'''@TheOperator: I could, it's not safe yet. Your information will be exposed all over the internet. You should forge your encryption keys first.''',
f'@{name}: Fine, but how do I do that?',
4 years ago
'@TheOperator: Visit the Keymaker.'
])
4 years ago
# ring ring!
self.status([None,ART_KEY,2],pause=False)
# some info
res = self.status([
# ART_KEY,
f'{ART_KEY}@{name}: Dear @Keymaker, I would like to forge a new set of keys.',
f'''@Keymaker: We will make two. A matching, "asymmetric" pair:''',
'\t1) A "public key" you can share with anyone.',
'\t2) A "private key" other no one can ever, ever see.',
'With both together, you can communicate privately and securely with anyone who also has their own key pair.'
])
while not passphrase:
passphrase1 = getpass(f'What is a *memorable* pass word or phrase? Do not write it down.\n@{name}: ')
passphrase2 = getpass(f'Could you repeat that?')
if passphrase1!=passphrase2:
self.status('Those passwords didn\'t match. Please try again.',clear=False,pause=False)
else:
passphrase=passphrase1
# if not name or name=='?': self.name = name = res['vals'].get('name')
res = self.status([
('passphrase',f'\nWhat is a memorable pass word or phrase? \n@{name}: ',getpass) if not passphrase else False
('passphrase2',f'\Could you repeat that? \n@{name}: ',getpass) if not passphrase else False
],clear=False)
if not passphrase:
p1,p2=res.get('passphrase'),res.get('passphrase2')
# if p1!=p2
# passphrase = passphrase
4 years ago
4 years ago
# make and save keys locally
uri_id,keys_returned = self.forge_new_keys(
name=name,
passphrase=passphrase,
keys_to_save = KEYMAKER_DEFAULT_KEYS_TO_SAVE_ON_CLIENT,
keys_to_return = KEYMAKER_DEFAULT_KEYS_TO_SAVE_ON_SERVER
)
self.log(f'my new uri is {uri_id} and I got new keys!: {dict_format(keys_returned)}')
# save the ones we should on server
4 years ago
data = {
4 years ago
**{'name':name, 'passphrase':self.crypt_keys.hash(passphrase.encode()), ROUTE_KEYNAME:'register_new_user'},
4 years ago
**keys_returned
}
4 years ago
self.log('sending to server:',dict_format(data,tab=2))
4 years ago
# msg_to_op = self.compose_msg_to(data, self.op)
# ring operator
4 years ago
# call from phone since I don't have pubkey on record on Op yet
resp_msg_obj = self.phone.ring_ring(data)
4 years ago
self.log('register got back from op:',dict_format(resp_msg_obj,tab=2))
4 years ago
4 years ago
4 years ago
def ring_ring(self,msg):
return super().ring_ring(msg)
4 years ago
4 years ago
def send_msg_to(self,msg,to_whom):
msg = self.compose_msg_to(msg,to_whom)
4 years ago
msg.encrypt()
4 years ago
4 years ago
{'_route':'deliver_to', 'msg':msg}
4 years ago
4 years ago
return self.ring_ring(msg)
4 years ago
4 years ago
4 years ago
4 years ago
def test_register():
import random
num = random.choice(list(range(0,1000)))
4 years ago
# botname=f'marx{str(num).zfill(3)}'
# marxbot = Persona(botname)
marxbot=Persona()
4 years ago
marxbot.register()
4 years ago
4 years ago
if __name__=='__main__':
4 years ago
marx = Persona('marx')
elon = Persona('elon')
4 years ago
4 years ago
marx.register()
4 years ago
# elon.register()
4 years ago
# person.register()
# print(person.pubkey)
4 years ago
# elon.send_msg_to('youre dumb',marx)
4 years ago
#Caller('elon').ring_ring({'_route':'say_hello','_msg':'my dumb message to operator'})
4 years ago
4 years ago
# print(marx.exists_on_server())