项目作者: sue445

项目描述 :
Verify ChatWork webhook signature
高级语言: Ruby
项目地址: git://github.com/sue445/chatwork_webhook_verify.git
创建时间: 2018-01-23T14:25:01Z
项目社区:https://github.com/sue445/chatwork_webhook_verify

开源协议:MIT License

下载


ChatworkWebhookVerify

Verify ChatWork webhook signature

Gem Version
test
Maintainability
Coverage Status

Installation

Add this line to your application’s Gemfile:

  1. gem 'chatwork_webhook_verify'

And then execute:

  1. $ bundle

Or install it yourself as:

  1. $ gem install chatwork_webhook_verify

Basic usage

  1. ChatworkWebhookVerify.verify?(token: token, body: body, signature: signature)
  2. #=> true | false

or

  1. ChatworkWebhookVerify.verify!(token: token, body: body, signature: signature)
  2. #=> raise ChatworkWebhookVerify::InvalidSignatureError if signature is invalid
  • token : webhook token (default: ChatworkWebhookVerify.config.token)
    • Either token or ChatworkWebhookVerify.config.token is required
  • body : request body from webhook
  • signature : chatwork_webhook_signature (query string) or X-ChatWorkWebhookSignature (request header)

for Rails

call verify_chatwork_webhook_signature! in your controller

Example 1

  1. # config/initializers/chatwork_webhook_verify.rb
  2. ChatworkWebhookVerify.config.token = ENV["CHATWORK_WEBHOOK_TOKEN"]
  1. # app/controllers/webhook_controller.rb
  2. class WebhookController < ApplicationController
  3. # `ChatworkWebhookVerify.config.token` is used
  4. before_action :verify_chatwork_webhook_signature!
  5. end

Example 2

  1. # app/controllers/webhook_controller.rb
  2. class WebhookController < ApplicationController
  3. before_action :verify_chatwork_webhook_signature_with_own_token!
  4. def verify_chatwork_webhook_signature_with_own_token!
  5. verify_chatwork_webhook_signature!("another_token")
  6. end
  7. end

for Sinatra

  1. # app.rb
  2. class App < Sinatra::Base
  3. before "/webhook" do
  4. token = ENV["CHATWORK_WEBHOOK_TOKEN"]
  5. body = request.body.read
  6. signature = request.env["HTTP_X_CHATWORKWEBHOOKSIGNATURE"]
  7. ChatworkWebhookVerify.verify!(token: token, body: body, signature: signature)
  8. end
  9. post "/webhook" do
  10. "ok"
  11. end
  12. end

Configuration

  1. ChatworkWebhookVerify.config.token = ENV["CHATWORK_WEBHOOK_TOKEN"]
  • token : default webhook token

Contributing

Contribution directions go here.

License

The gem is available as open source under the terms of the MIT License.