Kunal commited on
Commit ·
8f64006
1
Parent(s): bc03319
update auth.py
Browse files- src/auth.py +40 -64
src/auth.py
CHANGED
|
@@ -20,21 +20,42 @@ PORT = int(os.getenv("MAL_PORT", 8000))
|
|
| 20 |
|
| 21 |
class MALAuth:
|
| 22 |
def __init__(self):
|
| 23 |
-
|
| 24 |
-
self.
|
| 25 |
-
self.code_challenge = None
|
| 26 |
self.auth_code = None
|
| 27 |
self.error = None
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
auth_url = (
|
| 40 |
"https://myanimelist.net/v1/oauth2/authorize?"
|
|
@@ -43,60 +64,15 @@ class MALAuth:
|
|
| 43 |
f"code_challenge={self.code_challenge}&"
|
| 44 |
f"redirect_uri={REDIRECT_URI}"
|
| 45 |
)
|
| 46 |
-
|
| 47 |
-
st.link_button("Click here to authenticate with MyAnimeList", auth_url)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
self.error = query_params["error"][0] if isinstance(query_params["error"], list) else query_params["error"]
|
| 55 |
-
return None, self.error
|
| 56 |
|
| 57 |
-
|
| 58 |
-
if "localhost" in REDIRECT_URI or "127.0.0.1" in REDIRECT_URI:
|
| 59 |
-
try:
|
| 60 |
-
from http.server import HTTPServer, BaseHTTPRequestHandler
|
| 61 |
-
import threading
|
| 62 |
-
import time
|
| 63 |
-
class OAuthHandler(BaseHTTPRequestHandler):
|
| 64 |
-
auth_code = None
|
| 65 |
-
error = None
|
| 66 |
-
def do_GET(self):
|
| 67 |
-
from urllib.parse import urlparse, parse_qs
|
| 68 |
-
parsed = urlparse(self.path)
|
| 69 |
-
params = parse_qs(parsed.query)
|
| 70 |
-
if "code" in params:
|
| 71 |
-
OAuthHandler.auth_code = params["code"][0]
|
| 72 |
-
self.send_response(200)
|
| 73 |
-
self.end_headers()
|
| 74 |
-
self.wfile.write(b"Authorization successful! Return to the app.")
|
| 75 |
-
elif "error" in params:
|
| 76 |
-
OAuthHandler.error = params["error"][0]
|
| 77 |
-
self.send_response(400)
|
| 78 |
-
self.end_headers()
|
| 79 |
-
self.wfile.write(b"Authorization failed. Check your settings.")
|
| 80 |
-
else:
|
| 81 |
-
self.send_response(400)
|
| 82 |
-
self.end_headers()
|
| 83 |
-
self.wfile.write(b"Invalid request")
|
| 84 |
-
server = HTTPServer(('localhost', PORT), OAuthHandler)
|
| 85 |
-
server.timeout = 120
|
| 86 |
-
server_thread = threading.Thread(target=server.handle_request)
|
| 87 |
-
server_thread.daemon = True
|
| 88 |
-
server_thread.start()
|
| 89 |
-
import webbrowser
|
| 90 |
-
webbrowser.open(auth_url)
|
| 91 |
-
start_time = time.time()
|
| 92 |
-
while not OAuthHandler.auth_code and not OAuthHandler.error:
|
| 93 |
-
if time.time() - start_time > 120:
|
| 94 |
-
return None, "Authorization timed out"
|
| 95 |
-
time.sleep(0.5)
|
| 96 |
-
return OAuthHandler.auth_code, OAuthHandler.error
|
| 97 |
-
except Exception as e:
|
| 98 |
-
return None, f"Local OAuth server failed: {e}"
|
| 99 |
-
return None, None
|
| 100 |
|
| 101 |
def get_access_token(self, auth_code):
|
| 102 |
token_url = "https://myanimelist.net/v1/oauth2/token"
|
|
@@ -104,7 +80,7 @@ class MALAuth:
|
|
| 104 |
"client_id": CLIENT_ID,
|
| 105 |
"client_secret": CLIENT_SECRET,
|
| 106 |
"code": auth_code,
|
| 107 |
-
"code_verifier": self.code_verifier
|
| 108 |
"grant_type": "authorization_code",
|
| 109 |
"redirect_uri": REDIRECT_URI
|
| 110 |
}
|
|
|
|
| 20 |
|
| 21 |
class MALAuth:
|
| 22 |
def __init__(self):
|
| 23 |
+
self.code_verifier = secrets.token_urlsafe(64)
|
| 24 |
+
self.code_challenge = self.code_verifier # Using 'plain' method
|
|
|
|
| 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?"
|
|
|
|
| 64 |
f"code_challenge={self.code_challenge}&"
|
| 65 |
f"redirect_uri={REDIRECT_URI}"
|
| 66 |
)
|
| 67 |
+
webbrowser.open(auth_url)
|
|
|
|
| 68 |
|
| 69 |
+
start_time = time.time()
|
| 70 |
+
while not self.OAuthHandler.auth_code and not self.OAuthHandler.error:
|
| 71 |
+
if time.time() - start_time > 120:
|
| 72 |
+
return None, "Authorization timed out"
|
| 73 |
+
time.sleep(0.5)
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
return self.OAuthHandler.auth_code, self.OAuthHandler.error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
def get_access_token(self, auth_code):
|
| 78 |
token_url = "https://myanimelist.net/v1/oauth2/token"
|
|
|
|
| 80 |
"client_id": CLIENT_ID,
|
| 81 |
"client_secret": CLIENT_SECRET,
|
| 82 |
"code": auth_code,
|
| 83 |
+
"code_verifier": self.code_verifier,
|
| 84 |
"grant_type": "authorization_code",
|
| 85 |
"redirect_uri": REDIRECT_URI
|
| 86 |
}
|