Skip to content
Myr

Move auth rebuild to its own method.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Cory Benfield · committed by Cory Benfield · parent 6d7e8a9 · Browse files · 1 file+26 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+26 added−13 removed

  • other:+26 added−13 removed

1 file+26 added−13 removed

modified:requests/sessions.py+26 added-13 removed
152 unmodified lines
153 extract_cookies_to_jar(prepared_request._cookies, prepared_request, resp.raw)154 prepared_request._cookies.update(self.cookies)155 prepared_request.prepare_cookies(prepared_request._cookies)156157 if 'Authorization' in headers:158 # If we get redirected to a new host, we should strip out any159 # authentication headers.160 original_parsed = urlparse(resp.request.url)161 redirect_parsed = urlparse(url)157 self.rebuild_auth(prepared_request, resp)158163 if (original_parsed.hostname != redirect_parsed.hostname):164 del headers['Authorization']165166 # .netrc might have more auth for us.167 new_auth = get_netrc_auth(url) if self.trust_env else None168 if new_auth is not None:169 prepared_request.prepare_auth(new_auth)170159 resp = self.send(160 prepared_request,161 stream=stream,162 timeout=timeout,
7 unmodified lines
170171 i += 1172 yield resp173174 def rebuild_auth(self, prepared_request, response):175 """176 When being redirected we may want to strip authentication from the177 request to avoid leaking credentials. This method intelligently removes178 and reapplies authentication where possible to avoid credential loss.179 """180 headers = prepared_request.headers181 url = prepared_request.url182183 if 'Authorization' in headers:184 # If we get redirected to a new host, we should strip out any185 # authentication headers.186 original_parsed = urlparse(response.request.url)187 redirect_parsed = urlparse(url)188189 if (original_parsed.hostname != redirect_parsed.hostname):190 del headers['Authorization']191192 # .netrc might have more auth for us on our new host.193 new_auth = get_netrc_auth(url) if self.trust_env else None194 if new_auth is not None:195 prepared_request.prepare_auth(new_auth)196197 return198199200class Session(SessionRedirectMixin):201 """A Requests session.202203 Provides cookie persistence, connection-pooling, and configuration.
381 unmodified lines