2021-11-01 16:42:52 -06:00
|
|
|
import random
|
|
|
|
|
from http import HTTPStatus
|
|
|
|
|
from string import ascii_letters
|
|
|
|
|
|
|
|
|
|
from sanic import response
|
|
|
|
|
from sanic.request import Request
|
2022-01-13 09:29:58 -07:00
|
|
|
from sanic_ext import validate
|
|
|
|
|
from sanic_ext.extensions.openapi import openapi
|
2021-11-01 16:42:52 -06:00
|
|
|
|
|
|
|
|
from wrolpi.root_api import get_blueprint
|
2022-01-06 13:56:04 -07:00
|
|
|
from .common import generate_pdf, encrypt_otp, decrypt_otp
|
2021-11-01 16:42:52 -06:00
|
|
|
from .schema import EncryptOTPRequest, DecryptOTPRequest
|
|
|
|
|
|
|
|
|
|
bp = get_blueprint('OTP', '/api/otp')
|
|
|
|
|
|
|
|
|
|
|
2021-12-30 20:45:11 -07:00
|
|
|
@bp.post('/encrypt_otp')
|
2022-01-13 09:29:58 -07:00
|
|
|
@openapi.description('Encrypt a message with OTP.')
|
|
|
|
|
@validate(EncryptOTPRequest)
|
2021-11-01 16:42:52 -06:00
|
|
|
async def post_encrypt_otp(_: Request, data: dict):
|
|
|
|
|
data = encrypt_otp(data['otp'], data['plaintext'])
|
|
|
|
|
return response.json(data)
|
|
|
|
|
|
|
|
|
|
|
2021-12-30 20:45:11 -07:00
|
|
|
@bp.post('/decrypt_otp')
|
2022-01-13 09:29:58 -07:00
|
|
|
@openapi.description('Decrypt a message with OTP.')
|
|
|
|
|
@validate(DecryptOTPRequest)
|
2021-11-01 16:42:52 -06:00
|
|
|
async def post_decrypt_otp(_: Request, data: dict):
|
|
|
|
|
data = decrypt_otp(data['otp'], data['ciphertext'])
|
|
|
|
|
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 = generate_pdf()
|
|
|
|
|
return response.raw(contents, HTTPStatus.OK, headers)
|