Class: SalesforceFlo::Authentication::OauthWrapper
- Inherits:
-
Object
- Object
- SalesforceFlo::Authentication::OauthWrapper
- Defined in:
- lib/salesforce_flo/authentication/oauth_wrapper.rb
Constant Summary collapse
- OAUTH_ENDPOINT =
'/services/oauth2/authorize'
- DEFAULT_AUTH_HOST =
'https://login.salesforce.com'
- DEFAULT_CLIENT_ID =
'3MVG9CEn_O3jvv0zPd34OzgiH037XR5Deez3GW8PpsMdzoxecdKUW1s.8oYU9GoLS2Tykr4qTrCizaQBjRXNT'
- DEFAULT_REDIRECT_HOSTNAME =
'localhost'
- DEFAULT_LISTEN_PORT =
'3835'
Instance Method Summary collapse
-
#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.
-
#initialize(opts = {}) ⇒ OauthWrapper
constructor
Creates a new OauthWrapper instance.
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
35 36 37 38 39 40 41 42 43 |
# File 'lib/salesforce_flo/authentication/oauth_wrapper.rb', line 35 def initialize(opts={}) @client_id = opts.fetch(:client_id, DEFAULT_CLIENT_ID) @redirect_hostname = opts.fetch(:redirect_hostname, DEFAULT_REDIRECT_HOSTNAME) @port = opts.fetch(:port, DEFAULT_LISTEN_PORT) @auth_host = opts.fetch(:auth_host, DEFAULT_AUTH_HOST) @client = opts.fetch(:client, -> () { Restforce.new() }) 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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/salesforce_flo/authentication/oauth_wrapper.rb', line 51 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 = opts.merge(auth_details).merge(client_id: @client_id, api_version: '39.0').inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} [:oauth_token] = [:access_token] if [:access_token] @client.call() end |