gpg_key.rb 2.37 KB
Newer Older
Alexis Reigel's avatar
Alexis Reigel committed
1
2
3
4
class GpgKey < ActiveRecord::Base
  KEY_PREFIX = '-----BEGIN PGP PUBLIC KEY BLOCK-----'.freeze

  belongs_to :user
5
  has_many :gpg_signatures, dependent: :nullify
Alexis Reigel's avatar
Alexis Reigel committed
6

7
8
  validates :user, presence: true

Alexis Reigel's avatar
Alexis Reigel committed
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
33
  after_commit :update_invalid_gpg_signatures, on: :create
  after_commit :notify_user, on: :create
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

40
41
42
43
44
45
46
47
  def user_infos
    @user_infos ||= Gitlab::Gpg.user_infos_from_key(key)
  end

  def verified_user_infos
    user_infos.select do |user_info|
      user_info[:email] == user.email
    end
Alexis Reigel's avatar
Alexis Reigel committed
48
49
  end

50
  def emails_with_verified_status
51
    user_infos.map do |user_info|
Alexis Reigel's avatar
Alexis Reigel committed
52
      [
53
54
        user_info[:email],
        user_info[:email] == user.email
Alexis Reigel's avatar
Alexis Reigel committed
55
      ]
Alexis Reigel's avatar
Alexis Reigel committed
56
    end.to_h
Alexis Reigel's avatar
Alexis Reigel committed
57
58
  end

59
60
61
62
  def verified?
    emails_with_verified_status.any? { |_email, verified| verified }
  end

63
  def update_invalid_gpg_signatures
64
    InvalidGpgSignatureUpdateWorker.perform_async(self.id)
65
66
  end

Alexis Reigel's avatar
Alexis Reigel committed
67
68
69
70
71
72
73
74
75
76
77
  def revoke
    GpgSignature.where(gpg_key: self, valid_signature: true).find_each do |gpg_signature|
      gpg_signature.update_attributes!(
        gpg_key: nil,
        valid_signature: false
      )
    end

    destroy
  end

Alexis Reigel's avatar
Alexis Reigel committed
78
79
80
81
82
  private

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

86
87
88
89
90
91
  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

92
  def notify_user
93
    NotificationService.new.new_gpg_key(self)
94
  end
Alexis Reigel's avatar
Alexis Reigel committed
95
end