wechat 0.7.6 → 0.7.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bfc807dea051fded9c082a6bdbf4778433b2bbce
4
- data.tar.gz: 55e132829fc449ed8fd9ec591ee05710386037bb
3
+ metadata.gz: 1227afa2c59a9657b91eda79d71e05c582dfd13c
4
+ data.tar.gz: 1ed732c34360e25275a35fa5ddbc62f9fe945e4c
5
5
  SHA512:
6
- metadata.gz: f4b26ce081c96d50de0e02340a6745f3cdff6334dfbdc7a521f74ee06136521ccbd42e7cf0ae2e52ba71e9dc73384e287de0c76486a95a8f0c12c4cbc3d98437
7
- data.tar.gz: 2d6867ec622f4ae9748072eeeac18a19536579d8b74a1c56d3e59ebdf886b85a0de5dd694376fd2a0db7ca2b16542109f9eee5a6ad41d978e5dab3b903e3737a
6
+ metadata.gz: 358ebfbe074a0a0f631c2966b4cb8f0d3357d37fa8c6a0e67c87863d1b98030bcd625de8cdbbd36cac923e1378ed395599e8199659c8ccf41065085e7d05e57d
7
+ data.tar.gz: 95d512581d896582438b0a3c30d9994defaa485118ecf7c5fe90a0ad473ff4508f070aea1153abbabc2ce36bf3c8ad3a521319b9cf92b4cb98c2ed48999f9bc4
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.7.7 (released at 3/18/2016)
4
+
5
+ * New wechat_oauth2, only support enterprise account still.
6
+ * fix 'skip_before_action :verify_authenticity_token' bug for v5.0.0.beta3 by @vkill #97
7
+ * Support Rails 3.2 again after support Rails 5.0, by @guange2015 #96
8
+
3
9
  ## v0.7.6 (released at 3/05/2016)
4
10
 
5
11
  * Support wechat public account conditional menu. #95
@@ -13,7 +13,7 @@ WeChat gem 可以帮助开发者方便地在Rails环境中集成微信[公众平
13
13
 
14
14
  命令行工具`wechat`可以调用各种无需web环境的API。同时也提供了Rails Controller的responder DSL, 可以帮助开发者方便地在Rails应用中集成微信的消息处理,包括主动推送的和被动响应的消息。
15
15
 
16
- 如果您的App还需要集成微信OAuth2.0, 您可以考虑[omniauth-wechat-oauth2](https://github.com/skinnyworm/omniauth-wechat-oauth2), 以便和devise集成,提供完整的用户认证。
16
+ 如果您的App还需要集成微信OAuth2.0, 除了简便的`wechat_oauth2`指令,也可以考虑[omniauth-wechat-oauth2](https://github.com/skinnyworm/omniauth-wechat-oauth2), 以便和devise集成,提供完整的用户认证。
17
17
 
18
18
  如果您对如何制作微信网页UI没有灵感,可以参考官方的[weui](https://github.com/weui/weui),针对Rails的Gem是[weui-rails](https://github.com/Eric-Guo/weui-rails)。
19
19
 
@@ -192,6 +192,26 @@ end
192
192
 
193
193
  在开发模式下,由于程序往往通过微信调试工具的服务器端调试工具反向代理被访问,此时需要配置`trusted_domain_fullname`以便wechat gem可以使用正确的域名做JS-SDK的权限签名。
194
194
 
195
+ #### OAuth2.0验证接口支持
196
+
197
+ 目前企业号可以使用如下代码直接取得用户企业号userid:
198
+
199
+ ```ruby
200
+ class WechatsController < ActionController::Base
201
+ layout 'wechat'
202
+ wechat_responder
203
+ def apply_new
204
+ wechat_oauth2 do |userid|
205
+ @current_user = User.find_by(wechat_userid: userid)
206
+ @apply = Apply.new
207
+ @apply.user_id = @current_user.id
208
+ end
209
+ end
210
+ end
211
+ ```
212
+
213
+ `wechat_oauth2`封装了OAuth2.0验证接口和cookie处理逻辑,用户仅需提供业务代码块即可,userid就是微信成员UserID。
214
+
195
215
  ## 关于接口权限
196
216
 
197
217
  wechat gems 内部不会检查权限。但因公众号类型不同,和微信服务器端通讯时,可能会被拒绝,详细权限控制可参考[官方文档](http://mp.weixin.qq.com/wiki/7/2d301d4b757dedc333b9a9854b457b47.html)。
@@ -230,7 +250,6 @@ Wechat commands:
230
250
  wechat menu_delconditional [MENU_ID] # 删除个性化菜单
231
251
  wechat menu_delete # 删除菜单
232
252
  wechat menu_trymatch [USER_ID] # 测试个性化菜单匹配结果
233
- wechat oauth2_url [REDIRECT_URI] # 生成OAuth2.0验证URL
234
253
  wechat qrcode_create_limit_scene [SCENE_ID_OR_STR] # 请求永久二维码
235
254
  wechat qrcode_create_scene [SCENE_ID, EXPIRE_SECONDS] # 请求临时二维码
236
255
  wechat qrcode_download [TICKET, QR_CODE_PIC_PATH] # 通过ticket下载二维码
@@ -277,7 +296,6 @@ Wechat commands:
277
296
  wechat menu_create [MENU_YAML_PATH] # 创建菜单
278
297
  wechat menu_delete # 删除菜单
279
298
  wechat message_send [OPENID, TEXT_MESSAGE] # 发送文字消息
280
- wechat oauth2_url [REDIRECT_URI] # 生成OAuth2.0验证URL
281
299
  wechat qrcode_download [TICKET, QR_CODE_PIC_PATH] # 通过ticket下载二维码
282
300
  wechat tag [TAG_ID] # 获取标签成员
283
301
  wechat tag_add_department [TAG_ID, PARTY_IDS] # 增加标签部门
data/README.md CHANGED
@@ -20,7 +20,7 @@ WeChat gem trying to helping Rails developer to integrated [enterprise account](
20
20
 
21
21
  A responder DSL can used in Rails controller, so giving a event based interface to handler message sent by end user from wechat server.
22
22
 
23
- Wechat provide OAuth2.0 as authentication service and possible to intergrated with devise/other authorization gems, [omniauth-wechat-oauth2](https://github.com/skinnyworm/omniauth-wechat-oauth2) is a good start
23
+ Wechat provide OAuth2.0 authentication method `wechat_oauth2`, possible the easiest way, for the user perfer using devise style authorization gems, [omniauth-wechat-oauth2](https://github.com/skinnyworm/omniauth-wechat-oauth2) can be a good option.
24
24
 
25
25
  There is official [weui](https://github.com/weui/weui), which corresponding Rails gems called [weui-rails](https://github.com/Eric-Guo/weui-rails) available, if you prefer following the same UI design as wechat.
26
26
 
@@ -205,6 +205,27 @@ JS-SDK enable you control Wechat App behavior in html, by inject a config signat
205
205
 
206
206
  Configure the `trusted_domain_fullname` if you are in development mode and app running behind a reverse proxy server, otherwise wechat gem can not get the correct url to be signature later.
207
207
 
208
+ #### OAuth2.0 authentication
209
+
210
+ For enterprise account, user can using userid directly by provide a block in wechat_oauth2:
211
+
212
+ ```ruby
213
+ class WechatsController < ActionController::Base
214
+ layout 'wechat'
215
+ wechat_responder
216
+ def apply_new
217
+ wechat_oauth2 do |userid|
218
+ @current_user = User.find_by(wechat_userid: userid)
219
+ @apply = Apply.new
220
+ @apply.user_id = @current_user.id
221
+ end
222
+ end
223
+ end
224
+ ```
225
+
226
+ `wechat_oauth2` already implement the necessory OAuth2.0 and cookie logic, userid available as a member UserID for the whole block.
227
+
228
+
208
229
  ## The API privilege
209
230
 
210
231
  wechat gems won't handle any privilege exception. (except token time out, but it's not important to you as it's auto retry/recovery in gems internally), but Tencent will control a lot of privilege based on your public account type and certification, more info, please reference [official document](http://mp.weixin.qq.com/wiki/7/2d301d4b757dedc333b9a9854b457b47.html).
@@ -245,7 +266,6 @@ Wechat commands:
245
266
  wechat menu_delconditional [MENU_ID] # 删除个性化菜单
246
267
  wechat menu_delete # 删除菜单
247
268
  wechat menu_trymatch [USER_ID] # 测试个性化菜单匹配结果
248
- wechat oauth2_url [REDIRECT_URI] # 生成OAuth2.0验证URL
249
269
  wechat qrcode_create_limit_scene [SCENE_ID_OR_STR] # 请求永久二维码
250
270
  wechat qrcode_create_scene [SCENE_ID, EXPIRE_SECONDS] # 请求临时二维码
251
271
  wechat qrcode_download [TICKET, QR_CODE_PIC_PATH] # 通过ticket下载二维码
@@ -292,7 +312,6 @@ Wechat commands:
292
312
  wechat menu_create [MENU_YAML_PATH] # 创建菜单
293
313
  wechat menu_delete # 删除菜单
294
314
  wechat message_send [OPENID, TEXT_MESSAGE] # 发送文字消息
295
- wechat oauth2_url [REDIRECT_URI] # 生成OAuth2.0验证URL
296
315
  wechat qrcode_download [TICKET, QR_CODE_PIC_PATH] # 通过ticket下载二维码
297
316
  wechat tag [TAG_ID] # 获取标签成员
298
317
  wechat tag_add_department [TAG_ID, PARTY_IDS] # 增加标签部门
data/bin/wechat CHANGED
@@ -293,12 +293,6 @@ class App < Thor
293
293
  puts wechat_api.user(open_id)
294
294
  end
295
295
 
296
- desc 'oauth2_url [REDIRECT_URI]', '生成OAuth2.0验证URL'
297
- def oauth2_url(redirect_uri)
298
- appid = Wechat.config.corpid || Wechat.config.appid
299
- puts wechat_api.oauth2_url(redirect_uri, appid)
300
- end
301
-
302
296
  desc 'user_update_remark [OPEN_ID, REMARK]', '设置备注名'
303
297
  def user_update_remark(openid, remark)
304
298
  puts wechat_api.user_update_remark(openid, remark)
@@ -34,6 +34,8 @@ module Wechat
34
34
  end
35
35
 
36
36
  def oauth2_url(redirect_uri, appid)
37
+ ActiveSupport::Deprecation.warn('oauth2_url is deprecated. use wechat_oauth2 instead.')
38
+
37
39
  redirect_uri = CGI.escape(redirect_uri)
38
40
  "https://open.weixin.qq.com/connect/oauth2/authorize?appid=#{appid}&redirect_uri=#{redirect_uri}&response_type=code&scope=snsapi_base#wechat_redirect"
39
41
  end
@@ -9,8 +9,15 @@ module Wechat
9
9
  included do
10
10
  # Rails 5 remove before_filter and skip_before_filter
11
11
  if respond_to?(:skip_before_action)
12
- # Rails 5 API mode won't define verify_authenticity_token
13
- skip_before_action :verify_authenticity_token if respond_to?(:verify_authenticity_token)
12
+ if respond_to?(:verify_authenticity_token)
13
+ skip_before_action :verify_authenticity_token
14
+ else
15
+ # Rails 5 API mode won't define verify_authenticity_token
16
+ # https://github.com/rails/rails/blob/v5.0.0.beta3/actionpack/lib/abstract_controller/callbacks.rb#L66
17
+ # https://github.com/rails/rails/blob/v5.0.0.beta3/activesupport/lib/active_support/callbacks.rb#L640
18
+ skip_before_action :verify_authenticity_token, raise: false
19
+ end
20
+
14
21
  before_action :verify_signature, only: [:show, :create]
15
22
  else
16
23
  skip_before_filter :verify_authenticity_token
@@ -159,12 +166,47 @@ module Wechat
159
166
  self.class.wechat # Make sure user can continue access wechat at instance level similar to class level
160
167
  end
161
168
 
169
+ def wechat_oauth2(scope = 'snsapi_base', page_url = nil)
170
+ appid = self.class.corpid || self.class.appid
171
+ page_url ||= if self.class.trusted_domain_fullname
172
+ "#{self.class.trusted_domain_fullname}#{request.original_fullpath}"
173
+ else
174
+ request.original_url
175
+ end
176
+ redirect_uri = CGI.escape(page_url)
177
+ oauth2_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=#{appid}&redirect_uri=#{redirect_uri}&response_type=code&scope=#{scope}#wechat_redirect"
178
+
179
+ return oauth2_url unless block_given?
180
+ raise 'Currently wechat_oauth2 only support enterprise account.' unless self.class.corpid
181
+ if cookies.signed_or_encrypted[:we_deviceid].blank? && params[:code].blank?
182
+ redirect_to oauth2_url
183
+ elsif cookies.signed_or_encrypted[:we_deviceid].blank? && params[:code].present?
184
+ userinfo = Wechat.api.getuserinfo(params[:code])
185
+ cookies.signed_or_encrypted[:we_userid] = { value: userinfo['UserId'], expires: 1.hour.from_now }
186
+ cookies.signed_or_encrypted[:we_deviceid] = { value: userinfo['DeviceId'], expires: 1.hour.from_now }
187
+ cookies.signed_or_encrypted[:we_openid] = { value: userinfo['OpenId'], expires: 1.hour.from_now }
188
+ yield userinfo['UserId'], userinfo
189
+ else
190
+ yield cookies.signed_or_encrypted[:we_userid], { 'UserId' => cookies.signed_or_encrypted[:we_userid],
191
+ 'DeviceId' => cookies.signed_or_encrypted[:we_deviceid],
192
+ 'OpenId' => cookies.signed_or_encrypted[:we_openid] }
193
+ end
194
+ end
195
+
162
196
  def show
163
197
  if self.class.corpid.present?
164
198
  echostr, _corp_id = unpack(decrypt(Base64.decode64(params[:echostr]), self.class.encoding_aes_key))
165
- render plain: echostr
199
+ if Rails::VERSION::MAJOR >= 4
200
+ render plain: echostr
201
+ else
202
+ render text: echostr
203
+ end
166
204
  else
167
- render plain: params[:echostr]
205
+ if Rails::VERSION::MAJOR >= 4
206
+ render plain: params[:echostr]
207
+ else
208
+ render text: params[:echostr]
209
+ end
168
210
  end
169
211
  end
170
212
 
@@ -173,7 +215,11 @@ module Wechat
173
215
  response_msg = run_responder(request_msg)
174
216
 
175
217
  if response_msg.respond_to? :to_xml
176
- render plain: process_response(response_msg)
218
+ if Rails::VERSION::MAJOR >= 4
219
+ render plain: process_response(response_msg)
220
+ else
221
+ render text: process_response(response_msg)
222
+ end
177
223
  else
178
224
  render nothing: true, status: 200, content_type: 'text/html'
179
225
  end
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.7.6
4
+ version: 0.7.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Skinnyworm
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-03-05 00:00:00.000000000 Z
12
+ date: 2016-03-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport