Class: OmniAuth::Strategies::Force

Inherits:
OAuth2
  • Object
show all
Defined in:
lib/omniauth/strategies/force.rb

Overview

Authenticate to force.com utilizing OAuth 2.0 and retrieve basic user information.

Examples:

Basic Usage

use OmniAuth::Strategies::Force, 'consumer_key', 'consumer_secret'

Instance Method Summary collapse

Constructor Details

#initialize(app, consumer_key, consumer_secret, options = {}) ⇒ Force

Returns a new instance of Force.

Parameters:

  • app (Rack Application)

    standard middleware application parameter

  • app_id (String)

    the application id

  • app_secret (String)

    the application secret



15
16
17
18
19
20
21
# File 'lib/omniauth/strategies/force.rb', line 15

def initialize(app, consumer_key, consumer_secret, options = {})
  options[:site] = 'https://login.salesforce.com/'
  options[:authorize_path] = '/services/oauth2/authorize'
  options[:access_token_path] = '/services/oauth2/token'

  super(app, :force, consumer_key, consumer_secret, options)
end

Instance Method Details

#auth_hashObject



75
76
77
78
79
80
81
82
# File 'lib/omniauth/strategies/force.rb', line 75

def auth_hash
  OmniAuth::Utils.deep_merge(super, {
    'uid' => user_data['user_id'],
    'user_info' => ,
    'extra' => {'user_hash' => user_data},
    'credentials' => { 'instance_url' => @access_token['instance_url'] }
  })
end

#callback_phaseObject



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/omniauth/strategies/force.rb', line 52

def callback_phase
  if request.params['error'] || request.params['error_reason']
    raise CallbackError.new(request.params['error'], request.params['error_description'] || request.params['error_reason'], request.params['error_uri'])
  end

  @access_token = get_access_token(request.params['code'])
  @env['omniauth.auth'] = auth_hash
  call_app!
rescue ::OAuth2::HTTPError, ::OAuth2::AccessDenied, CallbackError => e
  fail!(:invalid_credentials, e)
end

#full_hostObject

Monkey patch scheme for callback url



28
29
30
31
32
33
34
# File 'lib/omniauth/strategies/force.rb', line 28

def full_host
  uri = URI.parse(request.url)
  uri.path = ''
  uri.query = nil
  uri.scheme = request.env['HTTP_X_FORWARDED_PROTO'] || request.scheme
  uri.to_s
end

#get_access_token(verifier) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/omniauth/strategies/force.rb', line 41

def get_access_token(verifier)
  access_token = client.web_server.get_access_token(
      verifier,
      :redirect_uri => callback_url,
      :grant_type => 'authorization_code').tap do |token|
    token.token_param = 'oauth_token'
  end

  access_token
end

#request_phaseObject



36
37
38
39
# File 'lib/omniauth/strategies/force.rb', line 36

def request_phase
  options[:response_type] ||= "code"
  super
end

#user_dataObject



23
24
25
# File 'lib/omniauth/strategies/force.rb', line 23

def user_data
  @data ||= MultiJson.decode(@access_token.get(@access_token['id']))
end

#user_infoObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/omniauth/strategies/force.rb', line 64

def 
  {
    'nickname' => user_data['nick_name'],
    'email' => user_data['email'],
    'name' => user_data['display_name'],
    'urls' => {
      'Force' => user_data['urls']['profile'],
    }
  }
end