rack-verify-line-bot 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +42 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rack-verify-line-bot.rb +1 -0
- data/lib/rack/verify/line/bot.rb +93 -0
- data/lib/rack/verify/line/bot/version.rb +9 -0
- data/rack-verify-line-bot.gemspec +38 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fc2410348e377e5252f08922b141238c78d192ef
|
4
|
+
data.tar.gz: 6bc8dff06c9deb310b70e5e022c5f883d845ad78
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 75bb63b561c7a79a0b6166bad5c465075ef99e9b3b8a33c7fcab1ef60d3a97bf7567878ddb5d9578a4427b283556c84e7e86855670c8e13084bcd2e74ccce946
|
7
|
+
data.tar.gz: 8212a9ad11fe3311e105fe455da399a6928107a44fcef518031648412dac2ffca2c3a96d77f5c3cabf736e8811c920a063ec27c697cac98879ce8b1cf972b087
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 dayflower
|
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,42 @@
|
|
1
|
+
# Rack::Verify::Line::Bot
|
2
|
+
|
3
|
+
[![CircleCI](https://circleci.com/gh/dayflower/rack-verify-line-bot.svg?style=svg)](https://circleci.com/gh/dayflower/rack-verify-line-bot)
|
4
|
+
|
5
|
+
Rack middleware which verifies signature of LINE Bot's webhook requests to ensure the hook is invoked from LINE.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
# config.ru
|
11
|
+
require 'rack/verify/line/bot'
|
12
|
+
|
13
|
+
use Rack::Verify::Line::Bot :secret => ENV['SECRET'], # channel secret (mandatory)
|
14
|
+
:path => '/hook' # path of webhook URI
|
15
|
+
```
|
16
|
+
|
17
|
+
### Options
|
18
|
+
|
19
|
+
- `:secret`
|
20
|
+
Specify channel secret. This option is mandatory.
|
21
|
+
- `:path`
|
22
|
+
Specify path component of webhook URL. If omitted, every POST requests will be examined.
|
23
|
+
|
24
|
+
## Installation
|
25
|
+
|
26
|
+
Add this line to your application's Gemfile:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
gem 'rack-verify-line-bot'
|
30
|
+
```
|
31
|
+
|
32
|
+
And then execute:
|
33
|
+
|
34
|
+
$ bundle
|
35
|
+
|
36
|
+
Or install it yourself as:
|
37
|
+
|
38
|
+
$ gem install rack-verify-line-bot
|
39
|
+
|
40
|
+
## License
|
41
|
+
|
42
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rack/verify/line/bot"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rack/verify/line/bot'
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require "rack/verify/line/bot/version"
|
2
|
+
require "openssl"
|
3
|
+
require "base64"
|
4
|
+
|
5
|
+
module Rack
|
6
|
+
module Verify
|
7
|
+
module Line
|
8
|
+
class Bot
|
9
|
+
def initialize(app, options = {})
|
10
|
+
@app = app
|
11
|
+
@options = options
|
12
|
+
|
13
|
+
@secret = @options[:secret]
|
14
|
+
unless @secret
|
15
|
+
raise 'missing secret parameter for ' + self.class.name
|
16
|
+
end
|
17
|
+
|
18
|
+
@buffer_size = @options[:buffer_size] || 65536
|
19
|
+
end
|
20
|
+
|
21
|
+
def call(env)
|
22
|
+
if need_verification?(env)
|
23
|
+
unless verify_signature(env)
|
24
|
+
return [
|
25
|
+
400,
|
26
|
+
{
|
27
|
+
'Content-Type' => 'text/plain',
|
28
|
+
'Content-Length' => '11',
|
29
|
+
},
|
30
|
+
[ 'Bad Request' ]
|
31
|
+
]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
@app.call(env)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
SIGNATURE_HEADER = 'HTTP_X_LINE_SIGNATURE'
|
41
|
+
|
42
|
+
def need_verification?(env)
|
43
|
+
return false unless env['REQUEST_METHOD'].upcase == 'POST'
|
44
|
+
|
45
|
+
if @options[:path]
|
46
|
+
return false unless env['PATH_INFO'] == @options[:path]
|
47
|
+
end
|
48
|
+
|
49
|
+
return true
|
50
|
+
end
|
51
|
+
|
52
|
+
def verify_signature(env)
|
53
|
+
given_sig = env[SIGNATURE_HEADER]
|
54
|
+
return false unless given_sig
|
55
|
+
|
56
|
+
hmac = OpenSSL::HMAC.new(@secret, OpenSSL::Digest::SHA256.new)
|
57
|
+
update_hmac(hmac, env['rack.input'])
|
58
|
+
sig = Base64.strict_encode64(hmac.digest)
|
59
|
+
|
60
|
+
secure_compare(given_sig, sig)
|
61
|
+
end
|
62
|
+
|
63
|
+
# from https://github.com/rails/rails/blob/master/activesupport/lib/active_support/security_utils.rb
|
64
|
+
# Rails is released under MIT license
|
65
|
+
def secure_compare(a, b)
|
66
|
+
return false unless a.bytesize == b.bytesize
|
67
|
+
|
68
|
+
l = a.unpack "C#{a.bytesize}"
|
69
|
+
|
70
|
+
res = 0
|
71
|
+
b.each_byte { |byte| res |= byte ^ l.shift }
|
72
|
+
res == 0
|
73
|
+
end
|
74
|
+
|
75
|
+
def update_hmac(hmac, handle)
|
76
|
+
return unless handle
|
77
|
+
|
78
|
+
handle.rewind
|
79
|
+
begin
|
80
|
+
buffer = ''
|
81
|
+
while handle.read(@buffer_size, buffer)
|
82
|
+
hmac.update(buffer)
|
83
|
+
end
|
84
|
+
|
85
|
+
hmac
|
86
|
+
ensure
|
87
|
+
handle.rewind
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rack/verify/line/bot/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rack-verify-line-bot"
|
8
|
+
spec.version = Rack::Verify::Line::Bot::VERSION
|
9
|
+
spec.authors = ["dayflower"]
|
10
|
+
spec.email = ["daydream.trippers@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Rack middleware for verifying signature of LINE Bot callback}
|
13
|
+
spec.description = %q{Rack middleware for verifying signature of LINE Bot callback}
|
14
|
+
spec.homepage = "https://github.com/dayflower/rack-verify-line-bot"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = %w[
|
18
|
+
Gemfile
|
19
|
+
LICENSE.txt
|
20
|
+
README.md
|
21
|
+
Rakefile
|
22
|
+
bin/console
|
23
|
+
bin/setup
|
24
|
+
lib/rack-verify-line-bot.rb
|
25
|
+
lib/rack/verify/line/bot.rb
|
26
|
+
lib/rack/verify/line/bot/version.rb
|
27
|
+
rack-verify-line-bot.gemspec
|
28
|
+
]
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "test-unit", ">= 3.0"
|
36
|
+
spec.add_development_dependency "rack", "~> 1"
|
37
|
+
spec.add_development_dependency "rack-test", "~> 0.6"
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-verify-line-bot
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- dayflower
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: test-unit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rack
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rack-test
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.6'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.6'
|
83
|
+
description: Rack middleware for verifying signature of LINE Bot callback
|
84
|
+
email:
|
85
|
+
- daydream.trippers@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- Gemfile
|
91
|
+
- LICENSE.txt
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- bin/console
|
95
|
+
- bin/setup
|
96
|
+
- lib/rack-verify-line-bot.rb
|
97
|
+
- lib/rack/verify/line/bot.rb
|
98
|
+
- lib/rack/verify/line/bot/version.rb
|
99
|
+
- rack-verify-line-bot.gemspec
|
100
|
+
homepage: https://github.com/dayflower/rack-verify-line-bot
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.0.14.1
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: Rack middleware for verifying signature of LINE Bot callback
|
124
|
+
test_files: []
|