Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Not found. Authentication passthru. with any Omniauth strategy #5236

Closed
lionelrudaz opened this issue May 8, 2020 · 33 comments · Fixed by #5327
Closed

Error: Not found. Authentication passthru. with any Omniauth strategy #5236

lionelrudaz opened this issue May 8, 2020 · 33 comments · Fixed by #5327

Comments

@lionelrudaz
Copy link

Hi everybody, I'm posting here, because none of the answers in StackOverflow seem to work. So either I've got a bug with the library, or this issue can lead to an improvement of the documentation.

Environment

  • Ruby 2.7.0
  • Rails 6.0.3
  • Devise 4.7.1
  • Omniauth 1.9.1
  • omniauth-facebook 6.0.0

Current behavior

I've published my repo here: https://github.com/lionelrudaz/dunphy-api/tree/omniauth-facebook

I've followed the guide to enable Facebook login with Omniauth. I've done it three times. I've checked all the possible similar questions on Stackoverflow, including:

When I'm entering http://localhost:3000/users/auth/facebook/, I always get the same error.

Looks like I'm not the only one that had struggle with the error. What's strange is that most of the time, the issue either was fixed by redoing everything from scratch, restarting the server (which I did many times), upgrading rails (which I did as well from 6.0.2 to 6.0.3) or by waiting for the app to be enabled...

I've got no logs to provide, except this error.

Will be even more helpful if you provide a sample application or a test case that reproduces the error.

Expected behavior

I should be redirected to Facebook login.

Let me know if you need more information.

@apchester
Copy link

Did you get any further with this? I'm getting the same problem.

@lionelrudaz
Copy link
Author

No, I didn't. I changed the flow to make it work differently, taking the authorization code from the client, then sending it to my Rails app.

@arcooverbeek
Copy link

@lionelrudaz and @apchester please make sure you use method: :post for you links. Also make sure JS is working.
= link_to "Sign in with Facebook", user_facebook_omniauth_authorize_path, method: :post

@apchester
Copy link

apchester commented Sep 17, 2020

@arcooverbeek Thanks! instantly fixes the issue for me, though this doesn't appear to be the default?

@lionelrudaz
Copy link
Author

@arcooverbeek the thing is that I was in API mode for my Rails app, and my calls were done from a different frontend application. In this case, I don't think it makes sense to post to the URL, don't you think?

@Pandenok
Copy link

@arcooverbeek using method: :post leads to WARN -- omniauth: Attack prevented by OmniAuth::AuthenticityTokenProtection. How ti fix Not found. Authentication passthru.?

@carlosantoniodasilva
Copy link
Member

Hey all, please note that using POST as the method is the new default in OmniAuth 2+, and that is to increase security and is definitely recommended.

Devise is upgrading to that default soon: #5327, and so should you if possible. Check their release notes for more info: https://github.com/omniauth/omniauth/releases/tag/v2.0.0.

If you're getting to that error it means OmniAuth is not catching the route and doing its thing, and instead you're landing directly in Rails/Devise.

@lionelrudaz are you still encountering the problem you originally reported?

@Pandenok
Copy link

Pandenok commented Jan 24, 2021

@carlosantoniodasilva thank you for coming back on this! So, another question, why OmniAuth is not catching the route?
routes.rb

devise_for :users, controllers: { omniauth_callbacks: 'omniauth_callbacks' }

Edit 1: I've recently upgraded to Rails 6.1.1 and Ruby 3.0.0. Could it be the reason?
Edit 2: I'm using this branch ref instead of the git ref directly:

gem 'devise', github: 'heartcombo/devise', branch: 'ca-omniauth-2'

Edit 3:

  • Tried to add method: :post to my link
  • skipped the authenticity check with skip_before_action :verify_authenticity_token, only: :facebook
  • kept getting WARN -- omniauth: Attack prevented by OmniAuth::AuthenticityTokenProtection

Temporarily solution which worked for me is to downgrade to gem omniauth", "~> 1.9.1" as by this answer.

@carlosantoniodasilva
Copy link
Member

Have you verified that the request is actually being sent as POST with the link using method: :post? That requires rails or jQuery UJS, if you're not using that in your app it may still be going through a normal GET request. You can also try changing to a button_to temporarily to test if that is the problem.

With OmniAuth 2.0+, OmniAuth.config.allowed_request_methods should default to just include :post, while previous versions should have :get and :post, that may be the reason that it's not working for you.

If that's not the case, it'd be nice if you could provide a sample app that shows the issue. I haven't been able to re-test everything myself yet with that branch. Thanks.

@jkowens
Copy link
Contributor

jkowens commented Jan 24, 2021

@Pandenok I think you need to set the OmniAuth.config.request_validation_phase.

See: https://github.com/omniauth/omniauth/wiki/Upgrading-to-2.0#rails

@Pandenok
Copy link

@jkowens YEAAAAH!

  1. Install the gem OmniAuth - Rails CSRF Protection
  2. Add the link user_facebook_omniauth_authorize_path method: :post
  3. Get the facebook omniauth working without downgrading!
    Thank youuuu!
    @carlosantoniodasilva sorry for the delay in answering. My bad...

@carlosantoniodasilva
Copy link
Member

Awesome, I'm happy to know that is working @Pandenok, and thanks for the direction @jkowens.

I'm gonna work on getting a new devise version released this week containing the patches in that branch.

@Pandenok
Copy link

@carlosantoniodasilva you're doing amazing job!

@juliaf1
Copy link

juliaf1 commented Jan 25, 2021

@carlosantoniodasilva I have been going through your suggestions on different forums to fix OmniAuth and have landed on an AuthenticityTokenProtection error just like @Pandenok.

I'm using these two gems:

gem 'devise', github: 'heartcombo/devise', branch: 'ca-omniauth-2'
gem 'omniauth-facebook'

And calling the user_facebook_omniauth_authorize_path with method: :post

I tried installing OmniAuth - Rails CSRF Protection, but didn't exactly understand where I should set OmniAuth.config.request_validation_phase, so that didn't work and I keep landing on this:

Started POST "/users/auth/facebook" for ::1 at 2021-01-25 11:56:40 -0300
D, [2021-01-25T11:56:40.532147 #4264] DEBUG -- omniauth: (facebook) Request phase initiated.
W, [2021-01-25T11:56:40.532787 #4264]  WARN -- omniauth: Attack prevented by OmniAuth::AuthenticityTokenProtection
E, [2021-01-25T11:56:40.532940 #4264] ERROR -- omniauth: (facebook) Authentication failure! authenticity_error: OmniAuth::AuthenticityError, Forbidden
Processing by Users::OmniauthCallbacksController#failure as HTML

Any thoughts on how to fix this would be super appreciated.

@jkowens
Copy link
Contributor

jkowens commented Jan 25, 2021

@juliaf1 did you see the Omniauth 2.0 upgrade wiki? https://github.com/omniauth/omniauth/wiki/Upgrading-to-2.0#rails

You can either add gem "omniauth-rails_csrf_protection" to your Gemfile or follow the instructions to manually set OmniAuth.config.request_validation_phase.

@carlosantoniodasilva
Copy link
Member

@juliaf1 right, from my understanding the only thing you should need (aside from using that branch and OmniAuth 2) is what @jkowens mentioned above: add the omniauth-rails_csrf_protection to your Gemfile, and make sure that's at least ~> 1.0.

That combined with initiating the OAuth flow using a POST request (using button_to or link_to + method: :post) should be enough to get it working.

I'll probably include some documentation around this on the Devise side (changelog/readme/wiki), I'm sure more people will hit issues upgrading and will come looking here first.

@Pandenok
Copy link

Pandenok commented Jan 25, 2021

@juliaf1 Julia,

I tried installing OmniAuth - Rails CSRF Protection, but didn't exactly understand where I should set OmniAuth.config.request_validation_phase, so that didn't work and I keep landing on this..

If you installed the gem, you don't need to add anything, it's the gem itself that will add request validation on any post request, so make sure as @carlosantoniodasilva stated to use a link_to + method: :post or button_to which will make by default a post request.
Please check that you omniauth gem is really updated to the latest version by running bundle omniauth update

Hope this will help!

Edit: typos and formatting edited

schneems added a commit to codetriage/CodeTriage that referenced this issue Apr 4, 2021
Omniauth 2 requires post. heartcombo/devise#5236

```
Install the gem OmniAuth - Rails CSRF Protection
Add the link user_facebook_omniauth_authorize_path method: :post
```

TODO: Convert the rest of the links to `user_github_omniauth_authorize_path` to be post
schneems added a commit to codetriage/CodeTriage that referenced this issue Apr 4, 2021
Omniauth 2 requires post. heartcombo/devise#5236

```
Install the gem OmniAuth - Rails CSRF Protection
Add the link user_facebook_omniauth_authorize_path method: :post
```

TODO: Convert the rest of the links to `user_github_omniauth_authorize_path` to be post
schneems added a commit to codetriage/CodeTriage that referenced this issue Apr 4, 2021
Updates omniauth because GitHub is deprecating query params which is used by the old omniauth.

To do this I updated to Omniauth 2+ which also required an update of devise. There's a change to the omniauth API which is talked about here:

heartcombo/devise#5236

Basically:
- Omniauth2 requires post (instead of GET)
- Omniauth 2 also needs this `omniauth-rails_csrf_protection` gem.

I added the gem and updated all `link_to` and `button_to` to include a `method: :post`. There is one controller redirect which apparently still seems to work, but it might be broken. I'm not sure how we could possibly preserve the existing behavior since you cannot redirect to a post. 

This gets tests to pass though. So it's good enough for the short term.

## Deprecation Email from GitHub

```
[GitHub API] Deprecation notice for authentication via URL query parameters
GitHub

Mar 6, 2021, 4:37 AM (5 days ago)

to Richard
Hi @schneems,

On March 6th, 2021 at 10:37 (UTC) your application (CodeTriage) used an access token (with the User-Agent Faraday v0.17.3) as part of a query parameter to access an endpoint through the GitHub API:

https://api.github.com/user

Please use the Authorization HTTP header instead as using the `access_token` query parameter is deprecated.

Depending on your API usage, we'll be sending you this email reminder on a monthly basis.

Visit https://developer.github.com/changes/2020-02-10-deprecating-auth-through-query-param for more information about suggested workarounds and removal dates.

Thanks,
The GitHub Team
```

Need to use devise from GitHub due to this not being released yet heartcombo/devise#5327

# This is the commit message #2:

WIP Move omniauth links to post

Omniauth 2 requires post. heartcombo/devise#5236

```
Install the gem OmniAuth - Rails CSRF Protection
Add the link user_facebook_omniauth_authorize_path method: :post
```

TODO: Convert the rest of the links to `user_github_omniauth_authorize_path` to be post
@michaelwebb76
Copy link

If anyone lands here and has just wasted an entire day trying to resolve this issue, re-reading this thread 20 times and screaming "WHY" into their clenched fist, you explicitly need to include gem "omniauth-rails_csrf_protection", "~> 1.0" to your Rails application Gemfile for the railtie to fire and setup the token verifier correctly - it's not sufficient to have that gem required by another gem if you, like us, have a single "authentication" gem that is used by all of your systems.

@Flip437
Copy link

Flip437 commented Jun 27, 2021

Hi,

what worked for me:

  • install ge "OmniAuth - Rails CSRF Protection"
  • add method: :post to your link_to button.
    no more config needed.

peace

matt-bernhardt added a commit to MITLibraries/thing that referenced this issue Jul 22, 2021
** Why are these changes being introduced:

* Something changed in a recent dependency upgrade, breaking local auth
  for development environments. The GitHub issue queue for Devise lists
  a set of steps that seems to work in local environments.

** Relevant ticket(s):

* n/a for now - although this might become its own ticket

** How does this address that need:

* This implements the recommended fix at
  heartcombo/devise#5236 (comment)
  Please note that the gem is installed development only, as the tests
  and production environments are unaffected as they don't use this auth
  method.

** Document any side effects to this change:

* I'm not sure - there's probably a better way to handle this.
matt-bernhardt added a commit to MITLibraries/thing that referenced this issue Jul 22, 2021
** Why are these changes being introduced:

* Something changed in a recent dependency upgrade, breaking local auth
  for development environments. The GitHub issue queue for Devise lists
  a set of steps that seems to work in local environments.

** Relevant ticket(s):

* n/a for now - although this might become its own ticket

** How does this address that need:

* This implements the recommended fix at
  heartcombo/devise#5236 (comment)

** Document any side effects to this change:

* I'm not sure - there's probably a better way to handle this.
marcodotcastro added a commit to desenvolvendo-me/desenvolvendome that referenced this issue Aug 7, 2021
@seanmsmith23
Copy link

Gems upgraded, method: :post, disable turbo. Same issue. Sigh...

@viktorianer
Copy link

Just in case it helps anyone.

If you got your Turbo enabled and smth like <%= link_to "Sign in with Facebook", user_facebook_omniauth_authorize_path, method: :post %> still brings you a CORS-related error, make sure you read this.

TL;DR: You'd better switch to form instead of a link and set a turbo form's param to false.

It helps to switch to button_to, as long as that is not resolved on Turbo side:
hotwired/turbo#45
hotwired/turbo#176
hotwired/turbo#288

This works for me too:

<%= button_to "Sign in with Microsoft", user_microsoft_graph_omniauth_authorize_path,
          method: :post, "data-turbo": false %>

@kg-currenxie
Copy link

This works for me with turbo

gem 'omniauth-rails_csrf_protection', '~> 1.0'

and 

<%= link_to user_facebook_omniauth_authorize_path, method: :post, class: 'facebook-button', data: { turbolinks: false }  %>

No other changes

@ragesoss
Copy link

ragesoss commented Jan 4, 2022

Anyone have advice for working around this problem for the case of "open link in new tab"? My login links work as expected when they are clicked normally, but "method: :post" doesn't prevent this error when a user tries to open the login in a new tab.

@Duartemartins
Copy link

This is happening to me on Rails 7 using https://github.com/andyw8/omniauth-pocket-oauth2 and none of these solutions seem to work :/.

jgreidy added a commit to cul-it/blacklight-cornell that referenced this issue Mar 14, 2022
jgreidy added a commit to cul-it/blacklight-cornell that referenced this issue Mar 14, 2022
@zal-lex
Copy link

zal-lex commented Mar 16, 2022

Only replacing <%= link_to "Sign in with #{provider}", user_provider_omniauth_authorize_path, method: :post %> with <%= button_to "Sign in with #{provider}", user_provider_omniauth_authorize_path %> worked for me.

@carlosantoniodasilva
Copy link
Member

@zal-lex are you using turbo? If so, there's a reason that it works with button_to and not link_to, see rails/rails#43430 and rails/rails#43941

jrthreadgill referenced this issue in TheGnarCo/GEAR Apr 8, 2022
Followed the guides provided on omniauth-google-oauth2 repo (https://github.com/zquestz/omniauth-google-oauth2\#devise) and devise wiki (https://github.com/heartcombo/devise/wiki/OmniAuth:-Overview). Note that `data: { turbo: "false" }` is required to make this work, as was noted in a few different Github and Stack Overflow threads (e.g., https://github.com/heartcombo/devise/issues/5236\#issuecomment-1004028752).
@seballot
Copy link

In case it helps someone else, for me the problem was with url comparaison
I don't know why but in omniauth middleware all my request path starts with double /. i.e. //users/auth/xxx instead of /users/auth/xxx

So a quick fix is to override this method in you omniauth stategy

module OmniAuth
  module Strategies
    class MyStrategy < OmniAuth::Strategies::OAuth2
       def on_path?(path)
          current_path.squeeze("/").casecmp(path.squeeze("/")).zero?
        end

       #...
     end
  end
end

See omniauth/omniauth#1079

@bjnord
Copy link
Contributor

bjnord commented Sep 4, 2022

Rails 7 doesn't have UJS by default, so the previous method: :post directive on link_to doesn't work any more. I found this on StackOverflow with the answer: Change method: :post to data: { 'turbo-method' => :post }

@ketan-survival
Copy link

Thanks @jkowens.

Adding gem 'omniauth-rails_csrf_protection' to the Gemfile and Using a form_tag with a submit button worked for me, a standalone button_to or link_to with method: :post wouldn't work.

Also gem 'omniauth-rails_csrf_protection' pulls version 1.0.1 at the time of this comment so no need to specify version.

      = form_tag(admin_user_google_oauth2_omniauth_authorize_path) do
        button(type='submit' class='btn btn-primary', style='width: 100%')
          i.fa.fa-google
          |  Sign in with Google

@hs-dliaska
Copy link

if you place button / link inside form it may cause the Not found. Authentication passthru as well. if you face this issue try removing your button / link outside the form.

blerner pushed a commit to CodeGrade/hourglass that referenced this issue Nov 20, 2023
… then the link will use a GET method. Per heartcombo/devise#5236 (comment), change to a button_to to ensure that it's a POST method in all cases.
blerner pushed a commit to CodeGrade/hourglass that referenced this issue Feb 21, 2024
… then the link will use a GET method. Per heartcombo/devise#5236 (comment), change to a button_to to ensure that it's a POST method in all cases.
@MarchandMD
Copy link

While using omniauth-github and developing locally, I was experiencing this same issue.

2 things definitely resolved this for me:

  1. changing link_to to button_to (even with method: :post and data: {turbo: false} this was still necessary)
  2. updating the callback url in my Oauth app setting on Github specifically. I had https://localhost:3000/users/auth/github and I needed to remove the s from https

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.