|
3 | 3 | import logging
|
4 | 4 | import logging.config
|
5 | 5 | import time
|
6 |
| -from functools import lru_cache |
7 | 6 |
|
8 | 7 | import jwt
|
9 | 8 | import requests
|
@@ -91,40 +90,39 @@ def set_token(self, token_secret):
|
91 | 90 | self.token = token_secret
|
92 | 91 |
|
93 | 92 | @staticmethod
|
94 |
| - def sha1_hash(value): |
95 |
| - hash_object = hashlib.sha1(value.encode("utf-8")) |
96 |
| - return "sha1:" + hash_object.hexdigest() |
| 93 | + def sha_hash(value): |
| 94 | + hash_object = hashlib.sha256(value.encode("utf-8")) |
| 95 | + return "sha256:" + hash_object.hexdigest() |
97 | 96 |
|
98 | 97 | @staticmethod
|
99 |
| - def get_ttl_hash(seconds=600): |
100 |
| - return round(time.time() / seconds) |
101 |
| - |
102 |
| - @lru_cache(maxsize=128) |
103 |
| - def decode_token(token, ttl_hash=None): |
| 98 | + def decode_token(auth_token): |
104 | 99 | return jwt.decode(
|
105 |
| - token, |
| 100 | + auth_token.encode(), |
106 | 101 | options={"verify_signature": False, "verify_exp": False},
|
107 | 102 | )
|
108 | 103 |
|
109 | 104 | def clean_pii(self, payload):
|
110 | 105 | oauth = False
|
111 | 106 | auth_token = None
|
112 | 107 |
|
113 |
| - for k, v in payload["req"].get("headers", {}).items(): |
114 |
| - if k.lower() == "authorization" and "bearer " in v.lower(): |
| 108 | + if auth_header := request.headers.get("Authorization", request.headers.get("authorization")): |
| 109 | + |
| 110 | + if "bearer " in auth_header.lower(): |
115 | 111 | oauth = True
|
116 |
| - auth_token = v.split(" ")[1] if " " in v else None |
| 112 | + auth_token = auth_header.split(" ")[1] if " " in auth_header else None |
| 113 | + |
| 114 | + for k, v in payload["request"].get("headers", {}).items(): |
117 | 115 | if k.lower() in self.scrub_headers:
|
118 |
| - payload["req"]["headers"][k] = "{SANITIZED_HEADER:" + self.sha1_hash(v) + "}" |
| 116 | + payload["request"]["headers"][k] = ["{SANITIZED_HEADER:" + self.sha_hash(item) + "}" for item in v] |
119 | 117 |
|
120 |
| - for k, v in payload["res"].get("headers", {}).items(): |
| 118 | + for k, v in payload["response"].get("headers", {}).items(): |
121 | 119 | if k.lower() in self.scrub_headers:
|
122 |
| - payload["res"]["headers"][k] = "{SANITIZED_HEADER:" + self.sha1_hash(v) + "}" |
123 | 120 |
|
| 121 | + payload["response"]["headers"][k] = ["{SANITIZED_HEADER:" + self.sha_hash(item) + "}" for item in v] |
124 | 122 | if auth_token not in [None, ""] and oauth and self.enrich_oauth:
|
125 | 123 | try:
|
126 |
| - jwt_decoded = self.decode_token(auth_token, ttl_hash=self.get_ttl_hash()) |
127 |
| - payload["oauth"] = {"sub": jwt_decoded["sub"]} |
| 124 | + jwt_decoded = self.decode_token(auth_token) |
| 125 | + payload["oauth"] = {"subject": jwt_decoded["sub"]} |
128 | 126 | except jwt.exceptions.DecodeError:
|
129 | 127 | pass
|
130 | 128 | return payload
|
|
0 commit comments