awacke1 commited on
Commit
efa2464
Β·
verified Β·
1 Parent(s): 7df41e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -21
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import streamlit as st
2
  import os
3
  import pandas as pd
4
- from azure.identity import ClientSecretCredential
5
  from azure.mgmt.resource import ResourceManagementClient
6
  from azure.cosmos import CosmosClient, exceptions
7
  from github import Github
@@ -9,12 +9,42 @@ from git import Repo
9
  import shutil
10
  from datetime import datetime
11
  import base64
 
 
12
 
13
  # Azure configuration
14
  ENDPOINT = "https://acae-afd.documents.azure.com:443/"
15
  DATABASE_NAME = os.environ.get("COSMOS_DATABASE_NAME")
16
  CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  # GitHub configuration
19
  def download_github_repo(url, local_path):
20
  if os.path.exists(local_path):
@@ -78,28 +108,51 @@ if 'logged_in' not in st.session_state:
78
  # Login section
79
  if not st.session_state.logged_in:
80
  st.subheader("πŸ” Login")
81
- tenant_id = st.text_input("Azure Tenant ID")
82
- client_id = st.text_input("Azure Client ID")
83
- client_secret = st.text_input("Azure Client Secret", type="password")
84
- subscription_id = st.text_input("Azure Subscription ID")
 
 
 
 
 
85
  if st.button("πŸš€ Login"):
86
- try:
87
- credential = ClientSecretCredential(
88
- tenant_id=tenant_id,
89
- client_id=client_id,
90
- client_secret=client_secret
91
- )
92
- # Try to get a token to verify the credential works
93
- credential.get_token("https://management.azure.com/.default")
94
- st.session_state.credential = credential
95
- st.session_state.subscription_id = subscription_id
96
- st.session_state.logged_in = True
97
- st.success("Successfully logged in!")
98
- st.rerun()
99
- except Exception as e:
100
- st.error(f"Failed to authenticate: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  else:
102
- # Main app content
103
  col1, col2 = st.columns([1, 3])
104
 
105
  with col1:
 
1
  import streamlit as st
2
  import os
3
  import pandas as pd
4
+ from azure.identity import AzureCliCredential, DefaultAzureCredential
5
  from azure.mgmt.resource import ResourceManagementClient
6
  from azure.cosmos import CosmosClient, exceptions
7
  from github import Github
 
9
  import shutil
10
  from datetime import datetime
11
  import base64
12
+ from playwright.sync_api import sync_playwright
13
+ import time
14
 
15
  # Azure configuration
16
  ENDPOINT = "https://acae-afd.documents.azure.com:443/"
17
  DATABASE_NAME = os.environ.get("COSMOS_DATABASE_NAME")
18
  CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
19
 
20
+ # Playwright function for Azure login
21
+ def azure_login_with_playwright(username, password):
22
+ with sync_playwright() as p:
23
+ browser = p.chromium.launch(headless=False) # Set to True for headless mode
24
+ page = browser.new_page()
25
+ page.goto("https://portal.azure.com")
26
+
27
+ # Wait for and fill in the email field
28
+ page.wait_for_selector('input[type="email"]')
29
+ page.fill('input[type="email"]', username)
30
+ page.click('input[type="submit"]')
31
+
32
+ # Wait for and fill in the password field
33
+ page.wait_for_selector('input[type="password"]')
34
+ page.fill('input[type="password"]', password)
35
+ page.click('input[type="submit"]')
36
+
37
+ # Wait for login to complete (adjust as needed)
38
+ page.wait_for_selector('button[id="nav_menu"]', timeout=60000)
39
+
40
+ # At this point, the login should be complete
41
+ # You may want to extract any necessary tokens or cookies here
42
+
43
+ browser.close()
44
+
45
+ # For now, we'll just return True to indicate successful login
46
+ return True
47
+
48
  # GitHub configuration
49
  def download_github_repo(url, local_path):
50
  if os.path.exists(local_path):
 
108
  # Login section
109
  if not st.session_state.logged_in:
110
  st.subheader("πŸ” Login")
111
+ st.write("Please enter your Azure Subscription ID. You can find this in the Azure Portal under Subscriptions.")
112
+ subscription_id = st.text_input("Azure Subscription ID", value="003fba60-5b3f-48f4-ab36-3ed11bc40816")
113
+
114
+ login_method = st.radio("Choose login method", ["Automatic", "Manual (Browser Simulation)"])
115
+
116
+ if login_method == "Manual (Browser Simulation)":
117
+ username = st.text_input("Azure Username/Email")
118
+ password = st.text_input("Azure Password", type="password")
119
+
120
  if st.button("πŸš€ Login"):
121
+ if login_method == "Automatic":
122
+ try:
123
+ # First, try AzureCliCredential
124
+ credential = AzureCliCredential()
125
+ credential.get_token("https://management.azure.com/.default")
126
+ except Exception:
127
+ # If AzureCliCredential fails, fall back to DefaultAzureCredential
128
+ try:
129
+ credential = DefaultAzureCredential()
130
+ credential.get_token("https://management.azure.com/.default")
131
+ except Exception as e:
132
+ st.error(f"Failed to authenticate automatically. Please try the Manual (Browser Simulation) method. Error: {str(e)}")
133
+ st.stop()
134
+ else:
135
+ if not username or not password:
136
+ st.error("Please enter both username and password for manual login.")
137
+ st.stop()
138
+ try:
139
+ if azure_login_with_playwright(username, password):
140
+ credential = DefaultAzureCredential()
141
+ credential.get_token("https://management.azure.com/.default")
142
+ else:
143
+ st.error("Failed to authenticate using browser simulation.")
144
+ st.stop()
145
+ except Exception as e:
146
+ st.error(f"Failed to authenticate using browser simulation. Error: {str(e)}")
147
+ st.stop()
148
+
149
+ st.session_state.credential = credential
150
+ st.session_state.subscription_id = subscription_id
151
+ st.session_state.logged_in = True
152
+ st.success("Successfully logged in!")
153
+ st.rerun()
154
  else:
155
+ # Main app content (unchanged)
156
  col1, col2 = st.columns([1, 3])
157
 
158
  with col1: