weixin_rails_middleware 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/weixin_rails_middleware/weixin_controller.rb +19 -26
- data/lib/generators/templates/initializer.rb +10 -8
- data/lib/generators/templates/weixin_controller.rb +0 -1
- data/lib/weixin_rails_middleware/configuration.rb +28 -6
- data/lib/weixin_rails_middleware/engine.rb +10 -1
- data/lib/weixin_rails_middleware/{weixin_message_helper.rb → helpers/reply_weixin_message_helper.rb} +1 -1
- data/lib/weixin_rails_middleware/{unique_token_helper.rb → helpers/unique_token_helper.rb} +2 -2
- data/lib/weixin_rails_middleware/helpers/weixin_server_url.rb +69 -0
- data/lib/weixin_rails_middleware/{weixin_token_form_helper.rb → helpers/weixin_token_form_helper.rb} +4 -3
- data/lib/weixin_rails_middleware/{message.rb → models/message.rb} +1 -1
- data/lib/weixin_rails_middleware/{reply_message.rb → models/reply_message.rb} +8 -8
- data/lib/weixin_rails_middleware/version.rb +1 -1
- data/lib/weixin_rails_middleware.rb +7 -7
- metadata +8 -9
- data/app/controllers/weixin_rails_middleware/application_controller.rb +0 -4
- data/lib/weixin_rails_middleware/weixin_server_url.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f864df00b4c607bd3d9c96217d8a448ca8d4b9ff
|
4
|
+
data.tar.gz: cf2e9c7be77b62d6e7bdb87c70eaad6351e2b00d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae8b8f0bd47219fe7876db2d728334d7c269320ab3b2a71a77ce1290f46baf3046ab765d2f3226ce1ce807e1ea8f5929536d688fac6fb8fd75cbfe451300a5a0
|
7
|
+
data.tar.gz: a674a9bfa8f46348b62aaa51cc15613544449f82ac40a5c8c725f3bf2e42b431e03f042c51d3a0ff4553e6a12efc0ecdf3180dd675f6e37f933af53a79776950
|
@@ -1,8 +1,9 @@
|
|
1
1
|
module WeixinRailsMiddleware
|
2
|
-
class WeixinController <
|
3
|
-
include
|
2
|
+
class WeixinController < ActionController::Base
|
3
|
+
include ReplyWeixinMessageHelper
|
4
|
+
include ConfigurationHelpers
|
4
5
|
|
5
|
-
|
6
|
+
skip_before_action :verify_authenticity_token
|
6
7
|
before_action :check_weixin_params, only: [:index, :reply]
|
7
8
|
before_action :set_weixin_public_account, :set_weixin_message, only: :reply
|
8
9
|
|
@@ -16,22 +17,20 @@ module WeixinRailsMiddleware
|
|
16
17
|
protected
|
17
18
|
|
18
19
|
def check_weixin_params
|
19
|
-
if check_weixin_token_valid?
|
20
|
-
|
21
|
-
render text: "Forbidden", status: 403
|
22
|
-
end
|
20
|
+
if check_weixin_token_valid? && !is_hexdigest?
|
21
|
+
render text: "Forbidden", status: 403
|
23
22
|
end
|
24
23
|
end
|
25
24
|
|
26
25
|
# check the token from Weixin Service is exist in local store.
|
27
26
|
def check_weixin_token_valid?
|
28
|
-
if
|
27
|
+
if token_string.blank?
|
29
28
|
if token_model_instance.blank?
|
30
29
|
render text: "Forbidden", status: 403
|
31
30
|
return false
|
32
31
|
end
|
33
32
|
else
|
34
|
-
if current_weixin_token !=
|
33
|
+
if current_weixin_token != token_string
|
35
34
|
render text: "Forbidden", status: 403
|
36
35
|
return false
|
37
36
|
end
|
@@ -42,26 +41,16 @@ module WeixinRailsMiddleware
|
|
42
41
|
def is_hexdigest?
|
43
42
|
signature = params[:signature] || ''
|
44
43
|
timestamp = params[:timestamp] || ''
|
45
|
-
nonce = params[:nonce]
|
44
|
+
nonce = params[:nonce] || ''
|
46
45
|
current_signature = Digest::SHA1.hexdigest([current_weixin_token, timestamp, nonce].sort.join)
|
47
46
|
return true if current_signature == signature
|
48
47
|
false
|
49
48
|
end
|
50
49
|
|
51
|
-
|
52
|
-
@weixin_token = params[:weixin_token]
|
53
|
-
end
|
54
|
-
|
55
|
-
def token_model_instance
|
56
|
-
token_model = WeixinRailsMiddleware.config.token_model_class
|
57
|
-
token_column = WeixinRailsMiddleware.config.token_column
|
58
|
-
token_model_instance = token_model.where("#{token_column}" => current_weixin_token).first
|
59
|
-
token_model_instance
|
60
|
-
end
|
61
|
-
|
50
|
+
## Callback
|
62
51
|
# e.g. will generate +@weixin_public_account+
|
63
52
|
def set_weixin_public_account
|
64
|
-
return nil if
|
53
|
+
return nil if token_string.present?
|
65
54
|
@weixin_public_account ||= token_model_instance
|
66
55
|
end
|
67
56
|
|
@@ -70,14 +59,18 @@ module WeixinRailsMiddleware
|
|
70
59
|
@weixin_message ||= current_weixin_message
|
71
60
|
end
|
72
61
|
|
73
|
-
|
74
|
-
|
75
|
-
|
62
|
+
def current_weixin_token
|
63
|
+
@weixin_token = params[:weixin_token]
|
64
|
+
end
|
65
|
+
|
66
|
+
def token_model_instance
|
67
|
+
token_model_instance = token_model_class.where("#{token_column}" => current_weixin_token).first
|
68
|
+
token_model_instance
|
76
69
|
end
|
77
70
|
|
78
71
|
# return a message class with current_weixin_params
|
79
72
|
def current_weixin_message
|
80
|
-
Message.factory(
|
73
|
+
Message.factory(request.body.read)
|
81
74
|
end
|
82
75
|
|
83
76
|
end
|
@@ -2,19 +2,21 @@
|
|
2
2
|
WeixinRailsMiddleware.configure do |config|
|
3
3
|
|
4
4
|
## NOTE:
|
5
|
-
##
|
5
|
+
## If you config all them, it will use `token_string` default
|
6
6
|
##
|
7
|
-
# Th
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# config
|
7
|
+
# Th first configure is fit for your weixin public_account is saved in database.
|
8
|
+
# +token_model+ and +token_column+ must in the same table.
|
9
|
+
# +token_model+ The class name that to save your public_account
|
10
|
+
# +token_column+ You can config a column name Optional, but you must have a column `weixin_token` default.
|
11
|
+
# config.token_model = ""
|
11
12
|
# config.token_column = "weixin_token"
|
12
13
|
|
13
|
-
#
|
14
|
-
#
|
14
|
+
# Or the other configure is fit for only one weixin public_account
|
15
|
+
# If you config `token_string`, so it will directly use it
|
15
16
|
# config.token_string = "token string"
|
16
17
|
|
17
|
-
#
|
18
|
+
# Router
|
19
|
+
# Default is "/", and recommend you use default directly.
|
18
20
|
# config.engine_path = "/"
|
19
21
|
|
20
22
|
end
|
@@ -9,14 +9,36 @@ module WeixinRailsMiddleware
|
|
9
9
|
@token_column = DEFAULT_TOKEN_COLUMN_NAME
|
10
10
|
end
|
11
11
|
|
12
|
+
end
|
13
|
+
|
14
|
+
module ConfigurationHelpers
|
15
|
+
extend ActiveSupport::Concern
|
16
|
+
|
17
|
+
def engine_path
|
18
|
+
@engine_path ||= WeixinRailsMiddleware.config.engine_path
|
19
|
+
end
|
20
|
+
|
21
|
+
def token_string
|
22
|
+
@token_string ||= WeixinRailsMiddleware.config.token_string.to_s
|
23
|
+
end
|
24
|
+
|
25
|
+
def token_column
|
26
|
+
@token_column ||= WeixinRailsMiddleware.config.token_column
|
27
|
+
end
|
28
|
+
|
29
|
+
def token_model
|
30
|
+
@token_model ||= WeixinRailsMiddleware.config.token_model
|
31
|
+
end
|
32
|
+
|
33
|
+
def is_default_engine_path?
|
34
|
+
engine_path == DEFAULT_ENGINE_PATH # "/"
|
35
|
+
end
|
36
|
+
|
12
37
|
def token_model_class
|
13
|
-
|
14
|
-
|
15
|
-
unless token_model_c.table_exists?
|
16
|
-
raise "You don't have #{token_model_c.table_name} table"
|
38
|
+
if token_model.blank?
|
39
|
+
raise "You need to config `token_model` in 'config/initializers/weixin_rails_middleware.rb'"
|
17
40
|
end
|
18
|
-
token_model_c
|
41
|
+
@token_model_c ||= token_model.constantize
|
19
42
|
end
|
20
|
-
|
21
43
|
end
|
22
44
|
end
|
@@ -1,5 +1,14 @@
|
|
1
1
|
module WeixinRailsMiddleware
|
2
2
|
class Engine < ::Rails::Engine
|
3
|
+
include ConfigurationHelpers
|
4
|
+
|
3
5
|
isolate_namespace WeixinRailsMiddleware
|
6
|
+
|
7
|
+
config.after_initialize do
|
8
|
+
if token_model_class.present?
|
9
|
+
token_model_class.send(:include, WeixinServerUrl::InstanceMethods)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
4
13
|
end
|
5
|
-
end
|
14
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module WeixinRailsMiddleware
|
2
|
-
module
|
2
|
+
module UniqueTokenHelper
|
3
3
|
def self.generate(options = {})
|
4
4
|
# SecureRandom: hex, base64, random_bytes, urlsafe_base64, random_number, uuid
|
5
5
|
generator_method_type = options.delete(:generator).try(:to_sym) || :hex
|
@@ -14,4 +14,4 @@ module WeixinRailsMiddleware
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
WeiXinUniqueToken = WeixinRailsMiddleware::
|
17
|
+
WeiXinUniqueToken = WeixinRailsMiddleware::UniqueTokenHelper
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# TODO: added weixin_server_url for User
|
2
|
+
# if config +token_model+, e.g. user = User.first; user.weixin_server_url
|
3
|
+
# if config +token_string+, e.g. weixin_server_url
|
4
|
+
# a public url looks like: weixin_server_url
|
5
|
+
module WeixinRailsMiddleware
|
6
|
+
module WeixinServerUrl
|
7
|
+
|
8
|
+
module Base
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def weixin_server_url_with_public_account(public_account)
|
13
|
+
base_host_url + current_token(public_account)
|
14
|
+
end
|
15
|
+
|
16
|
+
def weixin_server_url_without_public_account
|
17
|
+
base_host_url + token_string
|
18
|
+
end
|
19
|
+
|
20
|
+
def current_token(public_account)
|
21
|
+
if public_account.blank?
|
22
|
+
token_string
|
23
|
+
else
|
24
|
+
public_account.try(token_column)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def base_host_url
|
29
|
+
host_url = request.base_url
|
30
|
+
if is_default_engine_path? # "/"
|
31
|
+
"#{host_url}/weixin/"
|
32
|
+
else
|
33
|
+
"#{host_url}/#{engine_path}/weixin/"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module ViewHelpers
|
39
|
+
extend ActiveSupport::Concern
|
40
|
+
include ConfigurationHelpers
|
41
|
+
include Base
|
42
|
+
included do
|
43
|
+
def weixin_server_url(public_account=nil)
|
44
|
+
raise "You need pass a public_account instance." if token_string.blank? && public_account.blank?
|
45
|
+
if public_account.present?
|
46
|
+
weixin_server_url_with_public_account(public_account)
|
47
|
+
else
|
48
|
+
weixin_server_url_without_public_account(public_account)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
module InstanceMethods
|
56
|
+
extend ActiveSupport::Concern
|
57
|
+
include ConfigurationHelpers
|
58
|
+
include Base
|
59
|
+
included do
|
60
|
+
def weixin_server_url
|
61
|
+
# base_host_url + self.send(token_column)
|
62
|
+
warn "Hi, this method is Pending, please use view helper: +weixin_server_url+ :)"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
ActionView::Base.send :include, WeixinRailsMiddleware::WeixinServerUrl::ViewHelpers
|
data/lib/weixin_rails_middleware/{weixin_token_form_helper.rb → helpers/weixin_token_form_helper.rb}
RENAMED
@@ -10,11 +10,12 @@ module ActionView
|
|
10
10
|
@options = {value: generate_weixin_token }.merge!(@options)
|
11
11
|
super
|
12
12
|
end
|
13
|
+
|
13
14
|
private
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
def generate_weixin_token
|
17
|
+
@options.fetch("value"){value_before_type_cast(object)} || WeiXinUniqueToken.generate
|
18
|
+
end
|
18
19
|
end
|
19
20
|
end # end of Tags
|
20
21
|
|
@@ -9,10 +9,10 @@ module WeixinRailsMiddleware
|
|
9
9
|
xml_name :xml
|
10
10
|
#xml_convention :camelcase
|
11
11
|
|
12
|
-
xml_accessor :ToUserName, :cdata
|
12
|
+
xml_accessor :ToUserName, :cdata => true
|
13
13
|
xml_accessor :FromUserName, :cdata => true
|
14
|
-
xml_reader
|
15
|
-
xml_reader
|
14
|
+
xml_reader :CreateTime, :as => Integer
|
15
|
+
xml_reader :MsgType, :cdata => true
|
16
16
|
|
17
17
|
def initialize
|
18
18
|
@CreateTime = Time.now.to_i
|
@@ -34,8 +34,8 @@ module WeixinRailsMiddleware
|
|
34
34
|
class TextReplyMessage < ReplyMessage
|
35
35
|
xml_accessor :Content, :cdata => true
|
36
36
|
def initialize
|
37
|
-
|
38
|
-
|
37
|
+
super
|
38
|
+
@MsgType = 'text'
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
@@ -43,7 +43,7 @@ module WeixinRailsMiddleware
|
|
43
43
|
include ROXML
|
44
44
|
xml_accessor :Title, :cdata => true
|
45
45
|
xml_accessor :Description, :cdata => true
|
46
|
-
xml_accessor :MusicUrl,
|
46
|
+
xml_accessor :MusicUrl, :cdata => true
|
47
47
|
xml_accessor :HQMusicUrl, :cdata => true
|
48
48
|
end
|
49
49
|
|
@@ -74,7 +74,7 @@ module WeixinRailsMiddleware
|
|
74
74
|
xml_accessor :Title, :cdata => true
|
75
75
|
xml_accessor :Description, :cdata => true
|
76
76
|
xml_accessor :PicUrl, :cdata => true
|
77
|
-
xml_accessor :Url,
|
77
|
+
xml_accessor :Url, :cdata => true
|
78
78
|
end
|
79
79
|
|
80
80
|
# <xml>
|
@@ -180,4 +180,4 @@ module WeixinRailsMiddleware
|
|
180
180
|
end
|
181
181
|
end
|
182
182
|
|
183
|
-
end
|
183
|
+
end
|
@@ -1,11 +1,11 @@
|
|
1
|
-
require "weixin_rails_middleware/engine"
|
2
1
|
require "weixin_rails_middleware/configuration"
|
3
|
-
require "weixin_rails_middleware/
|
4
|
-
require "weixin_rails_middleware/
|
5
|
-
require "weixin_rails_middleware/
|
6
|
-
require "weixin_rails_middleware/
|
7
|
-
require "weixin_rails_middleware/
|
8
|
-
require "weixin_rails_middleware/
|
2
|
+
require "weixin_rails_middleware/engine"
|
3
|
+
require "weixin_rails_middleware/models/message"
|
4
|
+
require "weixin_rails_middleware/models/reply_message"
|
5
|
+
require "weixin_rails_middleware/helpers/reply_weixin_message_helper"
|
6
|
+
require "weixin_rails_middleware/helpers/weixin_token_form_helper"
|
7
|
+
require "weixin_rails_middleware/helpers/unique_token_helper"
|
8
|
+
require "weixin_rails_middleware/helpers/weixin_server_url"
|
9
9
|
|
10
10
|
module WeixinRailsMiddleware
|
11
11
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weixin_rails_middleware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- lanrion
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -103,7 +103,6 @@ extra_rdoc_files: []
|
|
103
103
|
files:
|
104
104
|
- MIT-LICENSE
|
105
105
|
- Rakefile
|
106
|
-
- app/controllers/weixin_rails_middleware/application_controller.rb
|
107
106
|
- app/controllers/weixin_rails_middleware/weixin_controller.rb
|
108
107
|
- config/routes.rb
|
109
108
|
- lib/generators/templates/initializer.rb
|
@@ -113,13 +112,13 @@ files:
|
|
113
112
|
- lib/weixin_rails_middleware.rb
|
114
113
|
- lib/weixin_rails_middleware/configuration.rb
|
115
114
|
- lib/weixin_rails_middleware/engine.rb
|
116
|
-
- lib/weixin_rails_middleware/
|
117
|
-
- lib/weixin_rails_middleware/
|
118
|
-
- lib/weixin_rails_middleware/
|
115
|
+
- lib/weixin_rails_middleware/helpers/reply_weixin_message_helper.rb
|
116
|
+
- lib/weixin_rails_middleware/helpers/unique_token_helper.rb
|
117
|
+
- lib/weixin_rails_middleware/helpers/weixin_server_url.rb
|
118
|
+
- lib/weixin_rails_middleware/helpers/weixin_token_form_helper.rb
|
119
|
+
- lib/weixin_rails_middleware/models/message.rb
|
120
|
+
- lib/weixin_rails_middleware/models/reply_message.rb
|
119
121
|
- lib/weixin_rails_middleware/version.rb
|
120
|
-
- lib/weixin_rails_middleware/weixin_message_helper.rb
|
121
|
-
- lib/weixin_rails_middleware/weixin_server_url.rb
|
122
|
-
- lib/weixin_rails_middleware/weixin_token_form_helper.rb
|
123
122
|
homepage: http://github.com/lanrion/weixin_rails_middleware
|
124
123
|
licenses: []
|
125
124
|
metadata: {}
|