Skip to content

Commit a351306

Browse files
committed
feat(api-gateway): add user ID header handling in proxy and user info injection
1 parent 4fa42d4 commit a351306

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

main.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,17 @@ func newProxy(targetURL, stripPrefix string) (*httputil.ReverseProxy, error) {
8686
proxy.Director = func(req *http.Request) {
8787
// keep user headers
8888
sub := req.Header.Get("X-User-Subject")
89+
userId := req.Header.Get("X-User-Id")
8990
roles := req.Header.Get("X-User-Roles")
9091

9192
orig(req)
9293
req.Host = target.Host
9394
if sub != "" {
9495
req.Header.Set("X-User-Subject", sub)
9596
}
97+
if userId != "" {
98+
req.Header.Set("X-User-Id", userId)
99+
}
96100
if roles != "" {
97101
req.Header.Set("X-User-Roles", roles)
98102
}
@@ -152,7 +156,10 @@ func injectUserInfo(next http.Handler) http.Handler {
152156
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
153157
if claims, ok := r.Context().Value(userClaimsKey).(jwt.MapClaims); ok {
154158
if sub, exists := claims["sub"]; exists {
155-
r.Header.Set("X-User-Subject", fmt.Sprintf("%v", sub))
159+
userIdStr := fmt.Sprintf("%v", sub)
160+
// Set both headers for compatibility with different services
161+
r.Header.Set("X-User-Subject", userIdStr)
162+
r.Header.Set("X-User-Id", userIdStr)
156163
}
157164
if roles, exists := claims["roles"]; exists {
158165
if rs, ok := roles.([]interface{}); ok {
@@ -163,7 +170,7 @@ func injectUserInfo(next http.Handler) http.Handler {
163170
r.Header.Set("X-User-Roles", strings.Join(parts, ","))
164171
}
165172
}
166-
logger.Info("injecting user info headers", "sub", r.Header.Get("X-User-Subject"))
173+
logger.Info("injecting user info headers", "sub", r.Header.Get("X-User-Subject"), "user-id", r.Header.Get("X-User-Id"))
167174
}
168175
next.ServeHTTP(w, r)
169176
})

0 commit comments

Comments
 (0)