omniauth-wechat 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OGZhY2RjNDIxZmY5Nzk2YWQ1YTlmYTlhNmU2MzQ5MDRmNDc5YjY1NA==
4
+ NDcxNTBjMTI0OTZmZDQ2NDM5ZTEyMzVmNDgzMzdmYzc2N2E0ODY1Mw==
5
5
  data.tar.gz: !binary |-
6
- MDFkNmEzYTU2NTA2ZGU4M2YxMDhlYzU4M2IxOWUwMmEzZjUwMzNjNw==
6
+ ZDk2M2Q1YzRlYTEyMDFjMmY1NzI3OWY5NDhmOGIwNDNjZWMzZDUxOQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- Mzk1NGE2NmM3Mjg4NjlmOWIxZDRhYjdjNWNiNTU5MDliYTc0ZWJlZTFiMGY2
10
- MmVjYzIxYTU5NDllMzUyOTcyM2U0NTIyMDUyYWYwMjRlOWZjMTIzMjVjNDhh
11
- YTZlZjI2MWZhNDZmZmEyNzUzOTk2ZTc4NTI2YjQ5MzM2ZTUxNjM=
9
+ ZjdhNzJmODk4MjE5NTZlNDhkNDdmNmRiN2NjMWJhOGY5NDMyOTcyMjA3MWRl
10
+ ZTJhNDQ1OGE4ZWU1MzUwYTNlZjRkOWQxYmU0MTBlMGMxMGViYTNjY2JiNTkx
11
+ NmM0ODdhOGU2ZGI4MDgzNTk4YzA4ZmY3ODVkZWVjNDk4MzI2ZWE=
12
12
  data.tar.gz: !binary |-
13
- NTFhNDMyNGQzMTI0ZWQ0N2FmM2Y2Mzg4ZjZiMTU4NWIzNjIzM2JmMjViN2Fi
14
- NTE0ZmIzZDA2ZDE5ZDBkYWM3OTBhOTRhNjA0Y2E1YzY3MzgzZDM3ZjEyN2Zk
15
- Yjk4NDAxMzU0NTZhM2MwODlhZjNmYWIzNGZlM2UzZGEwMjBlOTI=
13
+ M2FjMmVhM2QxYzY0MmEwMTllZmEzNWEyYWVlNDRlMzQ4OWVjNjExNzkzNDdh
14
+ YTk1MmVkMDZjOGZmOTczZGI3ZDY5OGMzMTYxNDBlYWYzYzNkMjFiYmJkYWU1
15
+ YTkxZDc3MTI0NmJmYTQ4NmI2ZGJiM2RjNWUxMGZmNTZmY2I1MzA=
@@ -1,44 +1,89 @@
1
- require 'omniauth/strategies/oauth2'
1
+ require 'cgi'
2
+ require 'uri'
3
+ require 'oauth2'
4
+ require 'omniauth'
5
+ require 'timeout'
6
+ require 'securerandom'
2
7
 
3
8
  module OmniAuth
4
9
  module Strategies
5
- class WeChat < OmniAuth::Strategies::OAuth2
10
+ class WeChat
11
+ include OmniAuth::Strategy
12
+ args [:appid, :secret]
13
+ option :appid,nil
14
+ option :secret,nil
6
15
  option :name, "wechat"
16
+ option :authorize_params, {}
17
+ option :authorize_options, [:scope]
18
+ option :token_params, {}
19
+ option :token_options, []
20
+ option :auth_token_params, {}
7
21
  option :client_options, {
8
22
  :site => 'http://open.weixin.qq.com',
9
23
  :authorize_url => 'https://open.weixin.qq.com/connect/oauth2/authorize',
10
24
  :token_url => "https://api.weixin.qq.com/sns/oauth2/access_token"
11
25
  }
12
- option :provider_ignores_state, true
26
+
27
+ attr_accessor :access_token
28
+
29
+ def client(opt={})
30
+ client_options = options.client_options.merge(opt)
31
+ ::OAuth2::Client.new(options.appid, options.secret, deep_symbolize(client_options))
32
+ end
33
+
34
+ def callback_url
35
+ full_host + script_name + callback_path
36
+ end
37
+
38
+ credentials do
39
+ hash = {'token' => access_token.token}
40
+ hash.merge!('refresh_token' => access_token.refresh_token) if access_token.expires? && access_token.refresh_token
41
+ hash.merge!('expires_at' => access_token.expires_at) if access_token.expires?
42
+ hash.merge!('expires' => access_token.expires?)
43
+ hash
44
+ end
13
45
 
14
46
  def request_phase
15
47
  redirect client.authorize_url(authorize_params)+'#wechat_redirect'
16
48
  end
17
49
 
18
50
  def authorize_params
19
-
20
- {:appid=>options.client_id,
51
+ options.authorize_params[:state] = SecureRandom.hex(24)
52
+ options.authorize_params.merge({:appid=>options.client_id,
21
53
  :redirect_uri => callback_url,
22
54
  :response_type => 'code',
23
55
  :scope => 'snsapi_userinfo'
24
- }
56
+ })
25
57
  end
26
58
 
27
59
  def callback_url
28
60
  full_host + script_name + callback_path
29
61
  end
62
+
30
63
  def token_params
31
- params = super
32
- params.merge({:appid=>options.client_id,:secret=>options.client_secret,:grant_type=>'authorization_code'})
64
+ {:appid=>options.appid,:secret=>options.secret}
33
65
  end
34
66
 
35
- def build_access_token
36
- client.auth_code.get_token(
37
- request.params['code'],
38
- {:redirect_uri => callback_url, :parse => :json}.merge(token_params.to_hash(:symbolize_keys => true)),
39
- {:mode => :query, :param_name => 'access_token'})
67
+ def callback_phase
68
+ if request.params['code']
69
+ raise CallbackError.new('noauthorize','user cancel authorizing')
70
+ end
71
+
72
+ self.access_token = build_access_token
73
+ self.access_token = refresh_access_token(access_token) if access_token.expired?
74
+ super
75
+ rescue ::OAuth2::Error, CallbackError => e
76
+ fail!(:invalid_credentials, e)
77
+ rescue ::MultiJson::DecodeError => e
78
+ fail!(:invalid_response, e)
79
+ rescue ::Timeout::Error, ::Errno::ETIMEDOUT, Faraday::Error::TimeoutError => e
80
+ fail!(:timeout, e)
81
+ rescue ::SocketError, Faraday::Error::ConnectionFailed => e
82
+ fail!(:failed_to_connect, e)
40
83
  end
41
84
 
85
+
86
+
42
87
  uid do
43
88
  @uid ||= begin
44
89
  access_token[:openid]
@@ -54,9 +99,9 @@ module OmniAuth
54
99
  end
55
100
 
56
101
  extra do
57
- {
58
- :raw_info => raw_info
59
- }
102
+ {
103
+ :raw_info => raw_info
104
+ }
60
105
  end
61
106
 
62
107
  def raw_info
@@ -69,6 +114,44 @@ module OmniAuth
69
114
  ).parsed
70
115
  end
71
116
  end
117
+ protected
118
+
119
+ def deep_symbolize(hash)
120
+ hash.inject({}) do |h, (k,v)|
121
+ h[k.to_sym] = v.is_a?(Hash) ? deep_symbolize(v) : v
122
+ h
123
+ end
124
+ end
125
+
126
+ def build_access_token
127
+ verifier = request.params['code']
128
+ request_params = {:code=>verifier,
129
+ :grant_type=>'authorization_code',:parse=>:json
130
+ }.merge(token_params.to_hash(:symbolize_keys => true))
131
+ client.get_token(request_params,{:mode=>:query})
132
+
133
+ end
134
+
135
+ def refresh_access_token(old_token)
136
+ request_params = { :grant_type=> 'refresh_token',
137
+ :refresh_token => old_token.refresh_token,
138
+ :appid=>options.appid,
139
+ :parse=>:json
140
+ }.merge(token_params.to_hash(:symbolize_keys => true))
141
+ client({:token_url=>'https://api.weixin.qq.com/sns/oauth2/refresh_token'}).get_token(request_params,{:mode=>:query})
142
+ end
143
+
144
+ class CallbackError < StandardError
145
+ attr_accessor :error, :error_reason
146
+ def initialize(error, error_reason=nil)
147
+ self.error = error
148
+ self.error_reason = error_reason
149
+ end
150
+
151
+ def message
152
+ [self.error, self.error_reason].compact.join(' | ')
153
+ end
154
+ end
72
155
  end
73
156
  end
74
157
  end
@@ -1,5 +1,5 @@
1
1
  module Omniauth
2
2
  module Wechat
3
- VERSION = "0.1.5"
3
+ VERSION = "0.1.6"
4
4
  end
5
5
  end
@@ -19,6 +19,6 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency 'omniauth', '~> 1.0'
22
- spec.add_dependency 'omniauth-oauth2', '~> 1.0'
22
+ spec.add_dependency 'oauth2', '~> 0.9.0'
23
23
  spec.add_dependency 'multi_json'
24
24
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-wechat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - victor
@@ -25,19 +25,19 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: omniauth-oauth2
28
+ name: oauth2
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: '1.0'
33
+ version: 0.9.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
- version: '1.0'
40
+ version: 0.9.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: multi_json
43
43
  requirement: !ruby/object:Gem::Requirement