diff --git a/01-tutorials/03-AgentCore-identity/05-Outbound_Auth_3lo/chatbot_app_cognito.py b/01-tutorials/03-AgentCore-identity/05-Outbound_Auth_3lo/chatbot_app_cognito.py index b807f2f7..fdcf3d38 100644 --- a/01-tutorials/03-AgentCore-identity/05-Outbound_Auth_3lo/chatbot_app_cognito.py +++ b/01-tutorials/03-AgentCore-identity/05-Outbound_Auth_3lo/chatbot_app_cognito.py @@ -1,4 +1,5 @@ import streamlit as st +import streamlit.components.v1 as components import os import json import requests @@ -17,6 +18,60 @@ CONTEXT_WINDOW = 10 # Number of turns (user+assistant pairs) to include in context +def store_token_in_browser(token: str): + """Store the access token in browser's localStorage using JavaScript.""" + components.html( + f""" + + """, + height=0, + ) + + +def get_token_from_browser(): + """Retrieve the access token from browser's localStorage using JavaScript.""" + token_html = components.html( + """ + + """, + height=0, + ) + return token_html + + +def clear_token_from_browser(): + """Clear the access token from browser's localStorage.""" + components.html( + """ + + """, + height=0, + ) + + def get_streamlit_url(): try: # Read the JSON file @@ -258,6 +313,72 @@ def main(): layout="wide", initial_sidebar_state="expanded", ) + + # Add custom CSS for better styling + st.markdown(""" + + """, unsafe_allow_html=True) + import boto3 # Check if configuration loading failed @@ -304,12 +425,38 @@ def main(): if st.session_state["cognito_access_token"] is None: st.markdown( """ -
-
+ +
+
-
🔐
+
🔐

Bedrock AgentCore AI Login

-

Secure access to your AI assistant
🔒 End-to-end encrypted • Never stored

+

Secure access to your AI assistant
🔒 End-to-end encrypted • Stored in browser

""", @@ -393,33 +540,63 @@ def main(): ) access_token = resp["AuthenticationResult"]["AccessToken"] st.session_state["cognito_access_token"] = access_token - st.success( - "Cognito authentication successful! Redirecting to chatbot..." - ) + + # Store token in browser's localStorage for OAuth flow + store_token_in_browser(access_token) + + # Show success with balloons! + st.balloons() + st.success("🎉 Authentication Successful!") + st.info("🔒 Token securely stored in browser localStorage") + + import time + time.sleep(1.5) # Show success message briefly st.rerun() except Exception as e: st.error(f"Cognito authentication failed: {e}") return # Only return here if not authenticated - # Enhanced system status panel + # Enhanced system status panel with animations st.markdown( f""" -
+ +
System Status
-
+
Region: {region}
-
+
Agent: - Active + Active
-
+
Session: - Connected + Connected
@@ -549,10 +726,35 @@ def main(): unsafe_allow_html=True, ) - # Enhanced sidebar + # Enhanced sidebar with animations st.sidebar.markdown( """ -
+ +