shmac 0.1.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: 134e0f7871345b0f56699849f2fcbb27da1b33b6
4
+ data.tar.gz: 3fa782727eea6bdcd2b7a5dea3ea0de352add1e5
5
+ SHA512:
6
+ metadata.gz: fe1a5876c513ac6d943be35ad2ff21b8c4280a02f3b9c769d9cb422c5fc8efa0b348a70c1754324385f2fcbfc65f960d7174c8e71479c43ef0aafa2e275388e4
7
+ data.tar.gz: 436a767db4e95c9594a6f18ded0a9e489ed422f808e4f1a1c6b19afec603a537fb6f00b77e029c574d8717ed52c17e6dc6b3bf1e3722745023085d5acc82347a
@@ -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.5
@@ -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 joel.junstrom@oktavilla.se. 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 shmac.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Joel Junström
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,35 @@
1
+ # Shmac
2
+
3
+ Simple HMAC authentication in a semi-usable state.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'shmac'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install shmac
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/oktavilla/shmac. 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.
30
+
31
+
32
+ ## License
33
+
34
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
35
+
@@ -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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "shmac"
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,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'rspec' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("rspec-core", "rspec")
@@ -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,22 @@
1
+ require "shmac/version"
2
+ require "shmac/authentication"
3
+ require "shmac/request_adapters"
4
+
5
+ module Shmac
6
+ def self.rails secret, request, namespace: nil
7
+ authentication(secret, request, namespace: namespace, request_adapter: RequestAdapters::Rails)
8
+ end
9
+
10
+ def self.net_http secret, request, namespace: nil
11
+ authentication(secret, request, namespace: namespace, request_adapter: RequestAdapters::NetHttp)
12
+ end
13
+
14
+ def self.authentication secret, request, namespace: nil, request_adapter: nil
15
+ Authentication.new(
16
+ secret,
17
+ request,
18
+ header_namespace: namespace,
19
+ request_adapter: request_adapter
20
+ )
21
+ end
22
+ end
@@ -0,0 +1,55 @@
1
+ require "shmac/signature_calculator"
2
+ require "shmac/authorization_header"
3
+
4
+ module Shmac
5
+ class Authentication
6
+ include Comparable
7
+
8
+ attr_reader :secret, :header_namespace
9
+
10
+ def self.generate_authorization_header request, secret:, access_key:, organization:, header_namespace: nil
11
+ AuthorizationHeader.generate(
12
+ organization: organization,
13
+ access_key: access_key,
14
+ signature: self.generate_signature(request, secret: secret, header_namespace: header_namespace)
15
+ ).to_s
16
+ end
17
+
18
+ def self.generate_signature request, secret:, header_namespace: nil
19
+ new(secret, request, header_namespace: header_namespace).signature
20
+ end
21
+
22
+ def initialize secret, request, header_namespace: nil, request_adapter: nil
23
+ @secret = secret
24
+ @request = request
25
+ @request_adapter = request_adapter
26
+ @header_namespace = header_namespace
27
+ end
28
+
29
+ def == other
30
+ other.is_a?(self.class) && self.signature == other.signature
31
+ end
32
+
33
+ def signature
34
+ SignatureCalculator.new(
35
+ secret: self.secret,
36
+ request: self.request,
37
+ header_namespace: self.header_namespace
38
+ ).to_s
39
+ end
40
+
41
+ def authentic?
42
+ return false if request.authorization.to_s.strip.empty?
43
+
44
+ AuthorizationHeader.new(request.authorization).signature == self.signature
45
+ end
46
+
47
+ def request
48
+ request_adapter.call @request
49
+ end
50
+
51
+ def request_adapter
52
+ @request_adapter ||= ->(r) { r }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,48 @@
1
+ module Shmac
2
+ class AuthorizationHeader
3
+ include Comparable
4
+ class FormatError < StandardError; end
5
+
6
+ AUTH_HEADER_PATTERN = /(.+) (.+):(.+)$/
7
+
8
+ attr_reader :parts
9
+
10
+ def self.generate organization:, access_key:, signature:
11
+ new("%s %s:%s" % [organization, access_key, signature])
12
+ end
13
+
14
+ def initialize value
15
+ @value = value
16
+ self.parts = value
17
+ end
18
+
19
+ def == other
20
+ other.is_a?(self.class) && self.to_s == other.to_s
21
+ end
22
+
23
+ def to_s
24
+ @value
25
+ end
26
+
27
+ def parts= value
28
+ matches = AUTH_HEADER_PATTERN.match(value)
29
+ unless matches
30
+ raise FormatError.new("#{value} does not match the expected authorization signature")
31
+ end
32
+
33
+ @parts = matches
34
+ end
35
+
36
+ def organization
37
+ parts[1]
38
+ end
39
+
40
+ def access_key_id
41
+ parts[2]
42
+ end
43
+
44
+ def signature
45
+ parts[3]
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,33 @@
1
+ module Shmac
2
+ class NormalizedHttpHeaders
3
+ def self.from_request_headers request_headers
4
+ new(
5
+ Hash[
6
+ request_headers.to_h.find_all { |(k, _)|
7
+ k.upcase.start_with?("HTTP_")
8
+ }
9
+ ]
10
+ )
11
+ end
12
+
13
+ attr_reader :headers
14
+
15
+ def initialize http_headers
16
+ self.headers = http_headers
17
+ end
18
+
19
+ def to_h
20
+ headers
21
+ end
22
+
23
+ def headers= http_headers
24
+ @headers = http_headers.each_with_object({}) { |(k,v), memo|
25
+ memo[normalize_key(k)] = Array(v).join(",")
26
+ }
27
+ end
28
+
29
+ def normalize_key key
30
+ key.to_s.downcase.tr("_", "-").gsub(/\Ahttp-/, "")
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,41 @@
1
+ require "shmac/normalized_http_headers"
2
+
3
+ module Shmac
4
+ class Request
5
+ attr_reader :path, :method, :headers, :body, :content_type
6
+
7
+ def initialize path:, method:, headers:, body: nil, content_type: nil
8
+ @path = path
9
+ @method = method
10
+ self.headers = headers
11
+ @body = body
12
+ @content_type = content_type
13
+ end
14
+
15
+ # Prefer the value of a namespaced date key if present
16
+ def date namespace = nil
17
+ date_key = [namespace, "date"].compact.join("-").downcase
18
+
19
+ headers.fetch(date_key) { headers["date"] }
20
+ end
21
+
22
+ def content_md5
23
+ # Fallback to x-content-md5 for clients that have issues with standard headers
24
+ headers.fetch("content-md5") { headers["x-content-md5"] }
25
+ end
26
+
27
+ def authorization
28
+ # Test for x-authorization for clients that have issues with standard headers
29
+ headers.fetch("x-authorization") { headers["authorization"] }
30
+ end
31
+
32
+ def headers= headers
33
+ @headers = NormalizedHttpHeaders.new(headers).to_h
34
+ end
35
+
36
+ def api_version
37
+ headers.fetch("x-authorization-version", 0).to_i
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,26 @@
1
+ require "shmac/request"
2
+ require "shmac/normalized_http_headers"
3
+
4
+ module Shmac
5
+ module RequestAdapters
6
+ Rails = ->(rails_request) {
7
+ Request.new(
8
+ path: rails_request.fullpath,
9
+ method: rails_request.request_method.to_s.upcase,
10
+ headers: Shmac::NormalizedHttpHeaders.from_request_headers(rails_request.headers).to_h,
11
+ body: rails_request.raw_post,
12
+ content_type: rails_request.content_type
13
+ )
14
+ }
15
+
16
+ NetHttp = ->(net_http_request) {
17
+ Request.new(
18
+ path: net_http_request.uri.path,
19
+ method: net_http_request.method.to_s.upcase,
20
+ headers: NormalizedHttpHeaders.new(net_http_request.to_hash).to_h,
21
+ body: net_http_request.body,
22
+ content_type: net_http_request.content_type
23
+ )
24
+ }
25
+ end
26
+ end
@@ -0,0 +1,59 @@
1
+ require "openssl"
2
+ require "base64"
3
+
4
+ module Shmac
5
+ class SignatureCalculator
6
+ attr_reader :secret, :request, :header_namespace
7
+
8
+ def initialize secret:, request:, header_namespace: nil
9
+ @secret = secret
10
+ @request = request
11
+ @header_namespace = header_namespace.downcase if header_namespace
12
+ end
13
+
14
+ def to_s
15
+ signature
16
+ end
17
+
18
+ def signature
19
+ digest = OpenSSL::Digest.new "sha1"
20
+ hmac = OpenSSL::HMAC.digest digest, secret, string_to_sign
21
+ Base64.strict_encode64 hmac
22
+ end
23
+
24
+ def string_to_sign
25
+ parts = [
26
+ request.method,
27
+ request.content_md5,
28
+ request.content_type,
29
+ request.date(self.header_namespace)
30
+ ]
31
+
32
+ platform_headers = canonicalized_platform_headers.to_s.strip
33
+ parts << platform_headers unless platform_headers.empty?
34
+
35
+ # The path is expected by spec but the DPO sends the same message (including headers) to several endpoints
36
+ # We introduce an api version so we do not lose messages
37
+ parts << request.path unless request.api_version > 0
38
+
39
+ parts.join("\n")
40
+ end
41
+
42
+ def canonicalized_platform_headers
43
+ return unless self.header_namespace
44
+
45
+ normalize_key = -> (key) { key.to_s.downcase.strip }
46
+ normalize_value = -> (value) { value.to_s.strip }
47
+ canonicalized_header_row = -> (k,v) {
48
+ "%s:%s" % [normalize_key.(k), normalize_value.(v)]
49
+ }
50
+ matches_namespace = ->(value) { value.start_with?(header_namespace) }
51
+
52
+ self.request.headers
53
+ .map(&canonicalized_header_row)
54
+ .find_all(&matches_namespace)
55
+ .sort
56
+ .join("\n")
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module Shmac
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'shmac/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "shmac"
8
+ spec.version = Shmac::VERSION
9
+ spec.authors = ["Joel Junström"]
10
+ spec.email = ["joel.junstrom@oktavilla.se"]
11
+
12
+ spec.summary = %q{Authenticates api requests using HMAC}
13
+ spec.homepage = "https://github.com/Oktavilla/shmac"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "timecop"
25
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shmac
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joel Junström
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-31 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: timecop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - joel.junstrom@oktavilla.se
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/rspec
86
+ - bin/setup
87
+ - lib/shmac.rb
88
+ - lib/shmac/authentication.rb
89
+ - lib/shmac/authorization_header.rb
90
+ - lib/shmac/normalized_http_headers.rb
91
+ - lib/shmac/request.rb
92
+ - lib/shmac/request_adapters.rb
93
+ - lib/shmac/signature_calculator.rb
94
+ - lib/shmac/version.rb
95
+ - shmac.gemspec
96
+ homepage: https://github.com/Oktavilla/shmac
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.6.4
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Authenticates api requests using HMAC
120
+ test_files: []