0

I need some help with this error, I have created a class User in my store_user_db.py file which basically store an encrypted password in the db using Fernet cryptography library. After the password is stored I need a function that decrypt the password so I have created a def decrypt_pwd function in the User class as below, the problem is that when I try to call this function from py other form.py file after importing the store_user_db with User class, it return the error:'module' object has no attribute 'decrypt_pwd'

Have I missed something? please edit my code in the correct way if you can.

#store_user_db.py
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data-users2.sqlite"
db = SQLAlchemy(app)


class User(db.Model):
    __tablename__ = "user"
    id = db.Column(db.Integer(), primary_key=True, autoincrement=True)
    username = db.Column(db.String(64), unique=True)
    pwd = db.Column(db.LargeBinary(), unique=True)

    def __init__(self, id, username, pwd):
        key = Fernet.generate_key()
        f = Fernet(key)
        token = f.encrypt(pwd)
        self.id = id
        self.username = username
        self.pwd = f.encrypt(pwd)

        def decrypt_pwd(self):
            p = db.session.query(User).filter_by(username="ita_itf").first()
            DESTINATION_PSW = f.decrypt(p.pwd)
            return DESTINATION_PSW

here I call the function:

        #form.py
        from store_user_db import User, db 
        import store_user_db
        
        DICP_FTP_DESTINATION_PSW = store_user_db.decrypt_pwd()

ERROR:

AttributeError: 'module' object has no attribute 'decrypt_pwd'

1 Answers1

0

try this code:

#store_user_db.py
class User(db.Model):
    __tablename__ = "user"
    id = db.Column(db.Integer(), primary_key=True, autoincrement=True)
    username = db.Column(db.String(64), unique=True)
    pwd = db.Column(db.LargeBinary(), unique=True)

    def __init__(self, username, pwd):
        self.username = username
        self.pwd = f.encrypt(pwd)

    
def decrypt_pwd():
    key = Fernet.generate_key()
    f = Fernet(key)
    token = f.encrypt(pwd)
    p = db.session.query(User).filter_by(username="ita_itf").first()
                DESTINATION_PSW = f.decrypt(p.pwd)
                return DESTINATION_PSW

#form.py
from store_user_db import decrypt_pwd
DICP_FTP_DESTINATION_PSW = decrypt_pwd()
Ahmad_R
  • 62
  • 1
  • 8
  • Yes! this is correct! now I have another small problem, if I write it that way it says that I didn't pass neither one argument to User() class, and that I should pass 4 arguments like defines in the class, but what if I just want to pass 0 ? Maybe if I put Kwargs** as argument in the class it should solve the issue? Kwargs should pass whatever argument even 0 right? – Marcello Ligi Sep 16 '20 at 08:09
  • ok now it returns an error: cryptography.fernet.InvalidToken no idea I have stored the token in the db now I'm retrieving it and decrypting it, it should work but seems invalid – Marcello Ligi Sep 21 '20 at 14:34
  • Decrypts a Fernet token. If successfully decrypted you will receive the original plaintext as the result, otherwise an exception will be raised. It is safe to use this data immediately as Fernet verifies that the data has not been tampered with prior to returning it. – Marcello Ligi Sep 21 '20 at 14:36