Created
October 3, 2019 11:10
-
-
Save ssugiyama/5a3c99fac89e46c6b60e2883760faf92 to your computer and use it in GitHub Desktop.
This file contains 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
require 'uri' | |
require 'optparse' | |
require 'securerandom' | |
require 'openssl' | |
opt = OptionParser.new | |
token = ENV['MAC_TOKEN'] | |
secret = ENV['MAC_SECRET'] | |
method = 'GET' | |
opt.on('--token token') {|v| token = v } | |
opt.on('--secret secret') {|v| secret = v } | |
opt.on('-X method', '--request method') {|v| method = v.upcase} | |
opt.parse!(ARGV) | |
uri = URI.parse(ARGV.last) | |
ts = Time.now.to_i.to_s | |
nonce = SecureRandom.base64(8) | |
request_uri = uri.path | |
request_uri += '?' + uri.query unless uri.query.nil? | |
data = [ts, nonce, method, request_uri, uri.host, uri.port.to_s, '', nil].join("\n") | |
p data | |
mac = [OpenSSL::HMAC.digest('sha256', secret, data)].pack('m0') | |
header = "Authorization: MAC id=\"#{token}\", nonce=\"#{nonce}\", ts=\"#{ts}\", mac=\"#{mac}\"" | |
puts header | |
system('curl', '-X', method, '-H', header, *ARGV) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment