Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make OAuth2 session picklable #444

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions requests_oauthlib/oauth2_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -97,6 +93,11 @@ def __init__(
"protected_request": set(),
}

def auth(self, r):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the same have been done with the lambda function?

# 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:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_oauth2_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from base64 import b64encode
from copy import deepcopy
from unittest import TestCase
import pickle

try:
import mock
Expand Down Expand Up @@ -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.
Expand Down