Class: WineBouncer::OAuth2

Inherits:
Grape::Middleware::Base
  • Object
show all
Includes:
Doorkeeper::Helpers::Controller
Defined in:
lib/wine_bouncer/oauth2.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#auth_strategyObject (readonly)

Strategy



98
99
100
# File 'lib/wine_bouncer/oauth2.rb', line 98

def auth_strategy
  @auth_strategy
end

Instance Method Details

#auth_scopesObject

Returns all auth scopes from an protected endpoint.

nil

if none, otherwise an array of [ :scopes ]



53
54
55
56
# File 'lib/wine_bouncer/oauth2.rb', line 53

def auth_scopes
  return *nil unless auth_strategy.has_auth_scopes?
  auth_strategy.auth_scopes
end

#beforeObject

Before do.



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/wine_bouncer/oauth2.rb', line 81

def before
  return if WineBouncer.configuration.disable_block.call

  set_auth_strategy(WineBouncer.configuration.auth_strategy)
  auth_strategy.api_context = context
  #extend the context with auth methods.
  context.extend(WineBouncer::AuthMethods)
  context.protected_endpoint = endpoint_protected?
  return unless context.protected_endpoint?
  self.doorkeeper_request = env # set request for later use.
  doorkeeper_authorize!(*auth_scopes)
  context.doorkeeper_access_token = doorkeeper_token
end

#contextObject

returns the api context



9
10
11
# File 'lib/wine_bouncer/oauth2.rb', line 9

def context
  env['api.endpoint']
end

#doorkeeper_authorize!(*scopes) ⇒ Object

This method handles the authorization, raises errors if authorization has failed.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wine_bouncer/oauth2.rb', line 61

def doorkeeper_authorize!(*scopes)
  scopes = Doorkeeper.configuration.default_scopes if scopes.empty?
  unless valid_doorkeeper_token?(*scopes)
    if !doorkeeper_token || !doorkeeper_token.accessible?
      error = Doorkeeper::OAuth::InvalidTokenResponse.from_access_token(doorkeeper_token)
      raise WineBouncer::Errors::OAuthUnauthorizedError, error
    else
      error = Doorkeeper::OAuth::ForbiddenTokenResponse.from_scopes(scopes)
      raise WineBouncer::Errors::OAuthForbiddenError, error
    end
  end
end

#doorkeeper_request=(env) ⇒ Object

Sets and converts a rack request to a ActionDispatch request, which is required for DoorKeeper to function.



20
21
22
# File 'lib/wine_bouncer/oauth2.rb', line 20

def doorkeeper_request=(env)
  @_doorkeeper_request = ActionDispatch::Request.new(env)
end

#endpoint_protected?Boolean

returns true if the endpoint is protected, otherwise false



45
46
47
# File 'lib/wine_bouncer/oauth2.rb', line 45

def endpoint_protected?
  auth_strategy.endpoint_protected?
end

#requestObject

Returns the request context.



27
28
29
# File 'lib/wine_bouncer/oauth2.rb', line 27

def request
  @_doorkeeper_request
end

#valid_doorkeeper_token?(*scopes) ⇒ Boolean

Returns true if the doorkeeper token is valid, false otherwise.



34
35
36
# File 'lib/wine_bouncer/oauth2.rb', line 34

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