class Doorkeeper::OAuth::ErrorResponse

Constants

NON_REDIRECTABLE_STATES

Public Class Methods

from_request(request, attributes = {}) click to toggle source
# File lib/doorkeeper/oauth/error_response.rb, line 10
def self.from_request(request, attributes = {})
  new(
    attributes.merge(
      name: error_name_for(request.error),
      exception_class: exception_class_for(request.error),
      state: request.try(:state),
      redirect_uri: request.try(:redirect_uri),
    ),
  )
end
new(attributes = {}) click to toggle source
# File lib/doorkeeper/oauth/error_response.rb, line 35
def initialize(attributes = {})
  @error = OAuth::Error.new(*attributes.values_at(:name, :state))
  @exception_class = attributes[:exception_class]
  @redirect_uri = attributes[:redirect_uri]
  @response_on_fragment = attributes[:response_on_fragment]
end

Private Class Methods

error_name_for(error) click to toggle source
# File lib/doorkeeper/oauth/error_response.rb, line 21
def self.error_name_for(error)
  error.respond_to?(:name_for_response) ? error.name_for_response : error
end
exception_class_for(error) click to toggle source
# File lib/doorkeeper/oauth/error_response.rb, line 25
def self.exception_class_for(error)
  return error if error.respond_to?(:name_for_response)

  "Doorkeeper::Errors::#{error.to_s.classify}".safe_constantize
end

Public Instance Methods

body() click to toggle source
# File lib/doorkeeper/oauth/error_response.rb, line 42
def body
  {
    error: name,
    error_description: description,
    state: state,
  }.reject { |_, v| v.blank? }
end
headers() click to toggle source
# File lib/doorkeeper/oauth/error_response.rb, line 70
def headers
  {
    "Cache-Control" => "no-store, no-cache",
    "Content-Type" => "application/json; charset=utf-8",
    "WWW-Authenticate" => authenticate_info,
  }
end
raise_exception!() click to toggle source
# File lib/doorkeeper/oauth/error_response.rb, line 78
def raise_exception!
  raise exception_class.new(self), description
end
redirect_uri() click to toggle source
# File lib/doorkeeper/oauth/error_response.rb, line 62
def redirect_uri
  if @response_on_fragment
    Authorization::URIBuilder.uri_with_fragment(@redirect_uri, body)
  else
    Authorization::URIBuilder.uri_with_query(@redirect_uri, body)
  end
end
redirectable?() click to toggle source
# File lib/doorkeeper/oauth/error_response.rb, line 58
def redirectable?
  !NON_REDIRECTABLE_STATES.include?(name) && !URIChecker.oob_uri?(@redirect_uri)
end
status() click to toggle source
# File lib/doorkeeper/oauth/error_response.rb, line 50
def status
  if name == :invalid_client || name == :unauthorized_client
    :unauthorized
  else
    :bad_request
  end
end

Protected Instance Methods

exception_class() click to toggle source
# File lib/doorkeeper/oauth/error_response.rb, line 88
def exception_class
  return @exception_class if @exception_class
  raise NotImplementedError, "error response must define #exception_class"
end
realm() click to toggle source
# File lib/doorkeeper/oauth/error_response.rb, line 84
def realm
  Doorkeeper.config.realm
end

Private Instance Methods

authenticate_info() click to toggle source
# File lib/doorkeeper/oauth/error_response.rb, line 95
def authenticate_info
  %(Bearer realm="#{realm}", error="#{name}", error_description="#{description}")
end