Skip to content

Commit 426ab6d

Browse files
authored
Merge pull request #64 from zalando/mask-access-token-from-logs
mask access token from logs
2 parents 620018f + b896871 commit 426ab6d

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

ginoauth2.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ package ginoauth2
5555
import (
5656
"encoding/json"
5757
"errors"
58+
"fmt"
5859
"io/ioutil"
5960
"net/http"
6061
"net/url"
62+
"regexp"
6163
"strings"
6264
"time"
6365

@@ -92,6 +94,34 @@ type Options struct {
9294
AccessTokenInHeader bool
9395
}
9496

97+
var accessTokenMask = regexp.MustCompile("[?&]access_token=[^&]+")
98+
99+
func maskAccessToken(a interface{}) string {
100+
s := fmt.Sprint(a)
101+
s = accessTokenMask.ReplaceAllString(s, "<MASK>")
102+
return s
103+
}
104+
105+
func logf(l func(string, ...interface{}), f string, args ...interface{}) {
106+
for i := range args {
107+
args[i] = maskAccessToken(args[i])
108+
}
109+
110+
l(f, args...)
111+
}
112+
113+
func errorf(f string, args ...interface{}) {
114+
logf(glog.Errorf, f, args...)
115+
}
116+
117+
func infof(f string, args ...interface{}) {
118+
logf(glog.Infof, f, args...)
119+
}
120+
121+
func infofv2(f string, args ...interface{}) {
122+
logf(glog.V(2).Infof, f, args...)
123+
}
124+
95125
func extractToken(r *http.Request) (*oauth2.Token, error) {
96126
hdr := r.Header.Get("Authorization")
97127
if hdr == "" {
@@ -179,20 +209,20 @@ func ParseTokenContainer(t *oauth2.Token, data map[string]interface{}) (*TokenCo
179209
func getTokenContainerForToken(o Options, token *oauth2.Token) (*TokenContainer, error) {
180210
body, err := requestAuthInfo(o, token)
181211
if err != nil {
182-
glog.Errorf("[Gin-OAuth] RequestAuthInfo failed caused by: %s", err)
212+
errorf("[Gin-OAuth] RequestAuthInfo failed caused by: %s", err)
183213
return nil, err
184214
}
185215
// extract AuthInfo
186216
var data map[string]interface{}
187217
err = json.Unmarshal(body, &data)
188218
if err != nil {
189-
glog.Errorf("[Gin-OAuth] JSON.Unmarshal failed caused by: %s", err)
219+
errorf("[Gin-OAuth] JSON.Unmarshal failed caused by: %s", err)
190220
return nil, err
191221
}
192222
if _, ok := data["error_description"]; ok {
193223
var s string
194224
s = data["error_description"].(string)
195-
glog.Errorf("[Gin-OAuth] RequestAuthInfo returned an error: %s", s)
225+
errorf("[Gin-OAuth] RequestAuthInfo returned an error: %s", s)
196226
return nil, errors.New(s)
197227
}
198228
return ParseTokenContainer(token, data)
@@ -208,16 +238,16 @@ func getTokenContainer(o Options, ctx *gin.Context) (*TokenContainer, bool) {
208238
var err error
209239

210240
if oauthToken, err = extractToken(ctx.Request); err != nil {
211-
glog.Errorf("[Gin-OAuth] Can not extract oauth2.Token, caused by: %s", err)
241+
errorf("[Gin-OAuth] Can not extract oauth2.Token, caused by: %s", err)
212242
return nil, false
213243
}
214244
if !oauthToken.Valid() {
215-
glog.Infof("[Gin-OAuth] Invalid Token - nil or expired")
245+
infof("[Gin-OAuth] Invalid Token - nil or expired")
216246
return nil, false
217247
}
218248

219249
if tc, err = getTokenContainerForToken(o, oauthToken); err != nil {
220-
glog.Errorf("[Gin-OAuth] Can not extract TokenContainer, caused by: %s", err)
250+
errorf("[Gin-OAuth] Can not extract TokenContainer, caused by: %s", err)
221251
return nil, false
222252
}
223253

@@ -322,16 +352,16 @@ func AuthChainOptions(o Options, accessCheckFunctions ...AccessCheckFunction) gi
322352
select {
323353
case ok := <-varianceControl:
324354
if !ok {
325-
glog.V(2).Infof("[Gin-OAuth] %12v %s access not allowed", time.Since(t), ctx.Request.URL.Path)
355+
infofv2("[Gin-OAuth] %12v %s access not allowed", time.Since(t), ctx.Request.URL.Path)
326356
return
327357
}
328358
case <-time.After(VarianceTimer):
329359
ctx.AbortWithError(http.StatusGatewayTimeout, errors.New("Authorization check overtime"))
330-
glog.V(2).Infof("[Gin-OAuth] %12v %s overtime", time.Since(t), ctx.Request.URL.Path)
360+
infofv2("[Gin-OAuth] %12v %s overtime", time.Since(t), ctx.Request.URL.Path)
331361
return
332362
}
333363

334-
glog.V(2).Infof("[Gin-OAuth] %12v %s access allowed", time.Since(t), ctx.Request.URL.Path)
364+
infofv2("[Gin-OAuth] %12v %s access allowed", time.Since(t), ctx.Request.URL.Path)
335365
}
336366
}
337367

@@ -368,7 +398,7 @@ func RequestLogger(keys []string, contentKey string) gin.HandlerFunc {
368398
values = append(values, val.(string))
369399
}
370400
}
371-
glog.Infof("[Gin-OAuth] Request: %+v for %s", data, strings.Join(values, "-"))
401+
infof("[Gin-OAuth] Request: %+v for %s", data, strings.Join(values, "-"))
372402
}
373403
}
374404
}

0 commit comments

Comments
 (0)