ms-tester/internal/transport/interceptors.go

156 lines
4.5 KiB
Go
Raw Normal View History

2024-07-27 07:31:04 +00:00
package transport
2024-08-16 11:05:29 +00:00
import (
"context"
2024-08-20 11:18:23 +00:00
"errors"
"git.sch9.ru/new_gate/ms-tester/internal/lib"
2024-08-25 16:52:00 +00:00
"git.sch9.ru/new_gate/ms-tester/pkg/models"
2024-08-16 17:09:03 +00:00
sessionv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/session/v1"
2024-08-16 11:05:29 +00:00
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
2024-08-20 11:18:23 +00:00
"google.golang.org/grpc/metadata"
2024-08-16 11:05:29 +00:00
"google.golang.org/grpc/status"
)
2024-08-20 11:18:23 +00:00
var defaultUser = &models.User{
UserId: nil,
Role: models.RoleSpectator.AsPointer(),
UpdatedAt: nil,
2024-08-16 11:05:29 +00:00
}
2024-08-22 22:56:03 +00:00
func extractToken(ctx context.Context) string {
2024-08-20 11:18:23 +00:00
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
2024-08-22 22:56:03 +00:00
return ""
2024-08-20 11:18:23 +00:00
}
tokens := md.Get("token")
if len(tokens) == 0 {
2024-08-22 22:56:03 +00:00
return ""
2024-08-20 11:18:23 +00:00
}
2024-08-22 22:56:03 +00:00
return tokens[0]
2024-08-20 11:18:23 +00:00
}
func (s *TesterServer) readSessionAndReadUser(ctx context.Context, token string) (*models.User, error) {
// FIXME: possible bottle neck: should we cache it? (think of it in future)
// FIXME: maybe use single connection instead of multiple requests
userId, err := s.sessionClient.Read(ctx, &sessionv1.ReadSessionRequest{Token: token})
if err != nil {
2024-08-22 22:56:03 +00:00
return nil, err
2024-08-20 11:18:23 +00:00
}
user, err := s.userService.ReadUserById(ctx, userId.GetUserId()) // FIXME: must be cached!
if err != nil {
2024-08-22 22:56:03 +00:00
if errors.Is(err, lib.ErrNotFound) {
user = &models.User{
UserId: lib.AsInt32P(userId.GetUserId()),
Role: models.RoleParticipant.AsPointer(),
}
err = s.userService.CreateUser(ctx, user)
if err != nil {
return nil, err
}
} else {
return nil, err
2024-08-16 11:05:29 +00:00
}
2024-08-20 11:18:23 +00:00
}
2024-08-16 11:05:29 +00:00
2024-08-20 11:18:23 +00:00
return user, nil
}
2024-08-16 11:05:29 +00:00
2024-08-20 11:18:23 +00:00
func insertUser(ctx context.Context, user *models.User) context.Context {
return context.WithValue(ctx, "user", user)
}
func (s *TesterServer) AuthUnaryInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
2024-08-22 22:56:03 +00:00
token := extractToken(ctx)
if token == "" {
2024-08-20 11:18:23 +00:00
return handler(insertUser(ctx, defaultUser), req)
2024-08-16 15:01:08 +00:00
}
2024-08-20 11:18:23 +00:00
user, err := s.readSessionAndReadUser(ctx, token)
2024-08-16 15:01:08 +00:00
if err != nil {
2024-08-20 11:18:23 +00:00
return handler(insertUser(ctx, defaultUser), req)
2024-08-16 15:01:08 +00:00
}
2024-08-20 11:18:23 +00:00
return handler(insertUser(ctx, user), req)
}
}
type ssWrapper struct {
grpc.ServerStream
ctx context.Context
}
func (s *ssWrapper) Context() context.Context {
return s.ctx
}
func (s *TesterServer) AuthStreamInterceptor() grpc.StreamServerInterceptor {
return func(server interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
ctx := ss.Context()
2024-08-22 22:56:03 +00:00
token := extractToken(ctx)
if token == "" {
2024-08-20 11:18:23 +00:00
return handler(server, &ssWrapper{ServerStream: ss, ctx: insertUser(ctx, defaultUser)})
}
user, err := s.readSessionAndReadUser(ctx, token)
if err != nil {
return handler(server, &ssWrapper{ServerStream: ss, ctx: insertUser(ctx, defaultUser)})
}
2024-08-16 15:01:08 +00:00
2024-08-20 11:18:23 +00:00
return handler(server, &ssWrapper{ServerStream: ss, ctx: insertUser(ctx, user)})
2024-08-16 11:05:29 +00:00
}
}
2024-08-22 22:56:03 +00:00
func ToGrpcError(err error) error {
if err == nil {
return nil
}
// should I use map instead?
switch {
case errors.Is(err, lib.ErrValidationFailed):
return status.Error(codes.InvalidArgument, err.Error())
case errors.Is(err, lib.ErrInternal):
return status.Error(codes.Internal, err.Error())
case errors.Is(err, lib.ErrExternal):
return status.Error(codes.Unavailable, err.Error())
case errors.Is(err, lib.ErrNoPermission):
return status.Error(codes.PermissionDenied, err.Error())
case errors.Is(err, lib.ErrUnknown):
return status.Error(codes.Unknown, err.Error())
case errors.Is(err, lib.ErrDeadlineExceeded):
return status.Error(codes.DeadlineExceeded, err.Error())
case errors.Is(err, lib.ErrNotFound):
return status.Error(codes.NotFound, err.Error())
case errors.Is(err, lib.ErrAlreadyExists):
return status.Error(codes.AlreadyExists, err.Error())
case errors.Is(err, lib.ErrConflict):
return status.Error(codes.Unimplemented, err.Error())
case errors.Is(err, lib.ErrUnimplemented):
return status.Error(codes.Unimplemented, err.Error())
case errors.Is(err, lib.ErrUnauthenticated):
return status.Error(codes.Unauthenticated, err.Error())
default:
return status.Error(codes.Unknown, err.Error())
}
}
func (s *TesterServer) ErrUnwrappingUnaryInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
resp, err := handler(ctx, req)
return resp, ToGrpcError(err)
}
}
func (s *TesterServer) ErrUnwrappingStreamInterceptor() grpc.StreamServerInterceptor {
return func(server interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
err := handler(server, ss)
return ToGrpcError(err)
}
}