Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from keylock_component import KeylockDecoderComponent
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# Mock credentials for login
|
| 5 |
MOCK_CREDENTIALS = {
|
| 6 |
"demo-user": {"password": "password123"},
|
| 7 |
"admin-user": {"password": "adminpass"}
|
|
@@ -9,6 +10,7 @@ MOCK_CREDENTIALS = {
|
|
| 9 |
|
| 10 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="sky")) as demo:
|
| 11 |
gr.Markdown("# 🔑 KeyLock Application Login")
|
|
|
|
| 12 |
with gr.Row():
|
| 13 |
with gr.Column(scale=2):
|
| 14 |
gr.Markdown("### 1. Login with Credentials")
|
|
@@ -17,13 +19,12 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="sky")) as demo:
|
|
| 17 |
password_input = gr.Textbox(label="Password", type="password", interactive=True)
|
| 18 |
login_button = gr.Button("Login", variant="primary")
|
| 19 |
login_status = gr.Markdown()
|
|
|
|
| 20 |
with gr.Column(scale=3):
|
| 21 |
gr.Markdown("### 2. Use a KeyLock Image to Auto-Fill")
|
| 22 |
-
keylock_decoder = KeylockDecoderComponent(
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# Button to trigger payload check (workaround for change event issue)
|
| 26 |
-
check_payload_button = gr.Button("Check Decoded Payload", variant="secondary", visible=False)
|
| 27 |
|
| 28 |
def handle_login(username, password):
|
| 29 |
user_data = MOCK_CREDENTIALS.get(username)
|
|
@@ -33,40 +34,22 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="sky")) as demo:
|
|
| 33 |
else:
|
| 34 |
gr.Error("Login Failed: Invalid username or password.")
|
| 35 |
return f"<p style='color:red;font-weight:bold'>❌ Login failed.</p>"
|
| 36 |
-
|
| 37 |
-
def
|
| 38 |
-
"""
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
user = payload_data.get("USER", "")
|
| 44 |
-
password = payload_data.get("PASSWORD", "")
|
| 45 |
return gr.update(value=user), gr.update(value=password)
|
| 46 |
return gr.update(), gr.update()
|
| 47 |
|
| 48 |
-
def check_component_value():
|
| 49 |
-
"""
|
| 50 |
-
Retrieves the current value of the KeylockDecoderComponent.
|
| 51 |
-
"""
|
| 52 |
-
return keylock_decoder.value
|
| 53 |
-
|
| 54 |
-
# Trigger check_payload_button automatically when keylock_decoder value changes
|
| 55 |
keylock_decoder.change(
|
| 56 |
-
fn=
|
| 57 |
-
inputs=[],
|
| 58 |
-
outputs=[decoded_payload],
|
| 59 |
-
js="() => { setTimeout(() => document.querySelector('button[aria-label=\"Check Decoded Payload\"]').click(), 100); return []; }"
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
# When check_payload_button is clicked (manually or via JS), update textboxes
|
| 63 |
-
check_payload_button.click(
|
| 64 |
-
fn=autofill_from_payload,
|
| 65 |
-
inputs=[decoded_payload],
|
| 66 |
outputs=[username_input, password_input]
|
| 67 |
)
|
| 68 |
-
|
| 69 |
-
# Login button validation
|
| 70 |
login_button.click(
|
| 71 |
fn=handle_login,
|
| 72 |
inputs=[username_input, password_input],
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from keylock_component import KeylockDecoderComponent, AppServerLogic
|
| 3 |
+
|
| 4 |
+
shared_server_logic = AppServerLogic()
|
| 5 |
|
|
|
|
| 6 |
MOCK_CREDENTIALS = {
|
| 7 |
"demo-user": {"password": "password123"},
|
| 8 |
"admin-user": {"password": "adminpass"}
|
|
|
|
| 10 |
|
| 11 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="sky")) as demo:
|
| 12 |
gr.Markdown("# 🔑 KeyLock Application Login")
|
| 13 |
+
|
| 14 |
with gr.Row():
|
| 15 |
with gr.Column(scale=2):
|
| 16 |
gr.Markdown("### 1. Login with Credentials")
|
|
|
|
| 19 |
password_input = gr.Textbox(label="Password", type="password", interactive=True)
|
| 20 |
login_button = gr.Button("Login", variant="primary")
|
| 21 |
login_status = gr.Markdown()
|
| 22 |
+
|
| 23 |
with gr.Column(scale=3):
|
| 24 |
gr.Markdown("### 2. Use a KeyLock Image to Auto-Fill")
|
| 25 |
+
keylock_decoder = KeylockDecoderComponent(
|
| 26 |
+
server_logic=shared_server_logic
|
| 27 |
+
)
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def handle_login(username, password):
|
| 30 |
user_data = MOCK_CREDENTIALS.get(username)
|
|
|
|
| 34 |
else:
|
| 35 |
gr.Error("Login Failed: Invalid username or password.")
|
| 36 |
return f"<p style='color:red;font-weight:bold'>❌ Login failed.</p>"
|
| 37 |
+
|
| 38 |
+
def autofill_from_key(decoder_result):
|
| 39 |
+
if decoder_result and decoder_result.get("status") == "Success":
|
| 40 |
+
payload = decoder_result.get("payload", {})
|
| 41 |
+
user = payload.get("USER", "")
|
| 42 |
+
user_data = MOCK_CREDENTIALS.get(user)
|
| 43 |
+
password = user_data.get("password", "") if user_data else ""
|
|
|
|
|
|
|
| 44 |
return gr.update(value=user), gr.update(value=password)
|
| 45 |
return gr.update(), gr.update()
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
keylock_decoder.change(
|
| 48 |
+
fn=autofill_from_key,
|
| 49 |
+
inputs=[keylock_decoder],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
outputs=[username_input, password_input]
|
| 51 |
)
|
| 52 |
+
|
|
|
|
| 53 |
login_button.click(
|
| 54 |
fn=handle_login,
|
| 55 |
inputs=[username_input, password_input],
|