Class: OmniAuth::Strategies::Eloqua

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

Instance Method Summary collapse

Instance Method Details

#auth_hash(result) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/omniauth/strategies/eloqua.rb', line 77

def auth_hash(result)
  detail = detail(result)
  hash = AuthHash.new(:provider => name, :uid => detail.uid)
  hash.info = detail.info unless skip_info?
  hash.credentials = AuthHash::InfoHash.new({
    token: result["access_token"],
    refresh_token: result["refresh_token"],
    expires_at: result["expires_in"],
    expires: result["expires_in"].present?
  })
  hash.extra = detail.extra
  hash
end

#callback_phaseObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/omniauth/strategies/eloqua.rb', line 36

def callback_phase
  error = request.params["error_reason"] || request.params["error"]
  if error
    fail!(error, CallbackError.new(request.params["error"],
      request.params["error_description"] ||
      request.params["error_reason"], request.params["error_uri"]))
  else
    conn = Faraday.new(:url => 'https://login.eloqua.com/auth/'\
      'oauth2/token') do |faraday|
        faraday.response :logger
        faraday.adapter  Faraday.default_adapter
    end

    token = Base64.strict_encode64("#{options.client_id.strip}:"\
      "#{options.client_secret.strip}").strip

    result = conn.post do |req|
      req.headers['Content-Type'] = 'application/json'
      req.headers['Authorization'] = "Basic #{token}"
      req.body = "{ 'grant_type': 'authorization_code', 'code': "\
        "'#{request.params["code"]}', 'redirect_uri': "\
        "'#{callback_url}?response_type=code&state="\
        "#{request.params['state']}' }"
    end
    @access_token = result
    result = JSON.parse(result.body)
    if @access_token.status != 200
      fail!(result['error'], Exception.new(result['error_description']))
    else
      env['omniauth.auth'] = auth_hash(result)
      call_app!
    end
  end
rescue ::OAuth2::Error, CallbackError => e
  fail!(:invalid_credentials, e)
rescue ::Timeout::Error, ::Errno::ETIMEDOUT => e
  fail!(:timeout, e)
rescue ::SocketError => e
  fail!(:failed_to_connect, e)
end

#request_phaseObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/omniauth/strategies/eloqua.rb', line 25

def request_phase
  redirect client.auth_code.authorize_url(
    {
      redirect_uri: "#{callback_url}?response_type=code&state="\
        "#{request.params['state']}"
    }.merge(
      options.authorize_params
    )
  )
end