github.com/codefly-dev/core@v0.1.107/wool/metadata.go (about)

     1  package wool
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"google.golang.org/grpc/metadata"
     9  )
    10  
    11  // HeaderKey sanitizes the header name to be used in metadata
    12  // Append wool:
    13  // Lower case
    14  // Suppress X-Codefly
    15  func HeaderKey(header string) string {
    16  	header = strings.ToLower(header)
    17  	header = strings.ReplaceAll(header, "-", ".")
    18  	if codeflyHeader, ok := strings.CutPrefix(header, "x.codefly."); ok {
    19  		return fmt.Sprintf("codefly.%s", codeflyHeader)
    20  	}
    21  	return header
    22  }
    23  
    24  func InjectMetadata(ctx context.Context) context.Context {
    25  	md, ok := metadata.FromIncomingContext(ctx)
    26  	if !ok {
    27  		md = metadata.New(map[string]string{})
    28  	}
    29  	for key, values := range md {
    30  		switch ContextKey(key) {
    31  		case UserAuthIDKey:
    32  			ctx = context.WithValue(ctx, UserAuthIDKey, values[0])
    33  		case UserEmailKey:
    34  			ctx = context.WithValue(ctx, UserEmailKey, values[0])
    35  		case UserNameKey:
    36  			ctx = context.WithValue(ctx, UserNameKey, values[0])
    37  		case UserGivenNameKey:
    38  			ctx = context.WithValue(ctx, UserGivenNameKey, values[0])
    39  		}
    40  	}
    41  	return metadata.NewIncomingContext(ctx, md)
    42  }