bitpay / ruby-client

Powerful, flexible, lightweight SDK for the BitPay Bitcoin Payment Gateway API.
MIT License
79 stars 64 forks source link

verify_tokens can not be used to check if client is paired #49

Open zedalaye opened 8 years ago

zedalaye commented 8 years ago

Hi,

It looks like verify_tokens can't be used to check whether the client has been paired with the BitPay server.

When no params has been passed and not tokens have been paired, it always returns true instead of false.

def verify_tokens(tokens: {})
  server_tokens={}
  tokens.each{|key, value| return false if server_tokens[key] != value}
  return true
end

If tokens is empty, it will "not" return and so pass to the next expression and then return true.

I used this code as workaround :

begin
  verified = client.verify_tokens && (tokens = client.instance_variable_get('@tokens')) && tokens.is_a?(Hash) && !tokens.empty?
rescue BitPay::BitPayError
  verified = false
end

I propose to rewrite verify_tokens as :

def verify_tokens(tokens: @tokens)
  server_tokens = refresh_tokens
  return !tokens.empty? && tokens.any? { |key, value| server_tokens[key] == value }
end

Or all? if all local tokens must match server tokens.

zedalaye commented 8 years ago

return here is useless.

And I think it must also check that server_tokens is not blank and that all local tokens are contained in server_tokens :

def verify_tokens(tokens: @tokens)
  server_tokens = refresh_tokens
  !tokens.empty? && !server_tokens.empty? && 
    (server_tokens.values_at(*tokens.keys) == tokens.values)
end
philosodad commented 8 years ago

This method should probably just be removed, no other code depends on it and it isn't part of the core functionality of the client.

zedalaye commented 8 years ago

Maybe but it's very useful.