wechat 0.8.7 → 0.8.8

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: d7ee715ed77e318662731915d4b04d1a3ddf57fd
4
- data.tar.gz: 5fba117fd1a11174d5453b16e7bfb800d6ce1fad
3
+ metadata.gz: 58507c5f6484c8cc80d7f7acc44e5ddd5029832d
4
+ data.tar.gz: 1fa69b7599611a9a993a619fd5b4366463cec05d
5
5
  SHA512:
6
- metadata.gz: 465dcca90573b199673602e8797ab078d27ff285b6639db1a923b377fafca9d98cf8244b78b33f07c07699d821c45639869fe97956943de38096779b8c215701
7
- data.tar.gz: 5efd883e21fc35ed05a225b30a08f83af1927f3989c302fce72432ec4e8a4a93a09c64a703171c26885cc6dd74dd318f4824a6e2531fbe7b372f0adf02d6d68a
6
+ metadata.gz: 94d9d5c231c5807793a471603eb62f70901d210b5a5c68a8481b5a63a8c02493addb07c647e407f85b3a2b3f1276ef2a80c0367f5e5935af54295f63f2a5f55b
7
+ data.tar.gz: 1890b6e8396a777b4802a1d7677bddae375c0e2bebd8d01b661363e6ac02954b7ccf95ebba71d24fd992058e40c315e697badd53ba04729e10c77f8cbe24c212
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.8.8 (released at 5/18/2017)
4
+
5
+ * Better support multiple account. by @xiewenwei #187
6
+ * Allow load figaro via RAILS_ENV. by @goofansu #186
7
+
3
8
  ## v0.8.7 (released at 4/23/2017)
4
9
 
5
10
  * Support new wxa_get_wxacode API for miniapp.
@@ -2,46 +2,56 @@ module ActionController
2
2
  module WechatResponder
3
3
  def wechat_api(opts = {})
4
4
  include Wechat::ControllerApi
5
- self.wechat_cfg_account = opts[:account].present? ? opts[:account].to_sym : :default
5
+ account = opts.delete(:account)
6
+ self.wechat_cfg_account = account ? account.to_sym : :default
6
7
  self.wechat_api_client = load_controller_wechat(wechat_cfg_account, opts)
7
8
  end
8
9
 
9
10
  def wechat_responder(opts = {})
10
11
  include Wechat::Responder
11
- self.wechat_cfg_account = opts[:account].present? ? opts[:account].to_sym : :default
12
+ account = opts.delete(:account)
13
+ self.wechat_cfg_account = account ? account.to_sym : :default
12
14
  self.wechat_api_client = load_controller_wechat(wechat_cfg_account, opts)
13
15
  end
14
16
 
15
- def wechat
16
- self.wechat_api_client ||= load_controller_wechat(wechat_cfg_account)
17
+ def wechat(account = nil)
18
+ if account && account != wechat_cfg_account
19
+ Wechat.api(account)
20
+ else
21
+ self.wechat_api_client ||= load_controller_wechat(wechat_cfg_account)
22
+ end
17
23
  end
18
24
 
19
25
  private
20
26
 
21
27
  def load_controller_wechat(account, opts = {})
22
- self.token = opts[:token] || Wechat.config(account).token
23
- self.appid = opts[:appid] || Wechat.config(account).appid
24
- self.corpid = opts[:corpid] || Wechat.config(account).corpid
25
- self.agentid = opts[:agentid] || Wechat.config(account).agentid
26
- self.encrypt_mode = opts[:encrypt_mode] || Wechat.config(account).encrypt_mode || corpid.present?
27
- self.timeout = opts[:timeout] || 20
28
- self.skip_verify_ssl = opts[:skip_verify_ssl]
29
- self.encoding_aes_key = opts[:encoding_aes_key] || Wechat.config(account).encoding_aes_key
30
- self.trusted_domain_fullname = opts[:trusted_domain_fullname] || Wechat.config(account).trusted_domain_fullname
31
- Wechat.config(account).oauth2_cookie_duration ||= 1.hour
32
- self.oauth2_cookie_duration = opts[:oauth2_cookie_duration] || Wechat.config(account).oauth2_cookie_duration.to_i.seconds
33
-
34
- access_token = opts[:access_token] || Wechat.config(account).access_token
35
- jsapi_ticket = opts[:jsapi_ticket] || Wechat.config(account).jsapi_ticket
36
-
37
- return self.wechat_api_client = Wechat.api if account == :default && opts.empty?
28
+ cfg = Wechat.config(account)
29
+ self.token = opts[:token] || cfg.token
30
+ self.appid = opts[:appid] || cfg.appid
31
+ self.corpid = opts[:corpid] || cfg.corpid
32
+ self.agentid = opts[:agentid] || cfg.agentid
33
+ self.encrypt_mode = opts[:encrypt_mode] || cfg.encrypt_mode || corpid.present?
34
+ self.encoding_aes_key = opts[:encoding_aes_key] || cfg.encoding_aes_key
35
+ self.trusted_domain_fullname = opts[:trusted_domain_fullname] || cfg.trusted_domain_fullname
36
+ self.oauth2_cookie_duration = opts[:oauth2_cookie_duration] || cfg.oauth2_cookie_duration.to_i.seconds
37
+ self.timeout = opts[:timeout] || cfg.timeout
38
+ if opts.key?(:skip_verify_ssl)
39
+ self.skip_verify_ssl = opts[:skip_verify_ssl]
40
+ else
41
+ self.skip_verify_ssl = cfg.skip_verify_ssl
42
+ end
43
+
44
+ return Wechat.api if account == :default && opts.empty?
45
+
46
+ access_token = opts[:access_token] || cfg.access_token
47
+ jsapi_ticket = opts[:jsapi_ticket] || cfg.jsapi_ticket
38
48
 
39
49
  if corpid.present?
40
- corpsecret = opts[:corpsecret] || Wechat.config(account).corpsecret
50
+ corpsecret = opts[:corpsecret] || cfg.corpsecret
41
51
  Wechat::CorpApi.new(corpid, corpsecret, access_token, \
42
52
  agentid, timeout, skip_verify_ssl, jsapi_ticket)
43
53
  else
44
- secret = opts[:secret] || Wechat.config(account).secret
54
+ secret = opts[:secret] || cfg.secret
45
55
  Wechat::Api.new(appid, secret, access_token, \
46
56
  timeout, skip_verify_ssl, jsapi_ticket)
47
57
  end
@@ -35,7 +35,7 @@ module Wechat
35
35
  private
36
36
 
37
37
  def redis_key
38
- "my_app_wechat_ticket_#{self.appid}"
38
+ "my_app_wechat_ticket_#{self.access_token.appid}"
39
39
  end
40
40
  end
41
41
  end
@@ -4,19 +4,15 @@ module Wechat
4
4
  account = options[:account] || :default
5
5
  c = ApiLoader.config(account)
6
6
 
7
- token_file = options[:token_file] || c.access_token || '/var/tmp/wechat_access_token'
8
- js_token_file = options[:js_token_file] || c.jsapi_ticket || '/var/tmp/wechat_jsapi_ticket'
7
+ token_file = options[:token_file] || c.access_token.presence || '/var/tmp/wechat_access_token'
8
+ js_token_file = options[:js_token_file] || c.jsapi_ticket.presence || '/var/tmp/wechat_jsapi_ticket'
9
9
 
10
10
  if c.appid && c.secret && token_file.present?
11
11
  Wechat::Api.new(c.appid, c.secret, token_file, c.timeout, c.skip_verify_ssl, js_token_file)
12
12
  elsif c.corpid && c.corpsecret && token_file.present?
13
13
  Wechat::CorpApi.new(c.corpid, c.corpsecret, token_file, c.agentid, c.timeout, c.skip_verify_ssl, js_token_file)
14
14
  else
15
- puts <<-HELP
16
- Need create ~/.wechat.yml with wechat appid and secret
17
- or running at rails root folder so wechat can read config/wechat.yml
18
- HELP
19
- exit 1
15
+ raise "Need create ~/.wechat.yml with wechat appid and secret or running at rails root folder so wechat can read config/wechat.yml"
20
16
  end
21
17
  end
22
18
 
@@ -50,6 +46,7 @@ HELP
50
46
  configs.each do |_, cfg|
51
47
  cfg[:timeout] ||= 20
52
48
  cfg[:have_session_class] = class_exists?('WechatSession')
49
+ cfg[:oauth2_cookie_duration] ||= 1.hour
53
50
  end
54
51
 
55
52
  # create config object using raw config data
@@ -69,11 +66,11 @@ HELP
69
66
  application_config_file = File.join(Dir.getwd, 'config/application.yml')
70
67
  home_config_file = File.join(Dir.home, '.wechat.yml')
71
68
  if File.exist?(rails_config_file)
69
+ rails_env = ENV['RAILS_ENV'] || 'development'
72
70
  if File.exist?(application_config_file) && !defined?(::Figaro)
73
71
  require 'figaro'
74
- Figaro::Application.new(path: application_config_file).load
72
+ Figaro::Application.new(path: application_config_file, environment: rails_env).load
75
73
  end
76
- rails_env = ENV['RAILS_ENV'] || 'development'
77
74
  config = resovle_config_file(rails_config_file, rails_env)
78
75
  if config.present? && (default = config[:default]) && (default['appid'] || default['corpid'])
79
76
  puts "Using rails project #{ENV['WECHAT_CONF_FILE'] || "config/wechat.yml"} #{rails_env} setting..."
@@ -7,40 +7,50 @@ module Wechat
7
7
  :skip_verify_ssl, :encoding_aes_key, :trusted_domain_fullname, :oauth2_cookie_duration
8
8
  end
9
9
 
10
- def wechat
11
- self.class.wechat # Make sure user can continue access wechat at instance level similar to class level
10
+ def wechat(account = nil)
11
+ # Make sure user can continue access wechat at instance level similar to class level
12
+ self.class.wechat(account)
12
13
  end
13
14
 
14
- def wechat_oauth2(scope = 'snsapi_base', page_url = nil, &block)
15
- appid = self.class.corpid || self.class.appid || lambda do
16
- self.class.wechat # to initialize wechat_api_client at first time call wechat_oauth2
17
- self.class.corpid || self.class.appid
18
- end.call
15
+ def wechat_oauth2(scope = 'snsapi_base', page_url = nil, account = nil, &block)
16
+ # ensure wechat initialization
17
+ self.class.corpid || self.class.appid || self.class.wechat
18
+
19
+ api = wechat(account)
20
+ if account
21
+ config = Wechat.config(account)
22
+ appid = config.corpid || config.appid
23
+ is_crop_account = !!config.corpid
24
+ else
25
+ appid = self.class.corpid || self.class.appid
26
+ is_crop_account = !!self.class.corpid
27
+ end
28
+
19
29
  raise 'Can not get corpid or appid, so please configure it first to using wechat_oauth2' if appid.blank?
20
30
 
21
- wechat.jsapi_ticket.ticket if wechat.jsapi_ticket.oauth2_state.nil?
31
+ api.jsapi_ticket.ticket if api.jsapi_ticket.oauth2_state.nil?
22
32
  oauth2_params = {
23
33
  appid: appid,
24
- redirect_uri: page_url,
34
+ redirect_uri: page_url || generate_redirect_uri(account),
25
35
  scope: scope,
26
36
  response_type: 'code',
27
- state: wechat.jsapi_ticket.oauth2_state
37
+ state: api.jsapi_ticket.oauth2_state
28
38
  }
29
39
 
30
40
  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)
41
+ is_crop_account ? wechat_corp_oauth2(oauth2_params, account, &block) : wechat_public_oauth2(oauth2_params, account, &block)
32
42
  end
33
43
 
34
44
  private
35
45
 
36
- def wechat_public_oauth2(oauth2_params)
46
+ def wechat_public_oauth2(oauth2_params, account = nil)
37
47
  openid = cookies.signed_or_encrypted[:we_openid]
38
48
  unionid = cookies.signed_or_encrypted[:we_unionid]
39
49
  we_token = cookies.signed_or_encrypted[:we_access_token]
40
50
  if openid.present?
41
51
  yield openid, { 'openid' => openid, 'unionid' => unionid, 'access_token' => we_token}
42
52
  elsif params[:code].present? && params[:state] == oauth2_params[:state]
43
- access_info = wechat.web_access_token(params[:code])
53
+ access_info = wechat(account).web_access_token(params[:code])
44
54
  cookies.signed_or_encrypted[:we_openid] = { value: access_info['openid'], expires: self.class.oauth2_cookie_duration.from_now }
45
55
  cookies.signed_or_encrypted[:we_unionid] = { value: access_info['unionid'], expires: self.class.oauth2_cookie_duration.from_now }
46
56
  cookies.signed_or_encrypted[:we_access_token] = { value: access_info['access_token'], expires: self.class.oauth2_cookie_duration.from_now }
@@ -50,13 +60,13 @@ module Wechat
50
60
  end
51
61
  end
52
62
 
53
- def wechat_corp_oauth2(oauth2_params)
63
+ def wechat_corp_oauth2(oauth2_params, account = nil)
54
64
  userid = cookies.signed_or_encrypted[:we_userid]
55
65
  deviceid = cookies.signed_or_encrypted[:we_deviceid]
56
66
  if userid.present? && deviceid.present?
57
67
  yield userid, { 'UserId' => userid, 'DeviceId' => deviceid }
58
68
  elsif params[:code].present? && params[:state] == oauth2_params[:state]
59
- userinfo = wechat.getuserinfo(params[:code])
69
+ userinfo = wechat(account).getuserinfo(params[:code])
60
70
  cookies.signed_or_encrypted[:we_userid] = { value: userinfo['UserId'], expires: self.class.oauth2_cookie_duration.from_now }
61
71
  cookies.signed_or_encrypted[:we_deviceid] = { value: userinfo['DeviceId'], expires: self.class.oauth2_cookie_duration.from_now }
62
72
  yield userinfo['UserId'], userinfo
@@ -65,13 +75,18 @@ module Wechat
65
75
  end
66
76
  end
67
77
 
68
- def generate_oauth2_url(oauth2_params)
69
- if oauth2_params[:redirect_uri].blank?
70
- page_url = (td = self.class.trusted_domain_fullname) ? "#{td}#{request.original_fullpath}" : request.original_url
71
- safe_query = request.query_parameters.reject { |k, _| %w(code state access_token).include? k }.to_query
72
- oauth2_params[:redirect_uri] = page_url.sub(request.query_string, safe_query)
78
+ def generate_redirect_uri(account = nil)
79
+ domain_name = if account
80
+ Wechat.config(account).trusted_domain_fullname
81
+ else
82
+ self.class.trusted_domain_fullname
73
83
  end
84
+ page_url = domain_name ? "#{domain_name}#{request.original_fullpath}" : request.original_url
85
+ safe_query = request.query_parameters.reject { |k, _| %w(code state access_token).include? k }.to_query
86
+ page_url.sub(request.query_string, safe_query)
87
+ end
74
88
 
89
+ def generate_oauth2_url(oauth2_params)
75
90
  if oauth2_params[:scope] == 'snsapi_login'
76
91
  "https://open.weixin.qq.com/connect/qrconnect?#{oauth2_params.to_query}#wechat_redirect"
77
92
  else
@@ -1,16 +1,33 @@
1
1
  module Wechat
2
2
  module Helpers
3
3
  def wechat_config_js(config_options = {})
4
- page_url = if controller.class.trusted_domain_fullname
5
- "#{controller.class.trusted_domain_fullname}#{controller.request.original_fullpath}"
4
+ account = config_options[:account]
5
+
6
+ # Get domain_name, api and app_id
7
+ if account.blank? || account == controller.class.wechat_cfg_account
8
+ # default account
9
+ domain_name = controller.class.trusted_domain_fullname
10
+ api = controller.wechat
11
+ app_id = controller.class.corpid || controller.class.appid
12
+ else
13
+ # not default account
14
+ config = Wechat.config(account)
15
+ domain_name = config.trusted_domain_fullname
16
+ api = controller.wechat(account)
17
+ app_id = config.corpid || config.appid
18
+ end
19
+
20
+ page_url = if domain_name
21
+ "#{domain_name}#{controller.request.original_fullpath}"
6
22
  else
7
23
  controller.request.original_url
8
24
  end
9
- js_hash = controller.wechat.jsapi_ticket.signature(page_url)
25
+ js_hash = api.jsapi_ticket.signature(page_url)
26
+
10
27
  config_js = <<-WECHAT_CONFIG_JS
11
28
  wx.config({
12
29
  debug: #{config_options[:debug]},
13
- appId: "#{controller.class.corpid || controller.class.appid}",
30
+ appId: "#{app_id}",
14
31
  timestamp: "#{js_hash[:timestamp]}",
15
32
  nonceStr: "#{js_hash[:noncestr]}",
16
33
  signature: "#{js_hash[:signature]}",
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.7
4
+ version: 0.8.8
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: 2017-04-23 00:00:00.000000000 Z
34
+ date: 2017-05-18 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activesupport
@@ -107,28 +107,28 @@ dependencies:
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '3.5'
110
+ version: '3.6'
111
111
  type: :development
112
112
  prerelease: false
113
113
  version_requirements: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '3.5'
117
+ version: '3.6'
118
118
  - !ruby/object:Gem::Dependency
119
119
  name: rails
120
120
  requirement: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
- version: '5'
124
+ version: '5.1'
125
125
  type: :development
126
126
  prerelease: false
127
127
  version_requirements: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - ">="
130
130
  - !ruby/object:Gem::Version
131
- version: '5'
131
+ version: '5.1'
132
132
  - !ruby/object:Gem::Dependency
133
133
  name: sqlite3
134
134
  requirement: !ruby/object:Gem::Requirement
@@ -206,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
206
206
  version: '0'
207
207
  requirements: []
208
208
  rubyforge_project:
209
- rubygems_version: 2.6.11
209
+ rubygems_version: 2.6.12
210
210
  signing_key:
211
211
  specification_version: 4
212
212
  summary: DSL for wechat message handling and API
metadata.gz.sig CHANGED
Binary file