Class: GatecoinAPI::Client::SigningMiddleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/gatecoin-api/client.rb

Constant Summary collapse

API_PUBLIC_KEY =
CaseSensitiveString.new('API_PUBLIC_KEY').freeze
API_REQUEST_DATE =
CaseSensitiveString.new('API_REQUEST_DATE').freeze
API_REQUEST_SIGNATURE =
CaseSensitiveString.new('API_REQUEST_SIGNATURE').freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, public_key, private_key) ⇒ SigningMiddleware

Returns a new instance of SigningMiddleware.



229
230
231
232
233
# File 'lib/gatecoin-api/client.rb', line 229

def initialize(app, public_key, private_key)
  @app         = app
  @public_key  = public_key
  @private_key = private_key
end

Instance Method Details

#call(env) ⇒ Object



235
236
237
238
239
240
# File 'lib/gatecoin-api/client.rb', line 235

def call(env)
  env[:request_headers][API_PUBLIC_KEY]        = @public_key
  env[:request_headers][API_REQUEST_DATE]      = Time.now.to_f.round(3).to_s
  env[:request_headers][API_REQUEST_SIGNATURE] = signature(env)
  @app.call(env)
end

#signature(env) ⇒ Object

Base64Encode(

HMAC-SHA256(
  (“POST" + "https://staging.gatecoin.com/api/RegisterUser” +
   "application/json" + "1447700841.477091").downcase,
  @private_key
)

)



249
250
251
252
253
254
# File 'lib/gatecoin-api/client.rb', line 249

def signature(env)
  http_method  = env[:method].to_s.upcase
  content_type = http_method == 'GET' ? '' : env[:request_headers][:content_type]
  message      = "#{http_method}#{env[:url]}#{content_type}#{env[:request_headers][API_REQUEST_DATE]}".downcase
  Base64.strict_encode64 OpenSSL::HMAC.digest('sha256', @private_key, message)
end