【问题标题】:Unable to override custom token error response in doorkeeper无法覆盖门卫中的自定义令牌错误响应
【发布时间】:2018-06-30 23:14:45
【问题描述】:

我想覆盖门卫令牌错误响应正文方法。目前,当我在http://localhost:3000/oauth/token url 传递错误的用户名和密码时,它会给出以下错误消息。

未经授权的默认门卫响应:

{
    "error": "invalid_grant",
    "error_description": "The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client."
}

但我希望我的 API 的错误消息有不同的结构。如下所示。

我的预期反应是:

{
    "status_code": 401,
    "message": "Invalid username or password."
    "result": []
}

我遵循here 的官方文档并在下面尝试完全满足我的期望。

尝试自定义响应:

lib/doorkeeper/oauth/error_response.rb

module Doorkeeper
  module OAuth
    class ErrorResponse
      def body
        {
          "status_code": 401,
          "message": "Invalid username or password."
          "result": []
        }
      end
    end
  end
end

门卫配置:

这是 config -> 初始化文件夹下的 doorkeeper.rb 文件

Doorkeeper.configure do
  ...
  # This block will be called to check whether the resource owner is authenticated or not.
  resource_owner_authenticator do
    fail "Please configure doorkeeper resource_owner_authenticator block located in #{__FILE__}"
  end

  # In this flow, a token is requested in exchange for the resource owner credentials (username and password)
  resource_owner_from_credentials do |routes|
    user = User.find_for_database_authentication(:username => params[:username])
    if user && user.valid_for_authentication? { user.valid_password?(params[:password]) }
      user
    end
  end
  ...
end

但它似乎不起作用。它给出了与以前相同的结果。它不会进入 lib/doorkeeper/oauth/error_response.rb 文件。

我在 applicatoin.rb 文件中自动加载 lib 文件夹

module DaihatsuMimamoriApi
  class Application < Rails::Application      
    # config.autoload_paths += %W(\#{config.root}/lib)
    # config.autoload_paths += Dir[Rails.root.join('app', 'lib', '{**/**}')]
    config.autoload_paths += Dir["#{config.root}/lib/**/"]
    # config.autoload_paths << Rails.root.join('lib')
  end
end

尝试了许多自动加载语法但没有成功。

【问题讨论】:

  • 您能否详细说明“它不起作用”。你观察到了什么?你希望看到什么?
  • @TarynEast,感谢您的建议 :) 。我更新了问题。现在你清楚了吗?
  • 嗯,是的,它似乎没有找到它。我对门卫不熟悉,所以我完全猜测您可能会尝试什么,以便进行调试。可能是加载顺序问题?在这种情况下,我会尝试将其复制/粘贴到您的配置/初始化程序代码的底部(甚至在您的控制器文件的顶部),看看它是否可以那样找到它(在将它放回应该的位置之前) ?
  • 或者,也许门卫有一个快速移动的代码库,并且自从编写了 wiki 帖子并且代码现在调用了不同的东西后,它的结构发生了微妙的变化 - 你是否查看过代码,看看它是否不再在这种情况下返回 ErrorResponse 对象?否则我不太确定......
  • 感谢您的回复@TarynEast。我检查了门卫代码,它与我用来覆盖 body 方法的结构相同。查看此链接以检查门卫github.com/doorkeeper-gem/doorkeeper/blob/master/lib/doorkeeper/… 中的默认响应方法。我正在尝试在我的 lib 文件夹下覆盖相同的方法。

标签: ruby-on-rails oauth ruby-on-rails-5 doorkeeper


【解决方案1】:

经过太多尝试,我得到了解决方案。我不知道这是不是好方法,但它现在正在工作。

我做的是

1) 在 lib 文件夹下创建custom_token_error_response.rb 文件。然后重写门卫oauth错误模块的body方法。

lib/custom_token_error_response.rb

module CustomTokenErrorResponse
  def body
    {
      status_code: 401,
      message: I18n.t('devise.failure.invalid', authentication_keys: User.authentication_keys.join('/')),
      result: []
    }
    # or merge with existing values by
    # super.merge({key: value})
  end
end

2) 在门卫ErrorResponse 模块的doorkeepr.rb 初始化文件中添加此模块。(检查下面代码中的最后一行)

config/initializer/doorkeeper.rb

Doorkeeper.configure do
  ...

  # In this flow, a token is requested in exchange for the resource owner credentials (username and password)
  resource_owner_from_credentials do |routes|
    user = User.find_for_database_authentication(:username => params[:username])
    if user && user.valid_for_authentication? { user.valid_password?(params[:password]) }
      user
    end
  end
  ...
  #
  # grant_flows %w(authorization_code client_credentials)
  grant_flows %w(password)

  # Under some circumstances you might want to have applications auto-approved,
  # so that the user skips the authorization step.
  # For example if dealing with a trusted application.
  # skip_authorization do |resource_owner, client|
  #   client.superapp? or resource_owner.admin?
  # end
  skip_authorization do
    true
  end
end

Doorkeeper::OAuth::ErrorResponse.send :prepend, CustomTokenErrorResponse

3) 现在重启你的 Rails 服务器就完成了。

你也可以参考我写的这个博客来集成Rails API + Devise + Doorkeeperhttps://scotch.io/@jiggs/rails-api-doorkeeper-devise

https://medium.com/@khokhanijignesh29/rails-api-doorkeeper-devise-4212115c9f0d

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 2022-10-22
    • 2014-10-20
    • 1970-01-01
    相关资源
    最近更新 更多