Kunal commited on
Commit ·
6c434ae
1
Parent(s): c86af03
update auth.py
Browse files- src/auth.py +52 -38
src/auth.py
CHANGED
|
@@ -25,38 +25,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,15 +33,60 @@ class MALAuth:
|
|
| 64 |
f"code_challenge={self.code_challenge}&"
|
| 65 |
f"redirect_uri={REDIRECT_URI}"
|
| 66 |
)
|
| 67 |
-
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
def get_access_token(self, auth_code):
|
| 78 |
token_url = "https://myanimelist.net/v1/oauth2/token"
|
|
|
|
| 25 |
self.auth_code = None
|
| 26 |
self.error = None
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def start_oauth_flow(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
auth_url = (
|
| 30 |
"https://myanimelist.net/v1/oauth2/authorize?"
|
| 31 |
f"response_type=code&"
|
|
|
|
| 33 |
f"code_challenge={self.code_challenge}&"
|
| 34 |
f"redirect_uri={REDIRECT_URI}"
|
| 35 |
)
|
| 36 |
+
st.markdown(f"[Click here to authenticate with MyAnimeList]({auth_url})", unsafe_allow_html=True)
|
| 37 |
|
| 38 |
+
# Streamlit Community Cloud: handle redirect via query params
|
| 39 |
+
query_params = st.experimental_get_query_params()
|
| 40 |
+
if "code" in query_params:
|
| 41 |
+
self.auth_code = query_params["code"][0]
|
| 42 |
+
return self.auth_code, None
|
| 43 |
+
elif "error" in query_params:
|
| 44 |
+
self.error = query_params["error"][0]
|
| 45 |
+
return None, self.error
|
| 46 |
|
| 47 |
+
# Local: try to run a local HTTP server if running on localhost
|
| 48 |
+
if "localhost" in REDIRECT_URI or "127.0.0.1" in REDIRECT_URI:
|
| 49 |
+
try:
|
| 50 |
+
from http.server import HTTPServer, BaseHTTPRequestHandler
|
| 51 |
+
import threading
|
| 52 |
+
import time
|
| 53 |
+
class OAuthHandler(BaseHTTPRequestHandler):
|
| 54 |
+
auth_code = None
|
| 55 |
+
error = None
|
| 56 |
+
def do_GET(self):
|
| 57 |
+
from urllib.parse import urlparse, parse_qs
|
| 58 |
+
parsed = urlparse(self.path)
|
| 59 |
+
params = parse_qs(parsed.query)
|
| 60 |
+
if "code" in params:
|
| 61 |
+
OAuthHandler.auth_code = params["code"][0]
|
| 62 |
+
self.send_response(200)
|
| 63 |
+
self.end_headers()
|
| 64 |
+
self.wfile.write(b"Authorization successful! Return to the app.")
|
| 65 |
+
elif "error" in params:
|
| 66 |
+
OAuthHandler.error = params["error"][0]
|
| 67 |
+
self.send_response(400)
|
| 68 |
+
self.end_headers()
|
| 69 |
+
self.wfile.write(b"Authorization failed. Check your settings.")
|
| 70 |
+
else:
|
| 71 |
+
self.send_response(400)
|
| 72 |
+
self.end_headers()
|
| 73 |
+
self.wfile.write(b"Invalid request")
|
| 74 |
+
server = HTTPServer(('localhost', PORT), OAuthHandler)
|
| 75 |
+
server.timeout = 120
|
| 76 |
+
server_thread = threading.Thread(target=server.handle_request)
|
| 77 |
+
server_thread.daemon = True
|
| 78 |
+
server_thread.start()
|
| 79 |
+
import webbrowser
|
| 80 |
+
webbrowser.open(auth_url)
|
| 81 |
+
start_time = time.time()
|
| 82 |
+
while not OAuthHandler.auth_code and not OAuthHandler.error:
|
| 83 |
+
if time.time() - start_time > 120:
|
| 84 |
+
return None, "Authorization timed out"
|
| 85 |
+
time.sleep(0.5)
|
| 86 |
+
return OAuthHandler.auth_code, OAuthHandler.error
|
| 87 |
+
except Exception as e:
|
| 88 |
+
return None, f"Local OAuth server failed: {e}"
|
| 89 |
+
return None, None
|
| 90 |
|
| 91 |
def get_access_token(self, auth_code):
|
| 92 |
token_url = "https://myanimelist.net/v1/oauth2/token"
|