File size: 2,382 Bytes
84133c2
29a8657
 
 
 
c23a319
33a3ad3
7880dfb
29a8657
 
 
 
 
 
 
691a1ad
 
29a8657
 
 
 
 
 
1c7692e
29a8657
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c7692e
29a8657
6f1f9a4
 
 
bee02cd
 
 
 
 
29a8657
 
 
 
 
 
bee02cd
29a8657
 
 
 
 
 
84133c2
29a8657
 
bee02cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import gradio as gr
import requests
import webbrowser
from http.server import BaseHTTPRequestHandler, HTTPServer
import threading
import spaces

# OAuth Configuration
TENANT_ID = '2b093ced-2571-463f-bc3e-b4f8bcb427ee'
CLIENT_ID = '2a7c884c-942d-49e2-9e5d-7a29d8a0d3e5'
CLIENT_SECRET = 'EOF8Q~kKHCRgx8tnlLM-H8e93ifetxI6x7sU6bGW'
REDIRECT_URI = 'https://sanjeevbora-chatbot.hf.space/' 
AUTH_URL = f"https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/authorize"
TOKEN_URL = f"https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/token"
SCOPE = 'User.Read'
access_token = None

class RequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        global access_token
        if self.path.startswith("/callback"):
            # Capture the authorization code
            code = self.path.split("code=")[1]
            response = requests.post(TOKEN_URL, data={
                'client_id': CLIENT_ID,
                'client_secret': CLIENT_SECRET,
                'grant_type': 'authorization_code',
                'code': code,
                'redirect_uri': REDIRECT_URI
            })
            token_data = response.json()
            access_token = token_data.get('access_token')
            self.send_response(200)
            self.end_headers()
            self.wfile.write(b"Login successful! You can close this window.")
            return

        self.send_response(404)
        self.end_headers()

def start_http_server():
    server_address = ('', 8080)
    httpd = HTTPServer(server_address, RequestHandler)
    httpd.serve_forever()

def login():
    auth_url = f"{AUTH_URL}?client_id={CLIENT_ID}&response_type=code&redirect_uri={REDIRECT_URI}&scope={SCOPE}"
    webbrowser.open(auth_url)

def check_login():
    return "You are logged in!" if access_token else "You are not logged in."

def handle_login_click():
    login()
    return check_login()

def gradio_interface():
    with gr.Blocks() as demo:
        gr.Markdown("### Welcome to the App")
        btn_login = gr.Button("Login with Microsoft")
        output = gr.Textbox(label="Status")

        btn_login.click(handle_login_click, None, output)

    return demo

if __name__ == "__main__":
    # Start the HTTP server in a separate thread
    threading.Thread(target=start_http_server, daemon=True).start()
    
    # Launch Gradio app
    demo = gradio_interface()
    demo.launch()