diff options
author | Azul <azul@leap.se> | 2013-11-26 14:39:42 +0100 |
---|---|---|
committer | Azul <azul@leap.se> | 2013-11-26 14:39:42 +0100 |
commit | e34141c3265c6daeda92bcb83fa508de00551bc3 (patch) | |
tree | 0ed90fb6b04f60e4b7e1cf8e22709d4ade216859 /users/app/models | |
parent | 7de12c71ce7eb4eeb6e0795275434ed4a4120c25 (diff) |
simple validation for pgp key format
Diffstat (limited to 'users/app/models')
-rw-r--r-- | users/app/models/pgp_key.rb | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/users/app/models/pgp_key.rb b/users/app/models/pgp_key.rb index fddec1e..66f8660 100644 --- a/users/app/models/pgp_key.rb +++ b/users/app/models/pgp_key.rb @@ -1,25 +1,48 @@ class PgpKey include ActiveModel::Validations + KEYBLOCK_IDENTIFIERS = [ + '-----BEGIN PGP PUBLIC KEY BLOCK-----', + '-----END PGP PUBLIC KEY BLOCK-----', + ] + # mostly for testing. - attr_accessor :key_block + attr_accessor :keyblock + + validate :validate_keyblock_format - def initialize(key_block = nil) - @key_block = key_block + def initialize(keyblock = nil) + @keyblock = keyblock end def to_s - @key_block + @keyblock end def present? - @key_block.present? + @keyblock.present? end - # let's allow comparison with plain key_block strings. + # allow comparison with plain keyblock strings. def ==(other) self.equal?(other) or - self.to_s == other + # relax the comparison on line ends. + self.to_s.tr_s("\n\r", '') == other.tr_s("\r\n", '') + end + + protected + + def validate_keyblock_format + if keyblock_identifier_missing? + errors.add :public_key_block, + "does not look like an armored pgp public key block" + end + end + + def keyblock_identifier_missing? + KEYBLOCK_IDENTIFIERS.find do |identify| + !@keyblock.include?(identify) + end end end |