alexa_verifier 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: dc08649cb9ee69a9726eaa93c234b58828b26e02
4
+ data.tar.gz: 09bba4949ec077e8d9041b2101ab50b78d61b8ba
5
+ SHA512:
6
+ metadata.gz: bb68dc5d02e75887efa2771e71c94871e57b10571a0a4b27d6b348d7c775f2c89ea9226819c0579abae34747c036f2900d85f9c450a26838abc6bbe37861862e
7
+ data.tar.gz: ed501217e4dc3a400a2b2ed4b04058be86894a05d4709c82c00322911ff9c6eac8812f5b4f93752c0bd77efb18f8288cc7973e2dcbcc6ac3dba5f56b1b4a466f
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in alexa_verifier.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Christopher
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,2 @@
1
+ # alexa_verifier
2
+ Rubygem to verify requests sent to an Alexa skill are sent from Amazon
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,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'alexa_verifier'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "alexa_verifier"
9
+ spec.version = AlexaVerifier::VERSION
10
+ spec.authors = ["Christopher Mullins"]
11
+ spec.email = ["chris@sidoh.org"]
12
+
13
+ spec.summary = %q{Verifies requests sent to an Alexa skill are sent from Amazon}
14
+ spec.homepage = "http://www.github.com/sidoh/alexa_verifier"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "curb", "~> 0.7.16"
26
+ spec.add_development_dependency "webmock"
27
+ end
@@ -0,0 +1,122 @@
1
+ require 'net/http'
2
+ require 'openssl'
3
+ require 'base64'
4
+ require 'time'
5
+ require 'json'
6
+
7
+ class AlexaVerifier
8
+ VERSION = '0.1.0'
9
+
10
+ class VerificationError < StandardError; end
11
+
12
+ DEFAULT_TIMESTAMP_TOLERANCE = 150
13
+
14
+ VALID_CERT_HOSTNAME = 's3.amazonaws.com'
15
+ VALID_CERT_PATH_START = '/echo.api/'
16
+ VALID_CERT_PORT = 443
17
+
18
+ class Builder
19
+ attr_accessor :verify_signatures, :verify_timestamps, :timestamp_tolerance
20
+
21
+ def initialize
22
+ @verify_signatures = true
23
+ @verify_timestamps = true
24
+ @timestamp_tolerance = DEFAULT_TIMESTAMP_TOLERANCE
25
+ end
26
+
27
+ def create
28
+ AlexaVerifier.new(verify_signatures, verify_timestamps, timestamp_tolerance)
29
+ end
30
+ end
31
+
32
+ def self.build(&block)
33
+ builder = Builder.new
34
+ block.call(builder)
35
+ builder.create
36
+ end
37
+
38
+ def initialize(verify_signatures = true, verify_timestamps = true, timestamp_tolerance = DEFAULT_TIMESTAMP_TOLERANCE)
39
+ @cert_cache = {}
40
+ @verify_signatures = verify_signatures
41
+ @verify_timestamps = verify_timestamps
42
+ @timestamp_tolerance = timestamp_tolerance
43
+ end
44
+
45
+ def verify!(cert_url, signature, request)
46
+ verify_timestamp!(request) if @verify_timestamps
47
+
48
+ if @verify_signatures
49
+ x509_cert = cert(cert_url)
50
+ public_key = x509_cert.public_key
51
+
52
+ unless public_key.verify(hash_type, Base64.decode64(signature), request)
53
+ raise VerificationError.new, 'Signature does not match!'
54
+ end
55
+ end
56
+
57
+ true
58
+ end
59
+
60
+ private
61
+
62
+ def verify_timestamp!(request)
63
+ request_json = JSON.parse(request)
64
+
65
+ if request_json['request'].nil? or request_json['request']['timestamp'].nil?
66
+ raise VerificationError.new, 'Timestamp field not present in request'
67
+ end
68
+
69
+ unless Time.parse(request_json['request']['timestamp']) >= (Time.now - @timestamp_tolerance)
70
+ raise VerificationError.new, "Request is from more than #{@timestamp_tolerance} seconds ago"
71
+ end
72
+ end
73
+
74
+ def hash_type
75
+ OpenSSL::Digest::SHA1.new
76
+ end
77
+
78
+ def cert(cert_url)
79
+ if @cert_cache[cert_url]
80
+ @cert_cache[cert_url]
81
+ else
82
+ cert_uri = URI.parse(cert_url)
83
+ validate_cert_uri!(cert_uri)
84
+ @cert_cache[cert_url] = OpenSSL::X509::Certificate.new(download_cert(cert_uri))
85
+ end
86
+ end
87
+
88
+ def download_cert(uri)
89
+ http = Net::HTTP.new(uri.host, uri.port)
90
+ http.use_ssl = true
91
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
92
+ http.start
93
+
94
+ response = http.request(Net::HTTP::Get.new(uri.request_uri))
95
+
96
+ http.finish
97
+
98
+ if response.code == '200'
99
+ response.body
100
+ else
101
+ raise VerificationError, "Failed to download certificate at: #{uri}. Response code: #{response.code}, error: #{response.body}"
102
+ end
103
+ end
104
+
105
+ def validate_cert_uri!(cert_uri)
106
+ unless cert_uri.scheme == 'https'
107
+ raise VerificationError, "Certificate URI MUST be https: #{cert_uri}"
108
+ end
109
+
110
+ unless cert_uri.port == VALID_CERT_PORT
111
+ raise VerificationError, "Certificate URI port MUST be #{VALID_CERT_PORT}, was: #{cert_uri.port}"
112
+ end
113
+
114
+ unless cert_uri.host == VALID_CERT_HOSTNAME
115
+ raise VerificationError, "Certificate URI hostname must be #{VALID_CERT_HOSTNAME}: #{cert_uri}"
116
+ end
117
+
118
+ unless cert_uri.request_uri.start_with?(VALID_CERT_PATH_START)
119
+ raise VerificationError, "Certificate URI path must start with #{VALID_CERT_PATH_START}: #{cert_uri}"
120
+ end
121
+ end
122
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alexa_verifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Mullins
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-03 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: curb
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.7.16
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.7.16
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - chris@sidoh.org
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - .travis.yml
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - alexa_verifier.gemspec
98
+ - lib/alexa_verifier.rb
99
+ homepage: http://www.github.com/sidoh/alexa_verifier
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.0.14
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Verifies requests sent to an Alexa skill are sent from Amazon
123
+ test_files: []