diff options
author | Jonathan Huot <JonathanHuot@users.noreply.github.com> | 2019-04-24 10:13:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-24 10:13:51 +0200 |
commit | 30321dd3c0ca784d3508a1970cf90d9f76835c79 (patch) | |
tree | c4dfbee381ba116ef2ecc72cec3fc420d31d776c | |
parent | 81a295de0d00abb37699b6ce6c75737a78b12458 (diff) | |
parent | 924a58fa1a9cc6eb2175ac038ca409708951915b (diff) | |
download | oauthlib-30321dd3c0ca784d3508a1970cf90d9f76835c79.tar.gz |
Fix issue #665: Add method to get / set debug flag (#668)Merge pull request #668 from Abhishek8394/issue-665
Fix issue #665: Add method to get / set debug flag
-rw-r--r-- | docs/error_reporting.rst | 6 | ||||
-rw-r--r-- | docs/oauth1/server.rst | 2 | ||||
-rw-r--r-- | docs/oauth2/server.rst | 3 | ||||
-rw-r--r-- | oauthlib/__init__.py | 17 | ||||
-rw-r--r-- | oauthlib/common.py | 3 | ||||
-rw-r--r-- | tests/__init__.py | 3 | ||||
-rw-r--r-- | tests/test_common.py | 16 |
7 files changed, 49 insertions, 1 deletions
diff --git a/docs/error_reporting.rst b/docs/error_reporting.rst index 705f447..a80287b 100644 --- a/docs/error_reporting.rst +++ b/docs/error_reporting.rst @@ -10,16 +10,20 @@ case where that is not true please let us know! When reporting bugs, especially when they are hard or impossible to reproduce, it is useful to include logging output. You can enable logging for all -oauthlib modules by adding a logger to the `oauthlib` namespace. +oauthlib modules by adding a logger to the `oauthlib` namespace. You might also +want to enable debugging mode to include request data in output. .. code-block:: python import logging + import oauthlib import sys + oauthlib.set_debug(True) log = logging.getLogger('oauthlib') log.addHandler(logging.StreamHandler(sys.stdout)) log.setLevel(logging.DEBUG) + If you are using a library that builds upon OAuthLib please also enable the logging for their modules, e.g. for `requests-oauthlib` diff --git a/docs/oauth1/server.rst b/docs/oauth1/server.rst index db469d2..2f30c65 100644 --- a/docs/oauth1/server.rst +++ b/docs/oauth1/server.rst @@ -441,7 +441,9 @@ Drop a line in our `Gitter OAuthLib community`_ or open a `GitHub issue`_ =) If you run into issues it can be helpful to enable debug logging:: import logging + import oauthlib import sys + oauthlib.set_debug(True) log = logging.getLogger('oauthlib') log.addHandler(logging.StreamHandler(sys.stdout)) log.setLevel(logging.DEBUG) diff --git a/docs/oauth2/server.rst b/docs/oauth2/server.rst index dad0aae..d9846c5 100644 --- a/docs/oauth2/server.rst +++ b/docs/oauth2/server.rst @@ -524,7 +524,10 @@ If you run into issues it can be helpful to enable debug logging. .. code-block:: python import logging + import oauthlib import sys + + oauthlib.set_debug(True) log = logging.getLogger('oauthlib') log.addHandler(logging.StreamHandler(sys.stdout)) log.setLevel(logging.DEBUG) diff --git a/oauthlib/__init__.py b/oauthlib/__init__.py index 8eb82a6..0e413bc 100644 --- a/oauthlib/__init__.py +++ b/oauthlib/__init__.py @@ -15,3 +15,20 @@ __author__ = 'The OAuthlib Community' __version__ = '3.0.2-dev' logging.getLogger('oauthlib').addHandler(NullHandler()) + +_DEBUG = False + +def set_debug(debug_val): + """Set value of debug flag + + :param debug_val: Value to set. Must be a bool value. + """ + global _DEBUG + _DEBUG = debug_val + +def get_debug(): + """Get debug mode value. + + :return: `True` if debug mode is on, `False` otherwise + """ + return _DEBUG diff --git a/oauthlib/common.py b/oauthlib/common.py index 96de1f1..5aeb015 100644 --- a/oauthlib/common.py +++ b/oauthlib/common.py @@ -14,6 +14,7 @@ import logging import re import sys import time +from . import get_debug try: from secrets import randbits @@ -435,6 +436,8 @@ class Request(object): raise AttributeError(name) def __repr__(self): + if not get_debug(): + return "<oauthlib.Request SANITIZED>" body = self.body headers = self.headers.copy() if body: diff --git a/tests/__init__.py b/tests/__init__.py index e69de29..f33236b 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1,3 @@ +import oauthlib + +oauthlib.set_debug(True) diff --git a/tests/test_common.py b/tests/test_common.py index 20d9f5b..ae2531b 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -1,8 +1,10 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, unicode_literals +import os import sys +import oauthlib from oauthlib.common import (CaseInsensitiveDict, Request, add_params_to_uri, extract_params, generate_client_id, generate_nonce, generate_timestamp, @@ -214,6 +216,20 @@ class RequestTest(TestCase): self.assertEqual(r.headers['token'], 'foobar') self.assertEqual(r.token, 'banana') + def test_sanitized_request_non_debug_mode(self): + """make sure requests are sanitized when in non debug mode. + For the debug mode, the other tests checking sanitization should prove + that debug mode is working. + """ + try: + oauthlib.set_debug(False) + r = Request(URI, headers={'token': 'foobar'}, body='token=banana') + self.assertNotIn('token', repr(r)) + self.assertIn('SANITIZED', repr(r)) + finally: + # set flag back for other tests + oauthlib.set_debug(True) + class CaseInsensitiveDictTest(TestCase): |