def test_encrypt_decrypt(self): pgp = nova.pgp.OpenPGP() pgp.create_key(self._userid_A, self._email_A, self._password_A) pgp.create_key(self._userid_B, self._email_B, self._password_B) with self.assertRaises(nova.exceptions.ParameterError): pgp.encrypt(True, self._userid_A) pgp.encrypt(self._message, 120) with self.assertRaises(nova.exceptions.EncryptionError): pgp.encrypt(self._message, 'userid_undefined') encrypted_message = pgp.encrypt(self._message, self._userid_A) decrypted_message = pgp.decrypt(encrypted_message, self._userid_A, self._password_A) self.assertEqual(self._message, decrypted_message) with self.assertRaises(nova.exceptions.DecryptionError): decrypted_message = pgp.decrypt(encrypted_message, self._userid_B, self._password_B) encrypted_message = pgp.encrypt(self._message, [self._userid_A, self._userid_B]) decrypted_message_A = pgp.decrypt(encrypted_message, self._userid_A, self._password_A) decrypted_message_B = pgp.decrypt(encrypted_message, self._userid_B, self._password_B) self.assertEqual(self._message, decrypted_message_A) self.assertEqual(self._message, decrypted_message_B)
def test_delete_key(self): pgp = nova.pgp.OpenPGP() pgp.create_key(self._userid_A, self._email_A, self._password_A) with self.assertRaises(nova.exceptions.ParameterError): pgp.delete_key(123908) pgp.delete_key(self._userid_A) self.assertIsNone(self._connector.get('_keys', self._userid_A))
def test_update_password(self): pgp = nova.pgp.OpenPGP() pgp.create_key(self._userid_A, self._email_A, self._password_A) with self.assertRaises(nova.exceptions.ParameterError): pgp.update_password(120931, self._password_A, self._password_B) pgp.update_password(self._userid_A, 8309123, self._password_B) pgp.update_password(self._userid_A, self._password_A, 1289379) encrypted_message = pgp.encrypt(self._message, self._userid_A) pgp.update_password(self._userid_A, self._password_A, self._password_B) decrypted_message = pgp.decrypt(encrypted_message, self._userid_A, self._password_B) self.assertEqual(self._message, decrypted_message)
def create(self, email, password, role='user'): if not isinstance(email, str) or not re.match(r'[^@]+@[^@]+\.[^@]+', email): raise nova.exceptions.ParameterError('The first parameter has to be a valid email address.') if not isinstance(password, str) and len(password) < 6: raise nova.exceptions.ParameterError('The second parameter has to be a valid password (at least 6 characters long).') if not isinstance(role, str) and role not in self._config.get_roles(): raise nova.exceptions.ParameterError('The role parameter has to be a valid role from config.database.DB_ROLES.') try: userid, user = self._find_user_by_email(email) raise nova.exceptions.UserExistsError('The user with the email "' + email + '" you tried to create already exists.') except nova.exceptions.UserNotFoundError as e: pass pgp = nova.pgp.OpenPGP() self.role = role self.userid = 'userid_' + uuid.uuid4().hex self.salt = uuid.uuid4().hex[1:9] self.hashed_password = hashlib.sha512(password.encode('utf-8')).hexdigest() self.email = email if role == 'admin': self.verified = True try: if not self._accessor.exists('_users'): self._accessor.set('_users', {}) self._accessor.set('_users.' + self.userid, { 'password': self.hashed_password, 'role': self.role, 'salt': self.salt, 'email': self.email, 'verified': self.verified, 'created': int(time.time()) }) except (nova.exceptions.ParameterError, nova.exceptions.DatabaseWriteError) as e: raise nova.exceptions.DatabaseWriteError('Could not write the new user to the database.') pgp.create_key(self.userid, self.email, password)
def test_create_key(self): pgp = nova.pgp.OpenPGP() pgp.create_key(self._userid_A, self._password_A, self._email_A) self.assertIsNotNone(self._connector.get('_keys', self._userid_A))