Class: ClWiki::User

Inherits:
UserBase show all
Includes:
ActiveModel::SecurePassword, ActiveModel::Serializers::JSON
Defined in:
app/models/cl_wiki/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from UserBase

#lockbox

Instance Attribute Details

#encryption_keyObject (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_digestObject

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

#usernameObject

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

.users_root(filename) ⇒ Object



51
52
53
54
# File 'app/models/cl_wiki/user.rb', line 51

def self.users_root(filename)
  root = FileUtils.makedirs(::File.join($wiki_conf.wiki_path, 'users'))
  ::File.join(root, filename)
end

Instance Method Details

#attributesObject



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

Returns:

  • (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

#nameObject



81
82
83
# File 'app/models/cl_wiki/user.rb', line 81

def name
  self.username
end

#saveObject



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