@@ -53,17 +53,23 @@ def get_token(logger):
5353
5454 if token is not None :
5555 return token
56- elif args .mode == "pat-auth" :
56+ if args .mode == "pat-auth" :
5757 token = GITHUB_TOKEN
58- elif args .mode == "app-auth" :
58+ return token
59+ if args .mode == "app-auth" :
5960 token = GITHUB_APP_TOKEN
6061 if not token :
61- logger .info (f "Obtain the Github App token by accessing: http://localhost:8000/auth" )
62- logger .info (f "and set GITHUB_APP_TOKEN as environment variable." )
63- raise Exception ("Github APP token not found." )
62+ logger .info ("Obtain the Github App token by accessing: http://localhost:8000/auth" )
63+ logger .info ("and set GITHUB_APP_TOKEN as environment variable." )
64+ raise RuntimeError ("Github APP token not found." )
6465 return token
6566
6667def authenticate_with_device_flow (logger ):
68+ """Initiates Github Device Authentication flow
69+
70+ Returns:
71+ token: Github App token for the authorized user
72+ """
6773 device_code_url = "https://github.com/login/device/code"
6874 client_id = APP_AUTH_ID
6975 payload = {
@@ -76,7 +82,7 @@ def authenticate_with_device_flow(logger):
7682 }
7783
7884 try :
79- response = requests .post (device_code_url , json = payload , headers = headers )
85+ response = requests .post (device_code_url , json = payload , headers = headers , timeout = 10 )
8086 if response .status_code == 200 :
8187 data = response .json ()
8288 device_code = data ['device_code' ]
@@ -86,29 +92,37 @@ def authenticate_with_device_flow(logger):
8692 logger .info (f"Activate GitHub authentication at: { verification_uri } " )
8793 logger .info (f"Enter activation code: { user_code } " )
8894
89- logger .info (f "Waiting 30 seconds for the user to authorize the device..." )
95+ logger .info ("Waiting 30 seconds for the user to authorize the device..." )
9096 time .sleep (30 )
9197
9298 token_url = "https://github.com/login/oauth/access_token"
9399 token_response = requests .post (token_url , json = {
94100 "client_id" : client_id ,
95101 "device_code" : device_code ,
96102 "grant_type" : "urn:ietf:params:oauth:grant-type:device_code"
97- }, headers = {"Accept" : "application/json" })
103+ }, headers = {"Accept" : "application/json" }, timeout = 10 )
98104
99105 token_data = token_response .json ()
100106 token_value = token_data .get ("access_token" )
101107
102108 if token_value is not None :
103109 logger .info ("Successfully obtained access token." )
104- logger .info (f"Please set env var GITHUB_APP_TOKEN={ token_value } and restart the app." )
105- else :
106- logger .error (f"Failed to obtain access token. Status: { token_response .status_code } , Response: { token_response .text } " )
110+ logger .info (
111+ "Please set env var GITHUB_APP_TOKEN=%s and restart the app." ,
112+ token_value
113+ )
107114 return None
108- else :
109- logger .error (f"Failed to initiate device flow: { response .status_code } { response .text } " )
115+ logger .error (
116+ "Failed to obtain access token. "
117+ "Status: %s, Response: %s" ,
118+ token_response .status_code ,
119+ token_response .text
120+ )
110121 return None
111122
123+ logger .error (f"Failed to initiate device flow: { response .status_code } { response .text } " )
124+ return None
125+
112126 except Exception as e :
113127 logger .exception (f"Error during device flow authentication: { str (e )} " )
114128 return None
0 commit comments