Class: Hubspot::Helpers::Signature
- Inherits:
-
Object
- Object
- Hubspot::Helpers::Signature
- Defined in:
- lib/hubspot/helpers/signature.rb
Constant Summary collapse
- MAX_ALLOWED_TIMESTAMP =
3000
Instance Method Summary collapse
- #get_signature(client_secret: String, request_body: String, signature_version: String, http_uri: nil, http_method: "POST", timestamp: nil) ⇒ Object
- #is_valid(signature: String, client_secret: String, request_body: String, http_uri: nil, http_method: 'POST', signature_version: 'v2', timestamp: nil) ⇒ Object
Instance Method Details
#get_signature(client_secret: String, request_body: String, signature_version: String, http_uri: nil, http_method: "POST", timestamp: nil) ⇒ Object
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 |
# File 'lib/hubspot/helpers/signature.rb', line 35 def get_signature( client_secret: String, request_body: String, signature_version: String, http_uri: nil, http_method: "POST", timestamp: nil ) case signature_version when "v1" source_string = "#{client_secret}#{request_body}" hash_result = Digest::SHA2.hexdigest(source_string.encode('utf-8')) return hash_result when "v2" source_string = "#{client_secret}#{http_method}#{http_uri}#{request_body}" hash_result = Digest::SHA2.hexdigest(source_string.encode('utf-8')) return hash_result when "v3" source_string = "#{http_method}#{http_uri}#{request_body}#{}" hash_result = OpenSSL::HMAC.base64digest('SHA256', client_secret, source_string.encode('utf-8')) return hash_result else raise InvalidSignatureVersionError.new(signature_version) end end |
#is_valid(signature: String, client_secret: String, request_body: String, http_uri: nil, http_method: 'POST', signature_version: 'v2', timestamp: nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/hubspot/helpers/signature.rb', line 8 def is_valid( signature: String, client_secret: String, request_body: String, http_uri: nil, http_method: 'POST', signature_version: 'v2', timestamp: nil ) if signature_version == "v3" current_time = DateTime.now.strftime("%s").to_i if current_time - .to_i > MAX_ALLOWED_TIMESTAMP raise InvalidSignatureTimestampError.new() end end hashed_signature = get_signature( client_secret: client_secret, request_body: request_body, signature_version: signature_version, http_uri: http_uri, http_method: http_method, timestamp: ) signature == hashed_signature end |