@@ -128,15 +128,15 @@ def _get_password(self, service_name: str, username: str) -> Optional[str]:
128
128
)
129
129
if res .returncode :
130
130
return None
131
- return res .stdout .decode ("utf-8" ).strip (" \n " )
131
+ return res .stdout .decode ("utf-8" ).strip (os . linesep )
132
132
133
133
def _set_password (self , service_name : str , username : str , password : str ) -> None :
134
134
"""Mirror the implementation of keyring.set_password using cli"""
135
135
if self .keyring is None :
136
136
return None
137
137
138
138
cmd = [self .keyring , "set" , service_name , username ]
139
- input_ = password . encode ("utf-8" ) + b" \n "
139
+ input_ = ( password + os . linesep ). encode ("utf-8" )
140
140
env = os .environ .copy ()
141
141
env ["PYTHONIOENCODING" ] = "utf-8"
142
142
res = subprocess .run (cmd , input = input_ , env = env )
@@ -190,10 +190,14 @@ def get_keyring_auth(url: Optional[str], username: Optional[str]) -> Optional[Au
190
190
191
191
class MultiDomainBasicAuth (AuthBase ):
192
192
def __init__ (
193
- self , prompting : bool = True , index_urls : Optional [List [str ]] = None
193
+ self ,
194
+ prompting : bool = True ,
195
+ index_urls : Optional [List [str ]] = None ,
196
+ use_keyring : bool = True ,
194
197
) -> None :
195
198
self .prompting = prompting
196
199
self .index_urls = index_urls
200
+ self .use_keyring = use_keyring
197
201
self .passwords : Dict [str , AuthInfo ] = {}
198
202
# When the user is prompted to enter credentials and keyring is
199
203
# available, we will offer to save them. If the user accepts,
@@ -227,7 +231,8 @@ def _get_index_url(self, url: str) -> Optional[str]:
227
231
def _get_new_credentials (
228
232
self ,
229
233
original_url : str ,
230
- allow_netrc : bool = True ,
234
+ * ,
235
+ allow_netrc : bool = False ,
231
236
allow_keyring : bool = False ,
232
237
) -> AuthInfo :
233
238
"""Find and return credentials for the specified URL."""
@@ -348,7 +353,7 @@ def __call__(self, req: Request) -> Request:
348
353
def _prompt_for_password (
349
354
self , netloc : str
350
355
) -> Tuple [Optional [str ], Optional [str ], bool ]:
351
- username = ask_input (f"User for { netloc } : " )
356
+ username = ask_input (f"User for { netloc } : " ) if self . prompting else None
352
357
if not username :
353
358
return None , None , False
354
359
auth = get_keyring_auth (netloc , username )
@@ -359,7 +364,7 @@ def _prompt_for_password(
359
364
360
365
# Factored out to allow for easy patching in tests
361
366
def _should_save_password_to_keyring (self ) -> bool :
362
- if get_keyring_provider () is None :
367
+ if not self . prompting or get_keyring_provider () is None :
363
368
return False
364
369
return ask ("Save credentials to keyring [y/N]: " , ["y" , "n" ]) == "y"
365
370
@@ -369,19 +374,22 @@ def handle_401(self, resp: Response, **kwargs: Any) -> Response:
369
374
if resp .status_code != 401 :
370
375
return resp
371
376
377
+ username , password = None , None
378
+
379
+ # Query the keyring for credentials:
380
+ if self .use_keyring :
381
+ username , password = self ._get_new_credentials (
382
+ resp .url ,
383
+ allow_netrc = False ,
384
+ allow_keyring = True ,
385
+ )
386
+
372
387
# We are not able to prompt the user so simply return the response
373
- if not self .prompting :
388
+ if not self .prompting and not username and not password :
374
389
return resp
375
390
376
391
parsed = urllib .parse .urlparse (resp .url )
377
392
378
- # Query the keyring for credentials:
379
- username , password = self ._get_new_credentials (
380
- resp .url ,
381
- allow_netrc = False ,
382
- allow_keyring = True ,
383
- )
384
-
385
393
# Prompt the user for a new username and password
386
394
save = False
387
395
if not username and not password :
0 commit comments