1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-10-27 02:33:19 +00:00

feat: major change the url storage

This commit is contained in:
Adrien Waksberg 2017-09-05 22:16:37 +02:00
parent cf5b7e0142
commit 8b5c7ae581
15 changed files with 87 additions and 145 deletions

View file

@ -48,22 +48,10 @@ OptionParser.new do |opts|
exit 0
end
opts.on('-H', '--host HOST', I18n.t('option.host')) do |host|
values[:host] = host
end
opts.on('-o', '--otp-code CODE', I18n.t('option.otp_code')) do |otp|
values[:otp_key] = otp
end
opts.on('-O', '--protocol PROTOCOL', I18n.t('option.protocol')) do |protocol|
values[:protocol] = protocol
end
opts.on('-P', '--port NUMBER', I18n.t('option.port')) do |port|
values[:port] = port
end
opts.on('-r', '--random', I18n.t('option.random_password')) do
options[:password] = true
end
@ -72,7 +60,11 @@ OptionParser.new do |opts|
options[:text_editor] = true
end
opts.on('-u', '--user USER', I18n.t('option.user')) do |user|
opts.on('-u', '--url URL', I18n.t('option.url')) do |url|
values[:url] = url
end
opts.on('-U', '--user USER', I18n.t('option.user')) do |user|
values[:user] = user
end

View file

@ -53,26 +53,14 @@ OptionParser.new do |opts|
exit 0
end
opts.on('-H', '--host HOST', I18n.t('option.host')) do |host|
values[:host] = host
end
opts.on('-o', '--otp-code CODE', I18n.t('option.otp_code')) do |otp|
values[:otp_key] = otp
end
opts.on('-O', '--protocol PROTOCOL', I18n.t('option.protocol')) do |protocol|
values[:protocol] = protocol
end
opts.on('-p', '--pattern PATTERN', I18n.t('option.pattern')) do |pattern|
search[:pattern] = pattern
end
opts.on('-P', '--port NUMBER', I18n.t('option.port')) do |port|
values[:port] = port
end
opts.on('-r', '--random', I18n.t('option.random_password')) do
options[:password] = true
end
@ -81,7 +69,11 @@ OptionParser.new do |opts|
options[:text_editor] = true
end
opts.on('-u', '--user USER', I18n.t('option.user')) do |user|
opts.on('-u', '--url URL', I18n.t('option.url')) do |url|
values[:url] = url
end
opts.on('-U', '--user USER', I18n.t('option.user')) do |user|
values[:user] = user
end

View file

@ -63,7 +63,6 @@ en:
gpg_key: "Specify a GPG key (ex: user@example.com)"
group: "Search the items with specified group"
help: "Show this help message"
host: "Specify a host or ip"
init: "Initialize mpw"
import: "Import item from an yaml file"
key: "Define the key name"
@ -77,8 +76,6 @@ en:
path: "Move the wallet in new specify directory"
pattern: "Given search pattern"
pinmode: "Enable pinentry mode (available with gpg >= 2.1)"
port: "Set a port of connexion"
protocol: "Set a protocol of connexion"
random_password: "Generate a random password"
setup: "Create a new configuration file"
setup_wallet: "Create a new configuration file for a wallet"
@ -87,6 +84,7 @@ en:
show_all: "Listing all items"
text_editor: "Use text editor to edit the item"
usage: "Use"
url: "Set an url (ex: https://example.com/path)"
user: "Set an user"
wallet: "Specify a wallet to use"
wallet_dir: "Set the wallets folder"

View file

@ -63,7 +63,6 @@ fr:
gpg_key: "Spécifie une clé GPG (ex: user@example.com)"
group: "Recherche les éléments appartenant au groupe spécifié"
help: "Affiche ce message d'aide"
host: "Spécifie le nom du serveur ou l'ip"
import: "Importe des éléments depuis un fichier yaml"
init: "Initialise mpw"
key: "Spécifie le nom d'une clé"
@ -77,8 +76,6 @@ fr:
path: "Déplace le portefeuille dans un nouveau dossier"
pattern: "Motif de donnée à chercher"
pinmode: "Active le mode pinentry (valable avec gpg >= 2.1)"
port: "Spécifie un port de connexion"
protocol: "Spécifie un protocol de connexion"
random_password: "Génére un mot de passe aléatoire"
setup: "Création d'un nouveau fichier de configuration"
setup_wallet: "Création d'un nouveau fichier de configuration pour un portefeuille"
@ -87,6 +84,7 @@ fr:
show_all: "Liste tous les éléments"
text_editor: "Active l'édition avec un éditeur de texte"
usage: "Utilisation"
url: "Spécifie l'url (ex: http://example.com/path)"
user: "Spécifie un utilisateur"
wallet: "Spécifie le portefeuille à utiliser"
wallet_dir: "Spécifie le répertoire des portefeuilles"

View file

@ -563,16 +563,14 @@ module MPW
items.each do |item|
data.merge!(
item.id => {
'host' => item.host,
'user' => item.user,
'group' => item.group,
'password' => @mpw.get_password(item.id),
'protocol' => item.protocol,
'port' => item.port,
'otp_key' => @mpw.get_otp_key(item.id),
'comment' => item.comment,
'last_edit' => item.last_edit,
'created' => item.created,
'group' => item.group,
'last_edit' => item.last_edit,
'otp_key' => @mpw.get_otp_key(item.id),
'password' => @mpw.get_password(item.id),
'url' => item.url,
'user' => item.user
}
)
end
@ -599,12 +597,10 @@ module MPW
Import.send(format, file).each_value do |row|
item = Item.new(
comment: row['comment'],
group: row['group'],
host: row['host'],
protocol: row['protocol'],
user: row['user'],
port: row['port'],
comment: row['comment']
url: row['url'],
user: row['user']
)
next if item.empty?

View file

@ -37,18 +37,12 @@ module MPW
end
data[id] = {
'comment' => comment,
'group' => row['group'],
'host' => row['url'],
'user' => row['user'],
'password' => row['password'],
'comment' => comment
'url' => row['url'],
'user' => row['user']
}
if row['url'] =~ %r{^((?<protocol>[a-z]+)://)?(?<host>[a-zA-Z0-9_.-]+)(:(?<port>[0-9]{1,5}))?$}
data[id]['protocol'] = Regexp.last_match(:protocol)
data[id]['port'] = Regexp.last_match(:port)
data[id]['host'] = Regexp.last_match(:host)
end
end
data

View file

@ -17,25 +17,27 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'i18n'
require 'uri'
module MPW
class Item
attr_accessor :id
attr_accessor :created
attr_accessor :comment
attr_accessor :group
attr_accessor :host
attr_accessor :protocol
attr_accessor :user
attr_accessor :port
attr_accessor :id
attr_accessor :otp
attr_accessor :comment
attr_accessor :port
attr_accessor :protocol
attr_accessor :last_edit
attr_accessor :created
attr_accessor :url
attr_accessor :user
# @param options [Hash] the option :host is required
def initialize(**options)
@host = ''
if !options.key?(:id) || options[:id].to_s.empty? || !options.key?(:created) || options[:created].to_s.empty?
if !options[:id] || !options[:created]
@id = generate_id
@created = Time.now.to_i
else
@ -51,43 +53,38 @@ module MPW
# Update the item
# @param options [Hash]
def update(**options)
unless options[:host] || options[:comment]
unless options[:url] || options[:comment]
raise I18n.t('error.update.host_and_comment_empty')
end
@group = options[:group] if options.key?(:group)
@host = options[:host] if options.key?(:host) && !options[:host].nil?
@protocol = options[:protocol] if options.key?(:protocol)
@user = options[:user] if options.key?(:user)
@port = options[:port].to_i if options.key?(:port) && !options[:port].to_s.empty?
@otp = options[:otp] if options.key?(:otp)
@comment = options[:comment] if options.key?(:comment)
@last_edit = Time.now.to_i unless options.key?(:no_update_last_edit)
if options[:url]
uri = URI(options[:url])
@host = uri.host || options[:url]
@port = uri.port || nil
@protocol = uri.scheme || nil
@url = options[:url]
end
@comment = options[:comment] if options.key?(:comment)
@group = options[:group] if options.key?(:group)
@last_edit = Time.now.to_i unless options.key?(:no_update_last_edit)
@otp = options[:otp] if options.key?(:otp)
@user = options[:user] if options.key?(:user)
end
# Delete all data
def delete
@id = nil
@group = nil
@host = nil
@protocol = nil
@user = nil
@port = nil
@otp = nil
@comment = nil
@created = nil
@group = nil
@host = nil
@last_edit = nil
end
# Return data on url format
# @return [String] an url
def url
url = ''
url += "#{@protocol}://" if @protocol
url += @host if @host
url += ":#{@port}" if @port
url
@otp = nil
@port = nil
@protocol = nil
@url = nil
@user = nil
end
def empty?

View file

@ -84,10 +84,8 @@ module MPW
Item.new(
id: d['id'],
group: d['group'],
host: d['host'],
protocol: d['protocol'],
user: d['user'],
port: d['port'],
url: d['url'],
otp: @otp_keys.key?(d['id']),
comment: d['comment'],
last_edit: d['last_edit'],
@ -114,10 +112,8 @@ module MPW
item.id => {
'id' => item.id,
'group' => item.group,
'host' => item.host,
'protocol' => item.protocol,
'user' => item.user,
'port' => item.port,
'url' => item.url,
'comment' => item.comment,
'last_edit' => item.last_edit,
'created' => item.created,

View file

@ -1,16 +1,12 @@
---
# <%= I18n.t('form.add_item.host') %>
host: <%= options[:host] %>
# <%= I18n.t('form.add_item.url') %>
url: <%= options[:url] %>
# <%= I18n.t('form.add_item.login') %>
user: <%= options[:user] %>
# <%= I18n.t('form.add_item.group') %>
group: <%= options[:group] %>
# <%= I18n.t('form.add_item.protocol') %>
protocol: <%= options[:protocol] %><% unless password %>
group: <%= options[:group] %><% unless password %>
# <%= I18n.t('form.add_item.password') %>
password:<% end %>
# <%= I18n.t('form.add_item.port') %>
port: <%= options[:port] %>
# <%= I18n.t('form.add_item.comment') %>
comment: <%= options[:comment] %>
# <%= I18n.t('form.add_item.otp_key') %>

View file

@ -1,16 +1,12 @@
---
# <%= I18n.t('form.update_item.host') %>
host: <% if options[:host] %><%= options[:host] %><% else %><%= item.host %><% end %>
# <%= I18n.t('form.update_item.url') %>
host: <% if options[:url] %><%= options[:url] %><% else %><%= item.url %><% end %>
# <%= I18n.t('form.update_item.login') %>
user: <% if options[:user] %><%= options[:user] %><% else %><%= item.user %><% end %><% unless password %>
# <%= I18n.t('form.update_item.password') %>
password: <% end %>
# <%= I18n.t('form.update_item.group') %>
group: <% if options[:group] %><%= options[:group] %><% else %><%= item.group %><% end %>
# <%= I18n.t('form.update_item.protocol') %>
protocol: <% if options[:protocol] %><%= options[:protocol] %><% else %><%= item.protocol %><% end %>
# <%= I18n.t('form.update_item.port') %>
port: <% if options[:port] %><%= options[:port] %><% else %><%= item.port %><% end %>
# <%= I18n.t('form.update_item.otp_key') %>
otp_key: <% if options[:otp_key] %><%= options[:otp_key] %><% end %>
# <%= I18n.t('form.update_item.comment') %>

View file

@ -1,19 +1,16 @@
---
1:
host: fric.com
url: https://fric.com
user: 230403
group: Bank
password: 5XdiTQOubRDw9B0aJoMlcEyL
protocol: https
port:
otp_key: 330223432
comment: I love my bank
2:
host: assurance.com
url: https://assurance.com:443
user: user_2132
host: assurance.com
group: Assurance
password: DMyK6B3v4bWO52VzU7aTHIem
protocol: https
port: 443
otp_key:
comment:

View file

@ -1,4 +1,5 @@
add:
url: 'https://example.com:8080'
group: 'Bank'
host: 'example.com'
protocol: 'https'
@ -9,6 +10,7 @@ add:
import:
id: 'TEST-ID-XXXXX'
url: 'https://gogole.com:8081/toto'
group: 'Cloud'
host: 'gogole.com'
protocol: 'https'
@ -19,6 +21,7 @@ import:
created: 1386752948
update:
url: 'ssh://example2.com:2222'
group: 'Assurance'
host: 'example2.com'
protocol: 'ssh'

View file

@ -29,9 +29,7 @@ class TestConfig < Test::Unit::TestCase
output = %x(
echo #{@password} | mpw add \
--host #{data['host']} \
--port #{data['port']} \
--protocol #{data['protocol']} \
--url #{data['url']} \
--user #{data['user']} \
--comment '#{data['comment']}' \
--group #{data['group']} \
@ -73,9 +71,7 @@ class TestConfig < Test::Unit::TestCase
output = %x(
echo #{@password} | mpw update \
-p #{@fixtures['add']['host']} \
--host #{data['host']} \
--port #{data['port']} \
--protocol #{data['protocol']} \
--url #{data['url']} \
--user #{data['user']} \
--comment '#{data['comment']}' \
--new-group #{data['group']}
@ -117,20 +113,22 @@ class TestConfig < Test::Unit::TestCase
error = true
YAML.load_file(file_export).each_value do |export|
next if import['host'] != export['host']
next if import['url'] != export['url']
%w[user group password protocol port otp_key comment].each do |key|
assert_equal(import[key].to_s, export[key].to_s)
end
error = false
break
end
assert(!error)
end
end
def test_06_copy
data = YAML.load_file('./test/files/fixtures-import.yml')[1]
data = YAML.load_file('./test/files/fixtures-import.yml')[2]
output = %x(
echo "#{@password}\np\nq" | mpw copy \

View file

@ -23,10 +23,8 @@ class TestItem < Test::Unit::TestCase
def test_01_add
data = {
group: @fixtures['add']['group'],
host: @fixtures['add']['host'],
protocol: @fixtures['add']['protocol'],
user: @fixtures['add']['user'],
port: @fixtures['add']['port'],
url: @fixtures['add']['url'],
comment: @fixtures['add']['comment']
}
@ -35,6 +33,7 @@ class TestItem < Test::Unit::TestCase
assert(!item.nil?)
assert(!item.empty?)
assert_equal(@fixtures['add']['url'], item.url)
assert_equal(@fixtures['add']['group'], item.group)
assert_equal(@fixtures['add']['host'], item.host)
assert_equal(@fixtures['add']['protocol'], item.protocol)
@ -47,10 +46,8 @@ class TestItem < Test::Unit::TestCase
data = {
id: @fixtures['import']['id'],
group: @fixtures['import']['group'],
host: @fixtures['import']['host'],
protocol: @fixtures['import']['protocol'],
user: @fixtures['import']['user'],
port: @fixtures['import']['port'],
url: @fixtures['import']['url'],
comment: @fixtures['import']['comment'],
created: @fixtures['import']['created']
}
@ -61,6 +58,7 @@ class TestItem < Test::Unit::TestCase
assert(!item.empty?)
assert_equal(@fixtures['import']['id'], item.id)
assert_equal(@fixtures['import']['url'], item.url)
assert_equal(@fixtures['import']['group'], item.group)
assert_equal(@fixtures['import']['host'], item.host)
assert_equal(@fixtures['import']['protocol'], item.protocol)
@ -73,10 +71,8 @@ class TestItem < Test::Unit::TestCase
def test_03_update
data = {
group: @fixtures['add']['group'],
host: @fixtures['add']['host'],
protocol: @fixtures['add']['protocol'],
user: @fixtures['add']['user'],
port: @fixtures['add']['port'],
url: @fixtures['add']['url'],
comment: @fixtures['add']['comment']
}
@ -90,10 +86,8 @@ class TestItem < Test::Unit::TestCase
data = {
group: @fixtures['update']['group'],
host: @fixtures['update']['host'],
protocol: @fixtures['update']['protocol'],
user: @fixtures['update']['user'],
port: @fixtures['update']['port'],
url: @fixtures['update']['url'],
comment: @fixtures['update']['comment']
}
@ -102,6 +96,7 @@ class TestItem < Test::Unit::TestCase
assert(!item.empty?)
assert_equal(@fixtures['update']['url'], item.url)
assert_equal(@fixtures['update']['group'], item.group)
assert_equal(@fixtures['update']['host'], item.host)
assert_equal(@fixtures['update']['protocol'], item.protocol)
@ -116,10 +111,8 @@ class TestItem < Test::Unit::TestCase
def test_05_update_one_element
data = {
group: @fixtures['add']['group'],
host: @fixtures['add']['host'],
protocol: @fixtures['add']['protocol'],
user: @fixtures['add']['user'],
port: @fixtures['add']['port'],
url: @fixtures['add']['url'],
comment: @fixtures['add']['comment']
}
@ -131,8 +124,9 @@ class TestItem < Test::Unit::TestCase
last_edit = item.last_edit
sleep(1)
assert(item.update(comment: @fixtures['update']['comment']))
item.update(comment: @fixtures['update']['comment'])
assert_equal(@fixtures['add']['url'], item.url)
assert_equal(@fixtures['add']['group'], item.group)
assert_equal(@fixtures['add']['host'], item.host)
assert_equal(@fixtures['add']['protocol'], item.protocol)
@ -146,10 +140,8 @@ class TestItem < Test::Unit::TestCase
def test_05_delete
data = {
group: @fixtures['add']['group'],
host: @fixtures['add']['host'],
protocol: @fixtures['add']['protocol'],
user: @fixtures['add']['user'],
port: @fixtures['add']['port'],
url: @fixtures['add']['url'],
comment: @fixtures['add']['comment']
}
@ -163,6 +155,7 @@ class TestItem < Test::Unit::TestCase
assert(item.empty?)
assert_equal(nil, item.id)
assert_equal(nil, item.url)
assert_equal(nil, item.group)
assert_equal(nil, item.host)
assert_equal(nil, item.protocol)

View file

@ -33,10 +33,8 @@ class TestMPW < Test::Unit::TestCase
def test_02_add_item
data = {
group: @fixtures['add']['group'],
host: @fixtures['add']['host'],
protocol: @fixtures['add']['protocol'],
user: @fixtures['add']['user'],
port: @fixtures['add']['port'],
url: @fixtures['add']['url'],
comment: @fixtures['add']['comment']
}
@ -93,10 +91,8 @@ class TestMPW < Test::Unit::TestCase
@fixtures.each_value do |v|
data = {
group: v['group'],
host: v['host'],
protocol: v['protocol'],
user: v['user'],
port: v['port'],
url: v['url'],
comment: v['comment']
}