weixin_rails_middleware 1.0.4 → 1.0.5

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: f864df00b4c607bd3d9c96217d8a448ca8d4b9ff
4
- data.tar.gz: cf2e9c7be77b62d6e7bdb87c70eaad6351e2b00d
3
+ metadata.gz: 7f65d96d4e58350a83ec0fba14b711b4250e79c3
4
+ data.tar.gz: 21999d40da51005a1ad025473a6fd987a3e77af5
5
5
  SHA512:
6
- metadata.gz: ae8b8f0bd47219fe7876db2d728334d7c269320ab3b2a71a77ce1290f46baf3046ab765d2f3226ce1ce807e1ea8f5929536d688fac6fb8fd75cbfe451300a5a0
7
- data.tar.gz: a674a9bfa8f46348b62aaa51cc15613544449f82ac40a5c8c725f3bf2e42b431e03f042c51d3a0ff4553e6a12efc0ecdf3180dd675f6e37f933af53a79776950
6
+ metadata.gz: f7178de0133a47bf05e75c1bbc9d11bcab4a1b9b86b5bfad3cecb255f206c7b6638d5c2daa5dd09edae8bf1bb8fcda83068b91bb9ac126e6464a937f82b37ef6
7
+ data.tar.gz: 20d4da31eb5d6fce51b72f40bd7ce1ffc672d1e82c4d408efdd2f574091a1fda836239e39b2692546bcae5d5bdcbf817b05e2b821c14de83ca2919335898da06
@@ -2,6 +2,7 @@ module WeixinRailsMiddleware
2
2
  class WeixinController < ActionController::Base
3
3
  include ReplyWeixinMessageHelper
4
4
  include ConfigurationHelpers
5
+ include WeixinAuthorizeHelper
5
6
 
6
7
  skip_before_action :verify_authenticity_token
7
8
  before_action :check_weixin_params, only: [:index, :reply]
@@ -16,37 +17,6 @@ module WeixinRailsMiddleware
16
17
 
17
18
  protected
18
19
 
19
- def check_weixin_params
20
- if check_weixin_token_valid? && !is_hexdigest?
21
- render text: "Forbidden", status: 403
22
- end
23
- end
24
-
25
- # check the token from Weixin Service is exist in local store.
26
- def check_weixin_token_valid?
27
- if token_string.blank?
28
- if token_model_instance.blank?
29
- render text: "Forbidden", status: 403
30
- return false
31
- end
32
- else
33
- if current_weixin_token != token_string
34
- render text: "Forbidden", status: 403
35
- return false
36
- end
37
- end
38
- true
39
- end
40
-
41
- def is_hexdigest?
42
- signature = params[:signature] || ''
43
- timestamp = params[:timestamp] || ''
44
- nonce = params[:nonce] || ''
45
- current_signature = Digest::SHA1.hexdigest([current_weixin_token, timestamp, nonce].sort.join)
46
- return true if current_signature == signature
47
- false
48
- end
49
-
50
20
  ## Callback
51
21
  # e.g. will generate +@weixin_public_account+
52
22
  def set_weixin_public_account
@@ -59,19 +29,5 @@ module WeixinRailsMiddleware
59
29
  @weixin_message ||= current_weixin_message
60
30
  end
61
31
 
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
69
- end
70
-
71
- # return a message class with current_weixin_params
72
- def current_weixin_message
73
- Message.factory(request.body.read)
74
- end
75
-
76
32
  end
77
33
  end
@@ -1,4 +1,4 @@
1
1
  WeixinRailsMiddleware::Engine.routes.draw do
2
- get 'weixin/:weixin_token', to: 'weixin#index'
3
- post 'weixin/:weixin_token', to: 'weixin#reply'
2
+ get 'weixin/:weixin_token', to: 'weixin#index', as: :weixin_index
3
+ post 'weixin/:weixin_token', to: 'weixin#reply', as: :weixin_reply
4
4
  end
@@ -5,7 +5,7 @@ require "weixin_rails_middleware/models/reply_message"
5
5
  require "weixin_rails_middleware/helpers/reply_weixin_message_helper"
6
6
  require "weixin_rails_middleware/helpers/weixin_token_form_helper"
7
7
  require "weixin_rails_middleware/helpers/unique_token_helper"
8
- require "weixin_rails_middleware/helpers/weixin_server_url"
8
+ require "weixin_rails_middleware/helpers/weixin_authorize_helper"
9
9
 
10
10
  module WeixinRailsMiddleware
11
11
 
@@ -1,14 +1,7 @@
1
1
  module WeixinRailsMiddleware
2
2
  class Engine < ::Rails::Engine
3
- include ConfigurationHelpers
4
-
5
3
  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
4
+ engine_name :weixin_engine
12
5
 
13
6
  end
14
7
  end
@@ -0,0 +1,54 @@
1
+ module WeixinRailsMiddleware
2
+ module WeixinAuthorizeHelper
3
+ extend ActiveSupport::Concern
4
+
5
+ protected
6
+
7
+ def check_weixin_params
8
+ if check_weixin_token_valid? && !is_hexdigest?
9
+ render text: "Forbidden", status: 403
10
+ end
11
+ end
12
+
13
+ # check the token from Weixin Service is exist in local store.
14
+ def check_weixin_token_valid?
15
+ if token_string.blank?
16
+ if token_model_instance.blank?
17
+ render text: "Forbidden", status: 403
18
+ return false
19
+ end
20
+ else
21
+ if current_weixin_token != token_string
22
+ render text: "Forbidden", status: 403
23
+ return false
24
+ end
25
+ end
26
+ true
27
+ end
28
+
29
+ def is_hexdigest?
30
+ signature = params[:signature] || ''
31
+ timestamp = params[:timestamp] || ''
32
+ nonce = params[:nonce] || ''
33
+ current_signature = Digest::SHA1.hexdigest([current_weixin_token, timestamp, nonce].sort.join)
34
+ return true if current_signature == signature
35
+ false
36
+ end
37
+
38
+ def current_weixin_token
39
+ @weixin_token = params[:weixin_token]
40
+ end
41
+
42
+ def token_model_instance
43
+ token_model_instance = token_model_class.where("#{token_column}" => current_weixin_token).first
44
+ token_model_instance
45
+ end
46
+
47
+ # return a message class with current_weixin_params
48
+ def current_weixin_message
49
+ Message.factory(request.body.read)
50
+ end
51
+
52
+ end
53
+ end
54
+
@@ -1,3 +1,3 @@
1
1
  module WeixinRailsMiddleware
2
- VERSION = "1.0.4".freeze
2
+ VERSION = "1.0.5".freeze
3
3
  end
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
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - lanrion
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-09 00:00:00.000000000 Z
11
+ date: 2014-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -114,7 +114,7 @@ files:
114
114
  - lib/weixin_rails_middleware/engine.rb
115
115
  - lib/weixin_rails_middleware/helpers/reply_weixin_message_helper.rb
116
116
  - lib/weixin_rails_middleware/helpers/unique_token_helper.rb
117
- - lib/weixin_rails_middleware/helpers/weixin_server_url.rb
117
+ - lib/weixin_rails_middleware/helpers/weixin_authorize_helper.rb
118
118
  - lib/weixin_rails_middleware/helpers/weixin_token_form_helper.rb
119
119
  - lib/weixin_rails_middleware/models/message.rb
120
120
  - lib/weixin_rails_middleware/models/reply_message.rb
@@ -1,69 +0,0 @@
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