@@ -9,12 +9,15 @@ import (
99 "net/http"
1010 "net/http/httputil"
1111 "os"
12+ "strings"
1213)
1314
1415var (
1516 // Verbose determines if debugging output is displayed to the user
1617 Verbose bool
1718 output io.Writer = os .Stderr
19+ // UnmaskAPIKey determines if the API key should de displayed during a dump
20+ UnmaskAPIKey bool
1821)
1922
2023// Println conditionally outputs a message to Stderr
@@ -41,6 +44,14 @@ func DumpRequest(req *http.Request) {
4144 body := io .TeeReader (req .Body , & bodyCopy )
4245 req .Body = ioutil .NopCloser (body )
4346
47+ temp := req .Header .Get ("Authorization" )
48+
49+ if ! UnmaskAPIKey {
50+ if token := strings .Split (temp , " " )[1 ]; token != "" {
51+ req .Header .Set ("Authorization" , "Bearer " + Redact (token ))
52+ }
53+ }
54+
4455 dump , err := httputil .DumpRequest (req , req .ContentLength > 0 )
4556 if err != nil {
4657 log .Fatal (err )
@@ -51,6 +62,7 @@ func DumpRequest(req *http.Request) {
5162 Println ("========================= END DumpRequest =========================" )
5263 Println ("" )
5364
65+ req .Header .Set ("Authorization" , temp )
5466 req .Body = ioutil .NopCloser (& bodyCopy )
5567}
5668
@@ -76,3 +88,10 @@ func DumpResponse(res *http.Response) {
7688
7789 res .Body = ioutil .NopCloser (body )
7890}
91+
92+ // Redact masks the given token by replacing part of the string with *
93+ func Redact (token string ) string {
94+ str := token [4 : len (token )- 3 ]
95+ redaction := strings .Repeat ("*" , len (str ))
96+ return string (token [:4 ]) + redaction + string (token [len (token )- 3 :])
97+ }
0 commit comments