Sha256: df2d89c8acaea1b3a45d876a3c6303274d43a4b19dc30b3e4e40db3b6750b9b6

Contents?: true

Size: 1.24 KB

Versions: 5

Compression:

Stored size: 1.24 KB

Contents

require 'doorkeeper/grape/authorization_decorator'

module Doorkeeper
  module Grape
    module Helpers
      extend ::Grape::API::Helpers
      include Doorkeeper::Rails::Helpers

      # endpoint specific scopes > parameter scopes > default scopes
      def doorkeeper_authorize!(*scopes)
        endpoint_scopes = env['api.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 = case error.status
                      when :unauthorized
                        401
                      when :forbidden
                        403
                      end

        error!({ error: error.description }, status_code)
      end

      private

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

      def decorated_request
        AuthorizationDecorator.new(request)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
doorkeeper-2.2.2 lib/doorkeeper/grape/helpers.rb
doorkeeper-3.0.0.rc1 lib/doorkeeper/grape/helpers.rb
doorkeeper-2.2.1 lib/doorkeeper/grape/helpers.rb
doorkeeper-2.2.0 lib/doorkeeper/grape/helpers.rb
doorkeeper-2.1.4 lib/doorkeeper/grape/helpers.rb