gpg_key.rb 2.09 KB
Newer Older
Alexis Reigel's avatar
Alexis Reigel committed
1
class GpgKey < ActiveRecord::Base
2
3
  include AfterCommitQueue

Alexis Reigel's avatar
Alexis Reigel committed
4
5
6
  KEY_PREFIX = '-----BEGIN PGP PUBLIC KEY BLOCK-----'.freeze

  belongs_to :user
7
  has_many :gpg_signatures, dependent: :nullify
Alexis Reigel's avatar
Alexis Reigel committed
8
9
10
11
12

  validates :key,
    presence: true,
    uniqueness: true,
    format: {
13
14
      with: /\A#{KEY_PREFIX}((?!#{KEY_PREFIX}).)+\Z/m,
      message: "is invalid. A valid public GPG key begins with '#{KEY_PREFIX}'"
Alexis Reigel's avatar
Alexis Reigel committed
15
16
    }

17
18
19
20
21
22
23
  validates :fingerprint,
    presence: true,
    uniqueness: true,
    # only validate when the `key` is valid, as we don't want the user to show
    # the error about the fingerprint
    unless: -> { errors.has_key?(:key) }

24
25
26
27
28
29
30
31
  validates :primary_keyid,
    presence: true,
    uniqueness: true,
    # only validate when the `key` is valid, as we don't want the user to show
    # the error about the fingerprint
    unless: -> { errors.has_key?(:key) }

  before_validation :extract_fingerprint, :extract_primary_keyid
32
  after_create :update_invalid_gpg_signatures_after_create
33
  after_create :notify_user
Alexis Reigel's avatar
Alexis Reigel committed
34
35
36
37
38
39

  def key=(value)
    value.strip! unless value.blank?
    write_attribute(:key, value)
  end

Alexis Reigel's avatar
Alexis Reigel committed
40
  def emails
41
42
43
    @emails ||= Gitlab::Gpg.emails_from_key(key)
  end

Alexis Reigel's avatar
Alexis Reigel committed
44
  def emails_with_verified_status
45
    emails.map do |email|
Alexis Reigel's avatar
Alexis Reigel committed
46
47
      [
        email,
Alexis Reigel's avatar
Alexis Reigel committed
48
        email == user.email
Alexis Reigel's avatar
Alexis Reigel committed
49
50
51
52
      ]
    end
  end

53
54
55
56
  def verified?
    emails_with_verified_status.any? { |_email, verified| verified }
  end

57
  def update_invalid_gpg_signatures
58
    InvalidGpgSignatureUpdateWorker.perform_async(self.id)
59
60
  end

Alexis Reigel's avatar
Alexis Reigel committed
61
62
63
64
65
  private

  def extract_fingerprint
    # we can assume that the result only contains one item as the validation
    # only allows one key
66
    self.fingerprint = Gitlab::Gpg.fingerprints_from_key(key).first
Alexis Reigel's avatar
Alexis Reigel committed
67
  end
68

69
70
71
72
73
74
  def extract_primary_keyid
    # we can assume that the result only contains one item as the validation
    # only allows one key
    self.primary_keyid = Gitlab::Gpg.primary_keyids_from_key(key).first
  end

75
76
77
  def notify_user
    run_after_commit { NotificationService.new.new_gpg_key(self) }
  end
78
79
80
81

  def update_invalid_gpg_signatures_after_create
    run_after_commit { update_invalid_gpg_signatures }
  end
Alexis Reigel's avatar
Alexis Reigel committed
82
end