package client import ( "net/http" "edge-infra.dev/pkg/sds/emergencyaccess/eaconst" "edge-infra.dev/pkg/sds/emergencyaccess/middleware" "edge-infra.dev/pkg/sds/emergencyaccess/types" ) // HTransport is an [http.RoundTripper] which extracts users authentication // details from the outgoing requests context, and adds the values into the // outgoing requests headers. Additionally extract the correlation ID from the // requests context and adds it to the outgoing requests headers type HTransport struct { T http.RoundTripper } func (t *HTransport) RoundTrip(req *http.Request) (*http.Response, error) { ctx := req.Context() // TODO: Don't modify the request user, ok := types.UserFromContext(ctx) if ok { addAuthHeaders(req, user) } correlationID := middleware.GetCorrelationID(ctx) if correlationID != "" { req.Header.Set(middleware.CorrelationIDKey, correlationID) } return t.T.RoundTrip(req) } // Modifies the request by adding the appropriate Auth headers. Overwrites any // existing auth headers func addAuthHeaders(req *http.Request, user types.User) { req.Header.Set(eaconst.HeaderAuthKeyUsername, user.Username) req.Header.Set(eaconst.HeaderAuthKeyEmail, user.Email) req.Header.Del(eaconst.HeaderAuthKeyRoles) req.Header.Del(eaconst.HeaderAuthKeyBanners) for _, role := range user.Roles { req.Header.Add(eaconst.HeaderAuthKeyRoles, role) } for _, banner := range user.Banners { req.Header.Add(eaconst.HeaderAuthKeyBanners, banner) } }