diff options
author | Alexis Reigel <mail@koffeinfrei.org> | 2017-02-22 12:49:17 +0100 |
---|---|---|
committer | Alexis Reigel <mail@koffeinfrei.org> | 2017-07-27 15:40:40 +0200 |
commit | fbf1fd1a204a24aef2b80473ec64a520ed2a2dfc (patch) | |
tree | eb1656bf0a16ab379b1ac1a5cd8cf86bf771b0c1 /app/models/gpg_key.rb | |
parent | 28bb5e3d53a585b1fb958d1d91622da0a038bea8 (diff) | |
download | gitlab-ce-fbf1fd1a204a24aef2b80473ec64a520ed2a2dfc.tar.gz |
add gpg key model
Diffstat (limited to 'app/models/gpg_key.rb')
-rw-r--r-- | app/models/gpg_key.rb | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/app/models/gpg_key.rb b/app/models/gpg_key.rb new file mode 100644 index 00000000000..16de0b542d7 --- /dev/null +++ b/app/models/gpg_key.rb @@ -0,0 +1,35 @@ +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 + + 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 |