wechat-handler 0.1
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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +22 -0
- data/Rakefile +24 -0
- data/app/assets/javascripts/wechat/handler/application.js +13 -0
- data/app/assets/stylesheets/wechat/handler/application.css +15 -0
- data/app/controllers/wechat/handler/application_controller.rb +2 -0
- data/app/controllers/wechat/handler/concerns/dispatcher.rb +153 -0
- data/app/helpers/wechat/handler/application_helper.rb +2 -0
- data/app/views/layouts/wechat/handler/application.html.erb +14 -0
- data/config/routes.rb +2 -0
- data/lib/tasks/handler_tasks.rake +4 -0
- data/lib/wechat/handler.rb +6 -0
- data/lib/wechat/handler/engine.rb +11 -0
- data/lib/wechat/handler/version.rb +5 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4f58d3fada36dad1133f0882822070b16de16e6e
|
4
|
+
data.tar.gz: eab5f5defb50d796139a000bde5e0253648e157b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e241748b05535822352f4c27fce061d73b321abdbf1e0c13ce6aa47b53176015b2029da6fbdd61f1797290d147bd6f64fd050ed34cccbeccc6fd884817190bab
|
7
|
+
data.tar.gz: ca08de2459ac04634d100f2e6b3417f3ec9598a4e2aaeca0fae19f193d7887c8859358a4c277535db3aa80291bd81a732a30700e77cfff00831d37b4c1002ef2
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Topbit Du
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Wechat Handler 微信回调处理引擎
|
2
|
+
|
3
|
+
[](http://opensource.org/licenses/MIT)
|
4
|
+
[](https://badge.fury.io/rb/wechat-handler)
|
5
|
+
|
6
|
+
The Wechat Handler engine handles the Wechat event & message notifications. 微信回调处理引擎处理微信服务器发出的事件通知和消息通知。
|
7
|
+
|
8
|
+
## Usage in Gemfile
|
9
|
+
```ruby
|
10
|
+
gem 'wechat-handler'
|
11
|
+
```
|
12
|
+
|
13
|
+
## Include the controller concern
|
14
|
+
```ruby
|
15
|
+
include ::Wechat::Handler::Concerns::Dispatcher
|
16
|
+
|
17
|
+
def on_event(pairs)
|
18
|
+
{ 'MsgType' => 'text', 'Content' => 'Aloha!' }
|
19
|
+
end
|
20
|
+
```
|
21
|
+
|
22
|
+
The Dispatcher handles the ToUserName, the FromUserName, and the CreateTime automatically.
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Wecaht Handler'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
load 'rails/tasks/statistics.rake'
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
Bundler::GemHelper.install_tasks
|
24
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,153 @@
|
|
1
|
+
module Wechat::Handler::Concerns::Dispatcher
|
2
|
+
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
self.included do |includer|
|
6
|
+
|
7
|
+
skip_before_filter :verify_authenticity_token
|
8
|
+
|
9
|
+
def create
|
10
|
+
|
11
|
+
signature = params[:signature]
|
12
|
+
timestamp = params[:timestamp]
|
13
|
+
nonce = params[:nonce]
|
14
|
+
encrypt_type = params[:encrypt_type] # aes
|
15
|
+
message_signature = params[:msg_signature]
|
16
|
+
|
17
|
+
xml_text = nil
|
18
|
+
replying_encryption = 'raw'
|
19
|
+
request_body = request.body.read
|
20
|
+
Rails.logger.warn ' >> The request.body is as the following:'
|
21
|
+
Rails.logger.warn request_body
|
22
|
+
|
23
|
+
if encrypt_type.blank? || 'raw'==encrypt_type
|
24
|
+
|
25
|
+
xml_text = request_body
|
26
|
+
|
27
|
+
elsif 'aes'==encrypt_type
|
28
|
+
|
29
|
+
replying_encryption = 'aes'
|
30
|
+
|
31
|
+
render text: 'sign_error' and return unless check_signature(signature, nonce, timestamp)
|
32
|
+
encoded_message = ::Wechat::Callback::XmlDocument.load(request_body)['Encrypt']
|
33
|
+
Rails.logger.info "The encoded_message is #{encoded_message.inspect}."
|
34
|
+
render text: 'msg_sign_error' and return unless check_message_signature(message_signature, encoded_message, nonce, timestamp)
|
35
|
+
|
36
|
+
message = ::Wechat::Callback::MessageDecryption.create encoded_message, Rails.application.secrets.wechat_encoding_aes_keys
|
37
|
+
random_bytes, xml_size, xml_text, app_id, padding_bytes = ::Wechat::Callback::SecureMessage.load message
|
38
|
+
Rails.logger.warn " >> Wechat Callback >> Message Controller >> App ID of the Secure Message = #{app_id}."
|
39
|
+
Rails.logger.warn ' >> Wechat Callback >> Message Controller >> XML Text of the Secure Message ='
|
40
|
+
Rails.logger.warn xml_text
|
41
|
+
render text: 'app_id_error' and return unless check_app_id(app_id)
|
42
|
+
|
43
|
+
else
|
44
|
+
|
45
|
+
render text: "Encrypt type #{encrypt_type} is not suppored"
|
46
|
+
return
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
pairs = ::Wechat::Callback::XmlDocument.load xml_text
|
51
|
+
replying_pairs = { 'ToUserName' => pairs['FromUserName'], 'FromUserName' => pairs['ToUserName'], 'CreateTime' => Time.now.to_i }
|
52
|
+
|
53
|
+
if respond_to? :on_event
|
54
|
+
replied_pairs = on_event pairs
|
55
|
+
replying_pairs = replying_pairs.merge! replied_pairs
|
56
|
+
else
|
57
|
+
Rails.logger.warn "The #{includer} does not have the #on_event method."
|
58
|
+
end
|
59
|
+
|
60
|
+
#replying_pairs = {
|
61
|
+
# 'ToUserName' => pairs['FromUserName'],
|
62
|
+
# 'FromUserName' => pairs['ToUserName'],
|
63
|
+
# 'CreateTime' => Time.now.to_i,
|
64
|
+
# 'MsgType' => 'text',
|
65
|
+
# 'Content' => '是紫色的。'
|
66
|
+
# }
|
67
|
+
replying_xml_text = ::Wechat::Callback::XmlDocument.create replying_pairs
|
68
|
+
|
69
|
+
if 'aes'==replying_encryption
|
70
|
+
random_bytes = ::Wechat::Callback::RandomByteArray.create 16
|
71
|
+
plain_text = ::Wechat::Callback::SecureMessage.create random_bytes, replying_xml_text, wechat_app_id
|
72
|
+
encrypted = ::Wechat::Callback::MessageEncryption.create plain_text, wechat_encoding_aes_keys
|
73
|
+
replying_singature = ::Wechat::Callback::Signature.create wechat_token, timestamp, nonce, encrypted
|
74
|
+
encrypted_replying_pairs = {
|
75
|
+
'Encrypt' => encrypted,
|
76
|
+
'MsgSignature' => replying_singature,
|
77
|
+
'TimeStamp' => timestamp,
|
78
|
+
'Nonce' => nonce
|
79
|
+
}
|
80
|
+
replying_xml_text = ::Wechat::Callback::XmlDocument.create encrypted_replying_pairs
|
81
|
+
|
82
|
+
# debugging
|
83
|
+
debugging_pairs = ::Wechat::Callback::XmlDocument.load replying_xml_text
|
84
|
+
Rails.logger.warn ' >> Wechat Callback >> Message Controller >> Debugging Pairs ='
|
85
|
+
Rails.logger.warn debugging_pairs.inspect
|
86
|
+
debugging_encrypted_message = debugging_pairs['Encrypt']
|
87
|
+
debugging_decrypted_message = ::Wechat::Callback::MessageDecryption.create debugging_encrypted_message, wechat_encoding_aes_keys
|
88
|
+
Rails.logger.warn ' >> Wechat Callback >> Message Controller >> Debugging Decrypted Message ='
|
89
|
+
Rails.logger.warn debugging_decrypted_message
|
90
|
+
debugging_random_bytes, debugging_xml_size, debugging_xml_text, debugging_app_id, debugging_padding_bytes = ::Wechat::Callback::SecureMessage.load debugging_decrypted_message
|
91
|
+
Rails.logger.warn " >> Wechat Callback >> Message Controller >> Debugging App ID of the Secure Message = #{debugging_app_id}."
|
92
|
+
Rails.logger.warn ' >> Wechat Callback >> Message Controller >> Debugging XML Text of the Secure Message ='
|
93
|
+
Rails.logger.warn debugging_xml_text
|
94
|
+
|
95
|
+
end
|
96
|
+
Rails.logger.warn ' >> The Replying XML Text is as the following:'
|
97
|
+
Rails.logger.warn replying_xml_text
|
98
|
+
|
99
|
+
render status: 200, xml: replying_xml_text
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
def check_parameter(name, value)
|
104
|
+
if value.blank?
|
105
|
+
Rails.logger.warn "The #{name} parameter is blank. Failed to validate URL by Wechat."
|
106
|
+
render text: ''
|
107
|
+
end
|
108
|
+
value.present?
|
109
|
+
end
|
110
|
+
|
111
|
+
def check_signature(signature, nonce, timestamp)
|
112
|
+
actual = ::Wechat::Callback::Signature.create wechat_token, timestamp, nonce
|
113
|
+
matched = signature==actual
|
114
|
+
Rails.logger.warn "Actual signature is #{actual}, which does not equal to the given signature #{signature}." unless matched
|
115
|
+
matched
|
116
|
+
end
|
117
|
+
|
118
|
+
def check_message_signature(message_signature, encoded_message, nonce, timestamp)
|
119
|
+
actual = ::Wechat::Callback::MessageSignature.create encoded_message, wechat_token, timestamp, nonce
|
120
|
+
matched = message_signature==actual
|
121
|
+
Rails.logger.warn "Actual message signature is #{actual}, which does not equal to the given message signature #{message_signature}." unless matched
|
122
|
+
matched
|
123
|
+
end
|
124
|
+
|
125
|
+
def check_app_id(app_id)
|
126
|
+
matched = wechat_app_id==app_id
|
127
|
+
Rails.logger.warn "Actual App ID is #{wechat_app_id}, which does not equal to the given App ID #{app_id}." unless matched
|
128
|
+
matched
|
129
|
+
end
|
130
|
+
|
131
|
+
def wechat_token
|
132
|
+
@token = @token||Rails.application.secrets.wechat_validation_token
|
133
|
+
Rails.logger.warn 'Please configure "wechat_validation_token" in the /config/secrets.yml file. Failed to validate URL by Wechat.' if @token.blank?
|
134
|
+
@token
|
135
|
+
end
|
136
|
+
|
137
|
+
def wechat_app_id
|
138
|
+
@app_id = @app_id||Rails.application.secrets.wechat_app_id
|
139
|
+
Rails.logger.warn 'Please configure "wechat_app_id" in the /config/secrets.yml file. Failed to validate URL by Wechat.' if @app_id.blank?
|
140
|
+
@app_id
|
141
|
+
end
|
142
|
+
|
143
|
+
def wechat_encoding_aes_keys
|
144
|
+
@encoding_aes_keys = @encoding_aes_keys||Rails.application.secrets.wechat_encoding_aes_keys
|
145
|
+
Rails.logger.warn 'Please configure "encoding_aes_keys" in the /config/secrets.yml file. Failed to validate URL by Wechat.' if @encoding_aes_keys.blank?
|
146
|
+
@encoding_aes_keys
|
147
|
+
end
|
148
|
+
|
149
|
+
private :check_parameter, :check_signature, :wechat_token
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Wechat Handler</title>
|
5
|
+
<%= stylesheet_link_tag "handler/application", media: "all" %>
|
6
|
+
<%= javascript_include_tag "handler/application" %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
data/config/routes.rb
ADDED
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wechat-handler
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Topbit Du
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: wechat-validation
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: wechat-validator
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.2'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: wechat-callback
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.3'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.3'
|
69
|
+
description: Wechat Handler engine handles the Wechat event & message notifications.
|
70
|
+
微信回调处理引擎处理微信服务器发出的事件通知和消息通知。
|
71
|
+
email:
|
72
|
+
- topbit.du@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- MIT-LICENSE
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- app/assets/javascripts/wechat/handler/application.js
|
81
|
+
- app/assets/stylesheets/wechat/handler/application.css
|
82
|
+
- app/controllers/wechat/handler/application_controller.rb
|
83
|
+
- app/controllers/wechat/handler/concerns/dispatcher.rb
|
84
|
+
- app/helpers/wechat/handler/application_helper.rb
|
85
|
+
- app/views/layouts/wechat/handler/application.html.erb
|
86
|
+
- config/routes.rb
|
87
|
+
- lib/tasks/handler_tasks.rake
|
88
|
+
- lib/wechat/handler.rb
|
89
|
+
- lib/wechat/handler/engine.rb
|
90
|
+
- lib/wechat/handler/version.rb
|
91
|
+
homepage: https://github.com/topbitdu/wechat-handler
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.4.5.1
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: Wechat Handler Engine 微信回调处理引擎
|
115
|
+
test_files: []
|