A long-lived project that still receives updates
Verify ChatWork webhook signature
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

ChatworkWebhookVerify

Verify ChatWork webhook signature

Gem Version Build Status Maintainability Coverage Status

Installation

Add this line to your application's Gemfile:

gem 'chatwork_webhook_verify'

And then execute:

$ bundle

Or install it yourself as:

$ gem install chatwork_webhook_verify

Basic usage

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

or

ChatworkWebhookVerify.verify!(token: token, body: body, signature: signature)
#=> 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

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

Example 2

# app/controllers/webhook_controller.rb
class WebhookController < ApplicationController
  before_action :verify_chatwork_webhook_signature_with_own_token!
  
  def verify_chatwork_webhook_signature_with_own_token!
    verify_chatwork_webhook_signature!("another_token")
  end
end

for Sinatra

# app.rb
class App < Sinatra::Base
  before "/webhook" do
    token     = ENV["CHATWORK_WEBHOOK_TOKEN"]
    body      = request.body.read
    signature = request.env["HTTP_X_CHATWORKWEBHOOKSIGNATURE"]

    ChatworkWebhookVerify.verify!(token: token, body: body, signature: signature)
  end

  post "/webhook" do
    "ok"
  end
end

Configuration

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.