import gradio as gr from .server import ( get_public_key, get_server_info, decode_data, generate_rsa_keys, PUBLIC_KEY_PEM_STRING, KEYLOCK_STATUS_MESSAGE ) theme = gr.themes.Soft() with gr.Blocks(title="Secure Decoder API", theme=theme) as demo: gr.Markdown("# 🔐 Secure KeyLock Decoder & Auth API") gr.Markdown("This application provides secure API endpoints to decrypt data from images and perform mock authentication.") gr.Markdown("This Server Base URL: https://broadfield-dev-keylock-auth-server-repo.hf.space") with gr.Tabs(): with gr.TabItem("🚀 API Documentation"): gr.Markdown("## How to Use This API") gr.Markdown( "This server exposes three main API endpoints for programmatic use:\n" "1. **`/keylock-info`**: A `GET` request returns a JSON object with server metadata.\n" "2. **`/keylock-pub`**: A `GET` request returns the server's public RSA key required for encryption.\n" "3. **`/keylock-server`**: A `POST` request with a base64-encoded image string decrypts the data and uses it to authenticate." ) gr.Markdown("### Required Payload for Authentication") gr.Markdown("Your client must encrypt a JSON object containing `API_KEY` and `USER` keys.") gr.Code( language="json", label="Example JSON Payload to Encrypt", value='{\n "API_KEY": "sk-12345-abcde",\n "USER": "demo-user"\n}' ) gr.Markdown("### Server's Public Key") gr.Code(value=PUBLIC_KEY_PEM_STRING, language="python", label="Server Public Key (from /keylock-pub)") with gr.TabItem("⚙️ Server Status & Admin"): gr.Markdown("## Server Status") gr.Textbox(label="Private Key Status", value=KEYLOCK_STATUS_MESSAGE, interactive=False, lines=3) gr.Markdown("---") with gr.Accordion("🔑 Admin: Key Pair Generator", open=False): gr.Markdown( "**For Administrators Only.** Use this tool to generate a new RSA key pair for the server. " "**This does NOT automatically apply the keys.** To use them, you must:\n" "1. Copy the **Private Key** and update the `KEYLOCK_PRIV_KEY` secret in your deployment environment.\n" "2. Restart the server for the changes to take effect. The public key will be derived automatically." ) gen_keys_button = gr.Button("⚙️ Generate New 2048-bit Key Pair", variant="secondary") with gr.Row(): output_private_key = gr.Textbox(lines=10, label="Generated Private Key (KEEP THIS SECRET)", interactive=False, show_copy_button=True) output_public_key = gr.Textbox(lines=10, label="Generated Public Key (will be auto-derived)", interactive=False, show_copy_button=True) gen_keys_button.click(fn=generate_rsa_keys, inputs=None, outputs=[output_private_key, output_public_key]) with gr.Row(visible=False): gr.Interface(fn=get_public_key, inputs=None, outputs=gr.Textbox(), api_name="keylock-pub") gr.Interface(fn=get_server_info, inputs=None, outputs=gr.JSON(), api_name="keylock-info") gr.Interface(fn=decode_data, inputs=gr.Textbox(), outputs=gr.JSON(), api_name="keylock-server")