Class: ClWiki::User
- Includes:
- ActiveModel::SecurePassword, ActiveModel::Serializers::JSON
- Defined in:
- app/models/cl_wiki/user.rb
Instance Attribute Summary collapse
-
#encryption_key ⇒ Object
readonly
Returns the value of attribute encryption_key.
-
#password_digest ⇒ Object
Returns the value of attribute password_digest.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
Instance Method Summary collapse
- #attributes ⇒ Object
- #attributes=(hash) ⇒ Object
-
#cached_encryption_key=(value) ⇒ Object
Never, never, persist this! It needs to be pushed in from the session store, for usage down deeper in the ClWiki ‘lib` code.
- #can_encrypt? ⇒ Boolean
-
#derive_encryption_key(password) ⇒ Object
Generate a consistent key that can be used with Lockbox for encrypting content, and that is not persisted anywhere.
- #name ⇒ Object
- #save ⇒ Object
Methods inherited from UserBase
Instance Attribute Details
#encryption_key ⇒ Object (readonly)
Returns the value of attribute encryption_key.
10 11 12 |
# File 'app/models/cl_wiki/user.rb', line 10 def encryption_key @encryption_key end |
#password_digest ⇒ Object
Returns the value of attribute password_digest.
11 12 13 |
# File 'app/models/cl_wiki/user.rb', line 11 def password_digest @password_digest end |
#username ⇒ Object
Returns the value of attribute username.
11 12 13 |
# File 'app/models/cl_wiki/user.rb', line 11 def username @username end |
Class Method Details
.create(username, password) ⇒ Object
26 27 28 29 30 31 32 |
# File 'app/models/cl_wiki/user.rb', line 26 def self.create(username, password) self.new.tap do |u| u.username = username u.password = password u.save end end |
.find(username) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'app/models/cl_wiki/user.rb', line 34 def self.find(username) user_file = users_root("#{username}.json") if ::File.exist?(user_file) user = self.new json = ::File.read(user_file) user.from_json(json) end end |
Instance Method Details
#attributes ⇒ Object
15 16 17 18 |
# File 'app/models/cl_wiki/user.rb', line 15 def attributes {username: self.username, password_digest: self.password_digest} end |
#attributes=(hash) ⇒ Object
20 21 22 23 24 |
# File 'app/models/cl_wiki/user.rb', line 20 def attributes=(hash) hash.each do |key, value| send("#{key}=", value) end end |
#cached_encryption_key=(value) ⇒ Object
Never, never, persist this! It needs to be pushed in from the session store, for usage down deeper in the ClWiki ‘lib` code.
77 78 79 |
# File 'app/models/cl_wiki/user.rb', line 77 def cached_encryption_key=(value) @encryption_key = value end |
#can_encrypt? ⇒ Boolean
85 86 87 |
# File 'app/models/cl_wiki/user.rb', line 85 def can_encrypt? true end |
#derive_encryption_key(password) ⇒ Object
Generate a consistent key that can be used with Lockbox for encrypting content, and that is not persisted anywhere.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/models/cl_wiki/user.rb', line 58 def derive_encryption_key(password) if authenticate(password) pass = 'secret' salt = 'static salt' # so the same key is derived each time iter = 10_000 hash = OpenSSL::Digest::SHA256.new len = hash.digest_length OpenSSL::KDF.pbkdf2_hmac(pass, salt: salt, iterations: iter, length: len, hash: hash) else raise 'Could not authenticate password' end end |
#name ⇒ Object
81 82 83 |
# File 'app/models/cl_wiki/user.rb', line 81 def name self.username end |
#save ⇒ Object
43 44 45 46 47 48 49 |
# File 'app/models/cl_wiki/user.rb', line 43 def save ::File.open(User.users_root("#{username}.json"), 'w') do |f| # as_json yields a Hash for some reason, which then can't be parsed # when read from disk. f.write(self.attributes.to_json) end end |