Doorkeeper Authentication from Rails GraphQL

Viewed 740

I'm trying to provide a "sign in" for GraphQL queries in a Rails (4.2.10) app. The "user" and "password" fields are being passed in. In my mutations.rb, I have:

field :signin, !types.Boolean do
  argument :user, !types.String
  argument :password, !types.String
  resolve -> (_, args, _) {
     #what here?
  }
end

Some documentation (out of date?) suggests:

Doorkeeper.authenticate args

This gets me a "undefined method `authorization'" in my tests, which I assume means I'm missing a callback somewhere. I've dug down but not been able to figure out what I need.

I've also tried

Doorkeeper::Application.by_uid_and_secret args.user, args.password

and this seems to return nil.

1 Answers

It seems like you're trying to build a signin mutation to take the OAuth credentials. This seems like a bad idea. OAuth is a well-established set of processes that API clients know how to follow. The OAuth Client Credentials flow process already includes a way to pass credentials to the OAuth server and get back an access token.

That access token should be used (as part of the value in an Authorization header for example) to authenticate subsequent GraphQL API calls.

So instead of implementing a mutation that replaces the OAuth flow, add an OAuth implementation to your API consumer, and enable the Client Credentials flow in Doorkeeper.

Related