Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ Here's an example *Auth Hash* available in `request.env['omniauth.auth']`:
:provider => 'intercom',
:uid => '342324',
:info => {
:name => 'Kevin Antoine',
:email => '[email protected]',
:name => 'Kevin Antoine'
:verified => true,
:image => 'https://static.intercomassets.com/avatars/343616/square_128/me.jpg?1454165491',
:time_zone => 'Dublin'
},
:credentials => {
:token => 'dG9rOmNdrWt0ZjtgzzE0MDdfNGM5YVe4MzsmXzFmOGd2MDhiMfJmYTrxOtA=', # OAuth 2.0 access_token, which you may wish to store
Expand All @@ -67,11 +70,11 @@ Here's an example *Auth Hash* available in `request.env['omniauth.auth']`:
:id_code => 'abc123', # Company app_id
:type => 'app',
:secure => true, # Secure mode enabled for this app
:timezone => "Dublin",
:name => "ProjectMap"
:timezone => 'Dublin',
:name => 'ProjectMap'
},
:avatar => {
:image_url => "https://static.intercomassets.com/avatars/343616/square_128/me.jpg?1454165491"
:image_url => 'https://static.intercomassets.com/avatars/343616/square_128/me.jpg?1454165491'
}
}
}
Expand Down
67 changes: 32 additions & 35 deletions lib/omniauth/strategies/intercom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,68 @@ class Intercom < OmniAuth::Strategies::OAuth2
option :name, 'intercom'

option :client_options, {
:site => 'https://api.intercom.io',
:authorize_url => 'https://app.intercom.com/oauth',
:token_url => 'https://api.intercom.io/auth/eagle/token'
site: 'https://api.intercom.io',
token_url: 'https://api.intercom.io/auth/eagle/token',
authorize_url: 'https://app.intercom.com/oauth'
}

option :verify_email, true

uid { raw_info['id'] }

info do
{
:name => raw_info['name'],
:email => raw_info['email'],
}.tap do |info|
avatar = raw_info['avatar'] && raw_info['avatar']['image_url']
next {} if raw_info.empty?

info[:image] = avatar if avatar
end
avatar = raw_info.fetch('avatar', {})
app = raw_info.fetch('app', {})

{
name: raw_info['name'],
email: raw_info['email'],
verfied: raw_info['email_verified'],
image: avatar['image_url'],
time_zone: app['timezone']
}
end

extra do
{
:raw_info => raw_info
raw_info: raw_info
}
end

def raw_info
accept_headers
access_token.options[:mode] = :body
@raw_info ||= begin
parsed = access_token.get('/me').parsed
if options.verify_email && parsed['email_verified'] != true
return {}
headers = { 'Accept' => 'application/vnd.intercom.3+json' }

@raw_info ||= access_token.get('/me', headers: headers).parsed.tap do |hash|
if options.verify_email && hash['email_verified'] != true
hash.clear
end
parsed
end
end

def request_phase
prepopulate_signup_fields_form if request.params.fetch('signup', false)
prepare_request_phase_for_signup
super
end

protected
protected

def accept_headers
access_token.client.connection.headers['Authorization'] = access_token.client.connection.basic_auth(access_token.token, '')
access_token.client.connection.headers['Accept'] = "application/vnd.intercom.3+json"
access_token.client.connection.headers['User-Agent'] = "omniauth-intercom/#{::OmniAuth::Intercom::VERSION}"
end

def prepopulate_signup_fields_form
def prepare_request_phase_for_signup
return unless request.params['signup']
options.client_options[:authorize_url] += '/signup'
signup_hash = signup_fields_hash
options.client_options[:authorize_url] += '?' + signup_hash.map{|k,v| [CGI.escape(k.to_s), "=", CGI.escape(v.to_s)]}.map(&:join).join("&") unless signup_hash.empty?

signup_params = build_signup_params(request.params)
return if signup_params.empty?

options.client_options[:authorize_url] += "?#{URI.encode_www_form(signup_params)}"
end

def signup_fields_hash
hash = {}
['name', 'email', 'app_name'].each do |field_name|
hash[field_name] = request.params.fetch(field_name) if request.params.fetch(field_name, false)
def build_signup_params(params)
%w[name email app_name].each_with_object({}) do |field_name, hash|
hash.merge!(field_name => params[field_name]) if params[field_name]
end
return hash
end

end
end
end
7 changes: 4 additions & 3 deletions spec/omniauth/strategies/intercom_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

describe OmniAuth::Strategies::Intercom do
let(:access_token) { double('AccessToken', options: {}) }
let(:access_token_headers) { {headers: {"Accept" => "application/vnd.intercom.3+json"}} }
let(:token) { 'some-token' }
let(:client) { double('Client') }
let(:connection) { double('Connection') }
Expand Down Expand Up @@ -29,13 +30,13 @@
let(:response) { double('Response', :parsed => parsed_response) }

before do
allow(access_token).to receive(:get).with('/me').and_return response
allow(access_token).to receive(:get).with('/me', access_token_headers).and_return response
allow(response).to receive(:parsed).and_return parsed_response
end

context '#raw_info' do
it 'request "me"' do
expect(access_token).to receive(:get).with('/me').and_return response
expect(access_token).to receive(:get).with('/me', access_token_headers).and_return response

subject.raw_info
end
Expand Down Expand Up @@ -106,7 +107,7 @@
let(:response) { double('Response', :parsed => parsed_response) }

before do
allow(access_token).to receive(:get).with('/me').and_return response
allow(access_token).to receive(:get).with('/me', access_token_headers).and_return response
allow(response).to receive(:parsed).and_return parsed_response
end

Expand Down