From a11777cadead7dab24775538be9f171c50d6dbb6 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Sun, 19 Nov 2017 21:25:42 +0100 Subject: [PATCH] feat: add keepass --- lib/mpw/import/keepass.rb | 50 +++++++++++++++++++++++ test/files/import-keepass.txt | 3 ++ test/test_import.rb | 74 ++++++++++++++++++++++++++--------- 3 files changed, 109 insertions(+), 18 deletions(-) create mode 100644 lib/mpw/import/keepass.rb create mode 100644 test/files/import-keepass.txt diff --git a/lib/mpw/import/keepass.rb b/lib/mpw/import/keepass.rb new file mode 100644 index 0000000..b9d7852 --- /dev/null +++ b/lib/mpw/import/keepass.rb @@ -0,0 +1,50 @@ +# MPW is a software to crypt and manage your passwords +# Copyright (C) 2017 Adrien Waksberg +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +require 'csv' + +module MPW + module Import + # Import an keepass2 export csv file + # @param file [String] the file path to import + def self.keepass(file) + data = {} + + CSV.foreach(file, headers: true) do |row| + id = "#{row['Group']} #{row['Title']}" + comment = + if row['Title'] && row['Notes'] + "#{row['Title']} #{row['Notes']}" + elsif row['Title'] + row['Title'] + elsif row['Notes'] + row['Notes'] + end + + data[id] = { + 'comment' => comment, + 'group' => row['Group'], + 'password' => row['Password'], + 'url' => row['URL'], + 'user' => row['Username'] + } + end + + data + end + end +end diff --git a/test/files/import-keepass.txt b/test/files/import-keepass.txt new file mode 100644 index 0000000..61ce7ad --- /dev/null +++ b/test/files/import-keepass.txt @@ -0,0 +1,3 @@ +"Group","Title","Username","Password","URL","Notes" +"Racine","Bank","123456","ywcExJW8qmBVTSyi","http://bank.com/login","My little bank" +"Racine/Cloud","GAFAM","wesh","superpassword","localhost.local","" diff --git a/test/test_import.rb b/test/test_import.rb index ae53eef..f1a8727 100644 --- a/test/test_import.rb +++ b/test/test_import.rb @@ -14,28 +14,66 @@ class TestImport < Test::Unit::TestCase @password = 'password' end - def test_00_import - Dir['./test/files/import-*.txt'].each do |file| - format = File.basename(file, '.txt').partition('-').last + def test_00_import_mpw_old + file = './test/files/import-mpw_old.txt' + format = 'mpw_old' - puts format + output = %x( + mpw import \ + --file #{file} \ + --format #{format} \ + --wallet #{format} + ) + assert_match(I18n.t('form.import.valid'), output) - output = %x( - mpw import \ - --file #{file} \ - --format #{format} \ - --wallet #{format} - ) - assert_match(I18n.t('form.import.valid'), output) + output = %x(echo #{@password} | mpw list --group Bank --wallet #{format}) + assert_match(%r{http://.*fric\.com.*12345.*Fric money money}, output) - output = %x(echo #{@password} | mpw list --group Bank --wallet #{format}) - assert_match(%r{http://.*fric\.com.*12345.*Fric money money}, output) + output = %x(echo #{@password} | mpw list --group Cloud --wallet #{format}) + assert_match(%r{ssh://.*fric\.com.*:4333.*username.*bastion}, output) - output = %x(echo #{@password} | mpw list --group Cloud --wallet #{format}) - assert_match(%r{ssh://.*fric\.com.*:4333.*username.*bastion}, output) + output = %x(echo #{@password} | mpw list --wallet #{format}) + assert_match(/server\.com.*My little server/, output) + end - output = %x(echo #{@password} | mpw list --wallet #{format}) - assert_match(/server\.com.*My little server/, output) - end + def test_01_import_gorilla + file = './test/files/import-gorilla.txt' + format = 'gorilla' + + output = %x( + mpw import \ + --file #{file} \ + --format #{format} \ + --wallet #{format} + ) + assert_match(I18n.t('form.import.valid'), output) + + output = %x(echo #{@password} | mpw list --group Bank --wallet #{format}) + assert_match(%r{http://.*fric\.com.*12345.*Fric money money}, output) + + output = %x(echo #{@password} | mpw list --group Cloud --wallet #{format}) + assert_match(%r{ssh://.*fric\.com.*:4333.*username.*bastion}, output) + + output = %x(echo #{@password} | mpw list --wallet #{format}) + assert_match(/server\.com.*My little server/, output) + end + + def test_02_import_keepass + file = './test/files/import-keepass.txt' + format = 'keepass' + + output = %x( + mpw import \ + --file #{file} \ + --format #{format} \ + --wallet #{format} + ) + assert_match(I18n.t('form.import.valid'), output) + + output = %x(echo #{@password} | mpw list --group 'Racine/Cloud' --wallet #{format}) + assert_match(/localhost\.local.*wesh.*GAFAM/, output) + + output = %x(echo #{@password} | mpw list --wallet #{format}) + assert_match(%r{http://.*bank\.com.*123456.*Bank My little bank}, output) end end