Module: Rahasia::Model
- Defined in:
- lib/rahasia/model.rb
Overview
Model for ActiveRecord extension github.com/ankane/lockbox/blob/80adc597b27172b5e420471db5888b8f962ad829/lib/lockbox/model.rb
Instance Method Summary collapse
- #default_string ⇒ Object
- #define_attributes(attributes) ⇒ Object
-
#define_decrypt(name, encrypted_attribute, decrypt_method_name) ⇒ Object
refresh_token token.
-
#define_decrypt_method_name(decrypt_method_name) ⇒ Object
———————————— available column refresh_token, name ———————————— Credential < ActiveRecord enrcypt_column :refresh_token end.
-
#define_setter(name) ⇒ Object
refresh_token=(string) # refresh_token_encrypted.
- #enrcypt_column(*attributes, **options) ⇒ Object
- #not_encrypted?(message) ⇒ Boolean
Instance Method Details
#default_string ⇒ Object
20 21 22 |
# File 'lib/rahasia/model.rb', line 20 def default_string "--encrypted:#{SecureRandom.hex(16)}---" end |
#define_attributes(attributes) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rahasia/model.rb', line 24 def define_attributes(attributes) attributes.each do |name| encrypted_attribute = "#{name}_encrypted" decrypt_method_name = "decrypt_#{encrypted_attribute}" define_decrypt_method_name(decrypt_method_name) define_decrypt(name, encrypted_attribute, decrypt_method_name) define_setter(name) end end |
#define_decrypt(name, encrypted_attribute, decrypt_method_name) ⇒ Object
refresh_token token
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rahasia/model.rb', line 63 def define_decrypt(name, encrypted_attribute, decrypt_method_name) define_method(name) do = super() if self.class.not_encrypted?() ciphertext = send(encrypted_attribute) = self.class.send(decrypt_method_name, ciphertext, context: self) end end end |
#define_decrypt_method_name(decrypt_method_name) ⇒ Object
available column refresh_token, name
Credential < ActiveRecord
enrcypt_column :refresh_token
end
available method
# decrypt_refresh_token_encrypted
# refresh_token_chipertext?
# refresh_token
# refresh_token=(string)
# refresh_token_encrypted
refresh_token_encrypted token_encrypted
52 53 54 55 56 57 58 59 |
# File 'lib/rahasia/model.rb', line 52 def define_decrypt_method_name(decrypt_method_name) define_singleton_method decrypt_method_name do |encrypted, **_opts| Rahasia.encryptor.decrypt( key: Rahasia.rahasia_key, value: encrypted ) end end |
#define_setter(name) ⇒ Object
refresh_token=(string)
# refresh_token_encrypted
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rahasia/model.rb', line 77 def define_setter(name) define_method("#{name}=") do |val| begin val = val.presence || '' str = Rahasia.encryptor.encrypt(key: Rahasia.rahasia_key, value: val) send("#{name}_encrypted=", str) super(self.class.default_string) rescue ArgumentError => e puts e end end end |
#enrcypt_column(*attributes, **options) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/rahasia/model.rb', line 7 def enrcypt_column(*attributes, **) unless [nil, :string].include?([:type]) raise ArgumentError, "UnknownType #{[:type]}." end activerecord = defined?(ActiveRecord::Base) && self < ActiveRecord::Base if [:type] && !activerecord raise ArgumentError, 'Type not supported yet with Mongoid' end define_attributes(attributes) end |
#not_encrypted?(message) ⇒ Boolean
90 91 92 93 94 |
# File 'lib/rahasia/model.rb', line 90 def not_encrypted?() return false if .nil? .match(/--encrypted:/) ? true : false end |