Skip to content

Custom callback_path uses bait and switch with request_path #630

Closed

Description

@canadaduane

It looks like a copy-paste error to me:

https://github.com/intridea/omniauth/blob/master/lib/omniauth/strategy.rb#L378

I expect to be able to write a custom callback_path such as:

provider :singly, ID, SECRET_ID, :callback_path => Proc.new{ |env| "/my/path/#{env[:key]}/callback" }

But line 378 uses request_path instead of callback_path for callables.

Activity

  1. watsonbox commented on Apr 10, 2013

    @watsonbox
    Contributor

    I think what you want is something like:

    provider :singly, ID, SECRET_ID, :callback_path => Proc.new{ |env| env['PATH_INFO'] == "/my/path/#{env[:key]}/callback" }

    Otherwise the custom callback path evaluator will always be truthy and so Omniauth will run the callback phase on every request.

    As for your question about the request_path typo, it's my understanding that this is not a typo, but rather allows for a kind of mapping of request paths to custom callback paths. For example:

    provider :singly, ID, SECRET_ID, :request_path => lambda { |env| "/my/auth/path/callback" if env['PATH_INFO'] == "/my/auth/path" }

    This example isn't dynamic as such, but you could use regex matching etc to achieve that.

  2. dlindahl commented on Jun 14, 2013

    @dlindahl

    While I don't entirely understand the reasoning behind mapping request paths to custom callback paths, I can certainly respect the decision.

    However, it seems that if I do not want this mapping feature and instead want to define an explicit callback_path evaluator, then OmniAuth shouldn't get in the way.

    I imagine that if you want a request/callback mapping, you wouldn't define a callback_path evaluator in the first place. So simply checking if there is a callback_path evaluator before falling back to the request_path evaluator should address both situations:

    Before:

    def callback_path
      options[:callback_path].is_a?(String) ? options[:callback_path] : (custom_path(:request_path) || "#{path_prefix}/#{name}/callback")
    end

    After

    def callback_path
      options[:callback_path].is_a?(String) ? options[:callback_path] : (custom_path(:callback_path) || custom_path(:request_path) || "#{path_prefix}/#{name}/callback")
    end

    Right?

    Additionally, is there any documentation on how you'd use and why you'd want the request/callback mapping?

    It seems like a confusing feature and the code itself failed the Principle of Least Astonishment, in my opinion, as I had the same "this must be a typo" reaction that @canadaduane did.

  3. BRMatt commented on Jul 2, 2013

    @BRMatt

    I agree with @dlindahl, this is somewhat hard to understand. It seems the helpers are trying to do 2 things:

    How would people feel if we changed it so there were a separate is_callback_path callback for dynamic path checking and the callback_path one is used to generate the path?

    def on_callback_path?
      on_path?(callback_path) || matches_dynamic_path?(:callback_path)
    end
    
    def matches_dynamic_path?(path)
      option = options[:"is_#{path}"]
      option.respond_to?(:call) && option.call(env)
    end

    And then an implementation of callback_path as @dlindahl described it?

  4. dlindahl commented on Jul 2, 2013

    @dlindahl

    Seems reasonable to me

  5. tmilewski commented on Feb 12, 2017

    @tmilewski
    Member

    Closing per #693

  6. emirikol commented on Aug 17, 2017

    @emirikol

    Was this ever addressed? it doesn't seem like #693 was actually merged.

  7. leo-souza commented on Feb 14, 2018

    @leo-souza

    I was facing issues with having a dynamic callback_path, so this is how we solved it (seems bit hackish but I couldn't find any other way)
    Our case: We have 2 authentications: one for User (login) and another for Profile (to grab page data).

    provider :facebook, ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_APP_SECRET"], image_size: 'large',
        scope: '...our scope..',
        info_fields: '..our info fields...',
        callback_path: ->(env) do
          env['REQUEST_PATH'].to_s.match(/\/auth\/facebook\/callback/)
        end,
        setup: ->(env) do
          req = Rack::Request.new(env)
          env['omniauth.strategy'].options[:callback_path] = "/#{req.params['resource'] == 'user' ? 'users' : 'profiles'}/auth/facebook/callback"
        end
    
    on_failure do |env|
      if env['omniauth.params'].to_h['resource'] == 'user'
        env["devise.mapping"] = Devise.mappings[:user]
        Users::OmniauthCallbacksController.action(:failure).call(env)
     else
        env["devise.mapping"] = Devise.mappings[:profile]
        Profiles::OmniauthCallbacksController.action(:failure).call(env)
      end
    end

    so for profiles our url is /auth/facebook?scope=<different scope> and for users is /auth/facebook?resource=user
    and we split the callback routes like this

    devise_scope :profile do
      get '/profiles/auth/facebook/callback', to: 'profiles/omniauth_callbacks#facebook'
    end
    
    devise_scope :user do
      get '/users/auth/facebook/callback', to: 'users/omniauth_callbacks#facebook'
    end
  8. added theissue type on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions