class Identity::Gateway::Provider

Public Class Methods

new(request) click to toggle source

Class initializer.

Parameters

  • request - Current request.

Returns

Assigns arguments to instance variables.

# File lib/identity/gateway/provider.rb, line 16
def initialize(request)
  @settings = Identity::Gateway.configuration
  @request  = request
  @response = {}
  @subject  = nil
  @model    = Object.const_get(@settings.model)
end

Public Instance Methods

authorize!() click to toggle source

Get token resource owner.

Returns

  • Object - HTTParty response or identity error.

# File lib/identity/gateway/provider.rb, line 28
def authorize!
  token = request_token
  @subject = @model.find_by(token: token)
  return unless (@subject && @subject.token_has_expired?) || @subject.nil?
  authorize_from_provider
end
current_resource() click to toggle source

The current instance object associated to the model define in the configuration.

Returns

  • Object - instance or nil.

# File lib/identity/gateway/provider.rb, line 40
def current_resource
  @subject
end
revoke_access!() click to toggle source

Revoke access token.

Returns

  • HTTParty::Response.

# File lib/identity/gateway/provider.rb, line 48
def revoke_access!
  self.class.post(
    "#{@settings.provider_url}/oauth/revoke",
    body: { token: request_token }.to_json,
    headers: api_headers
  )
end

Protected Instance Methods

api_headers() click to toggle source

Required headers.

Returns

  • Hash - Hash of headers.

# File lib/identity/gateway/provider.rb, line 84
def api_headers
  {
    'Content-Type'  => 'application/json',
    'Accept'        => @settings.version_header || '',
    'Authorization' => @request.headers['Authorization'] || ''
  }
end
authorize_from_provider() click to toggle source

Try to authorize token against provider.

Returns

  • ActiveRecord|Error - User object, error otherwise.

# File lib/identity/gateway/provider.rb, line 62
def authorize_from_provider
  @response = self.class.get(
    @settings.provider_url + @settings.identity_path,
    headers: api_headers
  )

  raise Unauthorized if @response.code == 401 || @response.parsed_response.nil?
  @subject = @model.from_oauth_provider(@response.parsed_response)
end
request_token() click to toggle source

Get oauth token from headers.

Returns

  • String - token.

# File lib/identity/gateway/provider.rb, line 76
def request_token
  (@request.headers['Authorization'] || '').gsub('Bearer ', '')
end