Sha256: be6a7eda75e41c452f36c488577f942e017f20ae49c086d2d9c3829329d736df

Contents?: true

Size: 1.97 KB

Versions: 4

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module Doorkeeper
  module OAuth
    class ErrorResponse < BaseResponse
      include OAuth::Helpers

      def self.from_request(request, attributes = {})
        new(
          attributes.merge(
            name: request.error,
            state: request.try(:state),
            redirect_uri: request.try(:redirect_uri)
          )
        )
      end

      delegate :name, :description, :state, to: :@error

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

      def body
        {
          error: name,
          error_description: description,
          state: state
        }.reject { |_, v| v.blank? }
      end

      def status
        :unauthorized
      end

      def redirectable?
        name != :invalid_redirect_uri && name != :invalid_client &&
          !URIChecker.native_uri?(@redirect_uri)
      end

      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

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

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

      protected

      delegate :realm, to: :configuration

      def configuration
        Doorkeeper.configuration
      end

      def exception_class
        raise NotImplementedError, "error response must define #exception_class"
      end

      private

      def authenticate_info
        %(Bearer realm="#{realm}", error="#{name}", error_description="#{description}")
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
doorkeeper-5.0.3 lib/doorkeeper/oauth/error_response.rb
doorkeeper-5.1.0.rc1 lib/doorkeeper/oauth/error_response.rb
doorkeeper-5.0.2 lib/doorkeeper/oauth/error_response.rb
doorkeeper-5.0.1 lib/doorkeeper/oauth/error_response.rb