keybase.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # frozen_string_literal: true
  2. class ProofProvider::Keybase
  3. BASE_URL = ENV.fetch('KEYBASE_BASE_URL', 'https://keybase.io')
  4. DOMAIN = ENV.fetch('KEYBASE_DOMAIN', Rails.configuration.x.web_domain)
  5. class Error < StandardError; end
  6. class ExpectedProofLiveError < Error; end
  7. class UnexpectedResponseError < Error; end
  8. def initialize(proof = nil)
  9. @proof = proof
  10. end
  11. def serializer_class
  12. ProofProvider::Keybase::Serializer
  13. end
  14. def worker_class
  15. ProofProvider::Keybase::Worker
  16. end
  17. def validate!
  18. unless @proof.token&.size == 66
  19. @proof.errors.add(:base, I18n.t('identity_proofs.errors.keybase.invalid_token'))
  20. return
  21. end
  22. # Do not perform synchronous validation for remote accounts
  23. return if @proof.provider_username.blank? || !@proof.account.local?
  24. if verifier.valid?
  25. @proof.verified = true
  26. @proof.live = false
  27. else
  28. @proof.errors.add(:base, I18n.t('identity_proofs.errors.keybase.verification_failed', kb_username: @proof.provider_username))
  29. end
  30. end
  31. def refresh!
  32. worker_class.new.perform(@proof)
  33. rescue ProofProvider::Keybase::Error
  34. nil
  35. end
  36. def on_success_path(user_agent = nil)
  37. verifier.on_success_path(user_agent)
  38. end
  39. def badge
  40. @badge ||= ProofProvider::Keybase::Badge.new(@proof.account.username, @proof.provider_username, @proof.token, domain)
  41. end
  42. def verifier
  43. @verifier ||= ProofProvider::Keybase::Verifier.new(@proof.account.username, @proof.provider_username, @proof.token, domain)
  44. end
  45. private
  46. def domain
  47. if @proof.account.local?
  48. DOMAIN
  49. else
  50. @proof.account.domain
  51. end
  52. end
  53. end