mirror of
https://github.com/nishiki/manage-password.git
synced 2024-11-23 13:57:52 +00:00
add folder format
This commit is contained in:
parent
be526c297f
commit
7597a8daf2
2 changed files with 72 additions and 77 deletions
|
@ -132,12 +132,12 @@ module MPW
|
||||||
@mpw.read_data
|
@mpw.read_data
|
||||||
else
|
else
|
||||||
begin
|
begin
|
||||||
@mpw = MPW.new(@config.gpg_key, @wallet_file, nil, @config.gpg_exe, @config.pinmode)
|
@mpw = MPW.new(@config.gpg_key, @wallet_path, nil, @config.gpg_exe, @config.pinmode)
|
||||||
|
|
||||||
@mpw.read_data
|
@mpw.read_data
|
||||||
rescue
|
rescue
|
||||||
@password = ask(I18n.t('display.gpg_password')) { |q| q.echo = false }
|
@password = ask(I18n.t('display.gpg_password')) { |q| q.echo = false }
|
||||||
@mpw = MPW.new(@config.gpg_key, @wallet_file, @password, @config.gpg_exe, @config.pinmode)
|
@mpw = MPW.new(@config.gpg_key, @wallet_path, @password, @config.gpg_exe, @config.pinmode)
|
||||||
|
|
||||||
@mpw.read_data
|
@mpw.read_data
|
||||||
end
|
end
|
||||||
|
@ -378,8 +378,10 @@ module MPW
|
||||||
def list_wallet
|
def list_wallet
|
||||||
wallets = @config.wallet_paths.keys
|
wallets = @config.wallet_paths.keys
|
||||||
|
|
||||||
Dir.glob("#{@config.wallet_dir}/*.mpw").each do |f|
|
Dir.glob("#{@config.wallet_dir}/*").each do |path|
|
||||||
wallet = File.basename(f, '.mpw')
|
next unless File.directory?(path)
|
||||||
|
|
||||||
|
wallet = File.basename(path)
|
||||||
wallet += ' *'.green if wallet == @config.default_wallet
|
wallet += ' *'.green if wallet == @config.default_wallet
|
||||||
wallets << wallet
|
wallets << wallet
|
||||||
end
|
end
|
||||||
|
@ -392,9 +394,10 @@ module MPW
|
||||||
def get_wallet(wallet = nil)
|
def get_wallet(wallet = nil)
|
||||||
@wallet =
|
@wallet =
|
||||||
if wallet.to_s.empty?
|
if wallet.to_s.empty?
|
||||||
wallets = Dir.glob("#{@config.wallet_dir}/*.mpw")
|
wallets = Dir.glob("#{@config.wallet_dir}/*").select { |path| File.directory?(path) }
|
||||||
|
|
||||||
if wallets.length == 1
|
if wallets.length == 1
|
||||||
File.basename(wallets[0], '.mpw')
|
File.basename(wallets.first)
|
||||||
elsif !@config.default_wallet.to_s.empty?
|
elsif !@config.default_wallet.to_s.empty?
|
||||||
@config.default_wallet
|
@config.default_wallet
|
||||||
else
|
else
|
||||||
|
@ -404,11 +407,11 @@ module MPW
|
||||||
wallet
|
wallet
|
||||||
end
|
end
|
||||||
|
|
||||||
@wallet_file =
|
@wallet_path =
|
||||||
if @config.wallet_paths.key?(@wallet)
|
if @config.wallet_paths.key?(@wallet)
|
||||||
"#{@config.wallet_paths[@wallet]}/#{@wallet}.mpw"
|
"#{@config.wallet_paths[@wallet]}/#{@wallet}"
|
||||||
else
|
else
|
||||||
"#{@config.wallet_dir}/#{@wallet}.mpw"
|
"#{@config.wallet_dir}/#{@wallet}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
118
lib/mpw/mpw.rb
118
lib/mpw/mpw.rb
|
@ -18,7 +18,6 @@
|
||||||
# specific language governing permissions and limitations
|
# specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
require 'rubygems/package'
|
|
||||||
require 'gpgme'
|
require 'gpgme'
|
||||||
require 'i18n'
|
require 'i18n'
|
||||||
require 'yaml'
|
require 'yaml'
|
||||||
|
@ -32,51 +31,38 @@ module MPW
|
||||||
# @param gpg_pass [String] password of the gpg key
|
# @param gpg_pass [String] password of the gpg key
|
||||||
# @param gpg_exe [String] path of the gpg executable
|
# @param gpg_exe [String] path of the gpg executable
|
||||||
# @param pinmode [Boolean] enable the gpg pinmode
|
# @param pinmode [Boolean] enable the gpg pinmode
|
||||||
def initialize(key, wallet_file, gpg_pass = nil, gpg_exe = nil, pinmode = false)
|
def initialize(key, wallet_path, gpg_pass = nil, gpg_exe = nil, pinmode = false)
|
||||||
@key = key
|
@key = key
|
||||||
@gpg_pass = gpg_pass
|
@gpg_pass = gpg_pass
|
||||||
@gpg_exe = gpg_exe
|
@gpg_exe = gpg_exe
|
||||||
@wallet_file = wallet_file
|
|
||||||
@pinmode = pinmode
|
@pinmode = pinmode
|
||||||
|
@wallet_path = wallet_path
|
||||||
GPGME::Engine.set_info(GPGME::PROTOCOL_OpenPGP, @gpg_exe, "#{Dir.home}/.gnupg") unless @gpg_exe.to_s.empty?
|
|
||||||
end
|
|
||||||
|
|
||||||
# Read mpw file
|
|
||||||
def read_data
|
|
||||||
@data = []
|
@data = []
|
||||||
@keys = {}
|
@keys = {}
|
||||||
@passwords = {}
|
@passwords = {}
|
||||||
@otp_keys = {}
|
@otp_keys = {}
|
||||||
|
|
||||||
data = nil
|
GPGME::Engine.set_info(GPGME::PROTOCOL_OpenPGP, @gpg_exe, "#{Dir.home}/.gnupg") unless @gpg_exe.to_s.empty?
|
||||||
|
|
||||||
return unless File.exist?(@wallet_file)
|
Dir.mkdir(@wallet_path) unless Dir.exist?(@wallet_path)
|
||||||
|
%w[passwords otp_keys keys].each do |folder|
|
||||||
|
Dir.mkdir("#{@wallet_path}/#{folder}") unless Dir.exist?("#{@wallet_path}/#{folder}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
Gem::Package::TarReader.new(File.open(@wallet_file)) do |tar|
|
# Read mpw file
|
||||||
tar.each do |f|
|
def read_data
|
||||||
case f.full_name
|
return unless Dir.exist?(@wallet_path)
|
||||||
when 'wallet/meta.gpg'
|
|
||||||
data = decrypt(f.read)
|
|
||||||
|
|
||||||
when %r{^wallet/keys/(?<key>.+)\.pub$}
|
meta_file = "#{@wallet_path}/meta.gpg"
|
||||||
key = Regexp.last_match('key')
|
data = File.exist?(meta_file) ? decrypt(File.read(meta_file)) : nil
|
||||||
|
|
||||||
|
Dir["#{@wallet_path}/keys/*.pub"].each do |file|
|
||||||
|
key = File.basename(file, '.pub')
|
||||||
|
@keys[key] = File.read(file)
|
||||||
|
|
||||||
if GPGME::Key.find(:public, key).empty?
|
if GPGME::Key.find(:public, key).empty?
|
||||||
GPGME::Key.import(f.read, armor: true)
|
GPGME::Key.import(@keys[key], armor: true)
|
||||||
end
|
|
||||||
|
|
||||||
@keys[key] = f.read
|
|
||||||
|
|
||||||
when %r{^wallet/passwords/(?<id>[a-zA-Z0-9]+)\.gpg$}
|
|
||||||
@passwords[Regexp.last_match('id')] = f.read
|
|
||||||
|
|
||||||
when %r{^wallet/otp_keys/(?<id>[a-zA-Z0-9]+)\.gpg$}
|
|
||||||
@otp_keys[Regexp.last_match('id')] = f.read
|
|
||||||
|
|
||||||
else
|
|
||||||
next
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -88,7 +74,7 @@ module MPW
|
||||||
group: d['group'],
|
group: d['group'],
|
||||||
user: d['user'],
|
user: d['user'],
|
||||||
url: d['url'],
|
url: d['url'],
|
||||||
otp: @otp_keys.key?(d['id']),
|
otp: File.exist?("#{@wallet_path}/otp_keys/#{d['id']}.gpg"),
|
||||||
comment: d['comment'],
|
comment: d['comment'],
|
||||||
last_edit: d['last_edit'],
|
last_edit: d['last_edit'],
|
||||||
created: d['created']
|
created: d['created']
|
||||||
|
@ -105,7 +91,6 @@ module MPW
|
||||||
# Encrypt all data in tarball
|
# Encrypt all data in tarball
|
||||||
def write_data
|
def write_data
|
||||||
data = {}
|
data = {}
|
||||||
tmp_file = "#{@wallet_file}.tmp"
|
|
||||||
|
|
||||||
@data.each do |item|
|
@data.each do |item|
|
||||||
next if item.empty?
|
next if item.empty?
|
||||||
|
@ -123,41 +108,25 @@ module MPW
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
Gem::Package::TarWriter.new(File.open(tmp_file, 'w+')) do |tar|
|
File.open("#{@wallet_path}/meta.gpg", 'w') do |file|
|
||||||
data_encrypt = encrypt(data.to_yaml)
|
file.chmod(0400)
|
||||||
tar.add_file_simple('wallet/meta.gpg', 0400, data_encrypt.length) do |io|
|
file << encrypt(data.to_yaml)
|
||||||
io.write(data_encrypt)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@passwords.each do |id, password|
|
%w[passwords otp_keys].each do |folder|
|
||||||
tar.add_file_simple("wallet/passwords/#{id}.gpg", 0400, password.length) do |io|
|
Dir["#{@wallet_path}/#{folder}/*.gpg"].each do |file|
|
||||||
io.write(password)
|
File.unlink(file) unless data.key?(File.basename(file, '.gpg'))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@otp_keys.each do |id, key|
|
|
||||||
tar.add_file_simple("wallet/otp_keys/#{id}.gpg", 0400, key.length) do |io|
|
|
||||||
io.write(key)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@keys.each do |id, key|
|
|
||||||
tar.add_file_simple("wallet/keys/#{id}.pub", 0400, key.length) do |io|
|
|
||||||
io.write(key)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
File.rename(tmp_file, @wallet_file)
|
|
||||||
rescue => e
|
rescue => e
|
||||||
File.unlink(tmp_file) if File.exist?(tmp_file)
|
|
||||||
|
|
||||||
raise "#{I18n.t('error.mpw_file.write_data')}\n#{e}"
|
raise "#{I18n.t('error.mpw_file.write_data')}\n#{e}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Get a password
|
# Get a password
|
||||||
# @param id [String] the item id
|
# @param id [String] the item id
|
||||||
def get_password(id)
|
def get_password(id)
|
||||||
|
@passwords[id] = File.read("#{@wallet_path}/passwords/#{id}.gpg") unless @passwords[id]
|
||||||
|
|
||||||
password = decrypt(@passwords[id])
|
password = decrypt(@passwords[id])
|
||||||
|
|
||||||
if /^\$[a-zA-Z0-9]{4,9}::(?<password>.+)$/ =~ password
|
if /^\$[a-zA-Z0-9]{4,9}::(?<password>.+)$/ =~ password
|
||||||
|
@ -172,9 +141,12 @@ module MPW
|
||||||
# @param password [String] the new password
|
# @param password [String] the new password
|
||||||
def set_password(id, password)
|
def set_password(id, password)
|
||||||
salt = MPW.password(length: Random.rand(4..9))
|
salt = MPW.password(length: Random.rand(4..9))
|
||||||
password = "$#{salt}::#{password}"
|
@passwords[id] = encrypt("$#{salt}::#{password}")
|
||||||
|
|
||||||
@passwords[id] = encrypt(password)
|
File.open("#{@wallet_path}/passwords/#{id}.gpg", 'w') do |file|
|
||||||
|
file.chmod(0400)
|
||||||
|
file << @passwords[id]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return the list of all gpg keys
|
# Return the list of all gpg keys
|
||||||
|
@ -197,6 +169,11 @@ module MPW
|
||||||
raise I18n.t('error.export_key') if data.to_s.empty?
|
raise I18n.t('error.export_key') if data.to_s.empty?
|
||||||
|
|
||||||
@keys[key] = data
|
@keys[key] = data
|
||||||
|
File.open("#{@wallet_path}/keys/#{key}.pub", 'w') do |file|
|
||||||
|
file.chmod(0400)
|
||||||
|
file << data
|
||||||
|
end
|
||||||
|
|
||||||
@passwords.each_key { |id| set_password(id, get_password(id)) }
|
@passwords.each_key { |id| set_password(id, get_password(id)) }
|
||||||
@otp_keys.each_key { |id| set_otp_key(id, get_otp_key(id)) }
|
@otp_keys.each_key { |id| set_otp_key(id, get_otp_key(id)) }
|
||||||
end
|
end
|
||||||
|
@ -204,6 +181,8 @@ module MPW
|
||||||
# Delete a public key
|
# Delete a public key
|
||||||
# @param key [String] public key to delete
|
# @param key [String] public key to delete
|
||||||
def delete_key(key)
|
def delete_key(key)
|
||||||
|
File.unlink("#{@wallet_path}/keys/#{key}.pub")
|
||||||
|
|
||||||
@keys.delete(key)
|
@keys.delete(key)
|
||||||
@passwords.each_key { |id| set_password(id, get_password(id)) }
|
@passwords.each_key { |id| set_password(id, get_password(id)) }
|
||||||
@otp_keys.each_key { |id| set_otp_key(id, get_otp_key(id)) }
|
@otp_keys.each_key { |id| set_otp_key(id, get_otp_key(id)) }
|
||||||
|
@ -257,20 +236,33 @@ module MPW
|
||||||
# @param id [String] the item id
|
# @param id [String] the item id
|
||||||
# @param key [String] the new key
|
# @param key [String] the new key
|
||||||
def set_otp_key(id, key)
|
def set_otp_key(id, key)
|
||||||
@otp_keys[id] = encrypt(key.to_s) unless key.to_s.empty?
|
return if key.to_s.empty?
|
||||||
|
|
||||||
|
@otp_keys[id] = encrypt(key.to_s)
|
||||||
|
|
||||||
|
File.open("#{@wallet_path}/otp_keys/#{id}.gpg", 'w') do |file|
|
||||||
|
file.chmod(0400)
|
||||||
|
file << @otp_keys[id]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Get an opt key
|
# Get an opt key
|
||||||
# @param id [String] the item id
|
# @param id [String] the item id
|
||||||
def get_otp_key(id)
|
def get_otp_key(id)
|
||||||
@otp_keys.key?(id) ? decrypt(@otp_keys[id]) : nil
|
otp_file = "#{@wallet_path}/otp_keys/#{id}.gpg"
|
||||||
|
File.exist?(otp_file) ? decrypt(File.read(otp_file)) : nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# Get an otp code
|
# Get an otp code
|
||||||
# @param id [String] the item id
|
# @param id [String] the item id
|
||||||
# @return [String] an otp code
|
# @return [String] an otp code
|
||||||
def get_otp_code(id)
|
def get_otp_code(id)
|
||||||
@otp_keys.key?(id) ? ROTP::TOTP.new(decrypt(@otp_keys[id])).now : 0
|
otp_file = "#{@wallet_path}/otp_keys/#{id}.gpg"
|
||||||
|
|
||||||
|
return 0 unless File.exist?(otp_file)
|
||||||
|
|
||||||
|
@otp_keys[id] = File.read(otp_file) unless @otp_keys[id]
|
||||||
|
ROTP::TOTP.new(decrypt(@otp_keys[id])).now
|
||||||
end
|
end
|
||||||
|
|
||||||
# Get remaining time before expire otp code
|
# Get remaining time before expire otp code
|
||||||
|
|
Loading…
Reference in a new issue