gpt4 book ai didi

ruby-on-rails - 无法覆盖门卫中的自定义 token 错误响应

转载 作者:行者123 更新时间:2023-12-03 09:04:09 27 4
gpt4 key购买 nike

我想重写门卫 token 错误响应正文方法。目前,当我在 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

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

最佳答案

经过多次尝试,我得到了解决方案。我不知道这是否是好方法,但到目前为止它正在发挥作用。

我所做的是

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 + Doorkeeperhttps://scotch.io/@jiggs/rails-api-doorkeeper-devise

或者

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

关于ruby-on-rails - 无法覆盖门卫中的自定义 token 错误响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48379623/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com