summaryrefslogtreecommitdiff
path: root/app/models/pgp_key.rb
blob: 3384f4cf56129f07fe69cb41ffc0471720f10868 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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)
    return false if (self.present? != other.present?)
    self.equal?(other) or
    # relax the comparison on line ends.
    self.to_s.tr_s("\n\r", '') == other.tr_s("\n\r", '')
  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