Class: Uberpass::FileHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/uberpass/file_handler.rb

Defined Under Namespace

Classes: ExistingEntryError

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.namespaceObject

Returns the value of attribute namespace.



9
10
11
# File 'lib/uberpass/file_handler.rb', line 9

def namespace
  @namespace
end

.pass_phraseObject

Returns the value of attribute pass_phrase.



9
10
11
# File 'lib/uberpass/file_handler.rb', line 9

def pass_phrase
  @pass_phrase
end

Class Method Details

.allObject



85
86
87
88
89
# File 'lib/uberpass/file_handler.rb', line 85

def all
  decrypted_passwords.map do |entry|
    Hash[*entry]
  end
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



11
12
13
# File 'lib/uberpass/file_handler.rb', line 11

def configure
  yield self
end

.decode(data) ⇒ Object



77
78
79
# File 'lib/uberpass/file_handler.rb', line 77

def decode(data)
  JSON.parse(data)
end

.decrypted_passwordsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/uberpass/file_handler.rb', line 57

def decrypted_passwords
  if File.exists?(passwords_file)
    decode(
      Decrypt.new(
        File.read(private_key_file),
        File.read(passwords_file),
        File.read(key_file),
        File.read(iv_file),
        pass_phrase
      ).decrypted_data
    )
  else
    {}
  end
end

.encode(data) ⇒ Object



73
74
75
# File 'lib/uberpass/file_handler.rb', line 73

def encode(data)
  JSON.pretty_generate(data)
end

.encrypt(key, password) ⇒ Object

Raises:



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/uberpass/file_handler.rb', line 99

def encrypt(key, password)
  passwords = decrypted_passwords
  raise ExistingEntryError, key unless passwords[key].nil?
  entry = passwords[key] = {
    "password" => password,
    "created_at" => Time.now
  }
  encryptor = Encrypt.new(File.read(public_key_file), encode(passwords), pass_phrase)
  write(encryptor)
  Hash[*[key, entry]]
end

.generate(key) ⇒ Object



95
96
97
# File 'lib/uberpass/file_handler.rb', line 95

def generate(key)
  encrypt key, SecureRandom.urlsafe_base64(24)
end

.generate_short(key) ⇒ Object



91
92
93
# File 'lib/uberpass/file_handler.rb', line 91

def generate_short(key)
  encrypt key, SecureRandom.urlsafe_base64(8)
end

.iv_fileObject



35
36
37
# File 'lib/uberpass/file_handler.rb', line 35

def iv_file
  File.expand_path("~/.uberpass/#{name_spaced_file("iv")}")
end

.key_fileObject



31
32
33
# File 'lib/uberpass/file_handler.rb', line 31

def key_file
  File.expand_path("~/.uberpass/#{name_spaced_file("key")}")
end

.name_spaced_file(file_name) ⇒ Object



15
16
17
# File 'lib/uberpass/file_handler.rb', line 15

def name_spaced_file(file_name)
  @namespace.nil? ? file_name : "#{file_name}_#{@namespace}"
end

.passwords_fileObject



27
28
29
# File 'lib/uberpass/file_handler.rb', line 27

def passwords_file
  File.expand_path("~/.uberpass/#{name_spaced_file("passwords")}")
end

.private_key_fileObject



19
20
21
# File 'lib/uberpass/file_handler.rb', line 19

def private_key_file
  File.expand_path("~/.uberpass/private.pem")
end

.public_key_fileObject



23
24
25
# File 'lib/uberpass/file_handler.rb', line 23

def public_key_file
  File.expand_path("~/.uberpass/public.pem")
end

.remove(key) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/uberpass/file_handler.rb', line 121

def remove(key)
  passwords = decrypted_passwords
  entry = passwords.delete key
  encryptor = Encrypt.new(File.read(public_key_file), encode(passwords))
  write(encryptor)
  Hash[*[key, entry]]
end

.rename(old, new) ⇒ Object

Raises:



111
112
113
114
115
116
117
118
119
# File 'lib/uberpass/file_handler.rb', line 111

def rename(old, new)
  passwords = decrypted_passwords
  raise ExistingEntryError, new unless passwords[new].nil?
  entry = passwords.delete old
  passwords[new] = entry
  encryptor = Encrypt.new(File.read(public_key_file), encode(passwords))
  write(encryptor)
  Hash[*[new, entry]]
end

.seppuku!Object



51
52
53
54
55
# File 'lib/uberpass/file_handler.rb', line 51

def seppuku!
  if File.exists?(passwords_file)
    File.unlink(passwords_file, key_file, iv_file)
  end
end

.show(key) ⇒ Object



81
82
83
# File 'lib/uberpass/file_handler.rb', line 81

def show(key)
  Hash[*[key, decrypted_passwords[key]]]
end

.write(encryptor) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/uberpass/file_handler.rb', line 39

def write(encryptor)
  File.open(passwords_file, "w+") { |file|
    file.write(encryptor.encrypted_data)
  }
  File.open(key_file, "w+") { |file|
    file.write(encryptor.encrypted_key)
  }
  File.open(iv_file, "w+") { |file|
    file.write(encryptor.encrypted_iv)
  }
end