jindouyun 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7782bce7f1a477f07c11399fe6a50dc70c3ec531
4
+ data.tar.gz: c2127d899578799b67274c91c444f3e1970e5f97
5
+ SHA512:
6
+ metadata.gz: 562245dbcc9487c5832aae19edc2713b6d364cab409b37f85d00c44ef3d2438eea05b0836a686d1d1f99c0e7060763c34f1cf82d91e49977e4e2445b83405667
7
+ data.tar.gz: a61674e2545f3f88cccee538f7ce59afea2947fd521d0f8e4e5975126900420cfd938bf04f1a0ee8bdae47d23a242a7eeb22f1e452436706f31a7c067ee39b3a
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ tags
14
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.7
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in jindouyun.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ jindouyun (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.4.2)
11
+ rspec (3.6.0)
12
+ rspec-core (~> 3.6.0)
13
+ rspec-expectations (~> 3.6.0)
14
+ rspec-mocks (~> 3.6.0)
15
+ rspec-core (3.6.0)
16
+ rspec-support (~> 3.6.0)
17
+ rspec-expectations (3.6.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.6.0)
20
+ rspec-mocks (3.6.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.6.0)
23
+ rspec-support (3.6.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ jindouyun!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 FENG Zhichao
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Jindouyun
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/jindouyun`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'jindouyun'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install jindouyun
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jindouyun.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,11 @@
1
+ module Jindouyun
2
+ class BaseController < ActionController::Base
3
+
4
+ private
5
+
6
+ def log message
7
+ Jindouyun.logger.info message
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,37 @@
1
+ module Jindouyun::Dingding
2
+ class EncryptedMessageController < ::Jindouyun::BaseController
3
+
4
+ before_action :decrypt_payload_and_verify, only: [:create]
5
+ before_action :parse_json_payload, only: [:create]
6
+
7
+ def create
8
+ end
9
+
10
+ private
11
+
12
+ def message_encryptor
13
+ @message_encryptor ||= Jindouyun::MessageEncryptor.new encoding_aes_key: Jindouyun::Setting.setting[:encoding_aes_key],
14
+ sign_token: Jindouyun::Setting.setting[:sign_token],
15
+ app_id: Jindouyun::Setting.setting[:suite_id]
16
+ end
17
+
18
+ def decrypt_payload_and_verify
19
+ @message, _app_id, calculated_sign = message_encryptor.decrypt params
20
+ head :bad_request unless params[:signature] == calculated_sign
21
+ end
22
+
23
+ def service_verify?
24
+ params[:echostr].present?
25
+ end
26
+
27
+ def parse_json_payload
28
+ @message_content = JSON.parse(@message)
29
+ end
30
+
31
+ def encrypted_user_text_response from_user_name, to_user_name, content
32
+ response_content = "<xml><ToUserName><![CDATA[#{to_user_name}]]></ToUserName><FromUserName><![CDATA[#{from_user_name}]]></FromUserName><CreateTime>#{Time.now.to_i}</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[#{content}]]></Content></xml>"
33
+ message_encryptor.encrypt_to_xml response_content
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,46 @@
1
+ module Jindouyun::Dingding
2
+ class SystemNotificationsController < ::Jindouyun::Dingding::EncryptedMessageController
3
+
4
+ def create
5
+ Jindouyun.logger.info "Dingding Message: #{@message_content}"
6
+
7
+ @response_message = 'success'
8
+
9
+ case @message_content['EventType']
10
+ when 'check_create_suite_url'
11
+ when 'check_update_suite_url'
12
+ Jindouyun.logger.info "Dingding check update suite url: #{@message_content['Random']}"
13
+ process_check_update_suite_url
14
+ when 'suite_ticket'
15
+ process_suite_ticket
16
+ when 'check_suite_license_code'
17
+ process_check_suite_license_code
18
+ when 'tmp_auth_code'
19
+ process_tmp_auth_code
20
+ when 'suite_relieve'
21
+ process_suite_relieve
22
+ end
23
+
24
+ render json: message_encryptor.encrypt_to_json(@response_message)
25
+ end
26
+
27
+ private
28
+
29
+ def process_check_update_suite_url
30
+ @response_message = @message_content['Random']
31
+ end
32
+
33
+ def process_suite_ticket
34
+ end
35
+
36
+ def process_check_suite_license_code
37
+ end
38
+
39
+ def process_tmp_auth_code
40
+ end
41
+
42
+ def process_suite_relieve
43
+ end
44
+
45
+ end
46
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'jindouyun'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/jindouyun.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'jindouyun/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'jindouyun'
8
+ spec.version = Jindouyun::VERSION
9
+ spec.authors = ['FENG Zhichao']
10
+ spec.email = ['flankerfc@gmail.com']
11
+
12
+ spec.summary = %q{Dingding (Dingtalk)}
13
+ spec.description = %q{Dingding (Dingtalk)}
14
+ spec.homepage = 'https://github.com/flanker/jindouyun'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'aescrypt'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.16'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ end
data/lib/jindouyun.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'aescrypt'
2
+
3
+ require 'jindouyun/setting'
4
+ require 'jindouyun/logger'
5
+ require 'jindouyun/rails'
6
+ require 'jindouyun/message_encryptor'
7
+
8
+ module Jindouyun
9
+ end
@@ -0,0 +1,13 @@
1
+ module Jindouyun
2
+
3
+ @@logger = ::Logger.new(STDOUT)
4
+
5
+ def self.logger
6
+ @@logger
7
+ end
8
+
9
+ def self.logger= value
10
+ @@logger = value
11
+ end
12
+
13
+ end
@@ -0,0 +1,109 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+ require 'securerandom'
4
+
5
+ module Jindouyun
6
+ class MessageEncryptor
7
+
8
+ BLOCK_SIZE = 32
9
+
10
+ attr_reader :encoding_aes_key, :sign_token, :app_id
11
+
12
+ def initialize(encoding_aes_key: nil, sign_token: nil, app_id: nil)
13
+ @encoding_aes_key = encoding_aes_key
14
+ @sign_token = sign_token
15
+ @app_id = app_id
16
+ end
17
+
18
+ def encrypt(payload)
19
+ aes_encrypt(payload).delete("\n")
20
+ end
21
+
22
+ def encrypt_with_signature(payload)
23
+ encrypted_payload = encrypt(payload)
24
+ timestamp = Time.now.to_i.to_s
25
+ nonce = SecureRandom.hex(8)
26
+ signature = generate_signature(encrypted_payload, timestamp, nonce)
27
+ return [encrypted_payload, timestamp, nonce, signature]
28
+ end
29
+
30
+ def encrypt_to_json(payload)
31
+ encrypted_payload, timestamp, nonce, signature = encrypt_with_signature(payload)
32
+ {
33
+ msg_signature: signature,
34
+ timeStamp: timestamp,
35
+ nonce: nonce,
36
+ encrypt: encrypted_payload
37
+ }
38
+ end
39
+
40
+ def decrypt(payload)
41
+ encrypted_payload = payload['echostr'] || payload['encrypt']
42
+ sign = generate_signature encrypted_payload, payload['timeStamp'], payload['nonce']
43
+ message, app_id = decrypt_payload encrypted_payload
44
+ return message, app_id, sign
45
+ end
46
+
47
+ def aes_decrypt(encrypted_message)
48
+ message, _app_id = decrypt_payload(encrypted_message)
49
+ message
50
+ end
51
+
52
+ private
53
+
54
+ def aes_key
55
+ @aes_key ||= Base64.decode64 encoding_aes_key + '='
56
+ end
57
+
58
+ def decrypt_payload(encrypted_payload)
59
+ text = Base64.decode64 encrypted_payload
60
+ text = handle_cipher(:decrypt, text)
61
+ result = pkcs7_decode(text)
62
+ content = result[16...result.length]
63
+ len_list = content[0...4].unpack('N')
64
+ xml_len = len_list[0]
65
+ payload = content[4...4 + xml_len]
66
+ app_id = content[xml_len+4...content.size]
67
+ return payload, app_id
68
+ end
69
+
70
+ def generate_signature(message, timestamp, nonce)
71
+ sorted_params = [message, sign_token, timestamp, nonce].sort.join
72
+ Digest::SHA1.hexdigest(sorted_params)
73
+ end
74
+
75
+ def aes_encrypt(text)
76
+ text = text.force_encoding('ASCII-8BIT')
77
+ random = SecureRandom.hex(8)
78
+ msg_len = [text.length].pack('N')
79
+ text = "#{random}#{msg_len}#{text}#{app_id}"
80
+ text = encode(text)
81
+ text = handle_cipher(:encrypt, text)
82
+ Base64.encode64(text)
83
+ end
84
+
85
+ def handle_cipher(action, text)
86
+ cipher = OpenSSL::Cipher.new('AES-256-CBC')
87
+ cipher.send(action)
88
+ cipher.padding = 0
89
+ cipher.key = aes_key
90
+ cipher.iv = aes_key[0...16]
91
+ cipher.update(text) + cipher.final
92
+ end
93
+
94
+ def pkcs7_decode(text)
95
+ pad = text[-1].ord
96
+ pad = 0 if pad < 1 || pad > BLOCK_SIZE
97
+ size = text.size - pad
98
+ text[0...size]
99
+ end
100
+
101
+ def encode(text)
102
+ amount_to_pad = BLOCK_SIZE - (text.length % BLOCK_SIZE)
103
+ amount_to_pad = BLOCK_SIZE if amount_to_pad.zero?
104
+ pad_chr = amount_to_pad.chr
105
+ "#{text}#{pad_chr * amount_to_pad}"
106
+ end
107
+
108
+ end
109
+ end
@@ -0,0 +1,7 @@
1
+ require 'jindouyun/rails/routes'
2
+
3
+ module Jindouyun
4
+ class Engine < ::Rails::Engine
5
+
6
+ end
7
+ end
@@ -0,0 +1,19 @@
1
+ module ActionDispatch::Routing
2
+ class Mapper
3
+
4
+ def with_jindouyun options
5
+ init_dingding_routes options
6
+ end
7
+
8
+ private
9
+
10
+ def init_dingding_routes options
11
+ notifications_controller = options[:system_notifications] ? options[:system_notifications] : 'jindouyun/dingding/system_notifications'
12
+
13
+ scope :dingding do
14
+ resources :system_notifications, controller: notifications_controller, only: [:create]
15
+ end
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module Jindouyun
2
+ class Setting
3
+
4
+ def self.setting
5
+ @dingding_setting
6
+ end
7
+
8
+ # {
9
+ # suite_id: '',
10
+ # sign_token: '',
11
+ # encoding_aes_key: ''
12
+ # }
13
+ def self.setting= settings
14
+ @dingding_setting = settings
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Jindouyun
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jindouyun
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - FENG Zhichao
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aescrypt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Dingding (Dingtalk)
70
+ email:
71
+ - flankerfc@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - app/controllers/jindouyun/base_controller.rb
85
+ - app/controllers/jindouyun/dingding/encrypted_message_controller.rb
86
+ - app/controllers/jindouyun/dingding/system_notifications_controller.rb
87
+ - bin/console
88
+ - bin/setup
89
+ - jindouyun.gemspec
90
+ - lib/jindouyun.rb
91
+ - lib/jindouyun/logger.rb
92
+ - lib/jindouyun/message_encryptor.rb
93
+ - lib/jindouyun/rails.rb
94
+ - lib/jindouyun/rails/routes.rb
95
+ - lib/jindouyun/setting.rb
96
+ - lib/jindouyun/version.rb
97
+ homepage: https://github.com/flanker/jindouyun
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.6.14
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Dingding (Dingtalk)
121
+ test_files: []