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-02-03 12:39:14 -07:00
|
|
|
from . import lib, schema
|
2021-11-01 16:42:52 -06:00
|
|
|
|
|
|
|
|
bp = get_blueprint('OTP', '/api/otp')
|
|
|
|
|
|
|
|
|
|
|
2021-12-30 20:45:11 -07:00
|
|
|
@bp.post('/encrypt_otp')
|
2022-02-03 11:36:26 -07:00
|
|
|
@openapi.definition(
|
|
|
|
|
summary='Encrypt a message with OTP.',
|
2022-02-03 12:39:14 -07:00
|
|
|
body=schema.EncryptOTPRequest,
|
2022-02-03 11:36:26 -07:00
|
|
|
)
|
2022-02-03 12:39:14 -07:00
|
|
|
@validate(schema.EncryptOTPRequest)
|
2021-11-01 16:42:52 -06:00
|
|
|
async def post_encrypt_otp(_: Request, data: dict):
|
2022-02-03 12:39:14 -07:00
|
|
|
data = lib.encrypt_otp(data['otp'], data['plaintext'])
|
2021-11-01 16:42:52 -06:00
|
|
|
return response.json(data)
|
|
|
|
|
|
|
|
|
|
|
2021-12-30 20:45:11 -07:00
|
|
|
@bp.post('/decrypt_otp')
|
2022-02-03 11:36:26 -07:00
|
|
|
@openapi.definition(
|
|
|
|
|
summary='Decrypt a message with OTP.',
|
2022-02-03 12:39:14 -07:00
|
|
|
body=schema.DecryptOTPRequest,
|
2022-02-03 11:36:26 -07:00
|
|
|
)
|
2022-02-03 12:39:14 -07:00
|
|
|
@validate(schema.DecryptOTPRequest)
|
2021-11-01 16:42:52 -06:00
|
|
|
async def post_decrypt_otp(_: Request, data: dict):
|
2022-02-03 12:39:14 -07:00
|
|
|
data = lib.decrypt_otp(data['otp'], data['ciphertext'])
|
2021-11-01 16:42:52 -06:00
|
|
|
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}'
|
|
|
|
|
}
|
2022-02-03 12:39:14 -07:00
|
|
|
contents = lib.generate_pdf()
|
2021-11-01 16:42:52 -06:00
|
|
|
return response.raw(contents, HTTPStatus.OK, headers)
|