Kunal
commited on
Commit
·
ec685b6
1
Parent(s):
2ecc14b
update auth.py
Browse files- src/auth.py +10 -41
src/auth.py
CHANGED
@@ -3,9 +3,7 @@ import streamlit as st
|
|
3 |
import requests
|
4 |
import webbrowser
|
5 |
import secrets
|
6 |
-
import threading
|
7 |
import time
|
8 |
-
from http.server import HTTPServer, BaseHTTPRequestHandler
|
9 |
from urllib.parse import urlparse, parse_qs
|
10 |
from dotenv import load_dotenv
|
11 |
import os
|
@@ -25,38 +23,7 @@ class MALAuth:
|
|
25 |
self.auth_code = None
|
26 |
self.error = None
|
27 |
|
28 |
-
class OAuthHandler(BaseHTTPRequestHandler):
|
29 |
-
auth_code = None
|
30 |
-
error = None
|
31 |
-
|
32 |
-
def do_GET(self):
|
33 |
-
parsed = urlparse(self.path)
|
34 |
-
params = parse_qs(parsed.query)
|
35 |
-
if "code" in params:
|
36 |
-
MALAuth.OAuthHandler.auth_code = params["code"][0]
|
37 |
-
self.send_response(200)
|
38 |
-
self.end_headers()
|
39 |
-
self.wfile.write(b"Authorization successful! Return to the app.")
|
40 |
-
elif "error" in params:
|
41 |
-
MALAuth.OAuthHandler.error = params["error"][0]
|
42 |
-
self.send_response(400)
|
43 |
-
self.end_headers()
|
44 |
-
self.wfile.write(b"Authorization failed. Check your settings.")
|
45 |
-
else:
|
46 |
-
self.send_response(400)
|
47 |
-
self.end_headers()
|
48 |
-
self.wfile.write(b"Invalid request")
|
49 |
-
|
50 |
-
def run_server(self):
|
51 |
-
server = HTTPServer(('localhost', PORT), self.OAuthHandler)
|
52 |
-
server.timeout = 120
|
53 |
-
server.handle_request()
|
54 |
-
|
55 |
def start_oauth_flow(self):
|
56 |
-
server_thread = threading.Thread(target=self.run_server)
|
57 |
-
server_thread.daemon = True
|
58 |
-
server_thread.start()
|
59 |
-
|
60 |
auth_url = (
|
61 |
"https://myanimelist.net/v1/oauth2/authorize?"
|
62 |
f"response_type=code&"
|
@@ -64,16 +31,18 @@ class MALAuth:
|
|
64 |
f"code_challenge={self.code_challenge}&"
|
65 |
f"redirect_uri={REDIRECT_URI}"
|
66 |
)
|
67 |
-
# webbrowser.open(auth_url) # Remove this for hosted environments
|
68 |
st.markdown(f"[Click here to authenticate with MyAnimeList]({auth_url})", unsafe_allow_html=True)
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
77 |
|
78 |
def get_access_token(self, auth_code):
|
79 |
token_url = "https://myanimelist.net/v1/oauth2/token"
|
|
|
3 |
import requests
|
4 |
import webbrowser
|
5 |
import secrets
|
|
|
6 |
import time
|
|
|
7 |
from urllib.parse import urlparse, parse_qs
|
8 |
from dotenv import load_dotenv
|
9 |
import os
|
|
|
23 |
self.auth_code = None
|
24 |
self.error = None
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
def start_oauth_flow(self):
|
|
|
|
|
|
|
|
|
27 |
auth_url = (
|
28 |
"https://myanimelist.net/v1/oauth2/authorize?"
|
29 |
f"response_type=code&"
|
|
|
31 |
f"code_challenge={self.code_challenge}&"
|
32 |
f"redirect_uri={REDIRECT_URI}"
|
33 |
)
|
|
|
34 |
st.markdown(f"[Click here to authenticate with MyAnimeList]({auth_url})", unsafe_allow_html=True)
|
35 |
|
36 |
+
# Check for code in query params (Streamlit Cloud compatible)
|
37 |
+
query_params = st.experimental_get_query_params()
|
38 |
+
if "code" in query_params:
|
39 |
+
self.auth_code = query_params["code"][0]
|
40 |
+
return self.auth_code, None
|
41 |
+
elif "error" in query_params:
|
42 |
+
self.error = query_params["error"][0]
|
43 |
+
return None, self.error
|
44 |
+
else:
|
45 |
+
return None, None
|
46 |
|
47 |
def get_access_token(self, auth_code):
|
48 |
token_url = "https://myanimelist.net/v1/oauth2/token"
|