Class: Uberpass::FileHandler

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.namespaceObject

Returns the value of attribute namespace.



43
44
45
# File 'lib/uberpass.rb', line 43

def namespace
  @namespace
end

Class Method Details

.allObject



104
105
106
107
108
# File 'lib/uberpass.rb', line 104

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

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

Yields:

  • (_self)

Yield Parameters:



45
46
47
# File 'lib/uberpass.rb', line 45

def configure
  yield self
end

.decrypted_passwordsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/uberpass.rb', line 85

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

.destroy(key) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/uberpass.rb', line 138

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

.encrypt(key, password) ⇒ Object



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

def encrypt(key, password)
  passwords = decrypted_passwords
  entry = passwords[key] = {
    "password" => password,
    "created_at" => Time.now
  }
  encryptor = Encrypt.new(File.read(public_key_file), passwords.to_yaml)
  write(encryptor)
  Hash[*[key, entry]]
end

.generate(key) ⇒ Object



114
115
116
# File 'lib/uberpass.rb', line 114

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

.generate_short(key) ⇒ Object



110
111
112
# File 'lib/uberpass.rb', line 110

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

.iv_fileObject



69
70
71
# File 'lib/uberpass.rb', line 69

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

.key_fileObject



65
66
67
# File 'lib/uberpass.rb', line 65

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

.name_spaced_file(file_name) ⇒ Object



49
50
51
# File 'lib/uberpass.rb', line 49

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

.passwords_fileObject



61
62
63
# File 'lib/uberpass.rb', line 61

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

.private_key_fileObject



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

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

.public_key_fileObject



57
58
59
# File 'lib/uberpass.rb', line 57

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

.rename(old, new) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/uberpass.rb', line 129

def rename(old, new)
  passwords = decrypted_passwords
  entry = passwords.delete old
  passwords[new] = entry
  encryptor = Encrypt.new(File.read(public_key_file), passwords.to_yaml)
  write(encryptor)
  Hash[*[new, entry]]
end

.show(key) ⇒ Object



100
101
102
# File 'lib/uberpass.rb', line 100

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

.write(encryptor) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/uberpass.rb', line 73

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