feat: integrate auth
This commit is contained in:
parent
4a48563ac7
commit
3e1d8439f0
2 changed files with 35 additions and 5 deletions
|
@ -2,6 +2,7 @@ package transport
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
sessionv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/session/v1"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/status"
|
"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) {
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
||||||
reqWithToken, ok := req.(ReqWithToken)
|
reqWithToken, ok := req.(ReqWithToken)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, status.Errorf(codes.Unknown, "") // FIXME
|
return nil, status.Errorf(codes.Unauthenticated, "") // FIXME
|
||||||
}
|
}
|
||||||
|
|
||||||
token := reqWithToken.GetToken()
|
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)
|
return handler(ctx, req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package transport
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"git.sch9.ru/new_gate/ms-tester/internal/models"
|
"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"
|
testerv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/tester/v1"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"google.golang.org/protobuf/types/known/timestamppb"
|
"google.golang.org/protobuf/types/known/timestamppb"
|
||||||
|
@ -18,22 +19,39 @@ type ProblemService interface {
|
||||||
DeleteProblem(ctx context.Context, id int32) error
|
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 {
|
type TesterServer struct {
|
||||||
testerv1.UnimplementedTesterServiceServer
|
testerv1.UnimplementedTesterServiceServer
|
||||||
problemService ProblemService
|
problemService ProblemService
|
||||||
|
|
||||||
gRPCServer *grpc.Server
|
sessionClient SessionClient
|
||||||
|
|
||||||
|
grpcServer *grpc.Server
|
||||||
logger *zap.Logger
|
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{
|
server := &TesterServer{
|
||||||
problemService: problemService,
|
problemService: problemService,
|
||||||
gRPCServer: gRPCServer,
|
sessionClient: sessionServer,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
}
|
}
|
||||||
|
|
||||||
testerv1.RegisterTesterServiceServer(gRPCServer, server)
|
grpcServer := grpc.NewServer(
|
||||||
|
grpc.UnaryInterceptor(server.AuthInterceptor()),
|
||||||
|
)
|
||||||
|
|
||||||
|
testerv1.RegisterTesterServiceServer(grpcServer, server)
|
||||||
|
|
||||||
return server
|
return server
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue