Created
November 28, 2013 01:03
-
-
Save hokamoto/7685761 to your computer and use it in GitHub Desktop.
Create a signature (Akamai OPEN API)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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