Class: Adamantite::Base::Adamantite

Inherits:
Object
  • Object
show all
Includes:
AdamantiteFileUtils
Defined in:
lib/base/adamantite.rb

Constant Summary collapse

OPSLIMIT =
2**20
MEMLIMIT =
2**24
DIGEST_SIZE =
32
LICENSE_ACTIVATION_URL =
'https://api.keygen.sh/v1/accounts/c8f50eb9-eb87-4431-a680-d8f181441ef8/licenses/actions/validate-key'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AdamantiteFileUtils

#delete_pw_file, #get_license_key, #get_master_encrypted_vault_key, #get_master_password_hash, #get_master_password_info, #get_master_password_salt, #get_pw_file, #get_stored_pws, #has_license_key?, #home_dir, #make_password_dir, #make_pwmanager_dir, #master_password_exists?, #password_file, #pw_file, #pw_file_exists?, #pwmanager_dir, #pwmanager_dir_exists?, #pwmanager_tmp_dir, #read_file, #write_pw_to_file, #write_to_file

Constructor Details

#initialize(master_password) ⇒ Adamantite

Returns a new instance of Adamantite.



22
23
24
25
# File 'lib/base/adamantite.rb', line 22

def initialize(master_password)
  @master_password = master_password
  @authenticated = false
end

Instance Attribute Details

#authenticatedObject (readonly)

Returns the value of attribute authenticated.



14
15
16
# File 'lib/base/adamantite.rb', line 14

def authenticated
  @authenticated
end

#master_license_keyObject (readonly)

Returns the value of attribute master_license_key.



14
15
16
# File 'lib/base/adamantite.rb', line 14

def master_license_key
  @master_license_key
end

#master_passwordObject (readonly)

Returns the value of attribute master_password.



14
15
16
# File 'lib/base/adamantite.rb', line 14

def master_password
  @master_password
end

#master_password_saltObject (readonly)

Returns the value of attribute master_password_salt.



14
15
16
# File 'lib/base/adamantite.rb', line 14

def master_password_salt
  @master_password_salt
end

#stored_passwordsObject (readonly)

Returns the value of attribute stored_passwords.



14
15
16
# File 'lib/base/adamantite.rb', line 14

def stored_passwords
  @stored_passwords
end

Instance Method Details

#activate_license!(master_license_key) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/base/adamantite.rb', line 50

def activate_license!(master_license_key)
  return unless authenticated?

  headers = {
    'Content-Type': 'application/vnd.api+json',
    'Accept': 'application/vnd.api+json'
  }
  body = {
    'meta': {
      'key': master_license_key,
      'scope': {
        'product': 'bb6542ab-7d74-44d0-b4f5-1fbc39cdeb99'
      }
    }
  }
  res = HTTParty.post(LICENSE_ACTIVATION_URL, headers: headers, body: body.to_json)

  if res['meta']['valid']
    @master_license_key = master_license_key
    write_to_file(password_file('master_license_key'), @vault.encrypt(@master_license_key), true)
    true
  end
  licensed?
end

#authenticate!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/base/adamantite.rb', line 27

def authenticate!
  if master_password_exists?
    master_password_salt = get_master_password_salt
    master_encrypted_vault_key = get_master_encrypted_vault_key
    entered_master_password_hash = rbnacl_scrypt_hash(@master_password, master_password_salt)
    vault = rbnacl_box(entered_master_password_hash)

    begin
      @master_vault_key = vault.decrypt(master_encrypted_vault_key)
      @authenticated = true
      @master_password_salt = master_password_salt
      @vault = rbnacl_box(@master_vault_key)
      update_stored_passwords!
      read_license_key! if has_license_key?
      true
    rescue RbNaCl::CryptoError
      false
    end
  else
    false
  end
end

#authenticated?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/base/adamantite.rb', line 149

def authenticated?
  @authenticated
end

#delete_password(password_dir_name) ⇒ Object



87
88
89
90
# File 'lib/base/adamantite.rb', line 87

def delete_password(password_dir_name)
  FileUtils.remove_entry_secure(password_file(password_dir_name))
  update_stored_passwords!
end

#licensed?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/base/adamantite.rb', line 163

def licensed?
  !@master_license_key.nil?
end

#retrieve_password_info(website_title, info_name) ⇒ Object



92
93
94
95
96
# File 'lib/base/adamantite.rb', line 92

def retrieve_password_info(website_title, info_name)
  return unless authenticated?

  @vault.decrypt(read_file(password_file(website_title, info_name), true))
end

#save_password(website_title, username, password, password_confirmation) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/base/adamantite.rb', line 75

def save_password(website_title, username, password, password_confirmation)
  return unless password == password_confirmation && authenticated?

  encrypted_file_name_ascii_8bit = @vault.encrypt(website_title)
  dir_name = Base64.urlsafe_encode64(encrypted_file_name_ascii_8bit)
  make_password_dir(dir_name)
  write_to_file(password_file(dir_name, 'username'), @vault.encrypt(username), true)
  write_to_file(password_file(dir_name, 'password'), @vault.encrypt(password), true)
  update_stored_passwords!
  dir_name
end

#serialize_master_password(master_password, master_password_confirmation) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/base/adamantite.rb', line 98

def serialize_master_password(master_password, master_password_confirmation)
  if master_password == master_password_confirmation
    master_password_salt = rbnacl_random_bytes
    master_password_hash = rbnacl_scrypt_hash(master_password, master_password_salt)
    vault_key = rbnacl_random_bytes
    vault = rbnacl_box(master_password_hash)
    encrypted_vault_key = vault.encrypt(vault_key)
    make_pwmanager_dir
    write_master_info(master_password_salt, encrypted_vault_key)
    true
  else
    false
  end
end

#update_master_password!(new_master_password, new_master_password_confirmation) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/base/adamantite.rb', line 113

def update_master_password!(new_master_password, new_master_password_confirmation)
  if new_master_password == new_master_password_confirmation && authenticated?
    new_master_password_salt = rbnacl_random_bytes
    new_master_password_hash = rbnacl_scrypt_hash(new_master_password, new_master_password_salt)
    vault_key = rbnacl_random_bytes
    vault = rbnacl_box(new_master_password_hash)
    encrypted_vault_key = vault.encrypt(vault_key)

    new_password_data = @stored_passwords.map do |stored_password|
      info = {}
      info['website_title'] = stored_password[:website_title]
      info['username'] = retrieve_password_info(stored_password[:dir_name], 'username')
      info['password'] = retrieve_password_info(stored_password[:dir_name], 'password')
      info
    end

    FileUtils.copy_entry(pwmanager_dir, pwmanager_tmp_dir)
    FileUtils.remove_entry_secure(pwmanager_dir)
    @vault = rbnacl_box(vault_key)
    make_pwmanager_dir
    new_password_data.each do |new_password|
      website_title = new_password['website_title']
      username = new_password['username']
      password = new_password['password']
      save_password(website_title, username, password, password)
    end
    FileUtils.remove_entry_secure(pwmanager_tmp_dir)
    write_master_info(new_master_password_salt, encrypted_vault_key)
    @master_password_salt = master_password_salt
    @master_encrypted_vault_key = encrypted_vault_key
    true
  else
    false
  end
end

#update_stored_passwords!Object



153
154
155
156
157
158
159
160
161
# File 'lib/base/adamantite.rb', line 153

def update_stored_passwords!
  @stored_passwords = get_stored_pws.map do |stored_password|
    {
      'dir_name': stored_password,
      'website_title': decode_encrypted_utf8_string(stored_password),
      'username': retrieve_password_info(stored_password, 'username')
    }
  end
end