github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/cd-service/pkg/interceptors/interceptors.go (about)

     1  /*This file is part of kuberpult.
     2  
     3  Kuberpult is free software: you can redistribute it and/or modify
     4  it under the terms of the Expat(MIT) License as published by
     5  the Free Software Foundation.
     6  
     7  Kuberpult is distributed in the hope that it will be useful,
     8  but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10  MIT License for more details.
    11  
    12  You should have received a copy of the MIT License
    13  along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
    14  
    15  Copyright 2023 freiheit.com*/
    16  
    17  package interceptors
    18  
    19  import (
    20  	"context"
    21  	"strings"
    22  
    23  	"github.com/freiheit-com/kuberpult/pkg/auth"
    24  	"google.golang.org/grpc"
    25  )
    26  
    27  // UnaryUserContextInterceptor assumes that there is a user in the context
    28  // if there is no user, it will return an auth error.
    29  // if there is a user, it will be written to the context.
    30  func UnaryUserContextInterceptor(ctx context.Context,
    31  	req interface{},
    32  	info *grpc.UnaryServerInfo,
    33  	handler grpc.UnaryHandler,
    34  	reader auth.GrpcContextReader) (interface{}, error) {
    35  
    36  	if strings.HasPrefix(info.FullMethod, "/repository.RepoServerService/") {
    37  		return handler(ctx, req)
    38  	}
    39  	user, err := reader.ReadUserFromGrpcContext(ctx)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	ctx = auth.WriteUserToContext(ctx, *user)
    44  
    45  	h, err := handler(ctx, req)
    46  	return h, err
    47  }