Can Generic OAuth authentication be able to used with other oauth2 provider?

I have an existing rails application and I use doorkeeper gem to have oauth2 provider. I can authorize and redirect to the Grafana, Somehow, it always returns an invalid username or password in Grafana log.

I just found the way to do it.
My objective is

  • I have a rails application using Devise gem for authentication.
  • I have a Grafana dashboard that requires a login.
  • I want to click a button on Rails app and auto login to Grafana using Oauth 2

My Solution

Web app: running on http://192.168.0.100:8000

  • Rails app: I use doorkeeper gem and follow its instruction until I can create a client application for having client_id, client_secret, scope read.

Grafana: running on localhost:3000

  • in grafana.ini, I config the setting like the following
[auth.generic_oauth]
enabled = true
client_id = xxxxxx
client_secret = yyyyyy
scopes = read
auth_url = http://192.168.0.100:8000/oauth/authorize
token_url = http://192.168.0.100:8000/oauth/token
api_url = http://192.168.10.100:8000/oauth/token/info
allow_sign_up = true
name = MYWEBAPP

Web app: routes.rb

use_doorkeeper do
    controllers :token_info => 'token_info'
end

So in my rails app, I create a controller called token_info_controllers.
I copy all content from doorkeeper token_info_controllers and add customs like the following:

protected
def doorkeeper_token_to_json
    obj = doorkeeper_token.as_json
    obj[:email] = User.find(doorkeeper_token.resource_owner_id).email
    obj
end

Note: I need to add an email because it is required for Grafana while the doorkeeper only returns token info.

1 Like