From 090bee36f6fe8ad3733206bcd8034aab5241f87c Mon Sep 17 00:00:00 2001 From: Satveer Khurpa Date: Mon, 10 Nov 2025 17:44:00 -0600 Subject: [PATCH] OAuth2 updates for session storage --- .../chatbot_app_cognito.py | 313 +++++++++++++++--- .../oauth2_callback_server.py | 266 ++++++++++++++- 2 files changed, 528 insertions(+), 51 deletions(-) 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( """ -
-
+ + """, @@ -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( """ -
+ +