class Jm81auth::OAuth::Base

Attributes

access_token[R]

Public Class Methods

new(params) click to toggle source

Setup @params from params param (Har, har). Also, set @access_token, either from params Hash, or by calling get_access_token. @params is the expected params needed by get_access_token.

@param params [Hash]

Expected to contain :code, :redirectUri, :clientId, and, optionally,
:access_token
# File lib/jm81auth/oauth/base.rb, line 16
def initialize params
  @params = {
    code: params[:code],
    redirect_uri: params[:redirectUri],
    client_id: params[:clientId],
    client_secret: Jm81auth.config.client_secrets[provider_name]
  }

  @access_token = params[:access_token] || get_access_token
end

Public Instance Methods

data() click to toggle source

@return [Hash] Data returned by accessing data URL.

# File lib/jm81auth/oauth/base.rb, line 28
def data
  @data or get_data
end
display_name() click to toggle source

@return [String] Display name (e.g. “Jane Doe”) from data.

# File lib/jm81auth/oauth/base.rb, line 33
def display_name
  data['name']
end
email() click to toggle source

@return [String] Email address from data.

# File lib/jm81auth/oauth/base.rb, line 38
def email
  data['email']
end
get_data() click to toggle source

Get data via get request to provider's data URL.

@return [Hash]

# File lib/jm81auth/oauth/base.rb, line 45
def get_data
  response = client.get(self.class::DATA_URL, access_token: @access_token)
  @data = JSON.parse(response.body)
end
provider_data() click to toggle source

@return [Hash] provider_name and provider_id

# File lib/jm81auth/oauth/base.rb, line 61
def provider_data
  { provider_name: provider_name, provider_id: provider_id }
end
provider_id() click to toggle source

@return [String] Provider assigned ID, from data.

# File lib/jm81auth/oauth/base.rb, line 56
def provider_id
  data['id'] || data['sub']
end
provider_name() click to toggle source

@return [String] Provider name, based on class name.

# File lib/jm81auth/oauth/base.rb, line 51
def provider_name
  self.class.name.split('::').last.downcase
end

Private Instance Methods

client() click to toggle source

@return [HTTPClient]

# File lib/jm81auth/oauth/base.rb, line 68
def client
  @client ||= HTTPClient.new
end