Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to read the Rails session secret?

I would like to access the Rails session secret programmatically (I am using it to generate a sign-on token).

Here's what I've come up with:

ActionController::Base.session.first[:secret]

This returns the session secret. However, every time you call ActionController::Base.session it adds another entry to an array so you end up with something like this:

[{:session_key=>"_new_app_session", :secret=>"totally-secret-you-guys"}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]

This strikes me as being no good.

Is there a better way to access the session secret?

like image 343
Luke Francl Avatar asked Oct 10 '08 16:10

Luke Francl


3 Answers

For Rails4

Rails.configuration.secret_token
Rails.configuration.secret_key_base

For Rails3

Rails.configuration.secret_token

But if for Rails2.x, like Don Parish mentioned

ActionController::Base.session_options[:secret]
like image 140
choonkeat Avatar answered Sep 23 '22 12:09

choonkeat


Thanks, Jake.

Since the secret doesn't change based on the request or the action, this also works:

ActionController::Base.session_options_for(nil,nil)[:secret]
like image 34
Luke Francl Avatar answered Sep 21 '22 12:09

Luke Francl


ActionController::Base.session_options_for(request,params[:action])[:secret]
like image 25
whoisjake Avatar answered Sep 22 '22 12:09

whoisjake