Sha256: 0e61a9d9f9169de8ab0fdb3670526f683e71d5e3ac21575acf73b500809fc66a

Contents?: true

Size: 1.53 KB

Versions: 4

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

require "doorkeeper/grape/authorization_decorator"

module Doorkeeper
  module Grape
    # Doorkeeper helpers for Grape applications.
    # Provides helpers for endpoints authorization based on defined set of scopes.
    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.config.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

4 entries across 4 versions & 1 rubygems

Version Path
doorkeeper-5.3.3 lib/doorkeeper/grape/helpers.rb
doorkeeper-5.3.2 lib/doorkeeper/grape/helpers.rb
doorkeeper-5.3.1 lib/doorkeeper/grape/helpers.rb
doorkeeper-5.3.0 lib/doorkeeper/grape/helpers.rb