Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to save backups to a PDF file #8

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ name = "pypi"
[dev-packages]

[packages]
rncryptor = "==3.2.0"
rncryptor = "==3.3.0"
bpylist = "==0.1.4"
click = "==6.7"
pyqrcode = "==1.2.1"
pycryptodome = "==3.9.1"
pypng = "==0.0.20"
reportlab = "==3.5.53"
pillow = "==7.2.0"

[requires]
python_version = "3.7"
164 changes: 127 additions & 37 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ pipenv run python decrypt_otpauth.py decrypt_backup --encrypted-otpauth-backup <
pipenv run python decrypt_otpauth.py decrypt_account --encrypted-otpauth-account <path to your OTP Auth account>
```

## Outputting PDF

If you'd prefer to store analog backups (e.g. by printing the QR codes and putting them in a safe), both decryption commands support an option to output a PDF file:

```
pipenv run python decrypt_otpauth.py decrypt_backup --encrypted-otpauth-backup <otp_auth_backup_path> --pdf-out <pdf_path>
```

## Demo

The project contains two OTP Auth exports for demo purposes:
Expand Down
57 changes: 51 additions & 6 deletions decrypt_otpauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import click
import getpass
import hashlib
from io import BytesIO

from enum import Enum
from urllib.parse import quote
Expand All @@ -16,6 +17,9 @@
from rncryptor import RNCryptor
from rncryptor import bord

import PIL
from reportlab.platypus import SimpleDocTemplate, Image, BalancedColumns, Paragraph, Flowable
from reportlab.lib.styles import getSampleStyleSheet

class Type(Enum):
Unknown = 0
Expand Down Expand Up @@ -196,6 +200,35 @@ def render_qr_to_terminal(otp_uri, type, issuer, label):
click.echo(qr.terminal(quiet_zone=4))
click.echo("")

def write_accounts_to_pdf(accounts, pdf_path):
class CaptionedQr(Flowable):
def __init__(self, img, caption):
self.img = img
self.caption = caption
self.width = 120
self.img_height = 120
self.height = 160
self.caption_line_padding = 10
def draw(self):
self.canv.drawInlineImage(self.img, 0, 0, self.width, self.img_height)
textobj = self.canv.beginText(0, -self.caption_line_padding)
# Decide whether to wrap to next line after writing each char
for char in self.caption:
textobj.textOut(char)
if textobj.getX() > self.width:
textobj.moveCursor(0, self.caption_line_padding)
self.canv.drawText(textobj)

pdf = SimpleDocTemplate(pdf_path)
flowables = []
for index, account in enumerate(accounts):
b = BytesIO()
qr = pyqrcode.create(account.otp_uri(), error="L")
qr.png(b, 5)
caption = f'{account.issuer} - {account.label}'
img = PIL.Image.open(b)
flowables.append(CaptionedQr(img, caption))
pdf.build([BalancedColumns(flowables, 3)])

@click.group()
def cli():
Expand All @@ -207,7 +240,10 @@ def cli():
help="path to your encrypted OTP Auth account (.otpauth)",
required=True,
type=click.File('rb'))
def decrypt_account(encrypted_otpauth_account):
@click.option('--pdf-out',
help="path to output an PDF file instead of printing to the terminal",
required=False)
def decrypt_account(encrypted_otpauth_account, pdf_out):
# Get password from user
password = getpass.getpass(f'Password for export file {encrypted_otpauth_account.name}: ')

Expand All @@ -230,7 +266,10 @@ def decrypt_account(encrypted_otpauth_account):
click.echo(f'Encountered unknow file version: {archive["Version"]}')
return

render_qr_to_terminal(account.otp_uri(), account.type, account.issuer, account.label)
if not pdf_out:
render_qr_to_terminal(account.otp_uri(), account.type, account.issuer, account.label)
else:
write_accounts_to_pdf([account], pdf_out)


def decrypt_account_11(archive, password):
Expand Down Expand Up @@ -266,7 +305,10 @@ def decrypt_account_12(archive, password):
help="path to your encrypted OTP Auth backup (.otpauthdb)",
required=True,
type=click.File('rb'))
def decrypt_backup(encrypted_otpauth_backup):
@click.option('--pdf-out',
help="path to output an PDF file instead of printing to the terminal",
required=False)
def decrypt_backup(encrypted_otpauth_backup, pdf_out):
# Get password from user
password = getpass.getpass(f'Password for export file {encrypted_otpauth_backup.name}: ')

Expand All @@ -289,9 +331,12 @@ def decrypt_backup(encrypted_otpauth_backup):
click.echo(f'Encountered unknow file version: {archive["Version"]}')
return

for account in accounts:
render_qr_to_terminal(account.otp_uri(), account.type, account.issuer, account.label)
input("Press Enter to continue...")
if not pdf_out:
for account in accounts:
render_qr_to_terminal(account.otp_uri(), account.type, account.issuer, account.label)
input("Press Enter to continue...")
else:
write_accounts_to_pdf(accounts, pdf_out)


def decrypt_backup_10(archive, password):
Expand Down