Removing Latex as dependency. Using browser to convert OTP pads into printable documents.

This commit is contained in:
Roland 2022-06-26 16:27:56 -06:00
parent 776ac7edb0
commit f639e01951
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
9 changed files with 45 additions and 74 deletions

View file

@ -16,7 +16,7 @@ jobs:
steps:
- checkout
- run: sudo apt-get update
- run: sudo apt-get install -y ffmpeg texlive-latex-base texlive-latex-extra
- run: sudo apt-get install -y ffmpeg
- run:
name: Install Requirements
command: |
@ -36,7 +36,7 @@ jobs:
steps:
- checkout
- run: sudo apt-get update
- run: sudo apt-get install -y ffmpeg texlive-latex-base texlive-latex-extra
- run: sudo apt-get install -y ffmpeg
- run:
name: Install Requirements
command: |
@ -56,7 +56,7 @@ jobs:
steps:
- checkout
- run: sudo apt-get update
- run: sudo apt-get install -y ffmpeg texlive-latex-base texlive-latex-extra
- run: sudo apt-get install -y ffmpeg
- run:
name: Install Requirements
command: |

View file

@ -129,7 +129,7 @@ class Decrypt extends React.Component {
class OTP extends React.Component {
render() {
let newPadURL = `http://${window.location.host}/api/otp/pdf`;
let newPadURL = `http://${window.location.host}/api/otp/html`;
let cheatSheetURL = `${process.env.PUBLIC_URL}/one-time-pad-cheat-sheet.pdf`;
return (

View file

@ -4,9 +4,8 @@ WORKDIR /opt/wrolpi
# Update dependencies for the services install.
RUN apt update
# ffmpeg for Videos and Latex for OTP PDF. osm2pgsql to import map files.
RUN apt-get install -y ffmpeg texlive-latex-base texlive-latex-extra
RUN pdflatex --version
# ffmpeg for Videos
RUN apt-get install -y ffmpeg
RUN ffmpeg -version
# Install WROLPi

View file

@ -1,7 +1,3 @@
import random
from http import HTTPStatus
from string import ascii_letters
from sanic import response
from sanic.request import Request
from sanic_ext import validate
@ -35,13 +31,7 @@ async def post_decrypt_otp(_: Request, body: schema.DecryptOTPRequest):
return response.json(data)
@bp.get('/pdf')
async def get_new_otp_pdf(_: Request):
random_name = ''.join(random.choice(ascii_letters) for _ in range(8))
filename = f'one-time-pad-{random_name}.pdf'
headers = {
'Content-type': 'application/pdf',
'Content-Disposition': f'attachment;filename={filename}'
}
contents = lib.generate_pdf()
return response.raw(contents, HTTPStatus.OK, headers)
@bp.get('/html')
async def get_new_otp_html(_: Request):
body = lib.generate_html()
return response.html(body)

View file

@ -1,9 +1,8 @@
import secrets
import subprocess
from functools import partial
from typing import Dict
from wrolpi.common import chunks, remove_whitespace, logger, temporary_directory_path
from wrolpi.common import chunks, remove_whitespace, logger
from wrolpi.errors import InvalidOTP, InvalidPlaintext, InvalidCiphertext
# These are the only characters we support.
@ -41,54 +40,40 @@ def generate_message():
return format_message(''.join(generate_char() for _ in range(400)), 16)
ONE_TIME_PAD_TEX = r'''
\documentclass{article}
\usepackage{hyperref}
\usepackage[margin=0.3in]{geometry}
\usepackage{mathptmx}
PAGE_HTML = '''
<html>
<title>One Time Pad - Unique, just for you</title>
<style>
/* Remove decoration from links so they are readable when printed */
a {{ text-decoration: none; }} // escaped for python
</style>
<body>
{messages}
\title{One Time Pad}
\begin{document}
\begin{verbatim}
%s
\end{verbatim}
Print this page and distribute the copies (along with the \href{https://lrnsr.co/aY6m}{One Time Pad Cheat Sheet
https://lrnsr.co/aY6m}) to all members of your group that you trust to receive your encrypted messages. Every person
must have their OWN copy of this “One Time Pad” to encrypt and decrypt messages.
\\
\textbf{Use each message ONLY ONCE}. Cut off and burn each message from this paper as it is used.
\\
To learn how to use this page, please visit: \href{https://lrnsr.co/H7Za}{https://lrnsr.co/H7Za}
\end{document}
Print this page and distribute the copies (along with the <a href="https://lrnsr.co/aY6m">One Time Pad Cheat Sheet
https://lrnsr.co/aY6m</a>) to all members of your group that you trust to receive your encrypted messages. Every
person must have their OWN copy of this "One Time Pad" to encrypt and decrypt messages.
<br>
<b>Use each message ONLY ONCE.</b> Cut off and burn each message from this paper as it is used.
<br>
If you want more One Time Pads, simply <a href=".">go here to refresh the page:
https://learningselfreliance.com/one_time_pad</a>. The server will generate a unique page just for you. This page
is not stored on the server, and cannot be retrieved once you close this window!
<br>
To learn how to use this page, please visit: <a href="https://lrnsr.co/H7Za">https://lrnsr.co/H7Za</a>
</body>
</html>
'''
def generate_pdf() -> bytes:
def generate_html() -> str:
"""
Create a PDF One Time Pad.
Create an HTML One-Time Pad page. This page will have instructions on how to use the OTP.
"""
messages = '\n\n'.join(f'MESSAGE {i}\n{generate_message()}' for i in range(1, 8))
with temporary_directory_path() as d:
tex_path = d / 'otp.tex'
tex_path.write_text(ONE_TIME_PAD_TEX % messages)
cmd = ('pdflatex', tex_path)
try:
subprocess.check_output(cmd, cwd=d)
# Output path is chosen by `pdflatex`.
pdf_path = tex_path.with_suffix('.pdf')
contents = pdf_path.read_bytes()
except Exception as e:
logger.fatal(f'Failed to generate One Time Pad PDF!', exc_info=e)
raise
# Files are cleaned up.
assert not d.is_dir()
return contents
messages = [generate_message() for _ in range(9)]
messages = '\n\n'.join(f'<pre>MESSAGE {i}</pre><pre>{j}</pre>' for i, j in enumerate(messages, 1))
return PAGE_HTML.format(messages=messages)
def validate_message(otp, error_class):

View file

@ -24,6 +24,6 @@ def test_decrypt(test_client):
)
def test_get_pdf(test_client):
request, response = test_client.get('/api/otp/pdf')
def test_get_html(test_client):
request, response = test_client.get('/api/otp/html')
assert response.status_code == HTTPStatus.OK

View file

@ -96,8 +96,6 @@ def test_format_message(message, expected):
assert result == expected
def test_latex_pdf():
"""
Generate a OTP PDF.
"""
assert lib.generate_pdf()
def test_get_new_otp_html():
"""Generate a OTP HTML page."""
assert lib.generate_html()

View file

@ -8,7 +8,7 @@ set -e
# Install dependencies
apt install -y apt-transport-https ca-certificates curl gnupg-agent gcc libpq-dev software-properties-common \
postgresql-13 nginx-full nginx-doc python3-full python3-dev python3-doc python3-venv htop vim ffmpeg hostapd \
texlive-latex-base texlive-latex-extra chromium chromium-driver cpufrequtils network-manager npm
chromium chromium-driver cpufrequtils network-manager npm
# Install Archiving tools.
npm install -g 'git+https://github.com/gildas-lormeau/SingleFile'

View file

@ -11,8 +11,7 @@ curl -fsSL https://deb.nodesource.com/setup_14.x | bash -
# Install dependencies
apt install -y apt-transport-https ca-certificates curl gnupg-agent gcc libpq-dev software-properties-common \
postgresql-12 nginx-full nginx-doc python3.8-minimal python3.8-dev python3.8-doc python3.8-venv \
ffmpeg hostapd nodejs texlive-latex-base texlive-latex-extra chromium-browser chromium-chromedriver \
cpufrequtils network-manager
ffmpeg hostapd nodejs chromium-browser chromium-chromedriver cpufrequtils network-manager
# Install Archiving tools.
npm install -g 'git+https://github.com/gildas-lormeau/SingleFile'