authograph 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2fa21ab9df699ee62947bfb58d37e05d55ae3eef
4
+ data.tar.gz: 64eacefff4e51281f71126ff9d89faa2ce0ad4c8
5
+ SHA512:
6
+ metadata.gz: 498b424873a5b7d2aaefc8f0d4c2990bde82e0219cbccb6cffc039b8549b9370bc696c805e7b6825669b605d25a69fe6ed4319064d0c593b33fbd47ee63730ba
7
+ data.tar.gz: 7c08538b13cbe9ab33ef66f9df03721b195825950126868252e49be754d6dd214182c7d7253fae3fb7cdccfd7787d925969522c662ea029cf2ff4d2b3a8bb3c5
@@ -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
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.4
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at iobaixas@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in authograph.gemspec
4
+ gemspec
@@ -0,0 +1,10 @@
1
+ watch ("Guardfile") do
2
+ UI.info "Exiting because Guard must be restarted for changes to take effect"
3
+ exit 0
4
+ end
5
+
6
+ guard :rspec, cmd: 'bundle exec rspec' do
7
+ watch(%r{^spec/.+_spec\.rb$})
8
+ watch(%r{^lib/authograph/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
9
+ watch('spec/spec_helper.rb') { "spec" }
10
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ignacio Baixas
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.
@@ -0,0 +1,93 @@
1
+ # Authograph
2
+
3
+ Flexible HTTP request HMAC signing and validation utility.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'authograph'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install authograph
20
+
21
+ ## Usage
22
+
23
+ To sign a request just will first need to generate a new secret key
24
+
25
+ ```ruby
26
+ my_secret = SecureRandom.base64(64)
27
+ ```
28
+
29
+ Now that you have a secret key, create a new instance of the signer
30
+
31
+ ```ruby
32
+ signer = Authograph.signer # initialize a time signer with default options
33
+ ```
34
+
35
+ And the call `sign` passing the request you need to sign and the secret
36
+
37
+ ```ruby
38
+ signer.sign(my_request, my_secret) # this will add the X-Signature header to the request
39
+ ```
40
+
41
+ Yo can later validate the request by using `authentic?`
42
+
43
+ ```ruby
44
+ signer.authentic?(my_request, my_secret) # this will check the signature and the date by default
45
+ ```
46
+
47
+
48
+ ### Signer options
49
+
50
+ **IMPORTANT** Remember to always configure both the signer-signer and the validator-signer using the same paremeters.
51
+
52
+ The following parameters are available when calling `Authograph.signer`:
53
+
54
+ * `digest`: which digest algorithm to use (`'sha384'` by default).
55
+ * `header`: header key to store signature in (`'X-Signature'` by default).
56
+ * `sign_headers`: array of additional headers to include in the signing process. (`[]` by default).
57
+ * `sign_date`: whether to include and validate the date (`true` by default).
58
+ * `date_header`: header key to store date in (`'X-Date'` by default).
59
+ * `date_max_skew`: maximum difference (in secs) between request time and validaton (`'600'` by default).
60
+
61
+
62
+ ### Generated signature structure
63
+
64
+ The signature is generated by building a canonical payload with the following structure:
65
+
66
+ ```
67
+ <request method (uppercase), ex: GET>
68
+ <request full path, ex: /some/path?sure>
69
+ <content type, ex: application/json>
70
+ <body md5 as base64>
71
+ <header #1 content>
72
+ <header #2 content>
73
+ ...
74
+ ```
75
+
76
+ And then applying the HMAC-SHA384 hashing function to it to get the signature. The signature is then written to the request header using the following format:
77
+
78
+ ```
79
+ HMAC-SHA384 <base64(signature)>
80
+ ```
81
+
82
+ When using date validation an additiona `X-Date` header is added to the request.
83
+
84
+
85
+ ## Contributing
86
+
87
+ Bug reports and pull requests are welcome on GitHub at https://github.com/SurBTC/authograph. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
88
+
89
+
90
+ ## License
91
+
92
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
93
+
@@ -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,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'authograph/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "authograph"
8
+ spec.version = Authograph::VERSION
9
+ spec.authors = ["Ignacio Baixas"]
10
+ spec.email = ["ignacio@platan.us"]
11
+
12
+ spec.summary = "Flexible HTTP request HMAC signing and validation"
13
+ spec.description = "
14
+ HTTP request signing and validation library with support for header signing and multiple backends.
15
+ "
16
+ spec.homepage = "https://github.com/SurBTC/authograph"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.12"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "guard", "~> 2.14"
28
+ spec.add_development_dependency "guard-rspec", "~> 4.7"
29
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "authograph"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ require "authograph/version"
2
+ require "authograph/adapters/base"
3
+ require "authograph/signer"
4
+
5
+ module Authograph
6
+ def self.signer(*_args)
7
+ Signer.new(*_args)
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Authograph::Adapters
2
+ class Base
3
+ # nothing for now
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+ module Authograph::Adapters
2
+ class Faraday < Base
3
+ def initialize(_request)
4
+ @request = _request
5
+ end
6
+
7
+ def get_header(_header)
8
+ @request.headers[_header]
9
+ end
10
+
11
+ def set_header(_header, _value)
12
+ @request.headers[_header] = _value
13
+ end
14
+
15
+ def method
16
+ @request.method.to_s.upcase
17
+ end
18
+
19
+ def path
20
+ URI(@request.path).request_uri
21
+ end
22
+
23
+ def content_type
24
+ @request.headers['Content-Type'] || ''
25
+ end
26
+
27
+ def body
28
+ @request.body
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,34 @@
1
+ module Authograph::Adapters
2
+ class Http < Base
3
+ def initialize(_request)
4
+ @request = _request
5
+ end
6
+
7
+ def get_header(_header)
8
+ @request[_header]
9
+ end
10
+
11
+ def set_header(_header, _value)
12
+ @request[_header] = _value
13
+ end
14
+
15
+ def method
16
+ @request.method.to_s.upcase
17
+ end
18
+
19
+ def path
20
+ @request.path
21
+ end
22
+
23
+ def content_type
24
+ @request['Content-Type'] || ''
25
+ end
26
+
27
+ def body
28
+ return '' unless @request.body_stream
29
+ data = @request.body_stream.read
30
+ @request.body_stream.rewind
31
+ data
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,40 @@
1
+ module Authograph::Adapters
2
+ class Rack < Base
3
+ def initialize(_request)
4
+ @request = _request
5
+ end
6
+
7
+ def get_header(_header)
8
+ @request.env[normalize_header(_header)]
9
+ end
10
+
11
+ def set_header(_header, _value)
12
+ @request.env[normalize_header(_header)] = _value
13
+ end
14
+
15
+ def method
16
+ @request.request_method.upcase
17
+ end
18
+
19
+ def path
20
+ @request.fullpath
21
+ end
22
+
23
+ def content_type
24
+ @request.content_type
25
+ end
26
+
27
+ def body
28
+ return '' unless @request.body
29
+ data = @request.body.read
30
+ @request.body.rewind
31
+ data
32
+ end
33
+
34
+ private
35
+
36
+ def normalize_header(_header)
37
+ 'HTTP_' + _header.underscore.upcase
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,108 @@
1
+ module Authograph
2
+ class Signer
3
+ DEFAULT_SIGN_HEADER = 'X-Signature'
4
+ DEFAULT_DATE_HEADER = 'X-Date'
5
+
6
+ def initialize(
7
+ digest: 'sha384',
8
+ header: DEFAULT_SIGN_HEADER,
9
+ sign_headers: [],
10
+ sign_date: true,
11
+ date_header: DEFAULT_DATE_HEADER,
12
+ date_max_skew: 600
13
+ )
14
+ @digest = digest
15
+ @header = header
16
+ @sign_headers = sign_headers
17
+
18
+ @sign_date = sign_date
19
+ @date_header = date_header
20
+ @date_max_skew = date_max_skew
21
+ @sign_headers << date_header if sign_date # ensure date header is signed too
22
+ end
23
+
24
+ def sign(_request, _key_secret)
25
+ _request = adapt _request
26
+
27
+ set_request_date(_request) if @sign_date
28
+ # TODO: set_hashed_content to discard invalid signatures before checking content?
29
+ set_request_authorization(_request, _key_secret)
30
+ end
31
+
32
+ def authentic?(_request, _key_secret)
33
+ _request = adapt _request
34
+
35
+ return false if !signatures_match? _request, _key_secret
36
+ return false if @sign_date && !request_within_time_window?(_request)
37
+ true
38
+ end
39
+
40
+ private
41
+
42
+ def adapt(_request) # rubocop:disable Metrics/MethodLength
43
+ return _request if _request.is_a? Adapters::Base
44
+
45
+ case _request.class.to_s
46
+ when 'ActionDispatch::Request'
47
+ require 'authograph/adapters/rack'
48
+ Adapters::Rack.new Rack::Request.new(_request.env)
49
+ when 'Rack::Request'
50
+ require 'authograph/adapters/rack'
51
+ Adapters::Rack.new _request
52
+ when /^Net::HTTP::.*/
53
+ require 'authograph/adapters/http'
54
+ Adapters::Http.new _request
55
+ when 'Faraday::Request'
56
+ require 'authograph/adapters/faraday'
57
+ Adapters::Faraday.new _request
58
+ else
59
+ raise ArgumentError, 'the given request type is not supported'
60
+ end
61
+ end
62
+
63
+ def set_request_date(_request)
64
+ _request.set_header @date_header, Time.now.utc.httpdate
65
+ end
66
+
67
+ def set_request_authorization(_request, _key_secret)
68
+ _request.set_header @header, calc_signature(_request, _key_secret)
69
+ end
70
+
71
+ def signatures_match?(_request, _key_secret)
72
+ calc_signature(_request, _key_secret) == _request.get_header(@header)
73
+ end
74
+
75
+ def request_within_time_window?(_request)
76
+ request_date = Time.httpdate(_request.get_header(@date_header)).utc
77
+ (request_date - Time.now.utc).abs <= @date_max_skew
78
+ rescue ArgumentError
79
+ false
80
+ end
81
+
82
+ def calc_signature(_request, _key_secret)
83
+ signature = OpenSSL::HMAC.digest(@digest, _key_secret, build_payload(_request))
84
+ "HMAC-#{@digest.upcase} #{[signature].pack('m0')}"
85
+ end
86
+
87
+ def build_payload(_request)
88
+ parts = [
89
+ _request.method,
90
+ _request.path,
91
+ _request.content_type || '',
92
+ body_md5(_request)
93
+ ]
94
+
95
+ # extra headers to be considered
96
+ @sign_headers.each { |h| parts << (_request.get_header(h) || '') }
97
+ parts.join "\n"
98
+ end
99
+
100
+ def body_md5(_request)
101
+ if %w[POST PUT].include?(_request.method)
102
+ Digest::MD5.base64digest _request.body
103
+ else
104
+ ''
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,3 @@
1
+ module Authograph
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: authograph
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ignacio Baixas
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-17 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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: '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: guard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.7'
83
+ description: |2
84
+
85
+ HTTP request signing and validation library with support for header signing and multiple backends.
86
+ email:
87
+ - ignacio@platan.us
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".travis.yml"
95
+ - CODE_OF_CONDUCT.md
96
+ - Gemfile
97
+ - Guardfile
98
+ - LICENSE.txt
99
+ - README.md
100
+ - Rakefile
101
+ - authograph.gemspec
102
+ - bin/console
103
+ - bin/setup
104
+ - lib/authograph.rb
105
+ - lib/authograph/adapters/base.rb
106
+ - lib/authograph/adapters/faraday.rb
107
+ - lib/authograph/adapters/http.rb
108
+ - lib/authograph/adapters/rack.rb
109
+ - lib/authograph/signer.rb
110
+ - lib/authograph/version.rb
111
+ homepage: https://github.com/SurBTC/authograph
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.6.4
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Flexible HTTP request HMAC signing and validation
135
+ test_files: []