dimanche 22 août 2021

When trying to authenticate, doorkeeper returns a 401 error "Client authentication failed due to unknown client, no client authentication included..."

So, I'm building an application using Rails for the backend and Ember.JS for one of the clients. I used CodingItWrong's apiup and emberup to generate project directories with some stuff pre-installed. I'm working on authentication currently, and OAuth2 is what apiup configures Rails to use. So, loosely watching the VOD of the stream where he implemented this, I tried to implement it myself. However, every time I'd send a request to the Rails server (via ember-simple-auth [ESA]), I would get an HTTP 401 with the JSON response:

{
    "error": "invalid_client",
    "error_description": "Client authentication failed due to unknown client, no client authentication included, or unsupported authentication method."
}

This surprised me, as ESA is meant to support OAuth2 out-of-the-box (and I'm sure it does-I did something wrong here!!). So, what would typically cause doorkeeper to send an error like this? Here is some of the troubleshooting I tried:

  • I checked to make sure the database query used was good (manually via psql)
  • I checked to make sure the doorkeeper authenticate statement worked (it did):
Doorkeeper.configure do
  # Change the ORM that doorkeeper will use (needs plugins)
  orm :active_record

  grant_flows %w[password]

  resource_owner_from_credentials do
    user = User.find_by(email: params[:username])
    if user&.authenticate(params[:password])
      puts 'Authentication Success'
      user
    else
      raise Doorkeeper::Errors::DoorkeeperError.new('invalid_user_or_password')
    end
  end

...

And low and behold, "Authentication Success" was printed to the console.

  • I checked my Ember app to make sure the host was correct (https://localhost:3000)

One thing I'm not 100% sure on is what the token endpoint is by default on doorkeeper? Ember has it set as '/oauth/token', generated by emberup.

Thank you so much for hearing me out :)

If you need any more code to help diagnose this, please let me know and I can edit this question.

Thanks!




Aucun commentaire:

Enregistrer un commentaire