module Bitcointerminal::JwtAuth

Private Instance Methods

authenticated_get(url, params = {}) click to toggle source
# File lib/bitcointerminal/jwt_auth.rb, line 7
def authenticated_get(url, params = {})
  new_rest_connection.get do |req|
    req.url build_url(url)
    params.each do |k,v|
      req.params[k] = v
    end
    req.headers = req.headers = get_headers()
    req.options.timeout = config.rest_timeout
    req.options.open_timeout = config.rest_open_timeout
  end
end
authenticated_patch(url, options = {}) click to toggle source
# File lib/bitcointerminal/jwt_auth.rb, line 32
def authenticated_patch(url, options = {})
  # p "authenticated_patch: #{url} #{options}"
  body = options[:params] || {}
  new_rest_connection.patch do |req|
    req.url build_url(url)
    req.headers = get_headers()
    req.body = body.to_json
    req.options.timeout = config.rest_timeout
    req.options.open_timeout = config.rest_open_timeout
  end
end
authenticated_post(url, options = {}) click to toggle source
# File lib/bitcointerminal/jwt_auth.rb, line 20
def authenticated_post(url, options = {})
  body = options[:params] || {}
  # p "x: #{url} #{body}"
  new_rest_connection.post do |req|
    req.url build_url(url)
    req.headers = get_headers()
    req.body = body.to_json
    req.options.timeout = config.rest_timeout
    req.options.open_timeout = config.rest_open_timeout
  end
end
base_api_endpoint() click to toggle source
# File lib/bitcointerminal/jwt_auth.rb, line 90
def base_api_endpoint
  # puts "config: #{config.api_endpoint}"
  url = URI.parse(config.api_endpoint)
  "#{url.scheme}://#{url.host}:#{url.port}"
end
build_url(url) click to toggle source
# File lib/bitcointerminal/jwt_auth.rb, line 74
def build_url(url)
  # URI.join(config.api_endpoint, url).path
  URI.join(config.api_endpoint, url).request_uri
end
fullpath(path, query) click to toggle source
# File lib/bitcointerminal/jwt_auth.rb, line 96
def fullpath(path, query)
  if query.empty?
    path
  else
    "#{path}?#{query}"
  end
end
get_bearer_token() click to toggle source
# File lib/bitcointerminal/jwt_auth.rb, line 61
def get_bearer_token
  url = URI(config.token_url)
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  # http.verify_mode =  OpenSSL::SSL::VERIFY_PEER
  request = Net::HTTP::Post.new(url)
  request["content-type"] = 'application/json'
  request.body = "{\"client_id\":\"#{config.client_id}\",\"client_secret\":\"#{config.client_secret}\",\"audience\":\"#{config.audience}\",\"grant_type\":\"#{config.grant_type}\"}"
  response = http.request(request)
  JSON.parse(response.body)
end
get_headers() click to toggle source
# File lib/bitcointerminal/jwt_auth.rb, line 45
def get_headers()
  headers = {}
  headers['Content-Type'] = 'application/json'
  #syscom
  if config.authorization_token
    headers['authorization'] = config.authorization_token
  else
    #auth0
    bearer = get_bearer_token
    headers['authorization'] = "#{bearer["token_type"]} #{bearer["access_token"]}"
  end

  puts "headers: #{headers}"
  return headers
end
new_rest_connection() click to toggle source
# File lib/bitcointerminal/jwt_auth.rb, line 79
def new_rest_connection
  Faraday.new(url: base_api_endpoint) do |conn|
    # conn.use Bitfinex::CustomErrors
    conn.response :logger, Logger.new(STDOUT) , bodies: true  if config.debug_connection
    # conn.request :json
    # conn.response :json, :content_type => /\bjson$/
    # conn.response :logger
    conn.adapter :net_http
  end
end