Search code examples
ruby-on-rails-4doorkeeper

raise custom error based on different conditions in resource_owner_authenticator in config/initializers/doorkeeper.rb


I want to raise custom error and customise error response based on different conditions. for authentication, I have a few conditions where a user is not authenticated, for example, not active user, user profile is incomplete etc etc.

I followed this, #315 , I configured the same but execution stops at line where it raises the custom doorkeeper error.

Doorkeeper.configure do resource_owner_authenticator do @user = User.find(session[:user_id]) || redirect_to(user_login_path(return_to: request.fullpath)) raise Doorkeeper::Errors::OwnError unless @user.status == 'active' end end

Please help if anybody have any clue.


Solution

  • I had the same issue until I read the NEWS.md file:

    - [#749] Allow user to raise authorization error with custom messages.
      Under `resource_owner_authenticator` block a user can
      `raise Doorkeeper::Errors::DoorkeeperError.new('custom_message')`
    

    For password flow POST /oauth/token:

     resource_owner_from_credentials do |routes|
        raise Doorkeeper::Errors::DoorkeeperError.new('custom_message')
     end
    

    Gives a response like:

     {
          "error": "custom_message",
          "error_description": "The authorization server encountered an unexpected condition which prevented it from fulfilling the request."
     }