Sha256: 92c6668e0ec7c4f28d8e52c71f4782a53dd4cc94b25ebdb653f15c6765e0fa29

Contents?: true

Size: 1.38 KB

Versions: 5

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require 'doorkeeper/grape/authorization_decorator'

module Doorkeeper
  module Grape
    module Helpers
      # These helpers are for grape >= 0.10
      extend ::Grape::API::Helpers
      include Doorkeeper::Rails::Helpers

      # endpoint specific scopes > parameter scopes > default scopes
      def doorkeeper_authorize!(*scopes)
        endpoint_scopes = endpoint.route_setting(:scopes) || endpoint.options[:route_options][:scopes]
        scopes = if endpoint_scopes
                   Doorkeeper::OAuth::Scopes.from_array(endpoint_scopes)
                 elsif scopes && !scopes.empty?
                   Doorkeeper::OAuth::Scopes.from_array(scopes)
                 end

        super(*scopes)
      end

      def doorkeeper_render_error_with(error)
        status_code = error_status_codes[error.status]
        error!({ error: error.description }, status_code, error.headers)
      end

      private

      def endpoint
        env['api.endpoint']
      end

      def doorkeeper_token
        @doorkeeper_token ||= OAuth::Token.authenticate(
          decorated_request,
          *Doorkeeper.configuration.access_token_methods
        )
      end

      def decorated_request
        AuthorizationDecorator.new(request)
      end

      def error_status_codes
        {
          unauthorized: 401,
          forbidden: 403
        }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
doorkeeper-5.0.3 lib/doorkeeper/grape/helpers.rb
doorkeeper-5.1.0.rc2 lib/doorkeeper/grape/helpers.rb
doorkeeper-5.1.0.rc1 lib/doorkeeper/grape/helpers.rb
doorkeeper-5.0.2 lib/doorkeeper/grape/helpers.rb
doorkeeper-5.0.1 lib/doorkeeper/grape/helpers.rb