module LookerSDK::Authentication

Authentication methods for {LookerSDK::Client}

Attributes

access_token_expires_at[RW]
access_token_type[RW]

Public Instance Methods

application_credentials?() click to toggle source

Indicates if the client has OAuth Application client_id and client_secret credentials

@see look TODO docs link @return Boolean

# File lib/looker-sdk/authentication.rb, line 86
def application_credentials?
  !!application_credentials
end
authenticate() click to toggle source

Authenticate to the server and get an access_token for use in future calls.

# File lib/looker-sdk/authentication.rb, line 49
def authenticate
  raise "client_id and client_secret required" unless application_credentials?

  set_access_token_from_params(nil)
  without_authentication do
    encoded_auth = Faraday::Utils.build_query(application_credentials)
    post("#{URI.parse(api_endpoint).path}/login", encoded_auth, header: {:'Content-Type' => 'application/x-www-form-urlencoded'})
    raise "login failure #{last_response.status}" unless last_response.status == 200
    set_access_token_from_params(last_response.data)
  end
end
ensure_logged_in() click to toggle source

This is called automatically by 'request'

# File lib/looker-sdk/authentication.rb, line 33
def ensure_logged_in
  authenticate unless token_authenticated? || @skip_authenticate
end
logout() click to toggle source
# File lib/looker-sdk/authentication.rb, line 72
def logout
  without_authentication do
    result = !!@access_token && ((delete("#{URI.parse(api_endpoint).path}/logout") ; delete_succeeded?) rescue false)
    set_access_token_from_params(nil)
    result
  end
end
set_access_token_from_params(params) click to toggle source
# File lib/looker-sdk/authentication.rb, line 61
def set_access_token_from_params(params)
  reset_agent
  if params
    @access_token = params[:access_token]
    @access_token_type = params[:token_type]
    @access_token_expires_at = Time.now + params[:expires_in]
  else
    @access_token = @access_token_type = @access_token_expires_at = nil
  end
end
token_authenticated?() click to toggle source

Indicates if the client has an OAuth access token

@see look TODO docs link @return [Boolean]

# File lib/looker-sdk/authentication.rb, line 95
def token_authenticated?
  !!(@access_token && (@access_token_expires_at.nil? || @access_token_expires_at > Time.now))
end
without_authentication() { || ... } click to toggle source
# File lib/looker-sdk/authentication.rb, line 37
def without_authentication
  begin
    old_skip = @skip_authenticate || false
    @skip_authenticate = true
    yield
  ensure
    @skip_authenticate = old_skip
  end
end

Private Instance Methods

application_credentials() click to toggle source
# File lib/looker-sdk/authentication.rb, line 101
def application_credentials
  if @client_id && @client_secret
    {
      :client_id     => @client_id,
      :client_secret => @client_secret
    }
  end
end
load_credentials_from_netrc() click to toggle source
# File lib/looker-sdk/authentication.rb, line 110
def load_credentials_from_netrc
  return unless netrc?

  require 'netrc'
  info = Netrc.read File.expand_path(netrc_file)
  netrc_host = URI.parse(api_endpoint).host
  creds = info[netrc_host]
  if creds.nil?
    # creds will be nil if there is no netrc for this end point
    looker_warn "Error loading credentials from netrc file for #{api_endpoint}"
  else
    self.client_id = creds[0]
    self.client_secret = creds[1]
  end
rescue LoadError
  looker_warn "Please install netrc gem for .netrc support"
end