How to return Doorkeeper access token as json

跟風遠走 提交于 2019-12-03 08:33:11

The way I handled this was to create a custom tokens controller and overriding the token request action. There I could append custom stuff to response.

# app/controllers/custom_tokens_controller.rb
class CustomTokensController < Doorkeeper::TokensController

  # Overriding create action
  # POST /oauth/token
  def create
    response = strategy.authorize
    body = response.body

    if response.status == :ok
      # User the resource_owner_id from token to identify the user
      user = User.find(response.token.resource_owner_id) rescue nil

      unless user.nil?
        ### If you want to render user with template
        ### create an ActionController to render out the user
        # ac = ActionController::Base.new()
        # user_json = ac.render_to_string( template: 'api/users/me', locals: { user: user})
        # body[:user] = Oj.load(user_json)

        ### Or if you want to just append user using 'as_json'
        body[:user] = user.as_json
      end
    end

    self.headers.merge! response.headers
    self.response_body = body.to_json
    self.status        = response.status

  rescue Doorkeeper::Errors::DoorkeeperError => e
    handle_token_exception e
  end
end

Just make sure that you point to this controller in routes.rb

# routes.rb
Rails.application.routes.draw do

  # Doorkeeper
  use_doorkeeper do
    controllers :tokens => 'custom_tokens'
  end

  # Your other routes here...

end

This is tested and it works, I am using it in my projects.

AndyRyan

The way I managed to solve this was to create my own AccessToken class that overloads the as_json method to include the fields I wanted.

e.g

class AccessToken < Doorkeeper::AccessToken
   def as_json(options={})
      {
        :token => self.token,
        #:resource_owner_id => self.resource_owner_id,
        #:scopes => self.scopes,
        :created_at => self.created_at,
        :expires_in_seconds => self.expires_in_seconds,
        #:application => { :uid => self.application.uid }
      }
   end
end

If anyone has a better solution I'm all ears

I know it's been a while this has been solved. I recently wanted to implement the same behavior on my API and I relied on model association in order to achieve it :

class User < ActiveRecord::Base
  has_one :token, -> { order 'created_at DESC' }, class: Doorkeeper::AccessToken, foreign_key: :resource_owner_id
end

Then using serializers :

class UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :email
  has_one :token, serializer: AccessTokenSerializer
end

class AccessTokenSerializer < ActiveModel::Serializer
  attributes :token, :expires_in_seconds
end

You could simply return your data as :

class DummyController < ApplicationController
  def dummy
    respond_with current_user, status: :ok
  end
end

which will output a JSON that looks like :

{
    "id": 17,
    "name": "Chuck Norris",
    "email": "chuck@norr.is",
    "token": {
        "token": "c62af258f2d1ac816f65606a2a8413b8e0c2ad5b4434f9c75b56765f54ee627b",
        "expires_in_seconds": "5427"
    }
}

Actually we can get access token with:

doorkeeper_token.token

you can include it in the json response so you don't need to edit anything inside doorkeeper itself.
However, don't forget to add some conditionals before including it in the json response as it doesn't seem so good to always show it in the response.

Easy! you don't need to inherit from Doorkeeper::TokensController but you can if it suits better to your case:

class CustomController < ApplicationController
  def create
    ...
    access_token = Doorkeeper::AccessToken.create!(application_id: @application_id, resource_owner_id: current_user.id, :expires_in => 168.hours)
    render json: Doorkeeper::OAuth::TokenResponse.new(access_token).body
  end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!