class WineBouncer::OAuth2

Attributes

auth_strategy[R]

Strategy

Public Instance Methods

auth_scopes() click to toggle source

Returns all auth scopes from an protected endpoint.

nil

if none, otherwise an array of [ :scopes ]

# File lib/wine_bouncer/oauth2.rb, line 53
def auth_scopes
  return *nil unless auth_strategy.has_auth_scopes?
  auth_strategy.auth_scopes
end
before() click to toggle source

Before do.

# 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
context() click to toggle source

returns the api context

# File lib/wine_bouncer/oauth2.rb, line 9
def context
  env['api.endpoint']
end
doorkeeper_authorize!(*scopes) click to toggle source

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

# 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) click to toggle source

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

# File lib/wine_bouncer/oauth2.rb, line 20
def doorkeeper_request=(env)
  @_doorkeeper_request = ActionDispatch::Request.new(env)
end
endpoint_protected?() click to toggle source

returns true if the endpoint is protected, otherwise false

# File lib/wine_bouncer/oauth2.rb, line 45
def endpoint_protected?
  auth_strategy.endpoint_protected?
end
request() click to toggle source

Returns the request context.

# File lib/wine_bouncer/oauth2.rb, line 27
def request
  @_doorkeeper_request
end
valid_doorkeeper_token?(*scopes) click to toggle source

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

# File lib/wine_bouncer/oauth2.rb, line 34
def valid_doorkeeper_token?(*scopes)
  doorkeeper_token && doorkeeper_token.acceptable?(scopes)
end

Private Instance Methods

set_auth_strategy(strategy) click to toggle source
# File lib/wine_bouncer/oauth2.rb, line 102
def set_auth_strategy(strategy)
  @auth_strategy = WineBouncer::AuthStrategies.const_get(strategy.to_s.capitalize.to_s).new
end