Ruby 为azure service bus/eventhubs生成有效的授权标头

Ruby 为azure service bus/eventhubs生成有效的授权标头,ruby,azure,azure-eventhub,Ruby,Azure,Azure Eventhub,我试图在ruby脚本中使用SAS身份验证,但我一直从事件中心收到401(拒绝访问)响应,似乎我生成的SAS令牌不正确 下面是我使用的代码,它基于我重写为ruby的Javascript示例(请注意它可能不是惯用的) 需要“optpass” 需要“CGI” 需要“openssl” 需要“base64” 需要“法拉第” 需要“摘要” def generateToken(url、keyname、keyvalue) encoded=CGI::转义(url) ttl=(Time.now+60*5).to_i

我试图在ruby脚本中使用SAS身份验证,但我一直从事件中心收到401(拒绝访问)响应,似乎我生成的SAS令牌不正确

下面是我使用的代码,它基于我重写为ruby的Javascript示例(请注意它可能不是惯用的)

需要“optpass”
需要“CGI”
需要“openssl”
需要“base64”
需要“法拉第”
需要“摘要”
def generateToken(url、keyname、keyvalue)
encoded=CGI::转义(url)
ttl=(Time.now+60*5).to_i
signature=“#{encoded}\n#{ttl}”.encode('utf-8'))
#签名
key=Base64.strict\u decode64(keyvalue)
dig=OpenSSL::HMAC.digest('sha256',密钥,签名)
#dig=摘要::HMAC.Digest(签名、密钥、摘要::SHA256)
hash=CGI.escape(Base64.strict_encode64(dig))
#放入散列
返回“SharedAccessSignature sig=#{hash}&se=#{ttl}&skn=#{keyname}&sr=#{encoded}”
结束
def build_连接(url、令牌)
conn=Faraday.new(:url=>url)do | Faraday|
请求:url#u encoded#form encode POST参数
faraday.response:logger#记录对标准输出的请求
faraday.adapter faraday.default#适配器#使用Net::HTTP发出请求
结束
conn.headers['Content-Type']='application/json'
conn.headers['Authorization']=令牌
返回控制
结束
如果文件=0美元

ARGV将我的代码与python sdk进行比较后,这是生成令牌的正确方法:

require "optparse"
require "CGI"
require 'openssl'
require "base64"
require "Faraday"
require 'Digest'

def generateToken(url,keyname,keyvalue)
    encoded = CGI::escape(url)
    ttl = (Time.now + 60*5).to_i
    signature = "#{encoded}\n#{ttl}"
    # puts signature
    key = keyvalue
    #dig  = OpenSSL::HMAC.digest('sha256', key, signature)
    dig = Digest::HMAC.digest(signature, key, Digest::SHA256)
    hash = CGI.escape(Base64.strict_encode64(dig))
    # puts hash
    return "SharedAccessSignature sig=#{hash}&se=#{ttl}&skn=#{keyname}&sr=#{encoded}" 
end

def build_connection(url,token)
    conn = Faraday.new(:url => url) do |faraday|
        faraday.request  :url_encoded             # form-encode POST params
        faraday.response :logger                  # log requests to STDOUT
        faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
    end
    conn.headers['Content-Type'] = 'application/json'
    conn.headers['Authorization'] = token
    return conn
end


if __FILE__ == $0
    ARGV << '-h' if ARGV.empty?
    options = {}
    OptionParser.new do |opts|
        opts.banner = "Usage: generateSasToken.rb [options]"
        opts.on('-u URL', '--url URL', 'url for access') { |v| options[:url] = v }
        opts.on('--keyname NAME','set key name') { |v| options[:keyname] = v }
        opts.on('--key KEY','set key value') { |v| options[:keyvalue] = v }
        opts.on_tail("-h", "--help", "Show this message") do
            puts opts
            exit
        end
    end.parse!
    token = generateToken(options[:url],options[:keyname],options[:keyvalue])
    conn = build_connection(options[:url],token)
    puts conn.headers
    response =  conn.post do |req|
                    req.body = '{"temprature":50}'
                    req.headers['content-length'] = req.body.length.to_s
                end
    puts response
end
需要“optpass”
需要“CGI”
需要“openssl”
需要“base64”
需要“法拉第”
需要“摘要”
def generateToken(url、keyname、keyvalue)
encoded=CGI::转义(url)
ttl=(Time.now+60*5).to_i
signature=“#{encoded}\n#{ttl}”
#签名
键=键值
#dig=OpenSSL::HMAC.digest('sha256',密钥,签名)
dig=摘要::HMAC.Digest(签名、密钥、摘要::SHA256)
hash=CGI.escape(Base64.strict_encode64(dig))
#放入散列
返回“SharedAccessSignature sig=#{hash}&se=#{ttl}&skn=#{keyname}&sr=#{encoded}”
结束
def build_连接(url、令牌)
conn=Faraday.new(:url=>url)do | Faraday|
请求:url#u encoded#form encode POST参数
faraday.response:logger#记录对标准输出的请求
faraday.adapter faraday.default#适配器#使用Net::HTTP发出请求
结束
conn.headers['Content-Type']='application/json'
conn.headers['Authorization']=令牌
返回控制
结束
如果文件=0美元

ARGV将我的代码与python sdk进行比较后,这是生成令牌的正确方法:

require "optparse"
require "CGI"
require 'openssl'
require "base64"
require "Faraday"
require 'Digest'

def generateToken(url,keyname,keyvalue)
    encoded = CGI::escape(url)
    ttl = (Time.now + 60*5).to_i
    signature = "#{encoded}\n#{ttl}"
    # puts signature
    key = keyvalue
    #dig  = OpenSSL::HMAC.digest('sha256', key, signature)
    dig = Digest::HMAC.digest(signature, key, Digest::SHA256)
    hash = CGI.escape(Base64.strict_encode64(dig))
    # puts hash
    return "SharedAccessSignature sig=#{hash}&se=#{ttl}&skn=#{keyname}&sr=#{encoded}" 
end

def build_connection(url,token)
    conn = Faraday.new(:url => url) do |faraday|
        faraday.request  :url_encoded             # form-encode POST params
        faraday.response :logger                  # log requests to STDOUT
        faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
    end
    conn.headers['Content-Type'] = 'application/json'
    conn.headers['Authorization'] = token
    return conn
end


if __FILE__ == $0
    ARGV << '-h' if ARGV.empty?
    options = {}
    OptionParser.new do |opts|
        opts.banner = "Usage: generateSasToken.rb [options]"
        opts.on('-u URL', '--url URL', 'url for access') { |v| options[:url] = v }
        opts.on('--keyname NAME','set key name') { |v| options[:keyname] = v }
        opts.on('--key KEY','set key value') { |v| options[:keyvalue] = v }
        opts.on_tail("-h", "--help", "Show this message") do
            puts opts
            exit
        end
    end.parse!
    token = generateToken(options[:url],options[:keyname],options[:keyvalue])
    conn = build_connection(options[:url],token)
    puts conn.headers
    response =  conn.post do |req|
                    req.body = '{"temprature":50}'
                    req.headers['content-length'] = req.body.length.to_s
                end
    puts response
end
需要“optpass”
需要“CGI”
需要“openssl”
需要“base64”
需要“法拉第”
需要“摘要”
def generateToken(url、keyname、keyvalue)
encoded=CGI::转义(url)
ttl=(Time.now+60*5).to_i
signature=“#{encoded}\n#{ttl}”
#签名
键=键值
#dig=OpenSSL::HMAC.digest('sha256',密钥,签名)
dig=摘要::HMAC.Digest(签名、密钥、摘要::SHA256)
hash=CGI.escape(Base64.strict_encode64(dig))
#放入散列
返回“SharedAccessSignature sig=#{hash}&se=#{ttl}&skn=#{keyname}&sr=#{encoded}”
结束
def build_连接(url、令牌)
conn=Faraday.new(:url=>url)do | Faraday|
请求:url#u encoded#form encode POST参数
faraday.response:logger#记录对标准输出的请求
faraday.adapter faraday.default#适配器#使用Net::HTTP发出请求
结束
conn.headers['Content-Type']='application/json'
conn.headers['Authorization']=令牌
返回控制
结束
如果文件=0美元
ARGV