36
36
keyring = None
37
37
38
38
39
- def get_keyring_auth (url , username ):
40
- # type: (Optional[str], Optional[str]) -> Optional[AuthInfo]
39
+ def get_keyring_auth (url : Optional [str ], username : Optional [str ]) -> Optional [AuthInfo ]:
41
40
"""Return the tuple auth for a given url from keyring."""
42
41
global keyring
43
42
if not url or not keyring :
@@ -71,20 +70,20 @@ def get_keyring_auth(url, username):
71
70
72
71
class MultiDomainBasicAuth (AuthBase ):
73
72
74
- def __init__ (self , prompting = True , index_urls = None ):
75
- # type: (bool, Optional[List[str]]) -> None
73
+ def __init__ (
74
+ self , prompting : bool = True , index_urls : Optional [List [str ]] = None
75
+ ) -> None :
76
76
self .prompting = prompting
77
77
self .index_urls = index_urls
78
- self .passwords = {} # type : Dict[str, AuthInfo]
78
+ self .passwords : Dict [str , AuthInfo ] = {}
79
79
# When the user is prompted to enter credentials and keyring is
80
80
# available, we will offer to save them. If the user accepts,
81
81
# this value is set to the credentials they entered. After the
82
82
# request authenticates, the caller should call
83
83
# ``save_credentials`` to save these.
84
- self ._credentials_to_save = None # type : Optional[Credentials]
84
+ self ._credentials_to_save : Optional [Credentials ] = None
85
85
86
- def _get_index_url (self , url ):
87
- # type: (str) -> Optional[str]
86
+ def _get_index_url (self , url : str ) -> Optional [str ]:
88
87
"""Return the original index URL matching the requested URL.
89
88
90
89
Cached or dynamically generated credentials may work against
@@ -106,9 +105,8 @@ def _get_index_url(self, url):
106
105
return u
107
106
return None
108
107
109
- def _get_new_credentials (self , original_url , allow_netrc = True ,
110
- allow_keyring = False ):
111
- # type: (str, bool, bool) -> AuthInfo
108
+ def _get_new_credentials (self , original_url : str , allow_netrc : bool = True ,
109
+ allow_keyring : bool = False ) -> AuthInfo :
112
110
"""Find and return credentials for the specified URL."""
113
111
# Split the credentials and netloc from the url.
114
112
url , netloc , url_user_password = split_auth_netloc_from_url (
@@ -157,8 +155,9 @@ def _get_new_credentials(self, original_url, allow_netrc=True,
157
155
158
156
return username , password
159
157
160
- def _get_url_and_credentials (self , original_url ):
161
- # type: (str) -> Tuple[str, Optional[str], Optional[str]]
158
+ def _get_url_and_credentials (
159
+ self , original_url : str
160
+ ) -> Tuple [str , Optional [str ], Optional [str ]]:
162
161
"""Return the credentials to use for the provided URL.
163
162
164
163
If allowed, netrc and keyring may be used to obtain the
@@ -197,8 +196,7 @@ def _get_url_and_credentials(self, original_url):
197
196
198
197
return url , username , password
199
198
200
- def __call__ (self , req ):
201
- # type: (Request) -> Request
199
+ def __call__ (self , req : Request ) -> Request :
202
200
# Get credentials for this request
203
201
url , username , password = self ._get_url_and_credentials (req .url )
204
202
@@ -215,8 +213,9 @@ def __call__(self, req):
215
213
return req
216
214
217
215
# Factored out to allow for easy patching in tests
218
- def _prompt_for_password (self , netloc ):
219
- # type: (str) -> Tuple[Optional[str], Optional[str], bool]
216
+ def _prompt_for_password (
217
+ self , netloc : str
218
+ ) -> Tuple [Optional [str ], Optional [str ], bool ]:
220
219
username = ask_input (f"User for { netloc } : " )
221
220
if not username :
222
221
return None , None , False
@@ -227,14 +226,12 @@ def _prompt_for_password(self, netloc):
227
226
return username , password , True
228
227
229
228
# Factored out to allow for easy patching in tests
230
- def _should_save_password_to_keyring (self ):
231
- # type: () -> bool
229
+ def _should_save_password_to_keyring (self ) -> bool :
232
230
if not keyring :
233
231
return False
234
232
return ask ("Save credentials to keyring [y/N]: " , ["y" , "n" ]) == "y"
235
233
236
- def handle_401 (self , resp , ** kwargs ):
237
- # type: (Response, **Any) -> Response
234
+ def handle_401 (self , resp : Response , ** kwargs : Any ) -> Response :
238
235
# We only care about 401 responses, anything else we want to just
239
236
# pass through the actual response
240
237
if resp .status_code != 401 :
@@ -286,16 +283,14 @@ def handle_401(self, resp, **kwargs):
286
283
287
284
return new_resp
288
285
289
- def warn_on_401 (self , resp , ** kwargs ):
290
- # type: (Response, **Any) -> None
286
+ def warn_on_401 (self , resp : Response , ** kwargs : Any ) -> None :
291
287
"""Response callback to warn about incorrect credentials."""
292
288
if resp .status_code == 401 :
293
289
logger .warning (
294
290
'401 Error, Credentials not correct for %s' , resp .request .url ,
295
291
)
296
292
297
- def save_credentials (self , resp , ** kwargs ):
298
- # type: (Response, **Any) -> None
293
+ def save_credentials (self , resp : Response , ** kwargs : Any ) -> None :
299
294
"""Response callback to save credentials on success."""
300
295
assert keyring is not None , "should never reach here without keyring"
301
296
if not keyring :
0 commit comments