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

41 lines
829 B
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
belongs_to :user
validates :fingerprint,
presence: true,
uniqueness: true
validates :key,
presence: true,
uniqueness: true,
format: {
with: /\A#{KEY_PREFIX}((?!#{KEY_PREFIX}).)+\Z/m
}
before_validation :extract_fingerprint
def key=(value)
value.strip! unless value.blank?
write_attribute(:key, value)
end
2017-02-22 14:37:49 +00:00
def emails
raw_key = GPGME::Key.get(fingerprint)
raw_key.uids.map(&:email)
end
2017-02-22 11:49:17 +00:00
private
def extract_fingerprint
import = GPGME::Key.import(key)
return if import.considered == 0
# we can assume that the result only contains one item as the validation
# only allows one key
self.fingerprint = import.imports.first.fingerprint
end
end