Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sign the auth token cookie and make it httpOnly #215

Merged
merged 1 commit into from Feb 21, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Expand Up @@ -117,7 +117,7 @@ def log_on_user(user)
user.auth_token = SecureRandom.hex(16)
user.save!
end
cookies.permanent[:_t] = user.auth_token
cookies.permanent.signed[:_t] = { :value => user.auth_token, :httponly => true }
end

# This is odd, but it seems that in Rails `render json: obj` is about
Expand Down
4 changes: 2 additions & 2 deletions lib/current_user.rb
Expand Up @@ -2,7 +2,7 @@ module CurrentUser

def self.lookup_from_env(env)
request = Rack::Request.new(env)
auth_token = request.cookies["_t"]
auth_token = request.cookies[:_t]
user = nil
if auth_token && auth_token.length == 32
user = User.where(auth_token: auth_token).first
Expand All @@ -16,7 +16,7 @@ def current_user

if session[:current_user_id].blank?
# maybe we have a cookie?
auth_token = cookies[:_t]
auth_token = cookies.signed[:_t]
if auth_token && auth_token.length == 32
@current_user = User.where(auth_token: auth_token).first
session[:current_user_id] = @current_user.id if @current_user
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/session_controller_spec.rb
Expand Up @@ -38,7 +38,7 @@
end

it 'sets a cookie with the auth token' do
cookies[:_t].should == user.auth_token
cookies.signed[:_t].should == user.auth_token
end
end

Expand Down