'When being redirected we may want to strip authentication from the
request to avoid leaking credentials. This method intelligently removes
and reapplies authentication where possible to avoid credential loss.'

def rebuild_auth(self, prepared_request, response):

    headers = prepared_request.headers
    url = prepared_request.url
    if ('Authorization' in headers):
        original_parsed = urlparse(response.request.url)
        redirect_parsed = urlparse(url)
        if (original_parsed.hostname != redirect_parsed.hostname):
            del headers['Authorization']
    new_auth = (get_netrc_auth(url) if self.trust_env else None)
    if (new_auth is not None):
        prepared_request.prepare_auth(new_auth)
    return
