-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunction_app.py
92 lines (67 loc) · 2.82 KB
/
function_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import logging
import azure.functions as func
from flask import Flask, jsonify, request
from crypto_utils import aes_encrypt, aes_decrypt
app = Flask(__name__)
# Configure logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
@app.route("/")
def index():
logger.info("Received request to index endpoint.")
return (
"Welcome to Hvalfangst Crypto API!\n\n"
"Available Endpoints:\n"
"1. POST /encrypt - Encrypt text\n"
" Use this endpoint to encrypt plain text with a provided encryption key.\n\n"
"2. POST /decrypt - Decrypt text\n"
" Use this endpoint to decrypt encrypted text with a provided encryption key.\n"
)
@app.route("/encrypt", methods=["POST"])
def encrypt():
logger.info("Received request to encrypt endpoint.")
# Extract data from JSON body
request_data = request.get_json()
# Check if 'plain_text' and 'encryption_key' fields are present in the JSON
if "plain_text" not in request_data or "encryption_key" not in request_data:
error_msg = "Missing 'plain_text' or 'encryption_key' in the request body"
logger.error(error_msg)
return jsonify({"error": error_msg}), 400
plain_text = request_data["plain_text"]
encryption_key = request_data["encryption_key"]
# Encrypt plain text using key provided in request body
encrypted_text = aes_encrypt(encryption_key, plain_text)
# Prepare response JSON
response = {
"mode": "encrypt",
"plain_text": plain_text,
"encrypted_text": encrypted_text
}
logger.info("Encryption successful.")
# Return JSON response
return jsonify(response)
@app.route("/decrypt", methods=["POST"])
def decrypt():
logger.info("Received request to decrypt endpoint.")
# Extract data from JSON body
request_data = request.get_json()
# Check if 'encrypted_text' and 'encryption_key' fields are present in the JSON
if "encrypted_text" not in request_data or "encryption_key" not in request_data:
error_msg = "Missing 'encrypted_text' or 'encryption_key' in the request body"
logger.error(error_msg)
return jsonify({"error": error_msg}), 400
encrypted_text = request_data["encrypted_text"]
encryption_key = request_data["encryption_key"]
# Decrypt encrypted text using key provided in request body
plain_text = aes_decrypt(encryption_key, encrypted_text)
# Prepare response JSON
response = {
"mode": "decrypt",
"encrypted_text": encrypted_text,
"plain_text": plain_text
}
logger.info("Decryption successful.")
# Return JSON response
return jsonify(response)
# Create an Azure Function which serves the above routes in our WSGI runtime (Gunicorn)
app = func.WsgiFunctionApp(app=app.wsgi_app, http_auth_level=func.AuthLevel.ANONYMOUS)