Class: SalesforceFlo::Authentication::OauthWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/salesforce_flo/authentication/oauth_wrapper.rb

Constant Summary collapse

OAUTH_ENDPOINT =
'/services/oauth2/authorize'
AUTH_HOST =
'https://login.salesforce.com'
DEFAULT_CLIENT_ID =
'3MVG9CEn_O3jvv0zPd34OzgiH037XR5Deez3GW8PpsMdzoxecdKUW1s.8oYU9GoLS2Tykr4qTrCizaQBjRXNT'
DEFAULT_REDIRECT_HOSTNAME =
'localhost'
DEFAULT_LISTEN_PORT =
'3835'

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ OauthWrapper

Creates a new OauthWrapper instance

that the user will be redirected to at the end of the Oauth authorization flow. This MUST match the redirect URL specified in the connected app settings. flow. This will be appended to the redirect_hostname

Parameters:

  • opts (Hash) (defaults to: {})

    The options needed to create the provider

Options Hash (opts):

  • :client_id (String)

    The client id of the connected app for Oauth authorization

  • :redirect_hostname (String) — default: http://localhost:3835

    The hostname portion of the uri

  • :port (String) — default: 3835

    The port that the user will be redirected to at the end of the Oauth

  • :client (#call)

    An object that produces a client when called with initialization options

Raises:

  • (ArgumentError)

    If client object does not respond_to?(:call)



33
34
35
36
37
38
39
40
# File 'lib/salesforce_flo/authentication/oauth_wrapper.rb', line 33

def initialize(opts={})
  @client_id = opts[:client_id] || DEFAULT_CLIENT_ID
  @redirect_hostname = opts[:redirect_hostname] || DEFAULT_REDIRECT_HOSTNAME
  @port = opts[:port] || DEFAULT_LISTEN_PORT
  @client = opts[:client] || -> (options) { Restforce.new(options) }

  raise ArgumentError.new(':client must respond to #call, try a lambda') unless @client.respond_to?(:call)
end

Instance Method Details

#call(opts = {}) ⇒ Object

Starts a temporary webserver on the specified port, and initiates an Oauth authorization flow, which will redirect the user back to localhost on the specified port.

from the salesforce that includes the access token

Parameters:

  • opts (Hash) (defaults to: {})

    Options that will be passed to the client when called, which will be merged with the response

Returns:

  • The result of invoking #call on the client object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/salesforce_flo/authentication/oauth_wrapper.rb', line 48

def call(opts={})
  server = WEBrick::HTTPServer.new(Port: @port)
  auth_details = {}

  server.mount_proc('/') do |req, res|
    res.body = js_template
  end

  server.mount_proc('/send_token') do |req, res|
    auth_details = JSON.parse(req.body)
    res.body = 'token sent'

    server.shutdown # server will shutdown after completing the request
  end

  trap "INT" do server.shutdown end

  Launchy.open("#{AUTH_HOST}#{OAUTH_ENDPOINT}?#{oauth_query_string}")
  server.start

  merged_options = opts.merge(auth_details).merge(client_id: @client_id, api_version: '39.0').inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
  merged_options[:oauth_token] = merged_options[:access_token] if merged_options[:access_token]
  @client.call(merged_options)
end