Module: Doorkeeper::Rails::Helpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/doorkeeper/rails/helpers.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#doorkeeper_authorize!(*scopes) ⇒ Object


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/doorkeeper/rails/helpers.rb', line 28

def doorkeeper_authorize!(*scopes)
  scopes ||= Doorkeeper.configuration.default_scopes

  unless valid_doorkeeper_token?(*scopes)
    if !doorkeeper_token || !doorkeeper_token.accessible?
      error = OAuth::InvalidTokenResponse.from_access_token(doorkeeper_token)
      options = doorkeeper_unauthorized_render_options
    else
      error = OAuth::ForbiddenTokenResponse.from_scopes(scopes)
      options = doorkeeper_forbidden_render_options
    end
    headers.merge!(error.headers.reject { |k| ['Content-Type'].include? k })
    doorkeeper_error_renderer(error, options)
  end
end

#doorkeeper_error_renderer(error, options = {}) ⇒ Object


52
53
54
55
56
57
58
59
60
# File 'lib/doorkeeper/rails/helpers.rb', line 52

def doorkeeper_error_renderer(error, options = {})
  if options.blank?
    head error.status
  else
    options[:status] = error.status
    options[:layout] = false if options[:layout].nil?
    render options
  end
end

#doorkeeper_forbidden_render_optionsObject


48
49
50
# File 'lib/doorkeeper/rails/helpers.rb', line 48

def doorkeeper_forbidden_render_options
  nil
end

#doorkeeper_tokenObject


20
21
22
# File 'lib/doorkeeper/rails/helpers.rb', line 20

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

#doorkeeper_unauthorized_render_optionsObject


44
45
46
# File 'lib/doorkeeper/rails/helpers.rb', line 44

def doorkeeper_unauthorized_render_options
  nil
end

#valid_doorkeeper_token?(*scopes) ⇒ Boolean

Returns:

  • (Boolean)

24
25
26
# File 'lib/doorkeeper/rails/helpers.rb', line 24

def valid_doorkeeper_token?(*scopes)
  doorkeeper_token && doorkeeper_token.acceptable?(scopes)
end