Add qr html export mode to `key export` command

This commit is contained in:
Martin Hostettler 2017-02-08 00:22:37 +01:00 committed by Marian Beermann
parent e0f36e8613
commit 179f1bc147
5 changed files with 27 additions and 6 deletions

View File

@ -140,7 +140,7 @@ html_favicon = '_static/favicon.ico'
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['borg_theme']
html_extra_path = ['paperkey.html']
html_extra_path = ['../src/borg/paperkey.html']
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.

View File

@ -666,6 +666,9 @@ setup(
'borgfs = borg.archiver:main',
]
},
package_data={
'borg': ['paperkey.html']
},
cmdclass=cmdclass,
ext_modules=ext_modules,
setup_requires=['setuptools_scm>=1.7'],

View File

@ -271,7 +271,10 @@ class Archiver:
if not args.path:
self.print_error("output file to export key to expected")
return EXIT_ERROR
manager.export(args.path)
if args.qr:
manager.export_qr(args.path)
else:
manager.export(args.path)
return EXIT_SUCCESS
@with_repository(lock=False, exclusive=False, manifest=False, cache=False)
@ -1938,6 +1941,9 @@ class Archiver:
subparser.add_argument('--paper', dest='paper', action='store_true',
default=False,
help='Create an export suitable for printing and later type-in')
subparser.add_argument('--qr-html', dest='qr', action='store_true',
default=False,
help='Create an html file suitable for printing and later type-in or qr scan')
key_import_epilog = process_epilog("""
This command allows to restore a key previously backed up with the

View File

@ -2,6 +2,7 @@ from binascii import unhexlify, a2b_base64, b2a_base64
import binascii
import textwrap
from hashlib import sha256
import pkgutil
from .key import KeyfileKey, RepoKey, PassphraseKey, KeyfileNotFoundError, PlaintextKey
from .helpers import Manifest, NoManifestError, Error, yes, bin_to_hex
@ -77,16 +78,27 @@ class KeyManager:
elif self.keyblob_storage == KEYBLOB_REPO:
self.repository.save_key(self.keyblob.encode('utf-8'))
def get_keyfile_data(self):
data = '%s %s\n' % (KeyfileKey.FILE_ID, bin_to_hex(self.repository.id))
data += self.keyblob
if not self.keyblob.endswith('\n'):
data += '\n'
return data
def store_keyfile(self, target):
with open(target, 'w') as fd:
fd.write('%s %s\n' % (KeyfileKey.FILE_ID, bin_to_hex(self.repository.id)))
fd.write(self.keyblob)
if not self.keyblob.endswith('\n'):
fd.write('\n')
fd.write(self.get_keyfile_data())
def export(self, path):
self.store_keyfile(path)
def export_qr(self, path):
with open(path, 'wb') as fd:
key_data = self.get_keyfile_data()
html = pkgutil.get_data('borg', 'paperkey.html')
html = html.replace(b'</textarea>', key_data.encode() + b'</textarea>')
fd.write(html)
def export_paperkey(self, path):
def grouped(s):
ret = ''