[英]Unable to override custom token error response in doorkeeper
I want to override doorkeeper token error response body method. 我想覆盖门卫令牌错误响应正文方法。 Currently when I pass username and password wrong at
http://localhost:3000/oauth/token
url then it will give below error message. 当前,当我在
http://localhost:3000/oauth/token
url传递用户名和密码错误时,它将给出以下错误消息。
Default doorkeeper response for unauthorised : 未经授权的默认门卫响应:
{
"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."
}
But I want different structure for error message for my API.something like below. 但是我想要API的错误消息结构不同,如下所示。
My expected response is : 我的预期回应是:
{
"status_code": 401,
"message": "Invalid username or password."
"result": []
}
I follow official documentation from here and tried below to full fill my expectation. 我从这里遵循官方文档,并在下面尝试完全满足我的期望。
Tried to make response custom : 试图使响应自定义:
Under lib/doorkeeper/oauth/error_response.rb
在
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 configuration : 门卫配置:
This is doorkeeper.rb
file under config -> initializer folder 这是
doorkeeper.rb
> 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
But it seems like it is not working. 但似乎无法正常工作。 It gives same result as it gives before.
它产生的结果与以前相同。 It is not going into
lib/doorkeeper/oauth/error_response.rb
file. 它不会进入
lib/doorkeeper/oauth/error_response.rb
文件。
I autoload lib folder in applicatoin.rb
file like 我像这样在
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
Tried many autoload syntax but not get success. 尝试了许多自动加载语法,但没有成功。
After too many tried I got solution. 经过太多尝试后,我得到了解决方案。 I don't know is it good way or not but it is working as of now.
我不知道这是否是好方法,但到目前为止它仍在工作。
What I done is 我所做的是
1) Create custom_token_error_response.rb
file under lib folder. 1)在lib文件夹下创建
custom_token_error_response.rb
文件。 And then override body
method of doorkeeper oauth error module. 然后重写门卫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) Prepend this module in doorkeeper ErrorResponse
module in doorkeepr.rb
initializer file.(check last line in below code) 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) Now restart your rails server and you are done. 3)现在,重新启动Rails服务器,您就完成了。
You can also refer this blog which I wrote to integrate Rails API + Devise + Doorkeeper . 您也可以参考我撰写的集成Rails API + Devise + Doorkeeper的博客。 https://scotch.io/@jiggs/rails-api-doorkeeper-devise
https://scotch.io/@jiggs/rails-api-doorkeeper-devise
OR 要么
https://medium.com/@khokhanijignesh29/rails-api-doorkeeper-devise-4212115c9f0d https://medium.com/@khokhanijignesh29/rails-api-doorkeeper-devise-4212115c9f0d
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.