]> git.parisson.com Git - django-social-auth.git/commitdiff
Helper function for 'clean' logging of data.
authorMathijs de Bruin <mathijs@mathijsfietst.nl>
Mon, 19 Sep 2011 12:37:38 +0000 (14:37 +0200)
committerMathijs de Bruin <mathijs@mathijsfietst.nl>
Mon, 19 Sep 2011 12:37:38 +0000 (14:37 +0200)
social_auth/utils.py

index 9d5484829c68d8e9ac82e3c64484a9417ae4e779..9f2775f98c5f5f73491556f13caa5569ac47b1a8 100644 (file)
@@ -4,6 +4,25 @@ from collections import defaultdict
 from social_auth.backends import BACKENDS, OpenIdAuth, BaseOAuth, BaseOAuth2
 
 
+def sanitize_log_data(secret, data=None, leave_characters=4):
+    """
+    Clean private/secret data from log statements and other data.
+
+    Assumes data and secret are strings. Replaces all but the first
+    `leave_characters` of `secret`, as found in `data`, with '*'.
+
+    If no data is given, all but the first `leave_characters` of secret
+    are simply replaced and returned.
+    """
+    replace_secret = (secret[:leave_characters] +
+                      (len(secret) - leave_characters) * '*')
+
+    if data:
+        return data.replace(secret, replace_secret)
+
+    return replace_secret
+
+
 def sanitize_redirect(host, redirect_to):
     """
     Given the hostname and an untrusted URL to redirect to,