verifier.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # frozen_string_literal: true
  2. class ProofProvider::Keybase::Verifier
  3. def initialize(local_username, provider_username, token)
  4. @local_username = local_username
  5. @provider_username = provider_username
  6. @token = token
  7. end
  8. def valid?
  9. request = Request.new(:get, "#{ProofProvider::Keybase::BASE_URL}/_/api/1.0/sig/proof_valid.json", params: query_params)
  10. request.perform do |res|
  11. json = Oj.load(res.body_with_limit, mode: :strict)
  12. if json.is_a?(Hash)
  13. json.fetch('proof_valid', false)
  14. else
  15. false
  16. end
  17. end
  18. rescue Oj::ParseError, HTTP::Error, OpenSSL::SSL::SSLError
  19. false
  20. end
  21. def on_success_path(user_agent = nil)
  22. url = Addressable::URI.parse("#{ProofProvider::Keybase::BASE_URL}/_/proof_creation_success")
  23. url.query_values = query_params.merge(kb_ua: user_agent || 'unknown')
  24. url.to_s
  25. end
  26. def status
  27. request = Request.new(:get, "#{ProofProvider::Keybase::BASE_URL}/_/api/1.0/sig/proof_live.json", params: query_params)
  28. request.perform do |res|
  29. raise ProofProvider::Keybase::UnexpectedResponseError unless res.code == 200
  30. json = Oj.load(res.body_with_limit, mode: :strict)
  31. raise ProofProvider::Keybase::UnexpectedResponseError unless json.is_a?(Hash) && json.key?('proof_valid') && json.key?('proof_live')
  32. json
  33. end
  34. rescue Oj::ParseError, HTTP::Error, OpenSSL::SSL::SSLError
  35. raise ProofProvider::Keybase::UnexpectedResponseError
  36. end
  37. private
  38. def query_params
  39. {
  40. domain: ProofProvider::Keybase::DOMAIN,
  41. kb_username: @provider_username,
  42. username: @local_username,
  43. sig_hash: @token,
  44. }
  45. end
  46. end