Skip to content
This repository has been archived by the owner on Oct 26, 2020. It is now read-only.

Require new passwords to meet security requirements. #20

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

eldenseropian
Copy link

New passwords must now be at least 8 characters long, and contain a number and a special character ($@%^!*).

This diff validates passwords both on the client side and when storing them to the DB. In addition to unit tests, I manually verified that changing a user's password in the "My Info" > "Administration" enforces the security requirements. I was not able to simulate setting the password on account creation locally, so the code to enforce it is there but has not been tested.

@eldenseropian
Copy link
Author

Just realized that the db-level validation will probably cause issues for users with existing bad passwords - will add logic to prompt users to change their bad passwords on login to this PR.

Copy link

@imogenkinsman imogenkinsman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really good - made a few suggestions.

@user.password = params[:password]
else
redirect_to({:action => "show", :id => @user.id}, {:notice => "Password does not meet our security requirements. Please use a password at least 8 characters long, including a number and a special character ($@%^!*). Help keep Trans Lifeline safe!"})
save = false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more readable way might be to remove the save variable, and then just have an early return here, which lets you remove the outer if statement below.

@@ -16,6 +16,7 @@ class User < ActiveRecord::Base

validates_confirmation_of :password, :unless => :no_password_required
validates_presence_of :password, :unless => :no_password_required
validates :password, :secure_password => true

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like the use of ActiveModel::EachValidator here 😸

@user.reload
login(@user)
redirect_to dashboard_url
if not UsersHelper.secure_password?(params[:user].slice(:password))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if not else is generally discouraged. You can flip the inner code blocks and remove the not.

There is an argument to be made for leaving it as is (it has our early exit at the start and then the meat of the method after). In that case, you can change if not to unless, which is more idiomatic.

@sarahmaeve
Copy link

Thank you for these changes!

@ninachaubal
Copy link

@jademcgough @sarahmaeve, can I merge this?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants