如何使用查询参数生成查询字符串哈希

时间:2018-11-08 10:12:34

标签: ruby-on-rails ruby sha

我正在使用JWT令牌身份验证向JIRA发送API请求。我将方法(get,post等)和端点添加到SHA256编码中。这样成功了:

qsh = Digest::SHA256.hexdigest("GET&#{endpoint}&")
jwt = JWT.encode({
                     qsh: qsh,
                     iat: issued_at,
                     exp: expires_at,
                     iss: key
                   }, secret)

但是,我无法将查询参数添加到URI。如果我追加查询参数:

qsh = Digest::SHA256.hexdigest("GET&#{endpoint}&start=50&limit=50")
jwt = JWT.encode({
                     qsh: qsh,
                     iat: issued_at,
                     exp: expires_at,
                     iss: key
                   }, secret)

我收到未经授权的回复401。

MVP:

jira_request(:get,"/rest/servicedeskapi/servicedesk/#{serviceDeskId}/organization", nil)
def jira_request(method, endpoint, data)
    request = Typhoeus::Request.new(jira_rest_api_url(method, endpoint),
                                    followlocation: true, method: method,
                                    body: data ? data.to_json : nil,
                                    headers: { 'X-ExperimentalApi' => 'opt-in',
                                               'Content-Type' => 'application/json' })

request.on_complete do |response|
  if response.success? && !response.body.blank?
    return JSON.parse(response.body)
  elsif response.code == 204
    return true
  else
    return false
  end
end
request.run


end

  # Creating JWT token to Auth for each request
  def jira_rest_api_url(method, endpoint)
    # Gets the ADDON details for generating JWT token
    jwt_auth = MyJToken.first

issued_at = Time.now.utc.to_i
expires_at = issued_at + 500

qsh = Digest::SHA256.hexdigest("#{method.to_s.upcase}&#{endpoint}&")

jwt = JWT.encode({   qsh: qsh,
                     iat: issued_at,
                     exp: expires_at,
                     iss: jwt_auth.key
                   }, jwt_auth.secret)

# return the service call URL with the JWT token added
  "#{jwt_auth.api_base_url}#{endpoint}?jwt=#{jwt}"
end
  end

1 个答案:

答案 0 :(得分:1)

哈希在其中的参数

qsh = Digest::SHA256.hexdigest("GET&#{endpoint}&limit=50&start=50")

应添加到请求网址中:

"#{jwt_auth.api_base_url}#{endpoint}?jwt=#{jwt}&start=50&limit=50"