omniauth_china 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- omniauth_china (0.0.3)
5
- multi_json (~> 0.0.2)
6
- nokogiri (~> 1.4.2)
7
- oa-core (~> 0.2.0.beta1)
8
- oa-oauth (~> 0.2.0.beta1)
9
- oauth (~> 0.4.0)
10
- oauth2 (~> 0.1.0)
4
+ omniauth_china (0.0.6)
5
+ oa-core (~> 0.2.0)
6
+ oa-oauth (~> 0.2.0)
11
7
 
12
8
  GEM
13
9
  remote: http://rubygems.org/
@@ -145,12 +141,6 @@ DEPENDENCIES
145
141
  capybara (>= 0.4.0)
146
142
  json (~> 1.4.3)
147
143
  mg (~> 0.0.8)
148
- multi_json (~> 0.0.2)
149
- nokogiri (~> 1.4.2)
150
- oa-core (~> 0.2.0.beta1)
151
- oa-oauth (~> 0.2.0.beta1)
152
- oauth (~> 0.4.0)
153
- oauth2 (~> 0.1.0)
154
144
  omniauth_china!
155
145
  rack
156
146
  rack-test (~> 0.5.4)
@@ -22,22 +22,11 @@ OmniAuth currently supports the following external providers:
22
22
  * T163 (credit: [he9qi](http://github.com/he9qi))
23
23
  * Tsohu (credit: [he9qi](http://github.com/he9qi))
24
24
  * Tqq (credit: [he9qi](http://github.com/he9qi))
25
- * Renren (Renren Connect of renren.com) (credit: [taweili](http://github.com/taweili), [rainux](http://github.com/rainux))
25
+ * Renren (credit: [taweili](http://github.com/taweili), [rainux](http://github.com/rainux), [he9qi](http://github.com/he9qi))
26
26
 
27
- ## 人人([taweili](http://github.com/taweili), [rainux](http://github.com/rainux))
27
+ ## 人人([taweili](http://github.com/taweili), [rainux](http://github.com/rainux), [he9qi](http://github.com/he9qi))
28
28
 
29
- Run the generator to generate `xd_receiver.html` and include helper into ApplicationHelper:
30
-
31
- rails g omniauth_renren:install
32
-
33
- Place the Renren Connect button on any page by simply call `omniauth_renren_connect_button` and `omniauth_renren_javascript`:
34
-
35
- <%= omniauth_renren_connect_button %>
36
- <%= omniauth_renren_javascript %>
37
-
38
- Route `/auth/renren` to the page that contain Renren Connect button:
39
-
40
- match '/auth/renren' => 'users#show'
29
+ 人人 uses Oauth 2.0 now, however, As of right now, 人人still needs session key to get more user information besides uid [see here](http://wiki.dev.renren.com/wiki/%E8%8E%B7%E5%8F%96%E4%BA%BA%E4%BA%BA%E7%BD%91%E8%B5%84%E6%BA%90), so we still need `session.rb` and `service.rb` if we want more user information. Hopefully this will change soon in the future.
41
30
 
42
31
  ## Usage
43
32
 
@@ -69,7 +58,3 @@ The `user_info` hash will automatically be populated with as much information ab
69
58
  ## Contributors (thanks!)
70
59
  * [huacnlee](http://github.com/huacnlee)
71
60
 
72
-
73
- ## TODO
74
-
75
- Write better tests!!
@@ -1,48 +1,79 @@
1
- require 'omniauth_china/oauth_china'
1
+ require 'omniauth/oauth'
2
+ require 'multi_json'
2
3
 
3
4
  module OmniAuth
4
5
  module Strategies
5
- class Renren
6
- include OmniAuth::Strategy
6
+
7
+ # Authenticate to Renren utilizing OAuth 2.0 and retrieve
8
+ # basic user information.
9
+ #
10
+ # @example Basic Usage
11
+ # use OmniAuth::Strategies::Renren, 'client_id', 'client_secret'
12
+ class Renren < OAuth2
7
13
  autoload :Session, 'omniauth_china/strategies/renren/session'
8
14
  autoload :Service, 'omniauth_china/strategies/renren/service'
9
- autoload :Helper, 'omniauth_china/strategies/renren/helper'
10
-
15
+
11
16
  class << self
12
- def api_key
13
- @@api_key
14
- end
15
-
16
- def secret_key
17
- @@secret_key
18
- end
17
+ def api_key; @@api_key; end
18
+ def secret_key; @@secret_key; end
19
19
  end
20
-
21
- def initialize(app, api_key, secret_key, options = {})
22
- @@api_key = api_key
23
- @@secret_key = secret_key
24
-
25
- super(app, :renren, options)
20
+
21
+ # @param [Rack Application] app standard middleware application parameter
22
+ # @param [String] client_id the application id as [registered on Renren](http://www.renren.com/developers/)
23
+ # @param [String] client_secret the application secret as registered on Renren
24
+ # @option options [String] :scope ('email') comma-separated extended permissions such as `email` and `manage_pages`
25
+ def initialize(app, client_id = nil, client_secret = nil, options = {}, &block)
26
+ @@api_key = client_id
27
+ @@secret_key = client_secret
28
+ super(app, :renren, client_id, client_secret, {:site => 'https://graph.renren.com/', :access_token_path => "/oauth/token"}, options, &block)
26
29
  end
27
-
30
+
31
+ def user_hash
32
+ @data ||= MultiJson.decode(@access_token.get('/renren_api/session_key', { :oauth_token => @access_token.token }, { "Accept-Language" => "zh;"}))
33
+ @renren_session ||= Renren::Session.new(@data)
34
+ end
35
+
28
36
  def request_phase
29
- @response.finish
37
+ options[:scope] ||= "email"
38
+ options[:response_type] ||= "code"
39
+ super
30
40
  end
31
-
41
+
42
+ # need to have :grant_type=>"authorization_code" for renren to work
32
43
  def callback_phase
33
- @renren_session = Renren::Session.new(request.cookies)
34
- super
44
+ if request.params['error'] || request.params['error_reason']
45
+ raise CallbackError.new(request.params['error'], request.params['error_description'] || request.params['error_reason'], request.params['error_uri'])
46
+ end
47
+ verifier = request.params['code']
48
+ @access_token = client.web_server.get_access_token(verifier, {:redirect_uri => callback_url, :grant_type=>"authorization_code" })
49
+ @env['omniauth.auth'] = auth_hash
50
+ call_app!
51
+ rescue ::OAuth2::HTTPError, ::OAuth2::AccessDenied, CallbackError => e
52
+ fail!(:invalid_credentials, e)
35
53
  end
36
-
54
+
37
55
  def auth_hash
38
56
  OmniAuth::Utils.deep_merge(super, {
39
- 'uid' => @renren_session.uid,
40
- 'user_info' => @renren_session.user,
57
+ 'uid' => user_hash.uid,
58
+ 'user_info' => user_info,
41
59
  'extra' => {
42
- 'renren_session' => @renren_session
60
+ 'user_hash' => user_hash.user
43
61
  }
44
62
  })
45
63
  end
64
+
65
+ def user_info
66
+ user_hash = self.user_hash.user
67
+ {
68
+ 'username' => user_hash['name'],
69
+ 'name' => user_hash['name'],
70
+ 'image' => user_hash['tinyurl'],
71
+ 'vip' => user_hash['vip'],
72
+ 'headurl' => user_hash['headurl']
73
+ }
74
+ end
75
+
46
76
  end
77
+
47
78
  end
48
- end
79
+ end
@@ -13,11 +13,10 @@ module OmniAuth
13
13
  attr_reader :expires
14
14
  attr_reader :uid
15
15
 
16
- def initialize(cookies)
17
- options = extract_renren_cookies(cookies)
18
- @expires = options['expires'] ? Integer(options['expires']) : 0
19
- @session_key = options['session_key']
20
- @uid = options['user']
16
+ def initialize(user_hash)
17
+ @expires = user_hash['renren_token']['expires_in'] ? user_hash['renren_token']['expires_in'].to_i : 0
18
+ @session_key = user_hash['renren_token']['session_key']
19
+ @uid = user_hash['user']['id']
21
20
  end
22
21
 
23
22
  def user
@@ -65,26 +64,6 @@ module OmniAuth
65
64
  end
66
65
 
67
66
  private
68
- def extract_renren_cookies(cookies)
69
- parsed = {}
70
- xn_cookie_names(cookies).each { |key| parsed[key[xn_cookie_prefix.size, key.size]] = cookies[key] }
71
-
72
- # #returning gracefully if the cookies aren't set or have expired
73
- # return unless parsed['session_key'] && parsed['user'] && parsed['expires'] && parsed['ss']
74
- # # TODO: check expires, why it alway less than Time.now
75
- # return unless (Time.at(parsed['expires'].to_s.to_f) > Time.now) || (parsed['expires'] == "0")
76
- # #if we have the unexpired cookies, we'll throw an exception if the sig doesn't verify
77
- verify_signature(parsed, cookies[Renren.api_key])
78
- parsed
79
- end
80
-
81
- def xn_cookie_names(cookies)
82
- xn_cookie_names = cookies.keys.select {|k| k && k.starts_with?(xn_cookie_prefix) }
83
- end
84
-
85
- def xn_cookie_prefix
86
- Renren.api_key + '_'
87
- end
88
67
 
89
68
  def verify_signature(renren_sig_params, expected_signature)
90
69
  self.class.send :verify_signature, renren_sig_params, expected_signature
@@ -1,3 +1,3 @@
1
1
  module OmniauthChina
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -0,0 +1,5 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe OmniAuth::Strategies::Renren do
4
+ it_should_behave_like "an oauth2 strategy"
5
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 6
9
- version: 0.0.6
8
+ - 7
9
+ version: 0.0.7
10
10
  platform: ruby
11
11
  authors:
12
12
  - Qi He
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-03-31 00:00:00 -07:00
17
+ date: 2011-04-26 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -69,7 +69,6 @@ files:
69
69
  - lib/omniauth_china/oauth_china.rb
70
70
  - lib/omniauth_china/strategies/douban.rb
71
71
  - lib/omniauth_china/strategies/renren.rb
72
- - lib/omniauth_china/strategies/renren/helper.rb
73
72
  - lib/omniauth_china/strategies/renren/service.rb
74
73
  - lib/omniauth_china/strategies/renren/session.rb
75
74
  - lib/omniauth_china/strategies/t163.rb
@@ -79,6 +78,7 @@ files:
79
78
  - lib/omniauth_china/version.rb
80
79
  - omniauth_china.gemspec
81
80
  - spec/omniauth_china/strategies/douban_spec.rb
81
+ - spec/omniauth_china/strategies/renren_spec.rb
82
82
  - spec/omniauth_china/strategies/t163_spec.rb
83
83
  - spec/omniauth_china/strategies/tqq_spec.rb
84
84
  - spec/omniauth_china/strategies/tsina_spec.rb
@@ -160,6 +160,7 @@ specification_version: 3
160
160
  summary: "OmniAuth extension: omniauth for china"
161
161
  test_files:
162
162
  - spec/omniauth_china/strategies/douban_spec.rb
163
+ - spec/omniauth_china/strategies/renren_spec.rb
163
164
  - spec/omniauth_china/strategies/t163_spec.rb
164
165
  - spec/omniauth_china/strategies/tqq_spec.rb
165
166
  - spec/omniauth_china/strategies/tsina_spec.rb
@@ -1,141 +0,0 @@
1
- module OmniAuth
2
- module Strategies
3
- class Renren
4
- module Helper
5
- def omniauth_renren_connect_button
6
- callback_path = "#{OmniAuth.config.path_prefix}/renren/callback"
7
- @renren_connect_form_id = 'omniauth_renren_connect_form'
8
-
9
- if defined?(::ActionView::Helpers::FormTagHelper)
10
-
11
- form_tag(callback_path, :id => @renren_connect_form_id) do
12
- renren_connect_button.html_safe
13
- end
14
- else
15
-
16
- <<-HTML
17
- <form accept-charset="UTF-8" action="#{callback_path}" id="#{@renren_connect_form_id}" method="post">
18
- #{renren_connect_button}
19
- </form>
20
- HTML
21
- end
22
- end
23
-
24
- def omniauth_simple_renren_connect_button(options = {})
25
- params = {
26
- :src => "http://pics.wanlibo.com/images_cn/registration/renren.png",
27
- :title => "renren connect",
28
- :alt => "renren connect"
29
- }.merge(options)
30
-
31
- p = ""
32
- params.each do |k, v|
33
- p += k.to_s + "='" + v + "' "
34
- end
35
- simple_renren_connect_button(p).html_safe
36
- end
37
-
38
- def omniauth_renren_friends(options = {})
39
- params = {
40
- :max_rows => "2",
41
- :face_space => "5",
42
- :width => "217"
43
- }.merge(options)
44
- renren_friends(params).html_safe
45
- end
46
-
47
- def omniauth_renren_live_widget(options = {})
48
- params = {
49
- :width => "370px",
50
- :height => "390px"
51
- }.merge(options)
52
- renren_live_widget(params).html_safe
53
- end
54
-
55
- def omniauth_renren_like_button(options = {})
56
- params = {
57
- :width => "200px",
58
- :height => "70px",
59
- :url => root_url
60
- }.merge(options)
61
- renren_like_button(params).html_safe
62
- end
63
-
64
- def omniauth_renren_invite(options = {})
65
- params = {
66
- :content => "Join us",
67
- :url1 => "http://www.renren.com",
68
- :label1 => "Go",
69
- :url2 => "http://apps.renren.com/yourapp",
70
- :label2 => "Accept",
71
- :action => "/yourapp/youraction",
72
- :friend_text => "Invite your friends",
73
- :max => "5",
74
- :mode => "all",
75
- :width => "735",
76
- :height => "450"
77
- }.merge(options)
78
- renren_invite(params).html_safe
79
- end
80
-
81
- def omniauth_renren_javascript
82
- renren_javascript.html_safe
83
- end
84
-
85
- private
86
-
87
- def renren_invite(options)
88
- <<-HTML
89
- <xn:serverxnml>
90
- <script type="text/xnml">
91
- <xn:request-form content="#{options[:content]} &lt;xn:req-choice url=&quot;#{options[:url1]}&quot; label=&quot;#{options[:label1]}&quot;&gt;&lt;xn:req-choice url=&quot;#{options[:url2]}&quot; label=&quot;#{options[:label2]}&quot;&gt;" action="#{options[:action]}">
92
- <xn:multi-friend-selector-x actiontext="#{options[:friend_text]}" max="#{options[:max]}" mode="#{options[:mode]}" width="#{options[:width]}" height="#{options[:height]}" />
93
- </xn:request-form></script></xn:serverxnml>
94
- HTML
95
- end
96
-
97
- def renren_like_button(options = {})
98
- <<-HTML
99
- <iframe scrolling="no" frameborder="0" allowtransparency="true" src="http://www.connect.renren.com/like?url=#{options[:url]}" style="width: #{options[:width]};height: #{options[:height]};"></iframe>
100
- HTML
101
- end
102
-
103
- def renren_live_widget(options = {})
104
- <<-HTML
105
- <iframe scrolling="no" frameborder="0" src="http://www.connect.renren.com/widget/liveWidget?api_key=#{Renren.api_key}&xid=default&desp=%E5%A4%A7%E5%AE%B6%E6%9D%A5%E8%AE%A8%E8%AE%BA" style="width: #{options[:width]};height: #{options[:height]};"></iframe>
106
- HTML
107
- end
108
-
109
- def renren_friends(options = {})
110
- <<-HTML
111
- <xn:friendpile show-faces="all" face-size="small" max-rows="#{options[:max_rows]}" face-space="#{options[:face_space]}" width="#{options[:width]}"></xn:friendpile>
112
- HTML
113
- end
114
-
115
- def simple_renren_connect_button(properties)
116
- callback_path = "#{OmniAuth.config.path_prefix}/renren/callback"
117
- <<-HTML
118
- <img #{properties} onclick="XN.Connect.requireSession(function(){window.location.href='#{callback_path}';});return false;"></img>
119
- HTML
120
- end
121
-
122
- def renren_connect_button
123
- <<-HTML
124
- <xn:login-button autologoutlink="true" onlogin="document.getElementById('#{@renren_connect_form_id}').submit();"></xn:login-button>
125
- HTML
126
- end
127
-
128
- def renren_javascript
129
- <<-HTML
130
- <script src="http://static.connect.renren.com/js/v1.0/FeatureLoader.jsp" type="text/javascript"></script>
131
- <script type="text/javascript">
132
- //<![CDATA[
133
- XN_RequireFeatures(['EXNML'], function(){ XN.Main.init('#{Renren.api_key}', '/xd_receiver.html'); });
134
- //]]>
135
- </script>
136
- HTML
137
- end
138
- end
139
- end
140
- end
141
- end