ms-auth/internal/transport/interceptors.go
Vyacheslav1557 ba26d05516 fix:
2024-10-05 19:30:54 +05:00

37 lines
842 B
Go

package transport
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type ReqWithToken interface {
GetToken() string
}
func (s *AuthServer) 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 handler(ctx, req)
}
token := reqWithToken.GetToken()
userId, err := s.sessionService.Read(ctx, token)
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "")
}
user, err := s.userService.ReadUser(ctx, *userId)
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "")
}
ctx = context.WithValue(ctx, "user", user)
return handler(ctx, req)
}
}