module Doorkeeper::Grape::Helpers

def decorated_request

def decorated_request
  AuthorizationDecorator.new(request)
end

def doorkeeper_authorize!(*scopes)

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.present?
             Doorkeeper::OAuth::Scopes.from_array(scopes)
           end
  super(*scopes)
end

def doorkeeper_render_error_with(error)

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

def doorkeeper_token

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

def endpoint

def endpoint
  env["api.endpoint"]
end

def error_status_codes

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