我想覆盖门卫令牌错误响应体方法。目前,当我在http://localhost:3000/oauth/token
网址错误传递用户名和密码时,会显示以下错误消息。
未经授权的默认门卫响应:
{
"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
尝试了很多自动加载语法,但没有成功。
答案 0 :(得分:2)
经过太多尝试,我得到了解决方案。我不知道这是不是好方法,但它现在正在运作。
我做的是
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 + Doorkeeper 。 https://scotch.io/@jiggs/rails-api-doorkeeper-devise
OR
https://medium.com/@khokhanijignesh29/rails-api-doorkeeper-devise-4212115c9f0d