forked from phoenix/litellm-mirror
build(app.py): add routing between admin and auth pages
This commit is contained in:
parent
8c6d11ea07
commit
1bfab87323
3 changed files with 66 additions and 52 deletions
38
ui/admin.py
38
ui/admin.py
|
@ -7,34 +7,34 @@ import base64
|
|||
# Replace your_base_url with the actual URL where the proxy auth app is hosted
|
||||
your_base_url = 'http://localhost:8501' # Example base URL
|
||||
|
||||
# Create a configuration placeholder
|
||||
st.session_state.setdefault('proxy_url', 'http://example.com')
|
||||
st.session_state.setdefault('allowed_email_subdomain', 'example.com')
|
||||
st.session_state.setdefault('user_auth_url', 'NOT_GIVEN')
|
||||
|
||||
# Function to encode the configuration
|
||||
def encode_config(proxy_url, allowed_email_subdomain):
|
||||
combined_string = f"proxy_url={proxy_url}&accepted_email_subdomain={allowed_email_subdomain}"
|
||||
return base64.b64encode(combined_string.encode('utf-8')).decode('utf-8')
|
||||
|
||||
# Simple function to update config values
|
||||
def update_config():
|
||||
def update_config(proxy_url, allowed_email_subdomain):
|
||||
st.session_state['proxy_url'] = proxy_url
|
||||
st.session_state['allowed_email_subdomain'] = allowed_email_subdomain
|
||||
st.session_state['user_auth_url'] = f"{your_base_url}/{encode_config(proxy_url=proxy_url, allowed_email_subdomain=allowed_email_subdomain)}"
|
||||
st.session_state['user_auth_url'] = f"{your_base_url}/?page={encode_config(proxy_url=proxy_url, allowed_email_subdomain=allowed_email_subdomain)}"
|
||||
|
||||
# Display the form for the admin to set the proxy URL and allowed email subdomain
|
||||
st.header("Admin Configuration")
|
||||
def admin_page():
|
||||
# Display the form for the admin to set the proxy URL and allowed email subdomain
|
||||
st.header("Admin Configuration")
|
||||
# Create a configuration placeholder
|
||||
st.session_state.setdefault('proxy_url', 'http://example.com')
|
||||
st.session_state.setdefault('allowed_email_subdomain', 'example.com')
|
||||
st.session_state.setdefault('user_auth_url', 'NOT_GIVEN')
|
||||
|
||||
with st.form("config_form", clear_on_submit=False):
|
||||
proxy_url = st.text_input("Set Proxy URL", st.session_state['proxy_url'])
|
||||
allowed_email_subdomain = st.text_input("Set Allowed Email Subdomain", st.session_state['allowed_email_subdomain'])
|
||||
submitted = st.form_submit_button("Save")
|
||||
with st.form("config_form", clear_on_submit=False):
|
||||
proxy_url = st.text_input("Set Proxy URL", st.session_state['proxy_url'])
|
||||
allowed_email_subdomain = st.text_input("Set Allowed Email Subdomain", st.session_state['allowed_email_subdomain'])
|
||||
submitted = st.form_submit_button("Save")
|
||||
|
||||
if submitted:
|
||||
update_config()
|
||||
if submitted:
|
||||
update_config(proxy_url=proxy_url, allowed_email_subdomain=allowed_email_subdomain)
|
||||
|
||||
# Display the current configuration
|
||||
st.write(f"Current Proxy URL: {st.session_state['proxy_url']}")
|
||||
st.write(f"Current Allowed Email Subdomain: {st.session_state['allowed_email_subdomain']}")
|
||||
st.write(f"Current User Auth URL: {st.session_state['user_auth_url']}")
|
||||
# Display the current configuration
|
||||
st.write(f"Current Proxy URL: {st.session_state['proxy_url']}")
|
||||
st.write(f"Current Allowed Email Subdomain: {st.session_state['allowed_email_subdomain']}")
|
||||
st.write(f"Current User Auth URL: {st.session_state['user_auth_url']}")
|
74
ui/app.py
74
ui/app.py
|
@ -1,35 +1,53 @@
|
|||
"""
|
||||
Routes between admin, auth, keys pages
|
||||
"""
|
||||
import streamlit as st
|
||||
import urllib.parse
|
||||
import base64, binascii
|
||||
from admin import admin_page
|
||||
from auth import auth_page
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
|
||||
def generate_key(name, description):
|
||||
# Code to generate and return a key goes here
|
||||
return "Generated Key"
|
||||
# Parse the query params in the URL
|
||||
def get_query_params():
|
||||
# Get the query params from Streamlit's `server.request` function
|
||||
# This functionality is not officially documented and could change in the future versions of Streamlit
|
||||
query_params = st.experimental_get_query_params()
|
||||
return query_params
|
||||
|
||||
|
||||
def main(api_base, url_hash):
|
||||
st.title("Key Request")
|
||||
|
||||
# Create input fields for key name and description
|
||||
name = st.text_input("Key Name")
|
||||
description = st.text_area("Key Description")
|
||||
|
||||
# Create a button to request the key
|
||||
if st.button("Request Key"):
|
||||
if name and description:
|
||||
key = generate_key(name, description)
|
||||
st.success(f"Your key: {key}")
|
||||
def is_base64(sb):
|
||||
try:
|
||||
if isinstance(sb, str):
|
||||
# Try to encode it to bytes if it's a unicode string
|
||||
sb_bytes = sb.encode('ascii')
|
||||
elif isinstance(sb, bytes):
|
||||
sb_bytes = sb
|
||||
else:
|
||||
st.error("Please enter a valid key name and description.")
|
||||
# If it is not a byte or a string, it is not base64
|
||||
return False
|
||||
|
||||
# Check if decoding is successful.
|
||||
# The result of the decode is not required, so it is ignored.
|
||||
_ = base64.urlsafe_b64decode(sb_bytes)
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Get the proxy URL and hash from the admin
|
||||
proxy_url = st.text_input("Admin Proxy URL")
|
||||
hash_input = st.text_input("URL Hash")
|
||||
# If the decode was successful, the input is likely base64
|
||||
return True
|
||||
except (binascii.Error, ValueError):
|
||||
# If an error occurs, return False, as the input is not base64
|
||||
return False
|
||||
|
||||
# Check the URL path and route to the correct page based on the path
|
||||
query_params = get_query_params()
|
||||
page_param = query_params.get('page', [None])[0]
|
||||
|
||||
# Generate the public URL with hash
|
||||
encoded_hash = urllib.parse.quote(hash_input.strip()) if hash_input else ""
|
||||
public_url = f"{proxy_url}/{encoded_hash}"
|
||||
|
||||
# Run the Streamlit app
|
||||
main(proxy_url, hash_input)
|
||||
# Route to the appropriate page based on the URL query param
|
||||
if page_param:
|
||||
try:
|
||||
# Try to decode the page_param from base64
|
||||
if is_base64(page_param):
|
||||
auth_page()
|
||||
else:
|
||||
st.error("Unknown page")
|
||||
except Exception as e:
|
||||
st.error("Failed to decode the page parameter. Error: " + str(e))
|
||||
else:
|
||||
admin_page()
|
||||
|
|
|
@ -24,7 +24,7 @@ def sign_in_with_otp(email: str):
|
|||
|
||||
|
||||
# Create the Streamlit app
|
||||
def main():
|
||||
def auth_page():
|
||||
st.title("User Authentication")
|
||||
|
||||
# User email input
|
||||
|
@ -33,7 +33,3 @@ def main():
|
|||
# Sign in button
|
||||
if st.button("Sign In"):
|
||||
sign_in_with_otp(email)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue