Module: SharedSecretAuthentication

Extended by:
SharedSecretAuthentication
Included in:
SharedSecretAuthentication
Defined in:
lib/shared-secret-authentication/generator.rb,
lib/shared-secret-authentication/hash_signatures.rb

Defined Under Namespace

Classes: Generator

Class Method Summary collapse

Class Method Details

.hash_signature(hash) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/shared-secret-authentication/hash_signatures.rb', line 14

def self.hash_signature(hash)
  d = Digest::SHA2.new
  string_keys = hash.keys.inject({}) {|keys, key| keys.merge!(key.to_s => key) }
  string_keys.keys.sort.each do |key|
    puts key.inspect
    d.update key
    value = hash[string_keys[key]]
    if value.instance_of? Hash
      value = value.collect {|k,v| 
        if v.respond_to? :strftime
          if v.respond_to? :utc

            k.to_s + v.utc.strftime('%a %b %m %H:%M:%S %Y')
          else
            k.to_s + v.strftime('%a %b %m %H:%M:%S %Y')
          end
        else
          k.to_s + v.to_s
        end
      }
    end
    
    if value.instance_of? Array
      puts value.inspect
      value = value.sort
      value = value.collect {|v|
        if v.respond_to? :strftime
          if v.respond_to? :utc
            puts 'here in utc'
            v.utc.strftime('%a %b %m %H:%M:%S %Y')
          else
            puts 'not in utc'
            v.strftime('%a %b %m %H:%M:%S %Y')
          end
        else
          v.to_s
        end
      
      }
    elsif value.respond_to? :strftime
      value = value.strftime('%a %b %m %H:%M:%S %Y')
    end
    puts 'in here'
    puts value.to_s.inspect
    d.update value.to_s
  end
  d.update SHARED_SECRET
  d.to_s
end

.hash_signature_correct?(hash) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


8
9
10
11
12
# File 'lib/shared-secret-authentication/hash_signatures.rb', line 8

def self.hash_signature_correct?(hash)
  raise ArgumentError.new("hash must be signed (have a key of 'signature')") unless expected_signature = hash.delete('signature')
  
  hash_signature(hash) == expected_signature
end

.sign_hash(hash) ⇒ Object



4
5
6
# File 'lib/shared-secret-authentication/hash_signatures.rb', line 4

def self.sign_hash(hash)
  hash.merge('signature' => hash_signature(hash))
end