omniauth / omniauth

OmniAuth is a flexible authentication system utilizing Rack middleware.
MIT License
7.91k stars 977 forks source link

`env['omniauth.auth']` is nil when customizing `callback_path` #767

Open mhutter opened 10 years ago

mhutter commented 10 years ago

I have a Rails app that "lives" in a subdirectory. To make sure the user gets to my app after authentication, I've added the following to my config/initializers/omniauth.rb:

callback_path: Rails.application.secrets.path+'/auth/ldap/callback'

which works fine so far. However, now in my callback controller, env['omniauth.auth'] is always nil

Any Ideas to why this is happening?

I have an almost identical setup in another App (that's served from a Root directory) which works just fine.

geori commented 10 years ago

I'm having the same exact problem with a Sinatra implementation of omniauth-twitter. If i set :callback_path, it returns to the correct place I specified in :callback_path, but request.env['omniauth.env'] is nil. And FYI, request.env['omniauth.origin'] is also nil, but you can access the value via rack session.

zmajstor commented 10 years ago

I'm also having the same problem. In my Rails 4.2 app, I've added following to my config/initializers/omniauth.rb:
provider :foo, name: :bar, callback_path: "/sub/auth/bar/callback"
and in my callback controller, env['omniauth.auth'] is always nil

delacruzjames commented 9 years ago

Any update im also having same problem?

mcfiredrill commented 9 years ago

I'm not sure if this is your issue, but I had an issue with the callback path not being recognised if it contained extraneous slashes (like //auth/ldap/callback).

You may want to try removing the first slash and see if it works.

mcfiredrill commented 9 years ago

If you don't see the Callback Phase Initiated message in your logs, then the callback_path was probably not recognised.

KELiON commented 9 years ago

+1, have almost the same issue with rails 4.2.3 and omniauth 1.2.2. I see double slashes after callback (//auth/:provider/callback) and no Callback Phase Initiated in logs. After removing one slash it works fine. But I don't customize callback_path param

PandaWhisperer commented 9 years ago

I'm having the same issue. Weirdly, it's not that request.env['omniauth.auth'] is nil, rather, it returns nil, which makes it even weirder. I inspected it in better_errors, and when I type just request.env, it clearly shows the 'omniauth.auth' key and it's value, but for some reason, request.env['omniauth.auth'] returns nil nevertheless.

As soon as I remove the customized callback path, this behavior goes away, as OP suggested.

tobias74 commented 9 years ago

this happens in the following situation: (applies to 1.2.2 as well as the current master)

the following situation arises: -> "request.path_info" is set to "/internal/callback" by rack -> "on_callback_path" compares "my/custom/callback" with "/internal/callback" and returns false --> the method callback_call is never executed. --> env['omniauth'] is nil

jiggneshhgohel commented 8 years ago

I am facing this similar issue. Following are the gem versions used in my application.

ruby (2.3.0) rails (4.2.6) omniauth-facebook (3.0.0) omniauth-oauth2 (1.4.0) omniauth (1.3.1)

Did anyone found a solution or a work-around for this?

jiggneshhgohel commented 8 years ago

I have resolved my problem through a workaround. I have posted my solution in a gist. Hoping that it will be beneficial to people like me looking out for a solution to this problem.

External Links:

http://stackoverflow.com/q/38627949/936494

simonneutert commented 7 years ago

i was able to retrieve the necessary data by changing env["omniauth.auth"] to request.env["omniauth.auth"]

Rails 5.1 Devise 4.3.0 omniauth (1.6.1) omniauth-facebook (4.0.0) omniauth-oauth2 (1.4.0)

SandipBhowmick commented 6 years ago

Provider Gem: 'omniauth', '~> 1.2.1' Ruby Version: ruby 2.2.1p85 Platform: Ubuntu 14.04 LTS I want to create different login flow for admin, so I have create another sessions controller under admin. But when I try to access env["omniauth.auth"] from admin/sessions_controller.rb it shows error "NoMethodError at /admin/auth/identity/callback undefined method `[]' for nil:NilClass" I found env["omniauth.auth"] return nil Please help me

nantestdeveloper commented 6 years ago

provider: identity

calling api/v1/auth/identity/callback for api return nil auth_hash and also set the routes for same

getting error

NoMethodError at /api/v1/auth/identity/callback

===============================================

undefined method `[]' for nil:NilClass

I want to call the post call api for sign in and sign up using omniauth identity

wangthony commented 4 years ago

This issue has sprawled out to a bunch of different cases, but it does seem like the callback_path option gets lost somehow.

For an alternative solution using querystring params, see https://github.com/omniauth/omniauth/issues/661#issuecomment-630300394

s1monj commented 4 years ago

I had the same issue as outlined by tobias74 above - my app is behind a reverse proxy with a RAILS_RELATIVE_URL_ROOT (also #903). My workaround is to add the relative URL component to full_host and set the callback_path to match the original (so "my/custom/callback" matches "/internal/callback" per tobias74) and use a querystring param (see above) if you then need to switch from the callback

  OmniAuth.config.full_host = lambda do |_|
    'https://www.example.com/my_relative_path'
  end

  config.omniauth :azure_oauth2,
    client_id: ENV['AZURE_CLIENT_ID'],
    client_secret: ENV['AZURE_CLIENT_SECRET'],
    tenant_id: ENV['AZURE_TENANT_ID'],
    callback_path: '/users/auth/azure_oauth2/callback'

rack 2.2.3 rails 5.2.0 omniauth 1.9.1

trkoch commented 3 years ago

For me, simply setting both client_options.redirect_uri and callback_path did the trick. Note the former is relative, while the latter is a full URL (i.e. do not set to the same value). I guess client_options.redirect_uri is to tell the provider, and callback_path is where to expect the callback (from the standpoint of Omniauth).

Not sure you're affected by this issue? If you don't see Callback phase initiated in your console after receiving the callback, Omniauth did not recognize the (customized) callback URL (i.e. it does not care that this particular request just came in).