OmniAuthのcallback先を動的に変更

More than 5 years have passed since last update.


config/initializers/omniauth.rb


# encoding: utf-8

require 'omniauth-openid'
require 'openid/store/filesystem'

# callback先のドメインを現在のものに合わせる => reverse proxy用
OmniAuth.config.full_host = lambda do |env|
# http or https
scheme = env['rack.url_scheme']

# アプリケーションサーバーのアドレス
local_host = env['HTTP_HOST']

# リバースプロキシサーバーのアドレス: リバースプロキシ側でヘッダーを送られないと意味がない
forwarded_host = env['HTTP_X_FORWARDED_HOST']

# リバースプロキシしてるかどうか
forwarded_host.blank? ? "#{scheme}://#{local_host}" : "#{scheme}://#{forwarded_host}"
end

Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, 'xxx', 'xxx'
provider :facebook, 'xxx', 'xxx'

# generic openid
provider :open_id, :store => OpenID::Store::Filesystem.new('/tmp')
provider :open_id, :name => 'google', :identifier => 'https://www.google.com/accounts/o8/id'
end