gitlab-org--gitlab-foss/app/models/gpg_key.rb

101 lines
2.5 KiB
Ruby
Raw Normal View History

2017-02-22 11:49:17 +00:00
class GpgKey < ActiveRecord::Base
KEY_PREFIX = '-----BEGIN PGP PUBLIC KEY BLOCK-----'.freeze
2017-07-20 13:44:15 +00:00
include ShaAttribute
sha_attribute :primary_keyid
sha_attribute :fingerprint
2017-02-22 11:49:17 +00:00
belongs_to :user
has_many :gpg_signatures, dependent: :nullify
2017-02-22 11:49:17 +00:00
2017-07-05 12:03:36 +00:00
validates :user, presence: true
2017-02-22 11:49:17 +00:00
validates :key,
presence: true,
uniqueness: true,
format: {
with: /\A#{KEY_PREFIX}((?!#{KEY_PREFIX}).)+\Z/m,
message: "is invalid. A valid public GPG key begins with '#{KEY_PREFIX}'"
2017-02-22 11:49:17 +00:00
}
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) }
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
after_commit :update_invalid_gpg_signatures, on: :create
after_commit :notify_user, on: :create
2017-02-22 11:49:17 +00:00
def key=(value)
value.strip! unless value.blank?
write_attribute(:key, value)
end
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
2017-02-22 14:37:49 +00:00
end
def emails_with_verified_status
user_infos.map do |user_info|
2017-02-24 20:28:26 +00:00
[
user_info[:email],
user_info[:email] == user.email
2017-02-24 20:28:26 +00:00
]
2017-07-05 11:16:50 +00:00
end.to_h
2017-02-24 20:28:26 +00:00
end
def verified?
emails_with_verified_status.any? { |_email, verified| verified }
end
def update_invalid_gpg_signatures
InvalidGpgSignatureUpdateWorker.perform_async(self.id)
end
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
2017-02-22 11:49:17 +00:00
private
def extract_fingerprint
# we can assume that the result only contains one item as the validation
# only allows one key
2017-02-22 16:20:42 +00:00
self.fingerprint = Gitlab::Gpg.fingerprints_from_key(key).first
2017-02-22 11:49:17 +00:00
end
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
2017-02-28 09:49:59 +00:00
def notify_user
NotificationService.new.new_gpg_key(self)
end
2017-02-22 11:49:17 +00:00
end