Giter Site home page Giter Site logo

sinatra-password-security's Introduction

Securing Passwords

Objectives

  1. Learn about bcrypt, a gem that works to encrypt passwords.
  2. Learn about Active Record's has_secure_password method.
  3. Sign up and log in a user with a secure, encrypted password.

Overview

Securing users' data is one of the most important jobs of a web developer. Despite frequent warnings against it, many of your users will use the same username and password combination across many different websites. This means that, in general, people will use the same password for our applications that they do for their bank.

Because of this, we never want to store our users' passwords in plain text in our database. Instead, we'll run the passwords through a hashing algorithm. A hashing algorithm manipulates data in such a way that it cannot be un-manipulated. This is to say that if someone got a hold of the hashed version of a password, they would have no way to turn it back into the original. In addition to hashing the password, we'll also add a "salt". A salt is simply a random string of characters that gets added into the hash. That way, if two of our users use the password "fido", they will end up with different hashes in our database.

We'll use an open-source gem, bcrypt, to implement this strategy.

Starter Code

We've got a basic Sinatra MVC application. In our application_controller we have two helper methods defined: logged_in? returns true or false based on the presence of a session[:user_id], and current_user returns the instance of the logged in user, based on the session[:user_id]. We have six actions defined:

  • get "/" renders an index.erb file with links to signup or login.
  • get '/signup' renders a form to create a new user. The form includes fields for username and password.
  • get '/login' renders a form for logging in.
  • get '/success' renders a success.erb page, which should be displayed once a user successfully logs in.
  • get '/failure' renders a failure.erb page. This will be accessed if there is an error logging in or signing up.
  • get '/logout' clears the session data and redirects to the homepage.

We've also stubbed out a user model in app/models/user.rb that inherits from ActiveRecord::Base.

Fork and clone this repository and run bundle install to get started!

Password Encryption with BCrypt

BCrypt will store a salted, hashed version of our users' passwords in our database in a column called password_digest. Essentially, once a password is salted and hashed, there is no way for anyone to decode it. This method requires that hackers use a 'brute force' approach to gain access to someone's account –– still possible, but more difficult.

Implementing BCrypt

We've created a migration file for you, but you'll need to fill it in. For now, we'll use def up and def down methods for this lab, but note that you will often see def change now when generating migrations. Let's edit that file so that it actually creates a users table. We'll have two columns: one for username and one for password_digest.

class CreateUsers < ActiveRecord::Migration[5.1]
  def up
    create_table :users do |t|
      t.string :username
      t.string :password_digest
    end
  end

  def down
    drop_table :users
  end
end

Run this migration using rake db:migrate. Preview your work by running shotgun and navigating to localhost:9393 in your browser. Awesome job!

NOTE: If you're in the Learn IDE, instead of going to localhost:9393 you'll navigate to the URL output by the shotgun command.

ActiveRecord's has_secure_password

Next, let's update our user model so that it includes has_secure_password. This ActiveRecord macro gives us access to a few new methods. A macro is a method that when called, creates methods for you. This is meta programming, which you don't need to worry about now. Just know that using a macro is just like calling a normal ruby method.

In this case, the macro has_secure_password is being called just like a normal ruby method. It works in conjunction with a gem called bcrypt and gives us all of those abilities in a secure way that doesn't actually store the plain text password in the database.

class User < ActiveRecord::Base
  has_secure_password
end

Next, let's handle signing up. In our post '/signup' action, let's make a new instance of our user class with a username and password from params. Note that even though our database has a column called password_digest, we still access the attribute of password. This is given to us by has_secure_password. You can read more about that in the Ruby Docs.

post "/signup" do
  user = User.new(:username => params[:username], :password => params[:password])
end

Because our user has has_secure_password, we won't be able to save this to the database unless our user filled out the password field. Calling user.save will return false if the user can't be persisted. Let's update this route so that we redirect to '/login' if the user is saved, or '/failure' if the user can't be saved. (For now, we'll make the user log in after they sign up successfully).

post "/signup" do
  user = User.new(:username => params[:username], :password => params[:password])

  if user.save
    redirect "/login"
  else
    redirect "/failure"
  end
end

Awesome! Test this feature out in your browser. Leaving the password field blank should land you at the "failure" page, while creating a valid user should take you to login.

Next, create at least one valid user, then let's build out our login action. In post '/login', let's find the user by username.

post "/login" do
  user = User.find_by(:username => params[:username])
end

Next, we need to check two conditions: first, did we find a user with that username? This can be written as user != nil or simply user.

post "/login" do
  user = User.find_by(:username => params[:username])
  if user
    redirect "/success"
  else
    redirect "/failure"
  end
end

We also need to check if that user's password matches up with the value in password_digest. Users must have both an account and know the password.

We validate password match by using a method called authenticate on our User model. We do not have to write this method ourselves. Rather when we added the line of code to User:

class User < ActiveRecord::Base
  has_secure_password
end

we told Ruby to add an authenticate method to our class (invisibly!) when the program runs. While we, as programmers can't see it, it will be there.

ASIDE This is one of the special powers of Ruby called "metaprogramming:" writing code that writes code. Ruby code can run methods on itself so that classes gain new methods or state when the code runs! Pretty cool! Ruby and only a few other languages have this ability.

Using metaprogramming is controversial, though. On the one hand, it can save developers time. On the other, and we see that in this lesson, it would be nice to point to where on some line, in some file, the authenticate method was defined. Reasonable developers can have differences of opinion as to whether to use metaprogramming. Understanding metaprogramming perfectly is not essential to being a Ruby or Rails developer.

Let's step through the process of how User's authenticate method works. It:

  1. Takes a String as an argument e.g. i_luv@byron_poodle_darling
  2. It turns the String into a salted, hashed version (76776516e058d2bf187213df6917a7e)
  3. It compares this salted, hashed version with the user's stored salted, hashed password in the database
  4. If the two versions match, authenticate will return the User instance; if not, it returns false

IMPORTANT At no point do we look at an unencrypted version of the user's password.

In the code below, we see how we can ensure that we have a User AND that that User is authenticated. If the user authenticates, we'll set the session[:user_id] and redirect to the /success route. Otherwise, we'll redirect to the /failure route so our user can try again.

post "/login" do
  user = User.find_by(:username => params[:username])

  if user && user.authenticate(params[:password])
    session[:user_id] = user.id
    redirect "/success"
  else
    redirect "/failure"
  end
end

Awesome job! We've now built out a basic authentication system for a user without storing a plain-text password in our database.

Resources

sinatra-password-security's People

Contributors

annjohn avatar aturkewi avatar bal360 avatar bhollan avatar curiositypaths avatar dakotalmartinez avatar deniznida avatar dependabot[bot] avatar drakeltheryuujin avatar gj avatar howardbdev avatar ihollander avatar ipc103 avatar kaylee42 avatar lizbur10 avatar maxwellbenton avatar onyoo avatar pletcher avatar rrcobb avatar ruchiramani avatar schanrai avatar sgharms avatar sophiedebenedetto avatar victhevenot avatar vinnyalfieri avatar weezwo avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sinatra-password-security's Issues

database error

In this lab, if when in the CreateUsers migration file, if I used the more recent def change ... end syntax, the tests will not recognize that there is a users table. In the screenshots I changed absolutely nothing, except that in one I used the up/down syntax, in the other I used the change syntax. This is very frustrating as we have been taught that it makes no difference, in fact that it is recommended in an earlier lesson, to just use change. I had a pry command stop the flow of tests, to explain the screenshots further.
errordata
errordata2
errordata3

session[:id] inconsistency

The session[:id] variable is inconsistent in this code-along styled lab.

The method as written in the lab will fail without being changed:
post "/login" do
user = User.find_by(:username => params[:username])

if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect "/success"
else
redirect "/failure"
end
end

shotgun before migration?

Was having trouble running rake db:migrate, was complaining about the CreateUsers constant.

I think you should have us edit the migration file before we shotgun, so we don't see the error in browser.

The following should probably be after the "IMPLEMENTING BCRYPT" section.

Preview your work by running shotgun and navigating to http://localhost:9393 in your browser.

test database is here on github, get's cloned down

from this issue raised by student in student repo: I know other people have completed this lab without this problem, so it's possible I did something wrong, but wanted to raise this issue. Upon initial cloning and trying to run
from this issue raised by a student in student repo:
learn I received an error and was told to run rake db:migrate SINATRA_ENV=test. However, when I did so I received this error message:

SQLite3::SQLException: table "users" already exists: CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "username" varchar, "password_digest" varchar) /Users/kaylee/.rvm/gems/ruby-2.2.0/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `initialize'

(full stack trace not included as it was very long)

This happened even after deleting the repo and forking/cloning again, and no matter with order I ran rake db:migrate and rake db:migrate SINATRA_ENV=test in. The problem was eventually solved by deleting the following files: db/schema.rb and db/test.sqlite before running migrations.

Also, am not sure if the lab was intended to be just a codealong or if this was intentional, but several tests do not pass due to capybara not being able to find elements just as an FYI.

Typo in Securing Passwords in Sinatra

In the lab https://learn.co/tracks/online-software-engineering-structured/sinatra/activerecord/securing-passwords-in-sinatra

There is an error in the code provided (codealong)
`
post "/login" do
user = User.find_by(:username => params[:username])

if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect "/success"
else
redirect "/failure"
end
end
`

If you see above the line
session[:user_id] = user.id

It uses a key of :user_id

However through the rest of the code session object is asked to look up :id
`
helpers do
def logged_in?
!!session[:id]
end

	def current_user
		User.find(session[:id])
	end
end

`

logged_in

logged_in? method on online structured master comes as

def logged_in? !!session[:id] end
instead of using :user_id

double quotation marks in index.erb fail tests

index.erb uses double quotation marks, and the test on sinatra_password_security_spec.rb:9 uses single quotation marks. Ugh. Also, login.erb and signup.erb should have a value of "submit" to pass the tests.

Quick Fix-Typo, Missing Word "to"

Last line is missing a "to"


We also need to check if that user's password matches up with the value in password_digest. Users must have both an account and know the password.

We validate password match by using a method called authenticate on our User model. We do not have write this method ourselves. Rather when we added the line of code to User:

typo in spec file: create_users_migration_spec.rb

There was an extra quotation mark ( ' ) at the end of the top line, causing the 'learn' command to fail.

original:
require_relative '../db/migrate/20150916154312_create_users.rb''

edited:
require_relative '../db/migrate/20150916154312_create_users.rb'

ActiveRecord version to be updated in the gemfile

Bundler loads version 5.2 which raises the following error:

config.ru:3:in block in <main>': undefined method needs_migration?' for ActiveRecord::Migrator:Class (NoMethodError)

Update gemfile (as follows) to resolve the error-
gem "activerecord", '<= 5.1'

the instructions are breaking the spec file

The instructions tell students to create a migration
screen shot 2016-03-16 at 10 59 26 pm

but this lab is requiring a specific migration in the create_users_migration_spec.rb
screen shot 2016-03-16 at 10 59 38 pm

The newly created migrations timestamp will never match the required migration

Mistake in the read me

Where it says session[:user_id] = user.id , it is supposed to be session[:id] as it says in the current_user helper method of this lab. All tests passed once I changed it to session[:id]

rspec error

The timestamp for the migration in the db folder doesn't match the timestamp of the file required in the rspec, meaning that tests will not run properly unless the student manually edits the required file in rspec

Quick Typo Fix: Missing "to"

2nd Line is missing word "to".

We validate password match by using a method called authenticate on our User model. We do not have write this method ourselves. Rather when we added the line of code to User:

Shotgun not included in gemfile

Get an error when attempt to run shotgun with this lab (error below)

[18:55:37] (master) sinatra-password-security-v-000 // ♥ shotgun /usr/local/rvm/rubies/ruby-2.2.3/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:133:inrequire': cannot load such file -- rack/com
monlogger (LoadError)
from /usr/local/rvm/rubies/ruby-2.2.3/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:133:in rescue in require' from /usr/local/rvm/rubies/ruby-2.2.3/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:40:inrequire'
from /usr/local/rvm/gems/ruby-2.2.3/gems/shotgun-0.9.1/bin/shotgun:112:in <top (required)>' from /usr/local/rvm/gems/ruby-2.2.3/bin/shotgun:22:inload'
from /usr/local/rvm/gems/ruby-2.2.3/bin/shotgun:22:in <main>'

I did attempt just add the shotgun gem to the gemfile and run bundle install again. Which did start up the server but when I viewed in my browser it gave me a boot error - problem with loading the config.ru file

Extra apostrophe in require_relative statement in spec

D, [2016-03-21T19:47:21.894337 #3761] DEBUG -- :   ActiveRecord::SchemaMigration Load (0.1ms)  SELECT "schema_migrations".* FROM "schema_migrations"
/Users/yonk/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load': /Users/yonk/Development/code/sinatra-password-security-v-000/spec/create_users_migration_spec.rb:3: syntax error, unexpected tIDENTIFIER, expecting end-of-input (SyntaxError)
require_relative 'spec_helper'
                             ^
    from /Users/yonk/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/yonk/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/yonk/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
    from /Users/yonk/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `block in load_spec_files'
    from /Users/yonk/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `each'
    from /Users/yonk/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `load_spec_files'
    from /Users/yonk/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:106:in `setup'
    from /Users/yonk/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:92:in `run'
    from /Users/yonk/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:78:in `run'
    from /Users/yonk/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:45:in `invoke'
    from /Users/yonk/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.4.4/exe/rspec:4:in `<top (required)>'
    from /Users/yonk/.rvm/gems/ruby-2.3.0/bin/rspec:23:in `load'
    from /Users/yonk/.rvm/gems/ruby-2.3.0/bin/rspec:23:in `<main>'
    from /Users/yonk/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
    from /Users/yonk/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
[19:47:21] (master) sinatra-password-security-v-000
# ./spec/create_users_migration_spec
require_relative '../db/migrate/20150916154312_create_users.rb''

Need to comment out this code to make the lab work

You get the following error in this lab before you even type anything:
NoMethodError:
undefined method `migration_context' for #ActiveRecord::ConnectionAdapters::SQLite3Adapter:0x007f99fdcb2d40
Did you mean? migration_keys

The solution is to comment out the following code in config.ru:

if ActiveRecord::Base.connection.migration_context.needs_migration?

raise 'Migrations are pending. Run rake db:migrate to resolve the issue.'

end

Thank you.

Quick Fix- Missing Word "to"

2nd line from lesson below is missing word "to".


We validate password match by using a method called authenticate on our User model. We do not have write this method ourselves. Rather when we added the line of code to User:

class User < ActiveRecord::Base
has_secure_password
end
we told Ruby to add an authenticate method to our class (invisibly!) when the program runs. While we, as programmers can't see it, it will be there.


Note: A minor thing, but want Flatiron to be all it can be! Thanks, Everyone.

ActiveRecord version problems

Worked through this issue on another lab this morning with tech coaches. In order to complete this lab, I had to revert back to ActiveRecord 4.2, change the inheritance in the class file (remove 5.2), change the gemfile, bundle install, and set everything up again. Not sure if this issue is happening with other labs, but it's worth fixing.

from migration file:
`class CreateUsers < ActiveRecord::Migration#[5.2]
def up
end

def down
end
end`

gemfile
`source "https://rubygems.org"

gem "sinatra"
gem "activerecord", "4.2", :require => 'active_record' #<-----reverted back to activerecord 4.2
gem "sinatra-activerecord"
gem "rake"
gem "bcrypt"
gem "rspec"
gem "require_all"
gem "capybara"
gem "pry"
gem "sqlite3"
gem 'shotgun'

group :development do
gem "tux"
end`

syntax issues with session[user_id]

The pre-written helper methods (application_controller.rb lines 64, 68):
helpers do
def logged_in?
!!session[user_id]
end

	def current_user
		User.find(session[user_id])
	end
end

we need to modify these to session[:user_id] in order to access the user's session ID correctly. The above code will throw an error for the last spec test. modifying to session[:user_id] fixes issue. Wasn't part of the lesson directions or tests, so looked like potential syntax issue

in progress

Refer to Readme for outline of content to be added.

Deadline- 9/15/2015

Typo in one line

When it says "We have five actions defined:" it really is six!

needs_migration? no_method error

if ActiveRecord::Migrator.needs_migration?

This line of code was throwing an error of no_method on the needs_migration? method when I was running my tests. I was able to comment out the code and have my tests pass, as I had already migrated SINATRA_ENV=test.

However, it can be solved by updating the gemfile
gem "activerecord", '<= 5.1'

Following code along will not pass the tests

@SophieDeBenedetto... I am not 100% sure if I did something wrong, but I had to make a few minor adjustments to the code provided in the lab and in the instructions to get the tests passing. Again, not sure if this was an error on my end, but I think I followed the directions correctly. Probably worth looking into.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.