Skip to content

Commit

Permalink
Merge 56cf912 into 3ccd093
Browse files Browse the repository at this point in the history
  • Loading branch information
fladson committed Dec 18, 2019
2 parents 3ccd093 + 56cf912 commit b86a724
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ fancy Rails integration.
## Usage

# Configuration
HMACAuth.secret = 't0p_s3cr3!!eins1'
HMACAuth.reject_keys = %w(action controller format)
HMACAuth.valid_for = 15.minutes
HMACAuth.secret = 't0p_s3cr3!!eins1'
HMACAuth.reject_keys = %w(action controller format)
HMACAuth.valid_for = 15.minutes
HMACAuth.keep_values_type = false

to_be_signed = {
b: 2,
Expand Down
5 changes: 4 additions & 1 deletion lib/hmac_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
module HMACAuth
mattr_accessor :secret,
:reject_keys,
:valid_for
:valid_for,
:keep_values_type

# The shared secret.
self.secret = nil
Expand All @@ -22,4 +23,6 @@ module HMACAuth
# Time the signature is valid when verifying
self.valid_for = 15.minutes

# Keep or not the values type when signing
self.keep_values_type = false
end
23 changes: 20 additions & 3 deletions lib/hmac_auth/signature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def initialize(params, options = {})
@secret = options.delete(:secret) || HMACAuth.secret
@valid_for = options.delete(:valid_for) || HMACAuth.valid_for
@reject_keys = options.delete(:reject_keys) || HMACAuth.reject_keys
@keep_values_type = options.delete(:keep_values_type) ||
HMACAuth.keep_values_type
@_params = params

raise Error.new 'You *must* tell me a secret!' unless @secret
Expand Down Expand Up @@ -50,6 +52,12 @@ def deep_stringify(hash)
end]
end

def deep_stringify_skip_values(hash)
Hash[hash.map do |k, v|
[k.to_s, v.is_a?(Hash) ? deep_stringify_skip_values(v) : v]
end]
end

def valid_timestamp
timestamp && timestamp >= valid_for.ago.to_i
end
Expand All @@ -69,11 +77,20 @@ def params_without_signature
end

def params
@params ||= deep_stringify(@_params.reject do |k, v|
reject_keys!
@params ||= if keep_values_type
deep_stringify_skip_values(reject_keys!)
else
deep_stringify(reject_keys!)
end
end

def reject_keys!
@_params.reject do |k, v|
reject_keys.include? k
end)
end
end

attr_reader :secret, :valid_for, :reject_keys
attr_reader :secret, :valid_for, :reject_keys, :keep_values_type
end
end
31 changes: 31 additions & 0 deletions spec/signature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,37 @@ def signature(hash)
signature(hasha).should == signature(hashd)
end
end

context 'when keep_values_type is true' do
describe 'hash' do
subject do
HMACAuth::Signature.sign(
params,
secret: secret,
keep_values_type: true
)
end

it { expect(subject).to be_a Hash }
it { expect(subject['signature']).to be_a String }
it { expect(subject['timestamp']).to be }
it { expect(subject['b']).to be_a Integer }

context 'nested hash' do
subject do
HMACAuth::Signature.sign(
params,
secret: secret,
keep_values_type: true
)['a']
end

it { expect(subject).to be_a Hash }
it { expect(subject['d']).to eq 4 }
it { expect(subject['c']).to eq 3 }
end
end
end
end
end
end

0 comments on commit b86a724

Please sign in to comment.