7878websocket_connector : ThreadSafeWebSocketConnector = None
7979github_login_status_change_updater_enabled = False
8080
81+ deprecated_user_data_file = os .path .join (os .path .expanduser ("~" ), ".jupyter" , "nbi-data.json" )
82+ user_data_file = os .path .join (os .path .expanduser ("~" ), ".jupyter" , "nbi" , "user-data.json" )
83+ access_token_password = os .getenv ("NBI_GH_ACCESS_TOKEN_PASSWORD" , "nbi-access-token-password" )
84+
85+
86+ def get_gh_access_token_from_env () -> str :
87+ access_token = os .environ .get ("NBI_GH_ACCESS_TOKEN_ENCRYPTED" )
88+ if access_token is not None :
89+ try :
90+ base64_bytes = base64 .b64decode (access_token .encode ("utf-8" ))
91+ return decrypt_with_password (access_token_password , base64_bytes ).decode ("utf-8" )
92+ except Exception as e :
93+ log .error (f"Failed to decrypt GitHub access token from environment variable: { e } " )
94+ return None
95+
8196
8297def enable_github_login_status_change_updater (enabled : bool ):
8398 global github_login_status_change_updater_enabled
@@ -109,11 +124,6 @@ def get_login_status():
109124 return response
110125
111126
112- deprecated_user_data_file = os .path .join (os .path .expanduser ("~" ), ".jupyter" , "nbi-data.json" )
113- user_data_file = os .path .join (os .path .expanduser ("~" ), ".jupyter" , "nbi" , "user-data.json" )
114- access_token_password = os .getenv ("NBI_GH_ACCESS_TOKEN_PASSWORD" , "nbi-access-token-password" )
115-
116-
117127def read_stored_github_access_token () -> str :
118128 try :
119129 if os .path .exists (user_data_file ):
@@ -186,7 +196,12 @@ def login_with_existing_credentials(store_access_token: bool):
186196 if github_auth ["status" ] is not LoginStatus .NOT_LOGGED_IN :
187197 return
188198
189- if store_access_token :
199+ # Check for GitHub access token in environment variable
200+ github_access_token_provided = get_gh_access_token_from_env ()
201+ if github_access_token_provided :
202+ log .info ("Using GitHub access token from environment variable" )
203+ remember_github_access_token = False # Do not store the token if it's from the environment
204+ elif store_access_token :
190205 github_access_token_provided = read_stored_github_access_token ()
191206 remember_github_access_token = True
192207 else :
@@ -331,8 +346,7 @@ def wait_for_user_access_token_thread_func():
331346
332347def get_token ():
333348 global github_auth , github_access_token_provided , API_ENDPOINT , PROXY_ENDPOINT , TOKEN_REFRESH_INTERVAL
334- access_token = github_auth ["access_token" ]
335-
349+ access_token = get_gh_access_token_from_env () or github_auth ["access_token" ]
336350 if access_token is None :
337351 return
338352
@@ -351,7 +365,6 @@ def get_token():
351365 )
352366
353367 resp_json = resp .json ()
354-
355368 if resp .status_code == 401 :
356369 github_access_token_provided = None
357370 logout ()
@@ -393,7 +406,9 @@ def get_token_thread_func():
393406 return
394407 token = github_auth ["token" ]
395408 # update token if 10 seconds or less left to expiration
396- if github_auth ["access_token" ] is not None and (
409+ access_token = get_gh_access_token_from_env () or github_auth ["access_token" ]
410+
411+ if access_token and (
397412 token is None
398413 or (dt .datetime .now () - github_auth ["token_expires_at" ]).total_seconds () > - 10
399414 ):
0 commit comments