From 3c50014f4dafc434953d700b416cdf03e597ee51 Mon Sep 17 00:00:00 2001 From: Brian Cavagnolo Date: Wed, 28 Apr 2021 13:45:55 -0700 Subject: [PATCH] make OAuth2 session picklable --- requests_oauthlib/oauth2_session.py | 9 +++++---- tests/test_oauth2_session.py | 8 ++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/requests_oauthlib/oauth2_session.py b/requests_oauthlib/oauth2_session.py index bfec8e8d..cc82aef1 100644 --- a/requests_oauthlib/oauth2_session.py +++ b/requests_oauthlib/oauth2_session.py @@ -85,10 +85,6 @@ def __init__( self.auto_refresh_kwargs = auto_refresh_kwargs or {} self.token_updater = token_updater - # Ensure that requests doesn't do any automatic auth. See #278. - # The default behavior can be re-enabled by setting auth to None. - self.auth = lambda r: r - # Allow customizations for non compliant providers through various # hooks to adjust requests and responses. self.compliance_hook = { @@ -97,6 +93,11 @@ def __init__( "protected_request": set(), } + def auth(self, r): + # Ensure that requests doesn't do any automatic auth. See #278. + # The default behavior can be re-enabled by setting auth to None. + return r + def new_state(self): """Generates a state string to be used in authorizations.""" try: diff --git a/tests/test_oauth2_session.py b/tests/test_oauth2_session.py index cfc62368..070b1f4f 100644 --- a/tests/test_oauth2_session.py +++ b/tests/test_oauth2_session.py @@ -7,6 +7,7 @@ from base64 import b64encode from copy import deepcopy from unittest import TestCase +import pickle try: import mock @@ -497,6 +498,13 @@ def fake_send(r, **kwargs): sess.fetch_token(url) self.assertTrue(sess.authorized) + def test_pickle_session(self): + url = "https://example.com/token" + + for client in self.clients: + sess = OAuth2Session(client=client) + self.assertTrue(pickle.dumps(sess) is not None) + class OAuth2SessionNetrcTest(OAuth2SessionTest): """Ensure that there is no magic auth handling.