about summary refs log tree commit diff
path: root/app/lib/proof_provider/keybase.rb
blob: 9680b90eedcd7e8bda75b019e3f6418793c189f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true

class ProofProvider::Keybase
  BASE_URL = ENV.fetch('KEYBASE_BASE_URL', 'https://keybase.io')
  DOMAIN   = ENV.fetch('KEYBASE_DOMAIN', Rails.configuration.x.local_domain)

  class Error < StandardError; end

  class ExpectedProofLiveError < Error; end

  class UnexpectedResponseError < Error; end

  def initialize(proof = nil)
    @proof = proof
  end

  def serializer_class
    ProofProvider::Keybase::Serializer
  end

  def worker_class
    ProofProvider::Keybase::Worker
  end

  def validate!
    unless @proof.token&.size == 66
      @proof.errors.add(:base, I18n.t('identity_proofs.errors.keybase.invalid_token'))
      return
    end

    # Do not perform synchronous validation for remote accounts
    return if @proof.provider_username.blank? || !@proof.account.local?

    if verifier.valid?
      @proof.verified = true
      @proof.live     = false
    else
      @proof.errors.add(:base, I18n.t('identity_proofs.errors.keybase.verification_failed', kb_username: @proof.provider_username))
    end
  end

  def refresh!
    worker_class.new.perform(@proof)
  rescue ProofProvider::Keybase::Error
    nil
  end

  def on_success_path(user_agent = nil)
    verifier.on_success_path(user_agent)
  end

  def badge
    @badge ||= ProofProvider::Keybase::Badge.new(@proof.account.username, @proof.provider_username, @proof.token, domain)
  end

  def verifier
    @verifier ||= ProofProvider::Keybase::Verifier.new(@proof.account.username, @proof.provider_username, @proof.token, domain)
  end

  private

  def domain
    if @proof.account.local?
      DOMAIN
    else
      @proof.account.domain
    end
  end
end