From 4e63a82535f573962acf33b20727f123e057a58f Mon Sep 17 00:00:00 2001 From: Antoine Guenet Date: Fri, 9 Feb 2018 13:43:35 +0100 Subject: [PATCH 1/3] Update for pep8 and pep257 compliance (including docstrings). --- Lesson_4/02_Adding Users and Logins/models.py | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/Lesson_4/02_Adding Users and Logins/models.py b/Lesson_4/02_Adding Users and Logins/models.py index b665a04..dfe117a 100644 --- a/Lesson_4/02_Adding Users and Logins/models.py +++ b/Lesson_4/02_Adding Users and Logins/models.py @@ -1,25 +1,46 @@ -from sqlalchemy import Column,Integer,String +"""Create a users database containing a User model.""" + +from sqlalchemy import Column, Integer, String from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relationship, sessionmaker from sqlalchemy import create_engine from passlib.apps import custom_app_context as pwd_context Base = declarative_base() + + class User(Base): + """Define the User model for the database.""" + __tablename__ = 'user' id = Column(Integer, primary_key=True) username = Column(String(32), index=True) password_hash = Column(String(64)) def hash_password(self, password): + """Store a hash of a plain user password string in the User table. + + Called when: + - a new user is registering with the server + - a user changes their password + + Argument: + password (string): plain password + """ self.password_hash = pwd_context.encrypt(password) def verify_password(self, password): + """Return true if a password is correct, false if it's not. + + Called when a user provides credentials that they need to be validated + + Argument: + password (string): plain password + """ return pwd_context.verify(password, self.password_hash) engine = create_engine('sqlite:///users.db') - + Base.metadata.create_all(engine) - From c511a891c07057b3124a9d8d75fbe2138d000063 Mon Sep 17 00:00:00 2001 From: Antoine Guenet Date: Fri, 9 Feb 2018 13:44:32 +0100 Subject: [PATCH 2/3] Add comments based on lesson for better understanding. --- Lesson_4/02_Adding Users and Logins/models.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Lesson_4/02_Adding Users and Logins/models.py b/Lesson_4/02_Adding Users and Logins/models.py index dfe117a..1233cc1 100644 --- a/Lesson_4/02_Adding Users and Logins/models.py +++ b/Lesson_4/02_Adding Users and Logins/models.py @@ -4,6 +4,10 @@ from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relationship, sessionmaker from sqlalchemy import create_engine + +# Passlib is dedicated to password hashing +# custom_app_context is an easy to use option +# base on the SHA256 hashing algorithm from passlib.apps import custom_app_context as pwd_context Base = declarative_base() @@ -15,6 +19,11 @@ class User(Base): __tablename__ = 'user' id = Column(Integer, primary_key=True) username = Column(String(32), index=True) + + # The hash of the user's password so passlib can verify a password by: + # 1. hashing it with the same function that was used during registration + # 2. compare the resulting hash against this one, stored in the database + # Thanks to this system, we never have to store a password in the database password_hash = Column(String(64)) def hash_password(self, password): From b734753b2dbdf750772b4b3fbd351601c7540283 Mon Sep 17 00:00:00 2001 From: Antoine Guenet Date: Fri, 9 Feb 2018 13:54:51 +0100 Subject: [PATCH 3/3] Add shebang. --- Lesson_4/02_Adding Users and Logins/models.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lesson_4/02_Adding Users and Logins/models.py b/Lesson_4/02_Adding Users and Logins/models.py index 1233cc1..e7f43d7 100644 --- a/Lesson_4/02_Adding Users and Logins/models.py +++ b/Lesson_4/02_Adding Users and Logins/models.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + """Create a users database containing a User model.""" from sqlalchemy import Column, Integer, String