Skip to content

Commit

Permalink
Merge pull request #355 from lepture/hide-password
Browse files Browse the repository at this point in the history
Hide password in logs
  • Loading branch information
lepture committed Jul 3, 2015
2 parents 6745435 + f88408d commit ff71270
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion oauthlib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
CLIENT_ID_CHARACTER_SET = (r' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN'
'OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}')

PASSWORD_PATTERN = re.compile(r'password=[^&]+')

always_safe = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'abcdefghijklmnopqrstuvwxyz'
Expand Down Expand Up @@ -408,8 +409,11 @@ def __getattr__(self, name):
raise AttributeError(name)

def __repr__(self):
body = self.body
if body and 'password=' in body:
body = PASSWORD_PATTERN.sub('password=***', body)
return '<oauthlib.Request url="%s", http_method="%s", headers="%s", body="%s">' % (
self.uri, self.http_method, self.headers, self.body)
self.uri, self.http_method, self.headers, body)

@property
def uri_query(self):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ def test_getattr_raise_attribute_error(self):
with self.assertRaises(AttributeError):
getattr(r, 'does_not_exist')

def test_password_body(self):
payload = 'username=foo&password=bar'
r = Request(URI, body=payload)
self.assertNotIn('bar', repr(r))

payload = 'password=bar&username=foo'
r = Request(URI, body=payload)
self.assertNotIn('bar', repr(r))


class CaseInsensitiveDictTest(TestCase):

Expand Down

0 comments on commit ff71270

Please sign in to comment.