forked from fortuity/rails3-mongoid-omniauth
/
omniauth.rb
73 lines (61 loc) · 2.86 KB
/
omniauth.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
require 'openid/store/filesystem'
#use OmniAuth::Strategies::OpenID, OpenID::Store::Filesystem.new('/tmp'), :name=> "google", :identifier => 'https://www.google.com/accounts/o8/id'
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
provider :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET']
provider :github, ENV['GITHUB_CLIENT_ID'], ENV['GITHUB_SECRET']
provider :linked_in, ENV['LINKED_IN_API_KEY'], ENV['LINKED_IN_SECRET_KEY']
provider :open_id, OpenID::Store::Filesystem.new('./tmp'), {:name => "google", :identifier => "https://www.google.com/accounts/o8/id" }
provider :open_id, OpenID::Store::Filesystem.new('./tmp'), {:name => "yahoo", :identifier => "https://me.yahoo.com"}
# provider :open_id, OpenID::Store::Filesystem.new('./tmp')
end
#This is a complete monkeypatch to fix the "MulitXml" bug.
require 'multi_xml'
require 'omniauth/oauth'
module OmniAuth
module Strategies
class LinkedIn < OmniAuth::Strategies::OAuth
def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
client_options = {
:site => 'https://api.linkedin.com',
:request_token_path => '/uas/oauth/requestToken',
:access_token_path => '/uas/oauth/accessToken',
:authorize_path => '/uas/oauth/authorize',
:scheme => :header
}
client_options[:authorize_path] = '/uas/oauth/authenticate' unless options[:sign_in] == false
super(app, :linked_in, consumer_key, consumer_secret, client_options, options, &block)
end
def auth_hash
hash = user_hash(@access_token)
OmniAuth::Utils.deep_merge(super, {
'uid' => hash.delete('id'),
'user_info' => hash
})
end
def user_hash(access_token)
person = MultiXml.parse(@access_token.get('/v1/people/~:(id,first-name,last-name,headline,member-url-resources,picture-url,location,public-profile-url)').body)['person']
hash = {
'id' => person['id'],
'first_name' => person['first_name'],
'last_name' => person['last_name'],
'nickname' => person['public_profile_url'].split('/').last,
'location' => person['location']['name'],
'image' => person['picture_url'],
'description' => person['headline'],
'public_profile_url' => person['public_profile_url']
}
hash['urls']={}
member_urls = person['member_url_resources']['member_url']
if (!member_urls.nil?) and (!member_urls.empty?)
[member_urls].flatten.each do |url|
hash['urls']["#{url['name']}"]=url['url']
end
end
hash['urls']['LinkedIn'] = person['public_profile_url']
hash['name'] = "#{hash['first_name']} #{hash['last_name']}"
hash
end
end
end
end