chatwork_webhook_verify 0.1.0

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.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +79 -0
  4. data/Rakefile +19 -0
  5. data/lib/chatwork_webhook_verify.rb +54 -0
  6. data/lib/chatwork_webhook_verify/configuration.rb +6 -0
  7. data/lib/chatwork_webhook_verify/controller_extension.rb +16 -0
  8. data/lib/chatwork_webhook_verify/railtie.rb +10 -0
  9. data/lib/chatwork_webhook_verify/version.rb +3 -0
  10. data/spec/chatwork_webhook_verify/controller_extension_spec.rb +37 -0
  11. data/spec/chatwork_webhook_verify_spec.rb +40 -0
  12. data/spec/dummy/Rakefile +6 -0
  13. data/spec/dummy/app/assets/config/manifest.js +1 -0
  14. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  15. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  16. data/spec/dummy/app/controllers/application_controller.rb +2 -0
  17. data/spec/dummy/app/controllers/webhook_controller.rb +7 -0
  18. data/spec/dummy/app/jobs/application_job.rb +2 -0
  19. data/spec/dummy/bin/bundle +3 -0
  20. data/spec/dummy/bin/rails +4 -0
  21. data/spec/dummy/bin/rake +4 -0
  22. data/spec/dummy/bin/setup +25 -0
  23. data/spec/dummy/bin/update +25 -0
  24. data/spec/dummy/config.ru +5 -0
  25. data/spec/dummy/config/application.rb +35 -0
  26. data/spec/dummy/config/boot.rb +5 -0
  27. data/spec/dummy/config/environment.rb +5 -0
  28. data/spec/dummy/config/environments/development.rb +40 -0
  29. data/spec/dummy/config/environments/production.rb +68 -0
  30. data/spec/dummy/config/environments/test.rb +36 -0
  31. data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
  32. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  33. data/spec/dummy/config/initializers/cors.rb +16 -0
  34. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  35. data/spec/dummy/config/initializers/inflections.rb +16 -0
  36. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  37. data/spec/dummy/config/initializers/wrap_parameters.rb +9 -0
  38. data/spec/dummy/config/locales/en.yml +33 -0
  39. data/spec/dummy/config/puma.rb +56 -0
  40. data/spec/dummy/config/routes.rb +3 -0
  41. data/spec/dummy/config/secrets.yml +2 -0
  42. data/spec/dummy/config/spring.rb +6 -0
  43. data/spec/dummy/log/development.log +0 -0
  44. data/spec/dummy/log/test.log +122 -0
  45. data/spec/rails_helper.rb +45 -0
  46. data/spec/spec_helper.rb +114 -0
  47. metadata +252 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9a1055ca5bbcbe6d7532aabd3c400770ed6672c3957c9beafa8ec0f799d03200
4
+ data.tar.gz: a9803300c09d7c8c75d8a09189d9bf92d648b2a78558c0cf952dc2884c3914c9
5
+ SHA512:
6
+ metadata.gz: afa01d6979dd651823a3181faea3784a07102dabd912d0ec1ed27194e26893bf9a1c7435c972035bb53eb48f39fb47ee57969e36ac96855c2393d08f9c74c66f
7
+ data.tar.gz: 07c9aafd537388d0a617049d123c95c4265431cd9b1ee766c8aa4bc373616775d53dbe153e56b7cd532759b811f968e793444da060e8d1da1b06b15058712eb0
@@ -0,0 +1,20 @@
1
+ Copyright 2018 sue445
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.
@@ -0,0 +1,79 @@
1
+ # ChatworkWebhookVerify
2
+ Verify ChatWork webhook signature
3
+
4
+ [![Build Status](https://travis-ci.org/sue445/chatwork_webhook_verify.svg?branch=master)](https://travis-ci.org/sue445/chatwork_webhook_verify)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/d7ea5e910c29987c7c0e/maintainability)](https://codeclimate.com/github/sue445/chatwork_webhook_verify/maintainability)
6
+ [![Coverage Status](https://coveralls.io/repos/github/sue445/chatwork_webhook_verify/badge.svg?branch=master)](https://coveralls.io/github/sue445/chatwork_webhook_verify?branch=master)
7
+ [![Dependency Status](https://gemnasium.com/badges/github.com/sue445/chatwork_webhook_verify.svg)](https://gemnasium.com/github.com/sue445/chatwork_webhook_verify)
8
+
9
+ ## Installation
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'chatwork_webhook_verify'
14
+ ```
15
+
16
+ And then execute:
17
+ ```bash
18
+ $ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+ ```bash
23
+ $ gem install chatwork_webhook_verify
24
+ ```
25
+
26
+ ## Basic usage
27
+ ```ruby
28
+ ChatworkWebhookVerify.verify?(token: token, body: body, signature: signature)
29
+ #=> true | false
30
+ ```
31
+
32
+ or
33
+
34
+ ```ruby
35
+ ChatworkWebhookVerify.verify!(token: token, body: body, signature: signature)
36
+ #=> raise ChatworkWebhookVerify::InvalidSignatureError if signature is invalid
37
+ ```
38
+
39
+ * `token` : webhook token (default: `ChatworkWebhookVerify.config.token`)
40
+ * Either `token` or `ChatworkWebhookVerify.config.token` is required
41
+ * `body` : request body from webhook
42
+ * `signature` : `chatwork_webhook_signature` (query string) or `X-ChatWorkWebhookSignature` (request header)
43
+
44
+ ## for Rails
45
+ call `verify_chatwork_webhook_signature!` in your controller
46
+
47
+ ### Example 1
48
+
49
+ ```ruby
50
+ class WebhookController < ApplicationController
51
+ # `ChatworkWebhookVerify.config.token` is used
52
+ before_action :verify_chatwork_webhook_signature!
53
+ end
54
+ ```
55
+
56
+ ### Example 2
57
+
58
+ ```ruby
59
+ class WebhookController < ApplicationController
60
+ before_action :verify_chatwork_webhook_signature_with_own_token!
61
+
62
+ def verify_chatwork_webhook_signature_with_own_token!
63
+ verify_chatwork_webhook_signature!("another_token")
64
+ end
65
+ end
66
+ ```
67
+
68
+ ## Configuration
69
+ ```ruby
70
+ ChatworkWebhookVerify.config.token = ENV["CHATWORK_WEBHOOK_TOKEN"]
71
+ ```
72
+
73
+ * `token` : default webhook token
74
+
75
+ ## Contributing
76
+ Contribution directions go here.
77
+
78
+ ## License
79
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,19 @@
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 = 'ChatworkWebhookVerify'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ load 'rails/tasks/statistics.rake'
18
+
19
+ require 'bundler/gem_tasks'
@@ -0,0 +1,54 @@
1
+ require "chatwork_webhook_verify/configuration"
2
+ require "chatwork_webhook_verify/railtie" if defined?(Rails)
3
+
4
+ module ChatworkWebhookVerify
5
+ require "base64"
6
+ require "openssl"
7
+
8
+ class InvalidSignatureError < StandardError; end
9
+
10
+ # Whether signature is valid
11
+ #
12
+ # @param token [String] webhook token (default: `ChatworkWebhookVerify.config.token`)
13
+ # @param body [String] request body
14
+ # @param signature [String] chatwork_webhook_signature or X-ChatWorkWebhookSignature
15
+ #
16
+ # @return [Boolean]
17
+ #
18
+ # @note Either `token` or `ChatworkWebhookVerify.config.token` is required
19
+ def self.verify?(token: nil, body:, signature:)
20
+ token ||= config.token
21
+
22
+ raise ArgumentError, "Either token or ChatworkWebhookVerify.config.token is required" if !token || token.empty?
23
+ raise ArgumentError, "signature is required" if !signature || signature.empty?
24
+
25
+ generate_signature(token: token, body: body) == signature
26
+ end
27
+
28
+ # Whether signature is valid
29
+ #
30
+ # @param token [String] webhook token (default: `ChatworkWebhookVerify.config.token`)
31
+ # @param body [String] request body
32
+ # @param signature [String] chatwork_webhook_signature or X-ChatWorkWebhookSignature
33
+ #
34
+ # @raise [InvalidSignatureError] signature is invalid
35
+ #
36
+ # @note Either `token` or `ChatworkWebhookVerify.config.token` is required
37
+ def self.verify!(token: nil, body:, signature:)
38
+ raise InvalidSignatureError unless verify?(token: token, body: body, signature: signature)
39
+ end
40
+
41
+ # @param token [String] webhook token (default: `ChatworkWebhookVerify.config.token`)
42
+ # @param body [String] request body
43
+ #
44
+ # @return [String]
45
+ def self.generate_signature(token:, body:)
46
+ secret_key = Base64.decode64(token)
47
+ Base64.strict_encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new("sha256"), secret_key, body))
48
+ end
49
+
50
+ # @return [ChatworkWebhookVerify::Configuration]
51
+ def self.config
52
+ @config ||= Configuration.new
53
+ end
54
+ end
@@ -0,0 +1,6 @@
1
+ module ChatworkWebhookVerify
2
+ class Configuration
3
+ # @return [String] default webhook token
4
+ attr_accessor :token
5
+ end
6
+ end
@@ -0,0 +1,16 @@
1
+ module ChatworkWebhookVerify
2
+ module ControllerExtension
3
+ # @param token [String] webhook token (default: `ChatworkWebhookVerify.config.token`)
4
+ #
5
+ # @raise [ActionController::BadRequest] signature is invalid
6
+ def verify_chatwork_webhook_signature!(token = nil)
7
+ ChatworkWebhookVerify.verify!(
8
+ token: token,
9
+ body: request.env["rack.input"].read,
10
+ signature: request.headers["X-ChatWorkWebhookSignature"] || params[:chatwork_webhook_signature],
11
+ )
12
+ rescue ChatworkWebhookVerify::InvalidSignatureError, ::ArgumentError
13
+ raise ActionController::BadRequest, "signature is invalid"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ module ChatworkWebhookVerify
2
+ class Railtie < ::Rails::Railtie
3
+ initializer "chatwork_webhook_verify.controller_extension" do
4
+ ActiveSupport.on_load(:action_controller) do
5
+ require "chatwork_webhook_verify/controller_extension"
6
+ ActionController::Metal.include(ChatworkWebhookVerify::ControllerExtension)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module ChatworkWebhookVerify
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,37 @@
1
+ require "rails_helper"
2
+ require "chatwork_webhook_verify/controller_extension"
3
+
4
+ RSpec.describe ChatworkWebhookVerify::ControllerExtension, type: :request do
5
+ # NOTE: there are valid
6
+ let(:token) { "iY/hmitgwCBlc5DnjAZ8pyRG1HF0zFflfmKmtCDg1wk=" }
7
+ let(:body) { '{"webhook_setting_id":"581","webhook_event_type":"mention_to_me","webhook_event_time":1516629767,"webhook_event":{"from_account_id":2861671,"to_account_id":2739132,"room_id":93207172,"message_id":"1007287971738591232","body":"[rp aid=2739132 to=93207172-1007287785163345920] sue445\ntest","send_time":1516629766,"update_time":0}}' }
8
+ let(:signature) { "NTIUzbXiwLvM7C/MT3Kd75Lw1w2N5tyWtcEieKiqhY0=" }
9
+
10
+ describe "#verify_chatwork_webhook_signature!" do
11
+ subject do
12
+ post "/webhook", params: body, headers: headers
13
+ response
14
+ end
15
+
16
+ before do
17
+ allow(ChatworkWebhookVerify.config).to receive(:token) { token }
18
+ end
19
+
20
+ let(:headers) do
21
+ {
22
+ "Content-Type" => "application/json",
23
+ "X-ChatWorkWebhookSignature" => signature,
24
+ }
25
+ end
26
+
27
+ context "with valid signature" do
28
+ its(:status) { should eq 200 }
29
+ end
30
+
31
+ context "with invalid signature" do
32
+ let(:signature) { "AAAAAAAAA" }
33
+
34
+ it { expect { subject }.to raise_error ActionController::BadRequest }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,40 @@
1
+ RSpec.describe ChatworkWebhookVerify do
2
+ # NOTE: there are valid
3
+ let(:token) { "iY/hmitgwCBlc5DnjAZ8pyRG1HF0zFflfmKmtCDg1wk=" }
4
+ let(:body) { '{"webhook_setting_id":"581","webhook_event_type":"mention_to_me","webhook_event_time":1516629767,"webhook_event":{"from_account_id":2861671,"to_account_id":2739132,"room_id":93207172,"message_id":"1007287971738591232","body":"[rp aid=2739132 to=93207172-1007287785163345920] sue445\ntest","send_time":1516629766,"update_time":0}}' }
5
+ let(:signature) { "NTIUzbXiwLvM7C/MT3Kd75Lw1w2N5tyWtcEieKiqhY0=" }
6
+
7
+ describe ".verify?" do
8
+ subject { ChatworkWebhookVerify.verify?(token: token, body: body, signature: signature) }
9
+
10
+ context "with valid signature" do
11
+ it { should eq true }
12
+ end
13
+
14
+ context "with invalid signature" do
15
+ let(:signature) { "AAAAAAAAA" }
16
+
17
+ it { should eq false }
18
+ end
19
+ end
20
+
21
+ describe ".verify!" do
22
+ subject { ChatworkWebhookVerify.verify!(token: token, body: body, signature: signature) }
23
+
24
+ context "with valid signature" do
25
+ it { expect { subject }.not_to raise_error }
26
+ end
27
+
28
+ context "with invalid signature" do
29
+ let(:signature) { "AAAAAAAAA" }
30
+
31
+ it { expect { subject }.to raise_error ChatworkWebhookVerify::InvalidSignatureError }
32
+ end
33
+ end
34
+
35
+ describe ".generate_signature" do
36
+ subject { ChatworkWebhookVerify.generate_signature(token: token, body: body) }
37
+
38
+ it { should eq signature }
39
+ end
40
+ end
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require_relative 'config/application'
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets .css
@@ -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. JavaScript code in this file should be added after the last require_* statement.
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 other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::API
2
+ end
@@ -0,0 +1,7 @@
1
+ class WebhookController < ApplicationController
2
+ before_action :verify_chatwork_webhook_signature!
3
+
4
+ def test
5
+ render plain: "OK", status: 200
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationJob < ActiveJob::Base
2
+ end
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../config/application', __dir__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fileutils'
3
+ include FileUtils
4
+
5
+ # path to your application root.
6
+ APP_ROOT = File.expand_path('..', __dir__)
7
+
8
+ def system!(*args)
9
+ system(*args) || abort("\n== Command #{args} failed ==")
10
+ end
11
+
12
+ chdir APP_ROOT do
13
+ # This script is a starting point to setup your application.
14
+ # Add necessary setup steps to this file.
15
+
16
+ puts '== Installing dependencies =='
17
+ system! 'gem install bundler --conservative'
18
+ system('bundle check') || system!('bundle install')
19
+
20
+ puts "\n== Removing old logs and tempfiles =="
21
+ system! 'bin/rails log:clear tmp:clear'
22
+
23
+ puts "\n== Restarting application server =="
24
+ system! 'bin/rails restart'
25
+ end
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fileutils'
3
+ include FileUtils
4
+
5
+ # path to your application root.
6
+ APP_ROOT = File.expand_path('..', __dir__)
7
+
8
+ def system!(*args)
9
+ system(*args) || abort("\n== Command #{args} failed ==")
10
+ end
11
+
12
+ chdir APP_ROOT do
13
+ # This script is a way to update your development environment automatically.
14
+ # Add necessary update steps to this file.
15
+
16
+ puts '== Installing dependencies =='
17
+ system! 'gem install bundler --conservative'
18
+ system('bundle check') || system!('bundle install')
19
+
20
+ puts "\n== Removing old logs and tempfiles =="
21
+ system! 'bin/rails log:clear tmp:clear'
22
+
23
+ puts "\n== Restarting application server =="
24
+ system! 'bin/rails restart'
25
+ end
@@ -0,0 +1,5 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require_relative 'config/environment'
4
+
5
+ run Rails.application
@@ -0,0 +1,35 @@
1
+ require_relative 'boot'
2
+
3
+ require "rails"
4
+ # Pick the frameworks you want:
5
+ # require "active_model/railtie"
6
+ # require "active_job/railtie"
7
+ # require "active_record/railtie"
8
+ # require "active_storage/engine"
9
+ require "action_controller/railtie"
10
+ # require "action_mailer/railtie"
11
+ # require "action_view/railtie"
12
+ # require "action_cable/engine"
13
+ # require "sprockets/railtie"
14
+ # require "rails/test_unit/railtie"
15
+
16
+ Bundler.require(*Rails.groups)
17
+ require "chatwork_webhook_verify"
18
+ require "chatwork_webhook_verify/railtie"
19
+
20
+ module Dummy
21
+ class Application < Rails::Application
22
+ # Initialize configuration defaults for originally generated Rails version.
23
+ # config.load_defaults 5.2
24
+
25
+ # Settings in config/environments/* take precedence over those specified here.
26
+ # Application configuration should go into files in config/initializers
27
+ # -- all .rb files in that directory are automatically loaded.
28
+
29
+ # Only loads a smaller set of middleware suitable for API only apps.
30
+ # Middleware like session, flash, cookies can be added back manually.
31
+ # Skip views, helpers and assets when generating a new resource.
32
+ config.api_only = true
33
+ end
34
+ end
35
+