Ruby on rails 无法覆盖doorkeeper中的自定义令牌错误响应
我想覆盖门卫令牌错误响应主体方法。当前当我在Ruby on rails 无法覆盖doorkeeper中的自定义令牌错误响应,ruby-on-rails,oauth,ruby-on-rails-5,doorkeeper,Ruby On Rails,Oauth,Ruby On Rails 5,Doorkeeper,我想覆盖门卫令牌错误响应主体方法。当前当我在http://localhost:3000/oauth/tokenurl,然后它将给出以下错误消息 未经授权的默认门卫响应: { "error": "invalid_grant", "error_description": "The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in t
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."
}
{
"status_code": 401,
"message": "Invalid username or password."
"result": []
}
但是我希望我的API的错误消息有不同的结构
我的预期反应是:
{
"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."
}
{
"status_code": 401,
"message": "Invalid username or password."
"result": []
}
我遵循下面的官方文件,并尝试完全满足我的期望
尝试自定义响应:
{
"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."
}
{
"status_code": 401,
"message": "Invalid username or password."
"result": []
}
在lib/doorkeeper/oauth/error\u response.rb下
module Doorkeeper
module OAuth
class ErrorResponse
def body
{
"status_code": 401,
"message": "Invalid username or password."
"result": []
}
end
end
end
end
门卫配置:
{
"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."
}
{
"status_code": 401,
"message": "Invalid username or password."
"result": []
}
这是config->initializer文件夹下的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\u response.rb
文件
我在applicationin.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
模块DaihatsuMimamoriApi
类应用程序 #config.autoload_路径尝试太多后,我得到了解决方案。我不知道这是一个好办法还是不好,但它是工作到现在
我所做的是
1) 在lib文件夹下创建custom\u-token\u-error\u-response.rb
文件。然后覆盖门卫oauth错误模块的body
方法
lib/custom\u-token\u-error\u-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
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
2) 在doorkeeprErrorResponse
模块的doorkeepr.rb
初始值设定文件中预先设置此模块。(检查下面代码中的最后一行)
config/initializer/doorkeeper.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
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+Desive+Doorkeeper而写的博客。
或
您能否进一步说明“它不起作用”。您观察到了什么?你希望看到什么?@TarynEast,谢谢你的建议:)。我更新了问题。现在你明白了吗?嗯,是的,好像找不到。我对看门人不熟悉,所以我完全在猜测你们可能会尝试什么,试着调试一下。可能是装货单问题?在这种情况下,我会尝试将其复制/粘贴到配置/初始值设定项代码的底部(甚至是控制器文件的顶部),以查看它是否可以通过这种方式找到它(然后再将其放回应该的位置)?或者doorkeeper有一个快速移动的代码库,并且在编写wiki帖子之后,在结构上发生了微妙的变化,代码现在正在调用不同的东西-您是否查看了代码,以确定在这种情况下它是否不再返回ErrorResponse对象?否则我不太确定…谢谢你的回复@TarynEast。我检查门卫代码,它的结构和我用来覆盖body方法的结构相同。签出此链接以检查doorkeeper中的默认响应方法。我正在尝试覆盖我的lib文件夹下的相同方法。