2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-05 21:20:51 +00:00
Comrad/komrade/backend/komrades.py

355 lines
11 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-14 06:02:17 +00:00
class KomradeX(Caller):
2020-09-08 11:23:41 +00:00
2020-09-14 08:25:55 +00:00
def __init__(self, name=None, pubkey=None):
super().__init__(name=name)
2020-09-14 06:02:17 +00:00
# self.log(f'booted komrade with {name} and {passphrase} and\n\n{dict_format(self.keychain())}')
2020-09-13 19:03:29 +00:00
# if SHOW_STATUS:
# from komrade.cli import CLI
# self.cli = CLI(name=name, komrade=self)
2020-09-13 19:26:55 +00:00
self.boot(create=False)
2020-09-13 19:04:43 +00:00
# self.name=name
# pass
2020-09-08 12:11:13 +00:00
2020-09-13 18:55:14 +00:00
def boot(self,create=False,ping=False):
2020-09-13 18:47:25 +00:00
# Do I already have my keys?
# yes? -- login
2020-09-08 12:11:13 +00:00
2020-09-13 18:47:25 +00:00
keys = self.keychain()
2020-09-14 07:58:26 +00:00
self.log(f'booting {self}!',dict_format(keys))
2020-09-13 18:47:25 +00:00
if keys.get('pubkey') and keys.get('privkey'):
2020-09-14 06:20:02 +00:00
# self.log('already booted! @'+self.name)
2020-09-13 18:47:25 +00:00
return True
2020-09-09 14:38:37 +00:00
2020-09-13 18:55:14 +00:00
if self.exists_locally_as_account():
self.log(f'this account (@{self.name}) can be logged into')
return self.login()
elif self.exists_locally_as_contact():
self.log(f'this account (@{self.name}) is a contact')
return #pass #???
elif ping and self.exists_on_server():
self.log(f'this account exists on server. introduce?')
return
elif create:
self.log('account is free: register?')
return self.register()
2020-09-09 14:38:37 +00:00
2020-09-14 04:43:59 +00:00
def exists_locally(self):
return bool(self.pubkey)
2020-09-09 17:34:19 +00:00
def exists_locally_as_contact(self):
2020-09-14 04:43:59 +00:00
return bool(self.pubkey) and not bool(self.privkey)
2020-09-09 17:34:19 +00:00
2020-09-13 18:55:14 +00:00
def exists_locally_as_account(self):
2020-09-14 04:43:59 +00:00
return bool(self.pubkey) and bool(self.privkey_encr)
2020-09-09 17:34:19 +00:00
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-14 06:02:17 +00:00
# global PHONEBOOK
2020-09-13 14:15:06 +00:00
# print('got name:',name)
2020-09-12 13:11:39 +00:00
## Defaults
2020-09-10 17:52:07 +00:00
if name and not self.name: self.name=name
if not name and self.name: name=self.name
2020-09-13 13:57:06 +00:00
# if not name and not self.name: name=''
2020-09-13 14:15:06 +00:00
# print('got name',name)
2020-09-14 04:43:59 +00:00
# already have it?
if self.exists_locally_as_account():
2020-09-14 06:20:02 +00:00
return {'success':False, 'status':'You have already created this account.'}
2020-09-14 04:43:59 +00:00
if self.exists_locally_as_contact():
2020-09-14 06:20:02 +00:00
return {'success':False, 'status':'This is already a contact of yours'}
2020-09-14 04:43:59 +00:00
2020-09-12 13:11:39 +00:00
## 1) Have name?
2020-09-13 13:57:06 +00:00
tolog=''
2020-09-11 14:35:47 +00:00
if SHOW_STATUS and show_intro:
2020-09-13 13:57:06 +00:00
self.name = name = self.cli.status_keymaker_part1(name)
2020-09-12 13:17:54 +00:00
elif not name:
2020-09-13 13:57:06 +00:00
self.name = name = input('\nHello, this is Komrade @')
print('\nI would like to sign up for the socialist network revolution.',flush=True)
do_pause()
else:
print(f'Hello, this is Komrade @{name}.\n\nI would like to sign up for the socialist network revolution.')
do_pause()
clear_screen()
self.log(f'@Keymaker: Excellent. But to communicate with komrades securely,\nyou must first cut your public & private encryption keys. ')
# do_pause()
2020-09-12 13:11:39 +00:00
## 2) Make pub public/private keys
2020-09-11 14:35:47 +00:00
keypair = KomradeAsymmetricKey()
pubkey,privkey = keypair.pubkey_obj,keypair.privkey_obj
2020-09-13 13:57:06 +00:00
self.log(f'@Keymaker: I have cut for you a private and public asymmetric key pair\nusing the Elliptic Curve algorithm from Themis cryptography library:\n\n(1) {pubkey}\n\n(2) {privkey}{ART_KEY_PAIR}',clear=False,pause=True)
2020-09-11 14:35:47 +00:00
2020-09-12 13:11:39 +00:00
## 3) Have passphrase?
2020-09-11 17:17:21 +00:00
if SHOW_STATUS and not passphrase:
2020-09-12 14:32:03 +00:00
passphrase = self.cli.status_keymaker_part2(name,passphrase,pubkey,privkey,hasher,self)
2020-09-11 14:35:47 +00:00
else:
2020-09-13 05:33:37 +00:00
if not passphrase: passphrase = DEBUG_DEFAULT_PASSPHRASE
2020-09-13 05:39:38 +00:00
while not passphrase:
2020-09-13 13:57:06 +00:00
passphrase=getpass(f'@Keymaker: Enter a memorable password to encrypt your private key with: \n\n@{self.name}: ')
2020-09-13 07:50:15 +00:00
clear_screen()
2020-09-12 13:11:39 +00:00
## 4) Get hashed password
2020-09-12 14:32:03 +00:00
passhash = hasher(passphrase)
2020-09-13 07:57:40 +00:00
self.log(f'''@Keymaker: I have replaced your password with a disguised, hashed version\nusing a salted SHA-256 algorithm from python's hashlib:\n\n\t{make_key_discreet_str(passhash)}''')
2020-09-12 13:11:39 +00:00
## 5) Encrypt private key
2020-09-12 14:32:03 +00:00
privkey_decr = KomradeSymmetricKeyWithPassphrase(passphrase)
2020-09-11 17:17:21 +00:00
privkey_encr = privkey_decr.encrypt(privkey.data)
2020-09-12 07:55:23 +00:00
privkey_encr_obj = KomradeEncryptedAsymmetricPrivateKey(privkey_encr)
2020-09-13 13:57:06 +00:00
self.log(f"@Keymaker: Store your private key on your device hardware ONLY\nas it was encrypted by your password-generated key:\n\n[Encrypted Private Key]\n({make_key_discreet_str(privkey_encr_obj.data_b64)})")
2020-09-12 07:55:23 +00:00
2020-09-12 13:17:54 +00:00
## 6) Test keychain works
2020-09-14 04:19:55 +00:00
#privkey_decr2 = KomradeSymmetricKeyWithPassphrase(passphrase)
#assert privkey_decr2.decrypt(privkey_encr) == privkey.data
2020-09-12 13:17:54 +00:00
2020-09-12 14:32:03 +00:00
self._keychain['pubkey']=pubkey
self._keychain['privkey_encr']=privkey_encr_obj
2020-09-14 04:19:55 +00:00
self._keychain['privkey']=privkey
2020-09-12 19:31:29 +00:00
# self._keychain['privkey_decr']=privkey_decr
2020-09-12 14:32:03 +00:00
# we should be able to reassemble privkey now?
2020-09-12 19:31:29 +00:00
# self.log('this is my keychain now:')
2020-09-14 04:19:55 +00:00
#assert 'privkey' in self.keychain()
2020-09-12 13:17:54 +00:00
2020-09-14 04:19:55 +00:00
self.log('My keychain now looks like:',dict_format(self.keychain()))
2020-09-12 13:17:54 +00:00
2020-09-14 04:43:59 +00:00
# More narration?
if SHOW_STATUS: self.cli.status_keymaker_part3(privkey,privkey_decr,privkey_encr,passphrase)
# 6) Save for now on client -- will delete if fails on server
2020-09-14 06:35:40 +00:00
2020-09-14 06:02:17 +00:00
# storing myself in memory phonebook
# PHONEBOOK[name]=self
2020-09-12 13:11:39 +00:00
## 7) Save data to server
2020-09-10 10:59:37 +00:00
data = {
2020-09-12 07:55:23 +00:00
'name':name,
2020-09-12 18:18:37 +00:00
'pubkey': pubkey.data,
2020-09-10 10:59:37 +00:00
}
2020-09-13 07:49:54 +00:00
self.log('@Keymaker: Store your public key both on your device hardware\nas well as register it with Komrade @Operator on the remote server:\n\n',dict_format(data,tab=2))
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
2020-09-12 13:11:39 +00:00
# self.log('my keychain:',self._keychain,pubkey,self.op._keychain)
2020-09-13 09:42:22 +00:00
resp_msg_d = self.ring_ring(
2020-09-12 18:18:37 +00:00
{
'name':name,
'pubkey': pubkey.data,
},
route='register_new_user'
)
2020-09-13 09:42:22 +00:00
if not resp_msg_d.get('success'):
self.log(f'Registration failed. Message from operator was:\n\n{dict_format(resp_msg_d)}')
return
# otherwise, save things on our end
2020-09-13 09:44:13 +00:00
self.log(f'Registration successful. Message from operator was:\n\n{dict_format(resp_msg_d)}')
2020-09-13 10:57:06 +00:00
2020-09-13 09:42:22 +00:00
self.name=resp_msg_d.get('name')
2020-09-13 13:04:44 +00:00
pubkey_b = resp_msg_d.get('pubkey')
2020-09-14 04:43:59 +00:00
assert pubkey_b == pubkey.data
2020-09-14 06:35:40 +00:00
uri_id = pubkey.data_b64
2020-09-13 13:04:44 +00:00
sec_login = resp_msg_d.get('secret_login')
2020-09-13 09:42:22 +00:00
2020-09-13 11:36:03 +00:00
self.log(f'''Now saving name and public key on local device:''')
2020-09-14 06:35:40 +00:00
self.crypt_keys.set(name, pubkey_b, prefix='/pubkey/')
self.crypt_keys.set(uri_id, name, prefix='/name/')
2020-09-14 04:43:59 +00:00
self.crypt_keys.set(uri_id,sec_login,prefix='/secret_login/')
2020-09-10 09:34:34 +00:00
2020-09-14 08:25:55 +00:00
# store privkey pieces
self.crypt_keys.set(uri_id, privkey_encr_obj.data, prefix='/privkey_encr/')
# just to show we used a passphrase -->
self.crypt_keys.set(uri_id, KomradeSymmetricKeyWithPassphrase.__name__, prefix='/privkey_decr/')
2020-09-13 09:42:22 +00:00
# save qr too:
2020-09-14 04:43:59 +00:00
self.save_uri_as_qrcode(uri_id)
2020-09-13 10:51:01 +00:00
# self.log(f'saved public key as QR code to:\n {fnfn}\n\n{qr_str}')
2020-09-13 09:43:15 +00:00
2020-09-13 14:18:36 +00:00
return resp_msg_d
2020-09-13 09:42:22 +00:00
# done!
2020-09-13 09:43:40 +00:00
self.log(f'Congratulations. Welcome, Komrade {self}.')
2020-09-08 15:14:48 +00:00
2020-09-13 11:18:04 +00:00
@property
def secret_login(self):
return self.crypt_keys.get(
2020-09-13 12:31:33 +00:00
self.pubkey.data_b64,
prefix='/secret_login/'
2020-09-13 11:18:04 +00:00
)
2020-09-13 12:27:45 +00:00
def login(self,passphrase=None):
2020-09-14 07:58:26 +00:00
# what keys do I have?
keys = self.keychain()
# self.log('here are my keys:',dict_format(keys))
2020-09-13 12:27:45 +00:00
2020-09-14 07:58:26 +00:00
# check hardware
if not 'pubkey' in keys:
emsg='''Login impossible. I do not have this komrade's public key, much less private one.'''
# self.log()
return {'success':False, 'status':emsg}
if not 'privkey_encr' in keys:
emsg='''Login impossible. I do not have this komrade's private key on this hardware.'''
self.log(emsg)
return {'success':False, 'status':emsg}
2020-09-13 11:18:04 +00:00
2020-09-14 07:58:26 +00:00
if not 'privkey' in keys:
emsg='''Login failed. Private key did not unlock from passphrase.'''
return {'success':False, 'status': emsg}
2020-09-13 11:18:04 +00:00
# compose message
msg = {
'name':self.name,
2020-09-14 07:58:26 +00:00
'pubkey':keys['pubkey'].data,
2020-09-13 11:18:04 +00:00
'secret_login':self.secret_login
}
# ask operator and get response
resp_msg = self.ring_ring(
msg,
route='login'
)
# get result
self.log('Got result back from operator:',resp_msg)
2020-09-13 12:28:29 +00:00
return resp_msg
2020-09-13 11:18:04 +00:00
2020-09-13 18:06:41 +00:00
## MEETING PEOPLE
def find(self,someone):
if type(someone)==str:
return Komrade(name=someone)
if type(someone)==bytes:
return Komrade(pubkey=someone)
self.log('what is type of someoen here?',type(someone))
return someone
def meet(self,someone):
# get person obj
someone = self.find(someone)
self.log('got someone =',someone,type(someone))
2020-09-13 11:18:04 +00:00
2020-09-08 15:14:48 +00:00
2020-09-12 18:18:37 +00:00
def ring_ring(self,msg,route=None,**y):
if type(msg)==dict and not ROUTE_KEYNAME in msg:
msg[ROUTE_KEYNAME]=route
return super().ring_ring(msg,caller=self,**y)
2020-09-09 15:30:34 +00:00
2020-09-13 14:48:26 +00:00
2020-09-14 07:58:26 +00:00
def msg(self,someone,something):
# find or boot
someone = Komrade(someone)
self.log(f'found {someone}')
# can we?
if not someone.pubkey:
self.log(f'''Don't know the public key of {someone}!''')
2020-09-13 19:42:04 +00:00
msg_obj = self.compose_msg_to(
2020-09-14 07:58:26 +00:00
something,
someone
2020-09-13 19:42:04 +00:00
)
self.log('composed msg:',msg_obj)
2020-09-14 07:58:26 +00:00
# encrypting
2020-09-13 19:42:04 +00:00
msg_obj.encrypt()
2020-09-14 07:58:26 +00:00
# attaching
direct_msg_data = msg_obj.msg
self.log('going to send only this to op:',direct_msg_data)
# enclosing
msg_to_op = {
'deliver_from':self.pubkey.data_b64,
'deliver_to':someone.pubkey.data_b64,
'deliver_msg':direct_msg_data,
}
self.log('going to send msg to op?',msg_to_op)
2020-09-09 15:30:34 +00:00
2020-09-14 07:58:26 +00:00
return self.ring_ring(
msg_to_op,
route='deliver_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)}'
2020-09-13 07:15:44 +00:00
marxbot = Komrade(botname)
# marxbot=Komrade()
2020-09-14 06:02:17 +00:00
marxbot.register(passphrase='boo')
2020-09-09 15:30:34 +00:00
2020-09-13 19:42:04 +00:00
2020-09-13 20:24:31 +00:00
2020-09-13 19:42:04 +00:00
def test_msg():
2020-09-13 20:24:31 +00:00
z = comlink('zuck')
2020-09-13 20:03:59 +00:00
z.login(passphrase='eee')
2020-09-13 20:24:31 +00:00
s = comlink('sergey')
z.send_msg_to('you ssssssuck')
2020-09-13 19:42:04 +00:00
2020-09-14 07:58:26 +00:00
def test_loading():
2020-09-14 08:25:55 +00:00
z1 = Komrade('zuck')
print(z1.keychain())
exit()
2020-09-14 07:58:26 +00:00
z2 = Komrade(b'VUVDMgAAAC08BCMVA+0dMJXc66/W7hty669+3/3S61Q1yjmgJW8I0k3lqfDi')
print(z2)
print(z2.keychain())
2020-09-08 15:14:48 +00:00
2020-09-14 07:58:26 +00:00
pprint(PHONEBOOK)
return
# z1.login()
2020-09-09 15:41:33 +00:00
2020-09-09 17:39:30 +00:00
2020-09-14 07:58:26 +00:00
if __name__=='__main__':
test_loading()