broadfield-dev commited on
Commit
811fa8b
Β·
verified Β·
1 Parent(s): 1049d4a

Create ui.py

Browse files
Files changed (1) hide show
  1. server/ui.py +56 -0
server/ui.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from .server import (
3
+ get_public_key,
4
+ get_server_info,
5
+ decode_data,
6
+ generate_rsa_keys,
7
+ PUBLIC_KEY_PEM_STRING,
8
+ KEYLOCK_STATUS_MESSAGE
9
+ )
10
+
11
+ theme = gr.themes.Soft()
12
+
13
+ with gr.Blocks(title="Secure Decoder API", theme=theme) as demo:
14
+ gr.Markdown("# πŸ” Secure KeyLock Decoder & Auth API")
15
+ gr.Markdown("This application provides secure API endpoints to decrypt data from images and perform mock authentication.")
16
+
17
+ with gr.Tabs():
18
+ with gr.TabItem("πŸš€ API Documentation"):
19
+ gr.Markdown("## How to Use This API")
20
+ gr.Markdown(
21
+ "This server exposes three main API endpoints for programmatic use:\n"
22
+ "1. **`/keylock-info`**: A `GET` request returns a JSON object with server metadata.\n"
23
+ "2. **`/keylock-pub`**: A `GET` request returns the server's public RSA key required for encryption.\n"
24
+ "3. **`/keylock-server`**: A `POST` request with a base64-encoded image string decrypts the data and uses it to authenticate."
25
+ )
26
+ gr.Markdown("### Required Payload for Authentication")
27
+ gr.Markdown("Your client must encrypt a JSON object containing `API_KEY` and `USER` keys.")
28
+ gr.Code(
29
+ language="json",
30
+ label="Example JSON Payload to Encrypt",
31
+ value='{\n "API_KEY": "sk-12345-abcde",\n "USER": "demo-user"\n}'
32
+ )
33
+ gr.Markdown("### Server's Public Key")
34
+ gr.Code(value=PUBLIC_KEY_PEM_STRING, language="pem", label="Server Public Key (from /keylock-pub)")
35
+
36
+ with gr.TabItem("βš™οΈ Server Status & Admin"):
37
+ gr.Markdown("## Server Status")
38
+ gr.Textbox(label="Private Key Status", value=KEYLOCK_STATUS_MESSAGE, interactive=False, lines=3)
39
+ gr.Markdown("---")
40
+ with gr.Accordion("πŸ”‘ Admin: Key Pair Generator", open=False):
41
+ gr.Markdown(
42
+ "**For Administrators Only.** Use this tool to generate a new RSA key pair for the server. "
43
+ "**This does NOT automatically apply the keys.** To use them, you must:\n"
44
+ "1. Copy the **Private Key** and update the `KEYLOCK_PRIV_KEY` secret in your deployment environment.\n"
45
+ "2. Restart the server for the changes to take effect. The public key will be derived automatically."
46
+ )
47
+ gen_keys_button = gr.Button("βš™οΈ Generate New 2048-bit Key Pair", variant="secondary")
48
+ with gr.Row():
49
+ output_private_key = gr.Textbox(lines=10, label="Generated Private Key (KEEP THIS SECRET)", interactive=False, show_copy_button=True)
50
+ output_public_key = gr.Textbox(lines=10, label="Generated Public Key (will be auto-derived)", interactive=False, show_copy_button=True)
51
+ gen_keys_button.click(fn=generate_rsa_keys, inputs=None, outputs=[output_private_key, output_public_key])
52
+
53
+ with gr.Row(visible=False):
54
+ gr.Interface(fn=get_public_key, inputs=None, outputs=gr.Textbox(), api_name="keylock-pub")
55
+ gr.Interface(fn=get_server_info, inputs=None, outputs=gr.JSON(), api_name="keylock-info")
56
+ gr.Interface(fn=decode_data, inputs=gr.Textbox(), outputs=gr.JSON(), api_name="keylock-server")