mirror of
https://github.com/nishiki/manage-password.git
synced 2025-02-20 01:50:04 +00:00
add manage otp code
This commit is contained in:
parent
cd0dd2d0aa
commit
b200b56469
2 changed files with 61 additions and 3 deletions
|
@ -30,10 +30,13 @@ class Cli
|
|||
# Constructor
|
||||
# @args: config -> the config
|
||||
# sync -> boolean for sync or not
|
||||
def initialize(config, clipboard=true, sync=true)
|
||||
# clipboard -> enable clopboard
|
||||
# otp -> enable otp
|
||||
def initialize(config, clipboard=true, sync=true, otp=false)
|
||||
@config = config
|
||||
@clipboard = clipboard
|
||||
@sync = sync
|
||||
@otp = otp
|
||||
end
|
||||
|
||||
# Create a new config file
|
||||
|
@ -203,11 +206,13 @@ class Cli
|
|||
print "#{I18n.t('display.login')}: ".cyan
|
||||
puts item.user
|
||||
print "#{I18n.t('display.password')}: ".cyan
|
||||
|
||||
if @clipboard
|
||||
puts '***********'
|
||||
else
|
||||
puts @mpw.get_password(item.id)
|
||||
end
|
||||
|
||||
print "#{I18n.t('display.port')}: ".cyan
|
||||
puts item.port
|
||||
print "#{I18n.t('display.comment')}: ".cyan
|
||||
|
@ -248,8 +253,15 @@ class Cli
|
|||
Clipboard.clear
|
||||
end
|
||||
|
||||
when 'o', 'otp'
|
||||
Clipboard.copy(@mpw.get_otp_code(item.id))
|
||||
puts I18n.t('form.clipboard.otp', time: @mpw.get_otp_remaining_time).yellow
|
||||
|
||||
else
|
||||
puts I18n.t('warning.select').yellow
|
||||
puts "----- #{I18n.t('form.clipboard.help.name')} -----".cyan
|
||||
puts I18n.t('form.clipboard.help.login')
|
||||
puts I18n.t('form.clipboard.help.password')
|
||||
puts I18n.t('form.clipboard.help.otp_code')
|
||||
next
|
||||
end
|
||||
end
|
||||
|
@ -333,10 +345,15 @@ class Cli
|
|||
options[:port] = ask(I18n.t('form.add_item.port')).to_s
|
||||
options[:comment] = ask(I18n.t('form.add_item.comment')).to_s
|
||||
|
||||
if @otp
|
||||
otp_key = ask(I18n.t('form.add_item.otp_key')).to_s
|
||||
end
|
||||
|
||||
item = Item.new(options)
|
||||
|
||||
@mpw.add(item)
|
||||
@mpw.set_password(item.id, password)
|
||||
@mpw.set_otp_key(item.id, otp_key)
|
||||
@mpw.write_data
|
||||
@mpw.sync(true) if @sync
|
||||
|
||||
|
@ -362,10 +379,15 @@ class Cli
|
|||
options[:port] = ask(I18n.t('form.update_item.port' , port: item.port)).to_s
|
||||
options[:comment] = ask(I18n.t('form.update_item.comment' , comment: item.comment)).to_s
|
||||
|
||||
if @otp
|
||||
otp_key = ask(I18n.t('form.update_item.otp_key')).to_s
|
||||
end
|
||||
|
||||
options.delete_if { |k,v| v.empty? }
|
||||
|
||||
|
||||
item.update(options)
|
||||
@mpw.set_password(item.id, password) if not password.empty?
|
||||
@mpw.set_otp_key(item.id, otp_key) if not otp_key.empty?
|
||||
@mpw.write_data
|
||||
@mpw.sync(true) if @sync
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ require 'rubygems/package'
|
|||
require 'gpgme'
|
||||
require 'i18n'
|
||||
require 'yaml'
|
||||
require 'rotp'
|
||||
require 'mpw/item'
|
||||
|
||||
module MPW
|
||||
|
@ -43,6 +44,7 @@ class MPW
|
|||
@data = []
|
||||
@keys = {}
|
||||
@passwords = {}
|
||||
@otp_keys = {}
|
||||
|
||||
data = nil
|
||||
|
||||
|
@ -68,6 +70,10 @@ class MPW
|
|||
|
||||
when /^wallet\/passwords\/(?<id>[a-zA-Z0-9]+)\.gpg$/
|
||||
@passwords[Regexp.last_match('id')] = f.read
|
||||
|
||||
when /^wallet\/otp_keys\/(?<id>[a-zA-Z0-9]+)\.gpg$/
|
||||
@otp_keys[Regexp.last_match('id')] = f.read
|
||||
|
||||
else
|
||||
next
|
||||
end
|
||||
|
@ -137,6 +143,12 @@ class MPW
|
|||
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)
|
||||
|
@ -413,6 +425,30 @@ class MPW
|
|||
raise "#{I18n.t('error.sync.general')}\n#{e}"
|
||||
end
|
||||
|
||||
# Set an opt key
|
||||
# args: id -> the item id
|
||||
# key -> the new key
|
||||
def set_otp_key(id, key)
|
||||
@otp_keys[id] = encrypt(key)
|
||||
end
|
||||
|
||||
# Get an otp code
|
||||
# @args: id -> the item id
|
||||
# @rtrn: an otp code
|
||||
def get_otp_code(id)
|
||||
if not @otp_keys.has_key?(id)
|
||||
return 0
|
||||
else
|
||||
return ROTP::TOTP.new(decrypt(@otp_keys[id])).now
|
||||
end
|
||||
end
|
||||
|
||||
# Get remaining time before expire otp code
|
||||
# @rtrn: return time in seconde
|
||||
def get_otp_remaining_time
|
||||
return (Time.now.utc.to_i / 30 + 1) * 30 - Time.now.utc.to_i
|
||||
end
|
||||
|
||||
# Generate a random password
|
||||
# @args: options -> :length, :special, :alpha, :numeric
|
||||
# @rtrn: a random string
|
||||
|
|
Loading…
Add table
Reference in a new issue