28
loading...
This website collects cookies to deliver better user experience
possible users
and common/weak passwords. Brute-force tests can then be conducted repeatedly against the system until access is gained.authenticate
method. Within it, you may find the following code snippet:raise "#{email} doesn't exist!" if !(user)
if user.password == Digest::MD5.hexdigest(password)
auth = user
else
raise "Incorrect Password!"
end
sessions_controller.rb
(app/controllers
folder), locate the create
method and change the following code snippetflash[:error] = e.message
flash[:error] = "Your credentials aren't valid."
validates :password,
presence: true,
confirmation: true,
length: {
within: 6..40
},
...
:format => {:with => /\A.*(?=.*[a-zA-Z])(?=.*[0-9])(?=.{10,}).*\z/},
document.cookie
in your JavaScript code.HttpOnly
flag.HttpOnly
flag can't be accessed from the JavaScript Document.cookie
API. This way, only the server will receive it.Secure
attribute, which will ensure that a cookie is only sent to the server if (and only if) the request happens within HTTPS (never within HTTP). This will make your requests safer in case someone is sniffing them as a man-in-the-middle.HttpOnly
flag. This is great because it helps unaware developers avoid having their apps hacked.session_store.rb
file, located in the config/initializers
folder. Check it out!<script>
alert(document.cookie);
</script>
HttpOnly
flag on your Rails apps.httponly: false
setting and restart the server. When you try to perform the same operation, the following alert message will be displayed:def hash_password
if self.password.present?
self.password = Digest::MD5.hexdigest(password)
end
end
gem install bcrypt
require 'bcrypt'
class User < ActiveRecord
include BCrypt
def password
@password ||= Password.new(password_hash)
end
def password=(new_password)
@password = Password.create(new_password)
self.password_hash = @password
end
end
password_hash
) at the table to store the password hash for the BCrypt algorithm.users_controller.rb
file in the api/v1
folder. There, you will find the following method:def show
respond_with @user.as_json
end
user
model but also other models that hold sensitive information need a way to select attributes that will be visible to APIs.as_json
method with the following:def as_json
super(only: [:id, :email])
end