· 6 years ago · Jul 04, 2019, 10:12 AM
1class Preferences(db.Model):
2 """ Store User Preferences here"""
3 __tablename__ = 'preferences'
4 id = db.Column(db.Integer, primary_key=True)
5 # some normal settings
6 # Mail Account Data
7 mail_username = db.Column(db.String(128))
8 mail_password_encrypted = db.Column(db.LargeBinary)
9
10 @property
11 def mail_password(self):
12 raise AttributeError('password is not a readable attribute')
13
14 def encrypt_mail_password(self, password):
15 """
16 Accepts the clear text password and stores it encrypted with the app´s secret key.
17
18 :param password: clear text password
19 :return:
20 """
21 secret_key = current_app.config['SECRET_KEY']
22 cryptor = rncryptor.RNCryptor()
23 encrypted_password = cryptor.encrypt(password, secret_key)
24 self.mail_password_encrypted = encrypted_password
25 db.session.commit()
26
27 def decrypt_mail_password(self):
28 """
29 Decrypts the encrypted password with the app´s secret key.
30 :return: decrypted password
31 """
32 secret_key = current_app.config['SECRET_KEY']
33 cryptor = rncryptor.RNCryptor()
34 decrypted_password = cryptor.decrypt(self.mail_password_encrypted, secret_key)
35 return decrypted_password