def create_master_key(self): key_key = SymEncPasswordKey() key = SymEncKey() to_store = { 'key_key': key_key.to_dict(), 'key': key.to_dict(key_key) } key_yaml = yaml.dump(to_store, default_flow_style=False) self.commit('key', "Generated Master Key", data=key_yaml) self.key = key
class User: def __init__(self, username, full_name, email): self.username = username self.full_name = full_name self.email = email def generate_key(self): self.password = SymEncPasswordKey() self.key = ASymKey() def to_dict(self): return { 'password': self.password.to_dict(), 'key': self.key.to_dict(self.password), 'username': self.username, 'full_name': self.full_name, 'email': self.email } def decrypt_key(self): self.password = SymEncPasswordKey.from_dict(self.password) self.key = ASymKey.from_dict(self.password, self.raw_key) @staticmethod def from_dict_auth(state, decrypt = False): user = User(state['username'], state['full_name'], state['email']) user.password = state['password'] user.raw_key = state['key'] if decrypt: user.decrypt_key() user.key = ASymKey.from_dict(None, user.raw_key) return user def __str__(self): return self.username def __repr__(self): return "%s <%s>" % (self.full_name, self.email)
def load_key(self): for key in self.keys(): key_key = key['key_key'] key_key = SymEncPasswordKey.from_dict(key_key) key = key['key'] key = SymEncKey.from_dict(key_key, key) self.key = key break # There should only be a single key, or we'll just use the first one
def decrypt_key(self): self.password = SymEncPasswordKey.from_dict(self.password) self.key = ASymKey.from_dict(self.password, self.raw_key)
def generate_key(self): self.password = SymEncPasswordKey() self.key = ASymKey()