wechat 0.8.2 → 0.8.3

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae9351f61a2b192bcb892dcdf95f85a540738b5a
4
- data.tar.gz: 6d2b9e4c9ebb1e68195abe00ccec070f991f8b92
3
+ metadata.gz: 1b273bb48c285ec8d726d97d680156b5180f9a68
4
+ data.tar.gz: 92b12bbec57a35485761ecf3dc42d38b404dbf38
5
5
  SHA512:
6
- metadata.gz: 1e8ccc4da8e5fec561612d22f3c2c7890d8447bd401e5c78541bf984378ecf2cc32473bc3d3b65a7dce7532b1872a0044b81ec9d54160f3f3552651476deb519
7
- data.tar.gz: fe059c2d7670b4fdf92c18386adc4158fd4c8c8d6fabfb0844a20954d5dc6a38470a0796adc8e990413ee29ff757cd594b2060bf87aeab079b047384355df823
6
+ metadata.gz: f044872a1d3403b948bb63cb9661cc0f2de98dd4b02b1a4bf22a94cc1a1863e99d2685090bfcd48b9cb567f61d6245468c53c9584e8d604efcf4c3ccfbc5af52
7
+ data.tar.gz: 4e1ba0770dc92898a9962d55d6e0738ce128cbd391ecb4f4a35bf240596baf3232877332cc743f5d84ea9e2d9cb76f398260eea3c33e94f769c44c131ffe37c7
Binary file
data.tar.gz.sig CHANGED
@@ -1,3 +1 @@
1
- x�gS�����xtv-(�ϥ93 k��fg?dm�� A���*[;l�Бh)� k%mN���2X�^�5_JcQ���jP,q�{)3�Nsĭ(10���D�^
2
- ���S8� !s��T�5N�
3
- �7��-�4�WO�H_X~"��-(����U3�f�����(Ո�!�z���v�elF�7��Oi����x�}�˭��d��*?���J���<q�B7Z"�VC�.ݰķ��+k53\9��� ���U
1
+ ��P��DpX���Lp1U�D����� ���S3@�Dvg�z��dz1iO[�| ����k_>��
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.8.3 (released at 11/26/2016)
4
+
5
+ # Fix wechat template key has camelCase problem, by @RyanChenDji #159
6
+ # Fix long time of oauth2_state bug for wechat_oauth2 methods, by @IvanChou #162
7
+
3
8
  ## v0.8.2 (released at 11/2/2016)
4
9
 
5
10
  # Bug which if not using multi-account but using web login.
@@ -12,61 +12,68 @@ module Wechat
12
12
  end
13
13
 
14
14
  def wechat_oauth2(scope = 'snsapi_base', page_url = nil, &block)
15
- appid = self.class.corpid || self.class.appid
16
- if appid.blank?
15
+ appid = self.class.corpid || self.class.appid || lambda do
17
16
  self.class.wechat # to initialize wechat_api_client at first time call wechat_oauth2
18
- appid = self.class.corpid || self.class.appid
19
- raise 'Can not get corpid or appid, so please configure it first to using wechat_oauth2' if appid.blank?
20
- end
21
- page_url ||= if self.class.trusted_domain_fullname
22
- "#{self.class.trusted_domain_fullname}#{request.original_fullpath}"
23
- else
24
- request.original_url
25
- end
26
- redirect_uri = CGI.escape(page_url)
27
- if scope == 'snsapi_login'
28
- oauth2_url = "https://open.weixin.qq.com/connect/qrconnect?appid=#{appid}&redirect_uri=#{redirect_uri}&response_type=code&scope=#{scope}&state=#{wechat.jsapi_ticket.oauth2_state}#wechat_redirect"
29
- else
30
- oauth2_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=#{appid}&redirect_uri=#{redirect_uri}&response_type=code&scope=#{scope}&state=#{wechat.jsapi_ticket.oauth2_state}#wechat_redirect"
31
- end
17
+ self.class.corpid || self.class.appid
18
+ end.call
19
+ raise 'Can not get corpid or appid, so please configure it first to using wechat_oauth2' if appid.blank?
32
20
 
33
- return oauth2_url unless block_given?
34
- if self.class.corpid
35
- wechat_corp_oauth2(oauth2_url, &block)
36
- else
37
- wechat_public_oauth2(oauth2_url, &block)
38
- end
21
+ wechat.jsapi_ticket.ticket if wechat.jsapi_ticket.oauth2_state.nil?
22
+ oauth2_params = {
23
+ appid: appid,
24
+ redirect_uri: page_url,
25
+ scope: scope,
26
+ response_type: 'code',
27
+ state: wechat.jsapi_ticket.oauth2_state
28
+ }
29
+
30
+ return generate_oauth2_url(oauth2_params) unless block_given?
31
+ self.class.corpid ? wechat_corp_oauth2(oauth2_params, &block) : wechat_public_oauth2(oauth2_params, &block)
39
32
  end
40
33
 
41
34
  private
42
35
 
43
- def wechat_public_oauth2(oauth2_url)
44
- if cookies.signed_or_encrypted[:we_openid].blank? && params[:code].blank?
45
- redirect_to oauth2_url
46
- elsif cookies.signed_or_encrypted[:we_openid].blank? &&
47
- params[:code].present? &&
48
- params[:state].to_s == wechat.jsapi_ticket.oauth2_state.to_s # params[:state] maybe '' and wechat.jsapi_ticket.oauth2_state may be nil
36
+ def wechat_public_oauth2(oauth2_params)
37
+ openid = cookies.signed_or_encrypted[:we_openid]
38
+ unionid = cookies.signed_or_encrypted[:we_unionid]
39
+ if openid.present?
40
+ yield openid, { 'openid' => openid, 'unionid' => unionid }
41
+ elsif params[:code].present? && params[:state] == oauth2_params[:state]
49
42
  access_info = wechat.web_access_token(params[:code])
50
43
  cookies.signed_or_encrypted[:we_openid] = { value: access_info['openid'], expires: self.class.oauth2_cookie_duration.from_now }
51
44
  cookies.signed_or_encrypted[:we_unionid] = { value: access_info['unionid'], expires: self.class.oauth2_cookie_duration.from_now }
52
45
  yield access_info['openid'], access_info
53
46
  else
54
- yield cookies.signed_or_encrypted[:we_openid], { 'openid' => cookies.signed_or_encrypted[:we_openid],
55
- 'unionid' => cookies.signed_or_encrypted[:we_unionid] }
47
+ redirect_to generate_oauth2_url(oauth2_params)
56
48
  end
57
49
  end
58
50
 
59
- def wechat_corp_oauth2(oauth2_url)
60
- if cookies.signed_or_encrypted[:we_deviceid].blank? && params[:code].blank?
61
- redirect_to oauth2_url
62
- elsif cookies.signed_or_encrypted[:we_deviceid].blank? && params[:code].present? && params[:state] == wechat.jsapi_ticket.oauth2_state
51
+ def wechat_corp_oauth2(oauth2_params)
52
+ userid = cookies.signed_or_encrypted[:we_userid]
53
+ deviceid = cookies.signed_or_encrypted[:we_deviceid]
54
+ if userid.present? && deviceid.present?
55
+ yield userid, { 'UserId' => userid, 'DeviceId' => deviceid }
56
+ elsif params[:code].present? && params[:state] == oauth2_params[:state]
63
57
  userinfo = wechat.getuserinfo(params[:code])
64
58
  cookies.signed_or_encrypted[:we_userid] = { value: userinfo['UserId'], expires: self.class.oauth2_cookie_duration.from_now }
65
59
  cookies.signed_or_encrypted[:we_deviceid] = { value: userinfo['DeviceId'], expires: self.class.oauth2_cookie_duration.from_now }
66
60
  yield userinfo['UserId'], userinfo
67
61
  else
68
- yield cookies.signed_or_encrypted[:we_userid], { 'UserId' => cookies.signed_or_encrypted[:we_userid],
69
- 'DeviceId' => cookies.signed_or_encrypted[:we_deviceid] }
62
+ redirect_to generate_oauth2_url(oauth2_params)
63
+ end
64
+ end
65
+
66
+ def generate_oauth2_url(oauth2_params)
67
+ if oauth2_params[:redirect_uri].blank?
68
+ page_url = (td = self.class.trusted_domain_fullname) ? "#{td}#{request.original_fullpath}" : request.original_url
69
+ safe_query = request.query_parameters.reject { |k, _| %w(code state access_token).include? k }.to_query
70
+ oauth2_params[:redirect_uri] = page_url.sub(request.query_string, safe_query)
71
+ end
72
+
73
+ if oauth2_params[:scope] == 'snsapi_login'
74
+ "https://open.weixin.qq.com/connect/qrconnect?#{oauth2_params.to_query}#wechat_redirect"
75
+ else
76
+ "https://open.weixin.qq.com/connect/oauth2/authorize?#{oauth2_params.to_query}#wechat_redirect"
70
77
  end
71
78
  end
72
79
  end
@@ -131,7 +131,7 @@ module Wechat
131
131
  end
132
132
 
133
133
  def template(opts = {})
134
- template_fields = camelize_hash_keys(opts.symbolize_keys.slice(:template_id, :topcolor, :url, :data))
134
+ template_fields = opts.symbolize_keys.slice(:template_id, :topcolor, :url, :data)
135
135
  update(MsgType: 'template', Template: template_fields)
136
136
  end
137
137
 
@@ -152,12 +152,13 @@ module Wechat
152
152
  TO_JSON_ALLOWED = %w(touser msgtype content image voice video file music news articles template agentid).freeze
153
153
 
154
154
  def to_json
155
+ keep_camel_case_key = message_hash[:MsgType] == 'template'
155
156
  json_hash = deep_recursive(message_hash) do |key, value|
156
157
  key = key.to_s
157
- [(TO_JSON_KEY_MAP[key] || key.downcase), value]
158
+ [(TO_JSON_KEY_MAP[key] || (keep_camel_case_key ? key : key.downcase)), value]
158
159
  end
160
+ json_hash = json_hash.transform_keys(&:downcase).select { |k, _v| TO_JSON_ALLOWED.include? k }
159
161
 
160
- json_hash = json_hash.select { |k, _v| TO_JSON_ALLOWED.include? k }
161
162
  case json_hash['msgtype']
162
163
  when 'text'
163
164
  json_hash['text'] = { 'content' => json_hash.delete('content') }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wechat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Skinnyworm
@@ -31,7 +31,7 @@ cert_chain:
31
31
  R5k6Ma92sW8jupX4cqbSu9rntdVQkNRpoHIrfU0MZT0cKsg/D1zMteylxrO3KMsz
32
32
  SPQRv+nrI1J0zevFqb8010heoR8SDyUA0Mm3+Q==
33
33
  -----END CERTIFICATE-----
34
- date: 2016-11-02 00:00:00.000000000 Z
34
+ date: 2016-11-26 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activesupport
metadata.gz.sig CHANGED
Binary file