无法覆盖门卫中的自定义令牌错误响应
Unable to override custom token error response in doorkeeper
我想覆盖看门人令牌错误响应正文方法。当前,当我在 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.something 错误消息的不同结构,如下所示。
我预期的回复是:
{
"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
门卫配置:
这是配置 -> 初始化程序文件夹下的 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
尝试了很多自动加载语法但没有成功。
经过多次尝试,我得到了解决方案。我不知道这是不是好方法,但它现在正在工作。
我做的是
1) 在 lib 文件夹下创建 custom_token_error_response.rb
文件。然后覆盖doorkeeper 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) 在 doorkeepr.rb
初始化文件中的 doorkeeper ErrorResponse
模块中添加此模块。(检查下面代码的最后一行)
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
或
https://medium.com/@khokhanijignesh29/rails-api-doorkeeper-devise-4212115c9f0d
我想覆盖看门人令牌错误响应正文方法。当前,当我在 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.something 错误消息的不同结构,如下所示。
我预期的回复是:
{
"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
门卫配置:
这是配置 -> 初始化程序文件夹下的 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
尝试了很多自动加载语法但没有成功。
经过多次尝试,我得到了解决方案。我不知道这是不是好方法,但它现在正在工作。
我做的是
1) 在 lib 文件夹下创建 custom_token_error_response.rb
文件。然后覆盖doorkeeper 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) 在 doorkeepr.rb
初始化文件中的 doorkeeper ErrorResponse
模块中添加此模块。(检查下面代码的最后一行)
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
或
https://medium.com/@khokhanijignesh29/rails-api-doorkeeper-devise-4212115c9f0d