Skip to content

Instantly share code, notes, and snippets.

@warmwaffles
Created September 19, 2014 03:01
Show Gist options
  • Save warmwaffles/b94b6943cd0b4c2a9e6b to your computer and use it in GitHub Desktop.
Save warmwaffles/b94b6943cd0b4c2a9e6b to your computer and use it in GitHub Desktop.
# Signs a URI with your appSID and Key.
# * :url describes the URL to sign
# * :appSID holds the appSID value
# * :key holds the key value
def self.sign(url, AppSID, AppKey)
url = URI.escape(url)
parsedURL = URI.parse(url)
urlToSign =''
if parsedURL.query.nil?
urlToSign = parsedURL.scheme+"://"+ parsedURL.host + parsedURL.path + "?appSID=" + AppSID
else
urlToSign = parsedURL.scheme+"://"+ parsedURL.host + parsedURL.path + '?' + parsedURL.query + "&appSID=" + AppSID
end
# create a signature using the private key and the URL
rawSignature = OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), AppKey, urlToSign)
#Convert raw to encoded string
signature = Base64.strict_encode64(rawSignature).tr('+/','-_')
#remove invalid character
signature = signature.gsub(/[=_-]/,'=' => '','_' => '%2f','-' => '%2b')
#Define expression
pat = Regexp.new("%[0-9a-f]{2}")
#Replace the portion matched to the above pattern to upper case
for i in 0..5
signature = signature.sub(pat,pat.match(signature).to_s.upcase)
end
# prepend the server and append the signature.
signedUrl = urlToSign + "&signature=#{signature}"
return signedUrl
end
@warmwaffles
Copy link
Author

Friends don't let non ruby developers write ruby code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment