未初始化的常量CustomTokenResponse(NameError)和未经授权的有效令牌访问

时间:2019-08-05 07:09:36

标签: ruby-on-rails ruby authentication devise doorkeeper

我一直在尝试将Rails应用程序迁移到api,并且我刚刚安装了Doorkeeper gem进行身份验证,它为有效用户生成了令牌。现在我有两个问题

  1. 即使我的访问令牌正确,当我尝试获取页面时,它也会在日志中显示未经授权的访问

  2. 我试图为登录和注销设置一个自定义令牌响应,因为它没有显示任何内容,但是当我尝试运行Rails服务器时却向我抛出错误

我已按照本指南进行操作

https://scotch.io/@jiggs/rails-api-doorkeeper-devise

# frozen_string_literal: true

#Doorkeeper.rb

Doorkeeper.configure do
  # Change the ORM that doorkeeper will use (needs plugins)
  orm :active_record

  # This block will be called to check whether the resource owner is authenticated or not.
  resource_owner_authenticator do
    # raise "Please configure doorkeeper resource_owner_authenticator block located in #{__FILE__}"
    # Put your resource owner authentication logic here.
    # Example implementation:
    current_user || warden.authenticate!(:scope => :user)
  end

   resource_owner_from_credentials do |routes|
    user = User.find_for_database_authentication(:email => params[:email])
    if user && user.valid_for_authentication? { user.valid_password?(params[:password]) }
      user
    end
  end

  skip_authorization do
    true
  end

  Doorkeeper::OAuth::TokenResponse.send :prepend, CustomTokenResponse
  Doorkeeper::OAuth::ErrorResponse.send :prepend, CustomTokenErrorResponse

  # WWW-Authenticate Realm (default "Doorkeeper").
  #
  # realm "Doorkeeper"
end


# lib/custom_token_response.rb

module CustomTokenResponse
  def body
    user_details = User.find(@token.resource_owner_id)
    # call original `#body` method and merge its result with the additional data hash
       super.merge({
           status_code: 200,
           message: I18n.t('devise.sessions.signed_in'),
           result: user_details
       })
  end
end
# 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

1:来自/home/rubyians/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/active_support.rb:8:在without_bootsnap_cache' /home/rubyians/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/active_support.rb:79:in中load_missing_constant中的代码块:未初始化的常量CustomTokenResponse(NameError)

0 个答案:

没有答案