Class: Doorkeeper::OAuth::ErrorResponse

Inherits:
BaseResponse show all
Includes:
Helpers
Defined in:
lib/doorkeeper/oauth/error_response.rb

Constant Summary collapse

NON_REDIRECTABLE_STATES =
%i[invalid_redirect_uri invalid_client].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseResponse

#description

Constructor Details

#initialize(attributes = {}) ⇒ ErrorResponse

Returns a new instance of ErrorResponse.



36
37
38
39
40
41
42
# File 'lib/doorkeeper/oauth/error_response.rb', line 36

def initialize(attributes = {})
  @error = OAuth::Error.new(*attributes.values_at(:name, :state, :translate_options))
  @exception_class = attributes[:exception_class]
  @redirect_uri = attributes[:redirect_uri]
  @response_on_fragment = attributes[:response_on_fragment]
  @issuer = attributes[:issuer]
end

Class Method Details

.from_request(request, attributes = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# 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),
      translate_options: request.error.try(:translate_options),
      state: request.try(:state),
      redirect_uri: request.try(:redirect_uri),
    ),
  )
end

Instance Method Details

#bodyObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/doorkeeper/oauth/error_response.rb', line 44

def body
  {
    error: name,
    error_description: description,
    state: state,
    # RFC 9207 scopes the issuer to authorization error responses sent to
    # the client, i.e. the ones redirected back to its redirect_uri. It is
    # supplied only by the authorization endpoint (never token,
    # introspection or protected resource), and gated on redirectable? so
    # non-redirectable errors (invalid_client, invalid_redirect_uri) and
    # out-of-band flows - which render to the user instead of redirecting
    # to the client - do not carry it. Blank values are dropped below.
    iss: (@issuer if redirectable?),
  }.reject { |_, v| v.blank? }
end

#headersObject



80
81
82
83
84
85
86
# File 'lib/doorkeeper/oauth/error_response.rb', line 80

def headers
  {
    "Cache-Control" => "no-store, no-cache",
    "Content-Type" => "application/json; charset=utf-8",
    "WWW-Authenticate" => authenticate_info,
  }
end

#raise_exception!Object

Raises:

  • (exception_class.new(self))


88
89
90
# File 'lib/doorkeeper/oauth/error_response.rb', line 88

def raise_exception!
  raise exception_class.new(self), description
end

#redirect_uriObject



72
73
74
75
76
77
78
# File 'lib/doorkeeper/oauth/error_response.rb', line 72

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?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/doorkeeper/oauth/error_response.rb', line 68

def redirectable?
  !NON_REDIRECTABLE_STATES.include?(name) && !URIChecker.oob_uri?(@redirect_uri)
end

#statusObject



60
61
62
63
64
65
66
# File 'lib/doorkeeper/oauth/error_response.rb', line 60

def status
  if name == :invalid_client
    :unauthorized
  else
    :bad_request
  end
end