Class: AzureCommunicationEmail::HmacAuth
- Inherits:
-
Object
- Object
- AzureCommunicationEmail::HmacAuth
- Defined in:
- lib/azure_communication_email/hmac_auth.rb
Instance Method Summary collapse
-
#initialize(endpoint:, access_key:) ⇒ HmacAuth
constructor
A new instance of HmacAuth.
-
#sign_request(http_method:, path_and_query:, body:) ⇒ Object
body: must be the exact JSON string that will go in request.body.
Constructor Details
#initialize(endpoint:, access_key:) ⇒ HmacAuth
Returns a new instance of HmacAuth.
15 16 17 18 |
# File 'lib/azure_communication_email/hmac_auth.rb', line 15 def initialize(endpoint:, access_key:) @endpoint = endpoint # e.g. "https://my-resource.communication.azure.com" @access_key = access_key end |
Instance Method Details
#sign_request(http_method:, path_and_query:, body:) ⇒ Object
body: must be the exact JSON string that will go in request.body
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 |
# File 'lib/azure_communication_email/hmac_auth.rb', line 21 def sign_request(http_method:, path_and_query:, body:) uri = URI.join(@endpoint, path_and_query) content_bytes = body.encode("utf-8") # RFC1123 UTC timestamp date = Time.now.utc.httpdate # Base64(SHA256(request-body-bytes)) content_hash = base64_sha256(content_bytes) # StringToSign host = uri.host.downcase string_to_sign = [ http_method.upcase, path_and_query, "#{date};#{host};#{content_hash}" ].join("\n") signature = base64_hmac_sha256(string_to_sign, @access_key) = "HMAC-SHA256 SignedHeaders=x-ms-date;host;x-ms-content-sha256&Signature=#{signature}" { "x-ms-date" => date, "x-ms-content-sha256" => content_hash, "Authorization" => , "Content-Type" => "application/json" } end |