From 86c839f5d2af6dbfbc332c1bad25e474ec53afd9 Mon Sep 17 00:00:00 2001 From: Michael Herman Date: Wed, 3 Sep 2014 16:59:07 -0700 Subject: [PATCH] password hashing --- app.py | 4 +++- db_create_users.py | 9 +++++++++ migrations/versions/3e0014fa59ca_.py | 26 ++++++++++++++++++++++++++ migrations/versions/c1b2d6a2aa6_.py | 26 ++++++++++++++++++++++++++ models.py | 9 ++++----- 5 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 db_create_users.py create mode 100644 migrations/versions/3e0014fa59ca_.py create mode 100644 migrations/versions/c1b2d6a2aa6_.py diff --git a/app.py b/app.py index 8871b38..9a58d42 100644 --- a/app.py +++ b/app.py @@ -3,9 +3,11 @@ url_for, request, session, flash from functools import wraps from flask.ext.sqlalchemy import SQLAlchemy +from flask.ext.bcrypt import Bcrypt -# create the application object +# create the application object, pass it into Bcrypt for hashing app = Flask(__name__) +bcrypt = Bcrypt(app) # config import os diff --git a/db_create_users.py b/db_create_users.py new file mode 100644 index 0000000..ecb4b1e --- /dev/null +++ b/db_create_users.py @@ -0,0 +1,9 @@ +from app import db +from models import User + +# insert data +db.session.add(User("michael", "michael@realpython.com", "i'll-never-tell")) +db.session.add(User("admin", "ad@min.com", "admin")) + +# commit the changes +db.session.commit() diff --git a/migrations/versions/3e0014fa59ca_.py b/migrations/versions/3e0014fa59ca_.py new file mode 100644 index 0000000..5b70472 --- /dev/null +++ b/migrations/versions/3e0014fa59ca_.py @@ -0,0 +1,26 @@ +"""empty message + +Revision ID: 3e0014fa59ca +Revises: 4a5b39003781 +Create Date: 2014-09-03 16:30:20.119025 + +""" + +# revision identifiers, used by Alembic. +revision = '3e0014fa59ca' +down_revision = '4a5b39003781' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.drop_column('users', 'password') + ### end Alembic commands ### + + +def downgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.add_column('users', sa.Column('password', sa.VARCHAR(), autoincrement=False, nullable=False)) + ### end Alembic commands ### diff --git a/migrations/versions/c1b2d6a2aa6_.py b/migrations/versions/c1b2d6a2aa6_.py new file mode 100644 index 0000000..5a21feb --- /dev/null +++ b/migrations/versions/c1b2d6a2aa6_.py @@ -0,0 +1,26 @@ +"""empty message + +Revision ID: c1b2d6a2aa6 +Revises: 3e0014fa59ca +Create Date: 2014-09-03 16:32:08.805429 + +""" + +# revision identifiers, used by Alembic. +revision = 'c1b2d6a2aa6' +down_revision = '3e0014fa59ca' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.add_column('users', sa.Column('password', sa.String(), nullable=True)) + ### end Alembic commands ### + + +def downgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.drop_column('users', 'password') + ### end Alembic commands ### diff --git a/models.py b/models.py index fc3742f..62f7fe1 100644 --- a/models.py +++ b/models.py @@ -1,4 +1,4 @@ -from app import db +from app import db, bcrypt from sqlalchemy import ForeignKey from sqlalchemy.orm import relationship @@ -28,14 +28,13 @@ class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) email = db.Column(db.String, nullable=False) - password = db.Column(db.String, nullable=False) + password = db.Column(db.String) posts = relationship("BlogPost", backref="author") - def __init__(self, name, email, password, favorite_color): + def __init__(self, name, email, password): self.name = name self.email = email - self.password = password - self.favorite_color = favorite_color + self.password = bcrypt.generate_password_hash(password) def __repr__(self): return '