Skip to content

Instantly share code, notes, and snippets.

@hokamoto
Created November 28, 2013 01:03
Show Gist options
  • Save hokamoto/7685761 to your computer and use it in GitHub Desktop.
Save hokamoto/7685761 to your computer and use it in GitHub Desktop.
Create a signature (Akamai OPEN API)
SIGNING_ALGORITHM = 'EG1-HMAC-SHA256'
def create_auth_header(method, scheme, host, path, headers, payload = nil, _timestamp = nil, _nonce = nil)
timestamp = _timestamp.nil? ? Time.now.gmtime.strftime('%Y%m%dT%H:%M:%S%z') : _timestamp
nonce = _nonce.nil? ? UUIDTools::UUID.random_create.to_s : _nonce
signing_key = Base64.encode64(OpenSSL::HMAC::digest(OpenSSL::Digest::SHA256.new, @secret, timestamp)).strip
authorization_header = SIGNING_ALGORITHM + ' '
authorization_header << "client_token=#{@client_token};"
authorization_header << "access_token=#{@access_token};"
authorization_header << "timestamp=#{timestamp};"
authorization_header << "nonce=#{nonce};"
payload_digest = nil
payload_digest = Base64.encode64(OpenSSL::Digest::SHA256.digest(payload)).strip unless payload.nil?
data = [method.upcase, scheme.downcase, host.downcase, path, headers, payload_digest, authorization_header].join("\t")
signature = Base64.encode64(OpenSSL::HMAC::digest(OpenSSL::Digest::SHA256.new, signing_key, data)).strip
authorization_header << "signature=#{signature}"
authorization_header
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment