Module: Legion::Data::Encryption::SequelPlugin::ClassMethods

Defined in:
lib/legion/data/encryption/sequel_plugin.rb

Instance Method Summary collapse

Instance Method Details

#encrypted_column(name, key_scope: :default) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/legion/data/encryption/sequel_plugin.rb', line 43

def encrypted_column(name, key_scope: :default)
  col_scope = key_scope
  encrypted_columns[name] = { key_scope: col_scope }

  define_method(name) do
    raw = super()
    return nil if raw.nil?

    begin
      decrypt_encrypted_column(name, raw, key_scope: col_scope)
    rescue StandardError => e
      Legion::Data::Encryption::SequelPlugin.handle_exception(
        e,
        level:       :warn,
        handled:     false,
        operation:   :decrypt_column,
        table:       self.class.table_name,
        primary_key: pk,
        column:      name
      )
      raise
    end
  end

  define_method(:"#{name}=") do |value|
    if value.nil?
      clear_pending_encrypted_column(name)
      super(nil)
    else
      begin
        remember_pending_encrypted_column(name, value, key_scope: col_scope) if new?
        super(encrypt_encrypted_column(name, value, key_scope: col_scope, primary_key: pk || 0))
      rescue StandardError => e
        Legion::Data::Encryption::SequelPlugin.handle_exception(
          e,
          level:       :error,
          handled:     false,
          operation:   :encrypt_column,
          table:       self.class.table_name,
          primary_key: pk,
          column:      name
        )
        raise
      end
    end
  end
end

#encrypted_columnsObject



39
40
41
# File 'lib/legion/data/encryption/sequel_plugin.rb', line 39

def encrypted_columns
  @encrypted_columns ||= {}
end

#encryption_key_providerObject



91
92
93
# File 'lib/legion/data/encryption/sequel_plugin.rb', line 91

def encryption_key_provider
  @encryption_key_provider ||= KeyProvider.new
end