Skip to content

Passing parameters from a devise client to doorkeeper (like locale)

Nikita Bulai edited this page Sep 26, 2018 · 5 revisions

To pass the locale parameter from the client app we use the following devise strategy setup. In config/initializers/devise.rb:

require 'omniauth/strategies/doorkeeper'

config.omniauth :doorkeeper,
  Rails.application.secrets.doorkeeper_app_id,
  Rails.application.secrets.doorkeeper_app_secret, 
  client_options:  {
    site: Rails.application.secrets.doorkeeper_url
  },
  authorize_params: { locale: I18n.locale },
  setup: true

In the doorkeeper strategy:

option :authorize_params, { locale: "en" }

def setup_phase
  request.env['omniauth.strategy'].options[:authorize_params][:locale] = request.params["locale"]
end

Now the locale parameter is sent:

Started GET "/users/auth/doorkeeper?locale=fr" for 127.0.0.1 at 2014-08-15 11:08:55 +0200
I, [2014-08-15T11:08:55.275282 #2713]  INFO -- omniauth: (doorkeeper) Request phase initiated.

And received by the provider app to be processed:

Started GET "/oauth/authorize?client_id=9960df759869ff49a73ef25074d1615bd8829bb6932b2f7201dc55ca31ce79d3&locale=fr&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fusers%2Fauth%2Fdoorkeeper%2Fcallback&response_type=code&   state=62532ab121bb593d8c247619d9ef24e5376abccff73566e7" for 127.0.0.1 at 2014-08-15 11:08:55 +0200
ActiveRecord::SchemaMigration Load (0.4ms)  SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by CustomAuthorizationsController#new as HTML
Parameters: {"client_id"=>"9960df759869ff49a73ef25074d1615bd8829bb6932b2f7201dc55ca31ce79d3", "locale"=>"fr", "redirect_uri"=>"http://localhost:3000/users/auth/doorkeeper/callback", "response_type"=>"code", "state"=>"62532ab121bb593d8c247619d9ef24e5376abccff73566e7"}
Completed 401 Unauthorized in 364ms

Doorkeeper initializer sets the locale. In config/initializers/doorkeeper.rb:

resource_owner_authenticator do |routes|
  I18n.locale = request.params[:locale] unless request.params[:locale].blank?
  current_user || redirect_to(new_user_session_url(:locale => I18n.locale))
end

Being unauthorized, the devise sign in action is called. "Unauthenticated" flash message is localized and the Devise session#new form too.


Thanks to @erwin for explanation in: https://github.com/doorkeeper-gem/doorkeeper/issues/460#issuecomment-52288859.

Clone this wiki locally