38 lines
963 B
Go
38 lines
963 B
Go
package transport
|
|
|
|
import (
|
|
"context"
|
|
sessionv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/session/v1"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type ReqWithToken interface {
|
|
GetToken() string
|
|
}
|
|
|
|
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.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)
|
|
}
|
|
}
|