0

I'm using Flask-Security to implement an authentication system.

Here is the code I use coming from the document, I've only added SECURITY_PASSWORD_HASH and SECURITY_PASSWORD_SALT configs:

from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from flask_security import Security, SQLAlchemyUserDatastore, \
    UserMixin, RoleMixin, login_required

# Create app
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'super-secret'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'

app.config['SECURITY_PASSWORD_HASH'] = 'bcrypt'
app.config['SECURITY_PASSWORD_SALT'] = 'mypasswordsaltis12characterslong'  


# Create database connection object
db = SQLAlchemy(app)

# Define models
roles_users = db.Table('roles_users',
        db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
        db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))

class Role(db.Model, RoleMixin):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))
    active = db.Column(db.Boolean())
    confirmed_at = db.Column(db.DateTime())
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))

# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)

# Create a user to test with
@app.before_first_request
def create_user():
    db.create_all()
    user_datastore.create_user(email='matt@nobien.net', password='password')
    db.session.commit()

# Views
@app.route('/')
@login_required
def home():
    return render_template('index.html')

if __name__ == '__main__':
    app.run()

I don't really understand why my password is stored in plain text within the db. Also, when I try to display the password protected view and type in the credentials I get this error message

ValueError: expected des_crypt hash, got des_crypt config string instead

I really feel stuck at this point, it looks like my password is stored in plain text which I definitely don't want to.

Thanks for your help

doingmybest
  • 314
  • 4
  • 16
  • you read this post ? there are a bug in Flask-security https://stackoverflow.com/questions/23457827/flask-security-encrypt-passwordmypassword-varies-every-time-when-i-reload-th – Druta Ruslan May 19 '18 at 16:19

1 Answers1

1

So I had to add

from flask_security.utils import encrypt_password, verify_password

and at the password storage level I had to call the encrypt_password function

user_datastore.create_user(email='email@email.com', password=encrypt_password("my_password"))
doingmybest
  • 314
  • 4
  • 16