Skip to content

Commit e859dbd

Browse files
ryanhiebertsingingwolfboy
authored andcommitted
Avoid requests automatic auth (#279)
1 parent 4fb0db5 commit e859dbd

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

HISTORY.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ History
44
UNRELEASED
55
++++++++++
66

7-
nothing yet
7+
- Avoid automatic netrc authentication for OAuth2Session.
88

99
v1.1.0 (9 January 2019)
1010
+++++++++++++++++++++++

requests_oauthlib/oauth2_session.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ def __init__(self, client_id=None, client=None, auto_refresh_url=None,
7474
self.auto_refresh_kwargs = auto_refresh_kwargs or {}
7575
self.token_updater = token_updater
7676

77+
# Ensure that requests doesn't do any automatic auth. See #278.
78+
# The default behavior can be re-enabled by setting auth to None.
79+
self.auth = lambda r: r
80+
7781
# Allow customizations for non compliant providers through various
7882
# hooks to adjust requests and responses.
7983
self.compliance_hook = {

tests/test_oauth2_session.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from __future__ import unicode_literals
22
import json
33
import time
4+
import tempfile
5+
import shutil
6+
import os
47
from base64 import b64encode
58
from copy import deepcopy
69
from unittest import TestCase
@@ -242,3 +245,36 @@ def fake_send(r, **kwargs):
242245
self.assertIs(sess.authorized, False)
243246
sess.fetch_token(url)
244247
self.assertIs(sess.authorized, True)
248+
249+
250+
class OAuth2SessionNetrcTest(OAuth2SessionTest):
251+
"""Ensure that there is no magic auth handling.
252+
253+
By default, requests sessions have magic handling of netrc files,
254+
which is undesirable for this library because it will take
255+
precedence over manually set authentication headers.
256+
"""
257+
258+
def setUp(self):
259+
# Set up a temporary home directory
260+
self.homedir = tempfile.mkdtemp()
261+
self.prehome = os.environ.get('HOME', None)
262+
os.environ['HOME'] = self.homedir
263+
264+
# Write a .netrc file that will cause problems
265+
netrc_loc = os.path.expanduser('~/.netrc')
266+
with open(netrc_loc, 'w') as f:
267+
f.write(
268+
'machine i.b\n'
269+
' password abc123\n'
270+
' login [email protected]\n'
271+
)
272+
273+
super(OAuth2SessionNetrcTest, self).setUp()
274+
275+
def tearDown(self):
276+
super(OAuth2SessionNetrcTest, self).tearDown()
277+
278+
if self.prehome is not None:
279+
os.environ['HOME'] = self.prehome
280+
shutil.rmtree(self.homedir)

0 commit comments

Comments
 (0)