diff options
author | jessib <jessib@riseup.net> | 2013-12-02 10:17:19 -0800 |
---|---|---|
committer | jessib <jessib@riseup.net> | 2013-12-02 10:17:19 -0800 |
commit | 8de6f143e53af5287b41913dcf3c7969f452fbc9 (patch) | |
tree | 6a311eb5cf4c20fa2053022ca9a45a92f0502f87 /users/app/models/pgp_key.rb | |
parent | b75f8781bb55557f13a9a4ae48fc45e5d6f1ee86 (diff) | |
parent | dade6497424a869db5f1dfb030f88f4711278b81 (diff) |
Merge pull request #118 from azul/feature/validate-pgp-keys
Feature/validate pgp keys
Diffstat (limited to 'users/app/models/pgp_key.rb')
-rw-r--r-- | users/app/models/pgp_key.rb | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/users/app/models/pgp_key.rb b/users/app/models/pgp_key.rb new file mode 100644 index 0000000..66f8660 --- /dev/null +++ b/users/app/models/pgp_key.rb @@ -0,0 +1,48 @@ +class PgpKey + include ActiveModel::Validations + + KEYBLOCK_IDENTIFIERS = [ + '-----BEGIN PGP PUBLIC KEY BLOCK-----', + '-----END PGP PUBLIC KEY BLOCK-----', + ] + + # mostly for testing. + attr_accessor :keyblock + + validate :validate_keyblock_format + + def initialize(keyblock = nil) + @keyblock = keyblock + end + + def to_s + @keyblock + end + + def present? + @keyblock.present? + end + + # allow comparison with plain keyblock strings. + def ==(other) + self.equal?(other) or + # 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 |