add files shared by Chintan

This commit is contained in:
Julio Cesar
2025-07-07 10:09:08 +02:00
parent 514bd50368
commit afe348f092
3 changed files with 225 additions and 0 deletions

View File

@@ -0,0 +1,135 @@
{
"info": {
"_postman_id": "e77bfc0a-79fb-4594-be9d-06c53989ad2a",
"name": "GPG API for Kigen Flow",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "43522475",
"_collection_link": "https://chintan-533591.postman.co/workspace/Chintan's-Workspace~f62d7ab1-6c32-44dd-894e-371a5145aefa/collection/43522475-e77bfc0a-79fb-4594-be9d-06c53989ad2a?action=share&source=collection_link&creator=43522475"
},
"item": [
{
"name": "1. Check & Install GPG",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://3.29.0.254:8080/api/setup/gnupg",
"protocol": "http",
"host": [
"3",
"29",
"0",
"254"
],
"port": "8080",
"path": [
"api",
"setup",
"gnupg"
]
}
},
"response": []
},
{
"name": "2. Generate GPG Key",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"name\": \"RollxManager Platform\",\n \"email\": \"user@example.com\",\n \"comment\": \"PGP Key for Kigen\",\n \"passphrase\": \"your-secure-passphrase\"\n}"
},
"url": {
"raw": "http://3.29.0.254:8080/api/generate-key",
"protocol": "http",
"host": [
"3",
"29",
"0",
"254"
],
"port": "8080",
"path": [
"api",
"generate-key"
]
}
},
"response": []
},
{
"name": "3. Download Public Key",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://3.29.0.254:8080/api/download/public-key?email=user@example.com",
"protocol": "http",
"host": [
"3",
"29",
"0",
"254"
],
"port": "8080",
"path": [
"api",
"download",
"public-key"
],
"query": [
{
"key": "email",
"value": "user@example.com"
}
]
}
},
"response": []
},
{
"name": "4. Upload GPG Encrypted File & Decrypt",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "file",
"type": "file",
"src": "/path/to/your/zip.gpg"
},
{
"key": "passphrase",
"value": "your-secure-passphrase",
"type": "text"
}
]
},
"url": {
"raw": "http://3.29.0.254:8080/api/upload-decrypt",
"protocol": "http",
"host": [
"3",
"29",
"0",
"254"
],
"port": "8080",
"path": [
"api",
"upload-decrypt"
]
}
},
"response": []
}
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

90
gpg_key_ui_backend.py Normal file
View File

@@ -0,0 +1,90 @@
# gpg_api.py
from flask import Flask, request, jsonify, send_file
import subprocess
import tempfile
import os
from werkzeug.utils import secure_filename
app = Flask(__name__)
UPLOAD_DIR = "/tmp/gpg_files"
os.makedirs(UPLOAD_DIR, exist_ok=True)
@app.route("/api/setup/gnupg", methods=["GET"])
def setup_gnupg():
try:
subprocess.run(["gpg", "--version"], check=True)
return jsonify({"status": "GnuPG is already installed"})
except Exception:
subprocess.run(["apt", "update"], check=True)
subprocess.run(["apt", "install", "-y", "gnupg"], check=True)
return jsonify({"status": "GnuPG installed"})
@app.route("/api/generate-key", methods=["POST"])
def generate_key():
data = request.json
name = data['name']
email = data['email']
comment = data.get('comment', '')
passphrase = data['passphrase']
key_input = f"""
%echo Generating GPG Key
Key-Type: RSA
Key-Length: 4096
Name-Real: {name}
Name-Email: {email}
Name-Comment: {comment}
Expire-Date: 0
Passphrase: {passphrase}
%commit
%echo Done
"""
with tempfile.NamedTemporaryFile(mode='w+', delete=False) as f:
f.write(key_input)
keyfile_path = f.name
try:
subprocess.run(["gpg", "--batch", "--generate-key", keyfile_path], check=True)
return jsonify({"status": "Key generated successfully"})
except subprocess.CalledProcessError as e:
return jsonify({"error": "Key generation failed", "details": str(e)}), 500
finally:
os.remove(keyfile_path)
@app.route("/api/download/public-key", methods=["GET"])
def export_key():
email = request.args.get('email')
filename = os.path.join(UPLOAD_DIR, f"{secure_filename(email)}_public.asc")
try:
subprocess.run(["gpg", "--armor", "--output", filename, "--export", email], check=True)
return send_file(filename, as_attachment=True)
except Exception as e:
return jsonify({"error": "Export failed", "details": str(e)}), 500
@app.route("/api/upload-decrypt", methods=["POST"])
def upload_and_decrypt():
if 'file' not in request.files:
return jsonify({"error": "Missing file"}), 400
file = request.files['file']
passphrase = request.form.get('passphrase')
filename = secure_filename(file.filename)
gpg_path = os.path.join(UPLOAD_DIR, filename)
output_path = os.path.join(UPLOAD_DIR, f"decrypted_{filename[:-4]}.zip")
file.save(gpg_path)
try:
subprocess.run([
"gpg", "--batch", "--yes",
"--passphrase", passphrase,
"--output", output_path,
"--decrypt", gpg_path
], check=True)
return send_file(output_path, as_attachment=True)
except subprocess.CalledProcessError as e:
return jsonify({"error": "Decryption failed", "details": str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)