Class: HmacAuthentication::HmacAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/hmac_authentication/signature.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(digest_name, secret_key, signature_header, headers) ⇒ HmacAuth

Returns a new instance of HmacAuth.



28
29
30
31
32
33
34
35
36
37
# File 'lib/hmac_authentication/signature.rb', line 28

def initialize(digest_name, secret_key, signature_header, headers)
  @digest = HmacAuthentication.parse_digest digest_name
  if digest.nil?
    fail "HMAC authentication digest is not supported: #{digest_name}"
  end

  @secret_key = secret_key
  @signature_header = signature_header
  @headers = headers
end

Instance Attribute Details

#digestObject (readonly)

Returns the value of attribute digest.



26
27
28
# File 'lib/hmac_authentication/signature.rb', line 26

def digest
  @digest
end

#headersObject (readonly)

Returns the value of attribute headers.



26
27
28
# File 'lib/hmac_authentication/signature.rb', line 26

def headers
  @headers
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



26
27
28
# File 'lib/hmac_authentication/signature.rb', line 26

def secret_key
  @secret_key
end

#signature_headerObject (readonly)

Returns the value of attribute signature_header.



26
27
28
# File 'lib/hmac_authentication/signature.rb', line 26

def signature_header
  @signature_header
end

Instance Method Details

#authenticate_request(request) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hmac_authentication/signature.rb', line 57

def authenticate_request(request)
  header = signature_from_header request
  return NO_SIGNATURE unless header
  components = header.split ' '
  return INVALID_FORMAT, header unless components.size == 2
  parsed_digest = HmacAuthentication.parse_digest components.first
  return UNSUPPORTED_ALGORITHM, header unless parsed_digest
  computed = request_signature_impl request, parsed_digest
  [HmacAuthentication.compare_signatures(header, computed),
   header, computed]
end

#request_signature(request) ⇒ Object



49
50
51
# File 'lib/hmac_authentication/signature.rb', line 49

def request_signature(request)
  request_signature_impl request, digest
end

#sign_request(req) ⇒ Object



45
46
47
# File 'lib/hmac_authentication/signature.rb', line 45

def sign_request(req)
  req[signature_header] = request_signature req
end

#signature_from_header(request) ⇒ Object



53
54
55
# File 'lib/hmac_authentication/signature.rb', line 53

def signature_from_header(request)
  request[signature_header]
end

#string_to_sign(req) ⇒ Object



39
40
41
42
43
# File 'lib/hmac_authentication/signature.rb', line 39

def string_to_sign(req)
  [req.method,
   signed_headers(req).join("\n"),
   HmacAuthentication.hash_url(req)].join("\n") + "\n"
end