99import base64
1010import hashlib
1111import os
12- import sys
1312import time
1413import webbrowser
1514from datetime import datetime , timedelta
@@ -91,7 +90,7 @@ def do_GET(self):
9190 self .wfile .write (b"Authorization successful. You can close this tab." )
9291
9392
94- def first_authorization (client_id : str , client_secret : str | None , * , scope : str , redirect_uri : str ) -> bool :
93+ def first_authorization (client_id : str , client_secret : str | None , * , scope : str , redirect_uri : str ) -> None :
9594 """Perform auth via browser and cache tokens.
9695
9796 Uses Authorization Code + PKCE (S256) as required by XF for public clients.
@@ -145,14 +144,16 @@ def first_authorization(client_id: str, client_secret: str | None, *, scope: str
145144 headers = {"Content-Type" : "application/x-www-form-urlencoded" , "Accept" : "application/json" }
146145 response = httpx .post (TOKEN_ENDPOINT , headers = headers , data = payload , timeout = 10.0 )
147146 if not response .is_success :
148- print ("Failed to retrieve tokens." )
149- print (response .text )
150- return False
147+ details = response .text .strip ()
148+ if details :
149+ raise RuntimeError (f"Failed to retrieve tokens.\n { details } " )
150+ raise RuntimeError ("Failed to retrieve tokens." )
151151
152152 token_data = response .json ()
153153 if token_data .get ("error" ):
154- print (f"OAuth token error: { token_data .get ('error' )} { token_data .get ('error_description' , '' )} " .strip ())
155- return False
154+ raise RuntimeError (
155+ f"OAuth token error: { token_data .get ('error' )} { token_data .get ('error_description' , '' )} " .strip ()
156+ )
156157
157158 # Step 5: Cache tokens and basic user info
158159 store_tokens_in_keyring (token_data )
@@ -164,8 +165,6 @@ def first_authorization(client_id: str, client_secret: str | None, *, scope: str
164165 except Exception :
165166 pass
166167
167- return True
168-
169168
170169def _cache_user_info (access_token : str | None ) -> None :
171170 """Fetch /api/me and cache username/user_id (best-effort)."""
@@ -195,11 +194,7 @@ def authorize():
195194 redirect_uri = _get_setting ("OAUTH_REDIRECT_URI" , DEFAULT_REDIRECT_URI )
196195
197196 if not client_id :
198- print ("OAuth client is not configured." )
199- print ("Set one of the following:" )
200- print (" - Environment variable: REDFETCH_OAUTH_CLIENT_ID" )
201- print (" - Or add to your settings.local.toml: OAUTH_CLIENT_ID = \" ...\" " )
202- sys .exit (1 )
197+ raise RuntimeError ("OAuth client is not configured." )
203198
204199 data = get_cached_tokens ()
205200
@@ -216,9 +211,7 @@ def authorize():
216211
217212 # Fall back to interactive authorization
218213 print ("Performing full authorization..." )
219- if not first_authorization (client_id , client_secret , scope = scope , redirect_uri = redirect_uri ):
220- print ("Authorization failed." )
221- sys .exit (1 )
214+ first_authorization (client_id , client_secret , scope = scope , redirect_uri = redirect_uri )
222215
223216
224217def _port_from_redirect_uri (redirect_uri : str ) -> int :
@@ -372,16 +365,18 @@ def initialize_keyring():
372365 # Attempt to use the keyring to trigger any potential errors
373366 keyring .get_password ('test_service' , 'test_user' )
374367 except (NoKeyringError , ModuleNotFoundError ):
375- print ("No suitable keyring backend found, probably because you're not on Windows." )
376- print ("Please install `keyrings.alt` by running:" )
377- print (" pip install keyrings.alt" )
378- print ("Then restart the application." )
379- sys .exit (1 )
368+ raise RuntimeError (
369+ "No suitable keyring backend found, probably because you're not on Windows.\n \n "
370+ "Please install `keyrings.alt` by running:\n "
371+ " pip install keyrings.alt\n \n "
372+ "Then restart the application."
373+ )
380374 except Exception as e :
381375 # Catch any other exceptions that may occur and handle them gracefully
382- print (f"An error occurred while initializing keyring: { e } " )
383- print ("Please ensure that a suitable keyring backend is available." )
384- sys .exit (1 )
376+ raise RuntimeError (
377+ f"An error occurred while initializing keyring: { e } \n \n "
378+ "Please ensure that a suitable keyring backend is available."
379+ ) from e
385380
386381
387382if __name__ == "__main__" :
0 commit comments