hmac_auth 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +22 -0
- data/README.md +53 -0
- data/Rakefile +1 -0
- data/hmac_auth.gemspec +27 -0
- data/lib/hmac_auth.rb +25 -0
- data/lib/hmac_auth/error.rb +4 -0
- data/lib/hmac_auth/signature.rb +79 -0
- data/lib/hmac_auth/version.rb +4 -0
- data/spec/signature_spec.rb +68 -0
- data/spec/spec_helper.rb +26 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 04268ea75914ebc68d8fa7ba962c3ed8a59587b6
|
4
|
+
data.tar.gz: af2783c9026c0d7a8aed49d3ab41c4c857cf410f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 578a859ff43e9dffb42c8babc64d26719b0d31bc2c6d513960486af4094a5855de4c6b19975d96d4a7b3b2f3266ae5ec7aca26c1b1f4dd6397d4f377c9285e20
|
7
|
+
data.tar.gz: 33b8a97b3f2d96c0dc94d03c62d3db9f3399b5f500d26b786d3cb72ccd92ea4d56ee2503e6ddb09541afbfa7d94aec64aa6dd0a308a0a5c1acf59031e5c85e18
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
hmac_auth
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.0.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Gebhard Wöstemeyer <g.woestemeyer@gmail.com>
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# HMACAuth
|
2
|
+
|
3
|
+
[](https://travis-ci.org/gewo/hmac_auth/)
|
4
|
+
[](https://coveralls.io/r/gewo/hmac_auth)
|
5
|
+
|
6
|
+
```
|
7
|
+
__ ____ ______ _________ __ __
|
8
|
+
/ / / / |/ / | / ____/ | __ __/ /_/ /_
|
9
|
+
/ /_/ / /|_/ / /| |/ / / /| |/ / / / __/ __ \
|
10
|
+
/ __ / / / / ___ / /___/ ___ / /_/ / /_/ / / /
|
11
|
+
/_/ /_/_/ /_/_/ |_\____/_/ |_\__,_/\__/_/ /_/
|
12
|
+
|
13
|
+
```
|
14
|
+
|
15
|
+
Ruby gem providing HMAC based message signing and verification. Without
|
16
|
+
fancy Rails integration.
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem 'hmac_auth' # Gemfile
|
22
|
+
gem install hmac_auth # manual
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# Configuration
|
29
|
+
HMACAuth.secret = 't0p_s3cr3!!eins1'
|
30
|
+
HMACAuth.reject_keys = %w(action controller format)
|
31
|
+
HMACAuth.valid_for = 15.minutes
|
32
|
+
|
33
|
+
to_be_signed = {
|
34
|
+
b: 2,
|
35
|
+
a: { d: 4, c: 3 }
|
36
|
+
}
|
37
|
+
|
38
|
+
signed = HMACAuth::Signature.sign to_be_signed
|
39
|
+
# => Hash including 'timestamp' and 'signature'
|
40
|
+
|
41
|
+
HMACAuth::Signature.verify(signed) # => true
|
42
|
+
HMACAuth::Signature.verify(signed.merge(evil: 'yes')) # => false
|
43
|
+
HMACAuth::Signature.verify(signed, secret: 'good guess?') # => false
|
44
|
+
|
45
|
+
sleep 20.minutes
|
46
|
+
HMACAuth::Signature.verify(signed) # => false
|
47
|
+
|
48
|
+
# That's it. Nothing more, nothing less.
|
49
|
+
```
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
This is very much appreciated :-)
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/hmac_auth.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hmac_auth/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'hmac_auth'
|
8
|
+
gem.version = HmacAuth::VERSION
|
9
|
+
gem.authors = ['Gebhard Wöstemeyer']
|
10
|
+
gem.email = ['g.woestemeyer@gmail.com']
|
11
|
+
gem.description = 'HMAC based message signing and verification'
|
12
|
+
gem.summary = 'Ruby gem providing HMAC based message signing and ' \
|
13
|
+
'verification. Without fancy Rails integration.'
|
14
|
+
gem.homepage = 'https://github.com/gewo/hmac_auth'
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ['lib']
|
20
|
+
|
21
|
+
gem.add_runtime_dependency 'activesupport'
|
22
|
+
|
23
|
+
gem.add_development_dependency 'rake'
|
24
|
+
gem.add_development_dependency 'rspec'
|
25
|
+
gem.add_development_dependency 'simplecov'
|
26
|
+
gem.add_development_dependency 'coveralls'
|
27
|
+
end
|
data/lib/hmac_auth.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'openssl'
|
3
|
+
require 'json'
|
4
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
5
|
+
require 'active_support/core_ext/numeric/time'
|
6
|
+
|
7
|
+
require 'hmac_auth/version'
|
8
|
+
require 'hmac_auth/error'
|
9
|
+
require 'hmac_auth/signature'
|
10
|
+
|
11
|
+
module HMACAuth
|
12
|
+
mattr_accessor :secret,
|
13
|
+
:reject_keys,
|
14
|
+
:valid_for
|
15
|
+
|
16
|
+
# The shared secret.
|
17
|
+
self.secret = nil
|
18
|
+
|
19
|
+
# Keys to ignore when signing/verifying.
|
20
|
+
self.reject_keys = %w(action controller format)
|
21
|
+
|
22
|
+
# Time the signature is valid when verifying
|
23
|
+
self.valid_for = 15.minutes
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
module HMACAuth
|
3
|
+
class Signature
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def verify(params, options = {})
|
7
|
+
self.new(params, options).verify
|
8
|
+
end
|
9
|
+
|
10
|
+
def sign(params, options = {})
|
11
|
+
self.new(params, options).sign
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(params, options = {})
|
16
|
+
@secret = options.delete(:secret) || HMACAuth.secret
|
17
|
+
@valid_for = options.delete(:valid_for) || HMACAuth.valid_for
|
18
|
+
@reject_keys = options.delete(:reject_keys) || HMACAuth.reject_keys
|
19
|
+
@_params = params
|
20
|
+
|
21
|
+
raise Error.new 'You *must* tell me a secret!' unless @secret
|
22
|
+
end
|
23
|
+
|
24
|
+
def verify
|
25
|
+
valid_timestamp && signature == calculated_signature
|
26
|
+
end
|
27
|
+
|
28
|
+
# @return [Hash] Signed parameters
|
29
|
+
def sign
|
30
|
+
timestamp || params['timestamp'] = Time.now.to_i.to_s
|
31
|
+
params.merge('signature' => calculated_signature)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def calculated_signature
|
37
|
+
OpenSSL::HMAC.hexdigest(
|
38
|
+
OpenSSL::Digest::Digest.new('sha256'),
|
39
|
+
secret,
|
40
|
+
deep_sort(params_without_signature).to_json)
|
41
|
+
end
|
42
|
+
|
43
|
+
def deep_sort(hash)
|
44
|
+
Hash[hash.sort.map { |k, v| [k, v.is_a?(Hash) ? deep_sort(v) : v] }]
|
45
|
+
end
|
46
|
+
|
47
|
+
def deep_stringify(hash)
|
48
|
+
Hash[hash.map do |k, v|
|
49
|
+
[k.to_s, v.is_a?(Hash) ? deep_stringify(v) : v.to_s]
|
50
|
+
end]
|
51
|
+
end
|
52
|
+
|
53
|
+
def valid_timestamp
|
54
|
+
timestamp && timestamp >= valid_for.ago.to_i
|
55
|
+
end
|
56
|
+
|
57
|
+
def timestamp
|
58
|
+
params['timestamp'].present? &&
|
59
|
+
params['timestamp'].to_s =~ /\A\d+\Z/ &&
|
60
|
+
params['timestamp'].to_i
|
61
|
+
end
|
62
|
+
|
63
|
+
def signature
|
64
|
+
params['signature']
|
65
|
+
end
|
66
|
+
|
67
|
+
def params_without_signature
|
68
|
+
params.reject { |k, v| k == 'signature' }
|
69
|
+
end
|
70
|
+
|
71
|
+
def params
|
72
|
+
@params ||= deep_stringify(@_params.reject do |k, v|
|
73
|
+
reject_keys.include? k
|
74
|
+
end)
|
75
|
+
end
|
76
|
+
|
77
|
+
attr_reader :secret, :valid_for, :reject_keys
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
module HMACAuth
|
5
|
+
|
6
|
+
describe Signature do
|
7
|
+
let(:secret) { 't0p_s3cr3t!!eins1' }
|
8
|
+
let(:params) do
|
9
|
+
{
|
10
|
+
b: 2,
|
11
|
+
a: { d: 4, c: 3 }
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '.verify' do
|
16
|
+
describe 'timestamp' do
|
17
|
+
let(:timestamp) { Time.now.to_i }
|
18
|
+
let(:params_with_timestamp) { params.merge(timestamp: timestamp) }
|
19
|
+
let(:signed_params) do
|
20
|
+
Signature.sign(params_with_timestamp, secret: secret)
|
21
|
+
end
|
22
|
+
|
23
|
+
subject { Signature.verify(signed_params, secret: secret) }
|
24
|
+
|
25
|
+
context 'valid' do
|
26
|
+
let(:timestamp) { 10.minutes.ago.to_i.to_s }
|
27
|
+
it { should be_true }
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'invalid' do
|
31
|
+
let(:timestamp) { 20.minutes.ago.to_i }
|
32
|
+
it { should be_false }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '.sign' do
|
38
|
+
def signature(hash)
|
39
|
+
HMACAuth::Signature.sign(hash, secret: secret)['signature']
|
40
|
+
end
|
41
|
+
|
42
|
+
describe 'hash' do
|
43
|
+
subject { HMACAuth::Signature.sign(params, secret: secret) }
|
44
|
+
|
45
|
+
it { should be_a Hash }
|
46
|
+
its(['signature']) { should be_a String }
|
47
|
+
its(['timestamp']) { should be }
|
48
|
+
its(['b']) { should be_a String }
|
49
|
+
|
50
|
+
context 'nested hash' do
|
51
|
+
subject { HMACAuth::Signature.sign(params, secret: secret)['a'] }
|
52
|
+
it { should be_a Hash }
|
53
|
+
its(['d']) { should == '4' }
|
54
|
+
its(['c']) { should == '3' }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe 'unsorted input' do
|
59
|
+
let(:hasha) { { a: 1, b: { c: 3, d: 4 } } }
|
60
|
+
let(:hashd) { { b: { d: 4, c: 3 }, a: 1 } }
|
61
|
+
|
62
|
+
it 'calculates the same signature' do
|
63
|
+
signature(hasha).should == signature(hashd)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'simplecov'
|
4
|
+
require 'coveralls'
|
5
|
+
|
6
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
7
|
+
SimpleCov::Formatter::HTMLFormatter,
|
8
|
+
Coveralls::SimpleCov::Formatter
|
9
|
+
]
|
10
|
+
|
11
|
+
SimpleCov.at_exit do
|
12
|
+
File.open(File.join(SimpleCov.coverage_path, 'percent.txt'), 'w') do |f|
|
13
|
+
f.write SimpleCov.result.covered_percent
|
14
|
+
end
|
15
|
+
SimpleCov.result.format!
|
16
|
+
end
|
17
|
+
SimpleCov.start
|
18
|
+
|
19
|
+
require 'hmac_auth'
|
20
|
+
|
21
|
+
RSpec.configure do |config|
|
22
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
23
|
+
config.run_all_when_everything_filtered = true
|
24
|
+
config.filter_run :focus
|
25
|
+
config.order = 'random'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hmac_auth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gebhard Wöstemeyer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '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: simplecov
|
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
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: coveralls
|
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: HMAC based message signing and verification
|
84
|
+
email:
|
85
|
+
- g.woestemeyer@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .coveralls.yml
|
91
|
+
- .gitignore
|
92
|
+
- .rspec
|
93
|
+
- .ruby-gemset
|
94
|
+
- .ruby-version
|
95
|
+
- .travis.yml
|
96
|
+
- Gemfile
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- hmac_auth.gemspec
|
101
|
+
- lib/hmac_auth.rb
|
102
|
+
- lib/hmac_auth/error.rb
|
103
|
+
- lib/hmac_auth/signature.rb
|
104
|
+
- lib/hmac_auth/version.rb
|
105
|
+
- spec/signature_spec.rb
|
106
|
+
- spec/spec_helper.rb
|
107
|
+
homepage: https://github.com/gewo/hmac_auth
|
108
|
+
licenses: []
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - '>='
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.0.3
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Ruby gem providing HMAC based message signing and verification. Without fancy
|
130
|
+
Rails integration.
|
131
|
+
test_files:
|
132
|
+
- spec/signature_spec.rb
|
133
|
+
- spec/spec_helper.rb
|