Zac Hallett
Zac Hallett

Reputation: 494

Overriding default scope set in omniauth gem

I am trying to override a default scope set up in an omniauth gem. I've tried setting the scope via the initializer as well as trying to leave the scope blank. I want to be able to do

/auth/<provider>?scope=<scope>

as a link for signing in.

However, regardless of how I setup the provider in

config/initializers/omniauth.rb

ex:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :<provider>, <app_uid>, <app_secret>,
  :scope => "",
  :client_options => {
    :site          => "https://api.<provider_url>",
    :authorize_url => "https://api.<provider_url>/oauth/authorize",
    :token_url     => "https://api.<provider_url>/oauth/token"
  }
end

it still tries to send an omniauth / oauth-2 request using the default scope setup in the

omniauth-<provider> gem

How can I override the default scope declared in the gem via passing in a scope param to the url being passed to the provider?

Upvotes: 2

Views: 946

Answers (1)

Zac Hallett
Zac Hallett

Reputation: 494

I was able to do so using the following initializer.

Rails.application.config.middleware.use OmniAuth::Builder do
   :<provider>, <app_uid>, <app_secret>,
  :setup => lambda { |env| env["omniauth.strategy"].options[:scope] = env["rack.session"]["omniauth.params"]["scope"]},
  :client_options => {
    :site          => "https://api.<provider_url>",
    :authorize_url => "https://api.<provider_url>/oauth/authorize",
    :token_url     => "https://api.<provider_url>/oauth/token"
  }

end

Upvotes: 3

Related Questions