Last active
August 29, 2015 14:00
-
-
Save dengshuan/2dc171c707269c872ea5 to your computer and use it in GitHub Desktop.
flask-security encrypt_password varies on every load
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template | |
from flask.ext.sqlalchemy import SQLAlchemy | |
from flask.ext.security import Security, SQLAlchemyUserDatastore, \ | |
UserMixin, RoleMixin, login_required | |
from flask.ext.security.utils import encrypt_password, verify_password | |
# 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'] = 'sha512_crypt' | |
app.config['SECURITY_PASSWORD_SALT'] = 'fhasdgihwntlgy8f' | |
# 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='[email protected]', password='password') | |
db.session.commit() | |
# Views | |
@app.route('/') | |
#@login_required | |
def home(): | |
password = encrypt_password('mypassword') | |
print verify_password('mypassword', password) | |
return password | |
# return render_template('index.html') | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment