Files
weave-scope/vendor/github.com/weaveworks/common/user/grpc.go
2017-05-02 14:42:11 +02:00

52 lines
1.2 KiB
Go

package user
import (
"golang.org/x/net/context"
"google.golang.org/grpc/metadata"
)
// ExtractFromGRPCRequest extracts the user ID from the request metadata and returns
// the user ID and a context with the user ID injected.
func ExtractFromGRPCRequest(ctx context.Context) (string, context.Context, error) {
md, ok := metadata.FromContext(ctx)
if !ok {
return "", ctx, ErrNoOrgID
}
orgIDs, ok := md[lowerOrgIDHeaderName]
if !ok || len(orgIDs) != 1 {
return "", ctx, ErrNoOrgID
}
return orgIDs[0], InjectOrgID(ctx, orgIDs[0]), nil
}
// InjectIntoGRPCRequest injects the orgID from the context into the request metadata.
func InjectIntoGRPCRequest(ctx context.Context) (context.Context, error) {
orgID, err := ExtractOrgID(ctx)
if err != nil {
return ctx, err
}
md, ok := metadata.FromContext(ctx)
if !ok {
md = metadata.New(map[string]string{})
}
newCtx := ctx
if orgIDs, ok := md[lowerOrgIDHeaderName]; ok {
if len(orgIDs) == 1 {
if orgIDs[0] != orgID {
return ctx, ErrDifferentOrgIDPresent
}
} else {
return ctx, ErrTooManyOrgIDs
}
} else {
md = md.Copy()
md[lowerOrgIDHeaderName] = []string{orgID}
newCtx = metadata.NewContext(ctx, md)
}
return newCtx, nil
}