feat: integrate auth

This commit is contained in:
Vyacheslav1557 2024-08-16 20:01:08 +05:00
parent 4a48563ac7
commit 3e1d8439f0
2 changed files with 35 additions and 5 deletions

View file

@ -2,6 +2,7 @@ package transport
import (
"context"
sessionv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/session/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
@ -15,11 +16,22 @@ func (s *TesterServer) AuthInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
reqWithToken, ok := req.(ReqWithToken)
if !ok {
return nil, status.Errorf(codes.Unknown, "") // FIXME
return nil, status.Errorf(codes.Unauthenticated, "") // FIXME
}
token := reqWithToken.GetToken()
if token == "" {
return nil, status.Errorf(codes.Unauthenticated, "") // FIXME
}
userId, err := s.sessionClient.Read(ctx, &sessionv1.ReadSessionRequest{Token: token})
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "") // FIXME
}
ctx = context.WithValue(ctx, "user_id", userId)
return handler(ctx, req)
}
}

View file

@ -3,6 +3,7 @@ package transport
import (
"context"
"git.sch9.ru/new_gate/ms-tester/internal/models"
sessionv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/session/v1"
testerv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/tester/v1"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/timestamppb"
@ -18,22 +19,39 @@ type ProblemService interface {
DeleteProblem(ctx context.Context, id int32) error
}
type SessionClient interface {
Read(ctx context.Context,
in *sessionv1.ReadSessionRequest,
opts ...grpc.CallOption,
) (*sessionv1.ReadSessionResponse, error)
}
type TesterServer struct {
testerv1.UnimplementedTesterServiceServer
problemService ProblemService
gRPCServer *grpc.Server
sessionClient SessionClient
grpcServer *grpc.Server
logger *zap.Logger
}
func NewTesterServer(problemService ProblemService, gRPCServer *grpc.Server, logger *zap.Logger) *TesterServer {
func NewTesterServer(
problemService ProblemService,
sessionServer SessionClient,
logger *zap.Logger,
) *TesterServer {
server := &TesterServer{
problemService: problemService,
gRPCServer: gRPCServer,
sessionClient: sessionServer,
logger: logger,
}
testerv1.RegisterTesterServiceServer(gRPCServer, server)
grpcServer := grpc.NewServer(
grpc.UnaryInterceptor(server.AuthInterceptor()),
)
testerv1.RegisterTesterServiceServer(grpcServer, server)
return server
}