feat: add activity logs on user actions with authentication, resource… (#6748)

* feat: add activity logs on user actions with authentication, resourceAPI and sessionAPI

* feat: add activity logs on user actions with authentication, resourceAPI and sessionAPI

* feat: add activity logs on user actions with authentication, resourceAPI and sessionAPI

* feat: add activity logs on user actions with authentication, resourceAPI and sessionAPI

* feat: add activity logs on user actions with authentication, resourceAPI and sessionAPI

* fix: add unit tests to info package for context changes

* fix: add activity_interceptor.go suggestion

Co-authored-by: Tim Möhlmann <tim+github@zitadel.com>

* fix: refactoring and fixes through PR review

* fix: add auth service to lists of resourceAPIs

---------

Co-authored-by: Tim Möhlmann <tim+github@zitadel.com>
Co-authored-by: Fabi <fabienne@zitadel.com>
This commit is contained in:
Stefan Benz
2023-10-25 14:09:15 +02:00
committed by GitHub
parent 385a55bd21
commit 48ae5d58ac
13 changed files with 496 additions and 1 deletions

View File

@@ -0,0 +1,32 @@
package middleware
import (
"net/http"
"strings"
"github.com/zitadel/zitadel/internal/api/info"
)
func ActivityHandler(handlerPrefixes []string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
activityInfo := info.ActivityInfoFromContext(ctx)
hasPrefix := false
// only add path to context if handler is called
for _, prefix := range handlerPrefixes {
if strings.HasPrefix(r.URL.Path, prefix) {
activityInfo.SetPath(r.URL.Path)
hasPrefix = true
break
}
}
// last call is with grpc method as path
if !hasPrefix {
activityInfo.SetMethod(r.URL.Path)
}
ctx = activityInfo.SetRequestMethod(r.Method).IntoContext(ctx)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}