Skip to content

Instantly share code, notes, and snippets.

Created October 12, 2014 13:09
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Demonstration of Flask-Security #328
from flask import Flask, render_template
from flask.ext.sqlalchemy import SQLAlchemy
from import Security, SQLAlchemyUserDatastore, \
UserMixin, RoleMixin, login_required
from import *
# 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'] = "pbkdf2_sha512"
app.config['SECURITY_PASSWORD_SALT'] = "lorem ipsum dolor sit amet"
# Create database connection object
db = SQLAlchemy(app)
# Define models
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('')),
db.Column('role_id', db.Integer(), db.ForeignKey('')))
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)
with app.app_context():
user_datastore.create_user(email='', password='password')
p = User.query.filter_by(email='').first()
print "Password: %s" % p.password
print "Accepted: %s" % verify_and_update_password('password', p)
print ""
print "Password: %s" % p.password
print "Accepted: %s" % verify_and_update_password('password', p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment