Skip to content
Snippets Groups Projects
auth_interceptor.go 1.24 KiB
Newer Older
  • Learn to ignore specific revisions
  • package server
    
    import (
    	"context"
    
    
    	apb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/rbac"
    	"code.fbi.h-da.de/danet/gosdn/controller/rbac"
    
    	log "github.com/sirupsen/logrus"
    	"google.golang.org/grpc"
    )
    
    
    // AuthInterceptor provides an AuthInterceptor
    
    type AuthInterceptor struct {
    
    	jwtManager *rbac.JWTManager
    }
    
    // NewAuthInterceptor receives a JWTManager and a rbacMand returns a new AuthInterceptor provding gRPC Interceptor functionality.
    func NewAuthInterceptor(jwtManager *rbac.JWTManager) *AuthInterceptor {
    	return &AuthInterceptor{
    		jwtManager: jwtManager,
    	}
    
    // Unary provides middleware functionality
    
    func (auth AuthInterceptor) Unary() grpc.UnaryServerInterceptor {
    
    	return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
    
    		// TODO: Implement proper auth logic here
    
    		if _, ok := req.(*apb.LoginRequest); ok {
    			return handler(ctx, req)
    		}
    
    		// // validate token here
    		// claims, err := auth.jwtManager.VerifyToken("") // add token from context here!
    		// if err != nil {
    		// 	return nil, status.Errorf(codes.PermissionDenied, "%v", err)
    		// }
    		// // use claims for authorization
    		// log.Info("User: " + claims.Username)
    		log.Info("Interceptor called")
    
    
    		return handler(ctx, req)
    	}
    }