1

I am unable to read the variables I defined in environment.rb file for omniauth.

This is my code:

environment.rb

ENV['LINKEDIN_KEY'] = "key"
ENV['LINKEDIN_SECRET'] = "secret"

omniauth.rb initializer

OmniAuth.config.logger = Rails.logger

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :linkedin, ENV['linkedin_key'], ENV['linkedin_secret']
end

This only works for me if I hard code the key and secret. I am using Rails 4 and Ruby 2.

In the main example of omniauth they used it the same way:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :developer unless Rails.env.production?
  provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
end

I would love some help. Thanks.

sscirrus
  • 55,407
  • 41
  • 135
  • 228
guy schaller
  • 4,710
  • 4
  • 32
  • 54
  • Do you mean environment? – sscirrus Nov 24 '13 at 23:51
  • The point of using `ENV` variables is so that you can keep them out of source control like git. Adding `ENV` variables to the `environment.rb` file (which is usually commmited) is exactly the same as hard-coding the variables. Please check this other question: http://stackoverflow.com/questions/13294194/rails-how-to-store-mailer-password-safely/13296207 – Ashitaka Nov 25 '13 at 18:50
  • This is just to get started. this is not on git yet. it will later be moved of course. – guy schaller Nov 25 '13 at 20:40

2 Answers2

3

It is possible that the case is an issue in your middleware:

provider :linkedin, ENV['linkedin_key'], ENV['linkedin_secret']

In your environment.rb you are using upper case:

ENV['LINKEDIN_KEY'] = "key"
ENV['LINKEDIN_SECRET'] = "secret"

I also recommend you have a look at dotenv to manage you environment data - it keeps sensitive information from your source control.

Toby Hede
  • 36,755
  • 28
  • 133
  • 162
  • you are right about the upper and lower. but that's after a long time playing with it. its not the problem. as I see it now omniauth changed the builder in 1.0 and now they offer a new way to load: https://github.com/intridea/omniauth/wiki/Dynamic-Providers – guy schaller Nov 25 '13 at 07:04
  • I have this in my initializer for github Rails.application.config.middleware.use OmniAuth::Builder do provider :github, ENV["GITHUB_ID"], ENV["GITHUB_SECRET"] end – Toby Hede Nov 25 '13 at 11:33
2

After searching for the new dynamic-providers strategy omniauth suggest using I found this page: https://github.com/intridea/omniauth/wiki/Setup-Phase

so changed my code to:

SETUP_PROC = lambda do |env|
  env['omniauth.strategy'].options[:consumer_key] = ENV['LINKEDIN_KEY']
  env['omniauth.strategy'].options[:consumer_secret] = ENV['LINKEDIN_SECRET']
end

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :linkedin, setup: SETUP_PROC
end

And it works!

guy schaller
  • 4,710
  • 4
  • 32
  • 54