@@ -2,6 +2,8 @@ package commons
22
33import (
44 "akapurgo/api/v1alpha1"
5+ "encoding/base64"
6+ "encoding/json"
57 "github.com/gofiber/fiber/v2"
68 "github.com/valyala/fasthttp"
79 "regexp"
@@ -74,9 +76,13 @@ func replaceRequestHeaderTags(req *fasthttp.Request, textToProcess string) (resu
7476}
7577
7678// GetRequestLogFields returns the fields attached to a log message for the given HTTP request
77- func GetRequestLogFields (req * fasthttp.Request , configurationFields []string ) []interface {} {
79+ func GetRequestLogFields (req * fasthttp.Request , configurationFields []string , ctx v1alpha1. Context ) []interface {} {
7880 var logFields []interface {}
7981
82+ if ctx .Config .Logs .JwtUser .Enabled {
83+ logFields = addJwtUser (ctx , logFields , req )
84+ }
85+
8086 for _ , field := range configurationFields {
8187
8288 result := replaceRequestTags (req , field )
@@ -166,6 +172,43 @@ func GetResponseLogFields(resp *fasthttp.Response, configurationFields []string,
166172 return logFields
167173}
168174
175+ // addJwtUser
176+ func addJwtUser (ctx v1alpha1.Context , logFields []interface {}, req * fasthttp.Request ) []interface {} {
177+ cookie := string (req .Header .Peek (ctx .Config .Logs .JwtUser .Header ))
178+ if cookie != "" {
179+ jwtPayload := strings .Split (cookie , "." )
180+ if len (jwtPayload ) != 3 {
181+ ctx .Logger .Errorf ("Invalid JWT format: expected 3 parts but got %d\n " , len (jwtPayload ))
182+ return logFields
183+ }
184+
185+ jwtPart := strings .TrimSpace (jwtPayload [1 ])
186+ jwtPart = strings .ReplaceAll (jwtPart , "\n " , "" )
187+ jwtPart = strings .ReplaceAll (jwtPart , "\r " , "" )
188+ jwtPart = strings .ReplaceAll (jwtPart , " " , "" )
189+
190+ jwtDecoded , err := base64 .RawURLEncoding .DecodeString (jwtPart )
191+ if err != nil {
192+ ctx .Logger .Errorf ("Failed to decode JWT payload: %v\n " , err )
193+ return logFields
194+ }
195+
196+ var payload map [string ]interface {}
197+ if err := json .Unmarshal (jwtDecoded , & payload ); err != nil {
198+ ctx .Logger .Errorf ("Failed to parse JWT payload: %v\n " , err )
199+ return logFields
200+ }
201+
202+ email , ok := payload ["email" ].(string )
203+ if ok {
204+ logFields = append (logFields , "jwt_user" , email )
205+ return logFields
206+ }
207+ }
208+
209+ return logFields
210+ }
211+
169212// LogRequest logs the request and response of a given HTTP request
170213func LogRequest (ctx v1alpha1.Context ) fiber.Handler {
171214 return func (c * fiber.Ctx ) error {
@@ -181,7 +224,7 @@ func LogRequest(ctx v1alpha1.Context) fiber.Handler {
181224 // Log the request
182225 if ctx .Config .Logs .ShowAccessLogs {
183226 logFieldsReq := GetResponseLogFields (c .Response (), ctx .Config .Logs .AccessLogsFields , duration )
184- logFieldsResp := GetRequestLogFields (c .Request (), ctx .Config .Logs .AccessLogsFields )
227+ logFieldsResp := GetRequestLogFields (c .Request (), ctx .Config .Logs .AccessLogsFields , ctx )
185228 logFields := append (logFieldsReq , logFieldsResp ... )
186229 ctx .Logger .Infow ("request" , logFields ... )
187230 }
0 commit comments