2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 13:15:48 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-05-10 23:53:25 +02:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
2020-07-13 15:24:55 +02:00
|
|
|
"context"
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
kitot "github.com/go-kit/kit/tracing/opentracing"
|
2018-05-10 23:53:25 +02:00
|
|
|
kitgrpc "github.com/go-kit/kit/transport/grpc"
|
2019-07-15 18:28:15 +02:00
|
|
|
"github.com/golang/protobuf/ptypes/empty"
|
2018-05-10 23:53:25 +02:00
|
|
|
"github.com/mainflux/mainflux"
|
2022-01-27 17:03:57 +01:00
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
2018-05-15 17:13:09 +02:00
|
|
|
"github.com/mainflux/mainflux/things"
|
2019-07-18 15:01:09 +02:00
|
|
|
opentracing "github.com/opentracing/opentracing-go"
|
2018-05-10 23:53:25 +02:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
var _ mainflux.ThingsServiceServer = (*grpcServer)(nil)
|
2018-05-10 23:53:25 +02:00
|
|
|
|
|
|
|
type grpcServer struct {
|
2019-10-21 15:24:45 -06:00
|
|
|
canAccessByKey kitgrpc.Handler
|
|
|
|
canAccessByID kitgrpc.Handler
|
2021-02-22 19:41:59 +01:00
|
|
|
isChannelOwner kitgrpc.Handler
|
2019-10-21 15:24:45 -06:00
|
|
|
identify kitgrpc.Handler
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
// NewServer returns new ThingsServiceServer instance.
|
2019-07-18 15:01:09 +02:00
|
|
|
func NewServer(tracer opentracing.Tracer, svc things.Service) mainflux.ThingsServiceServer {
|
2018-05-17 20:17:02 +02:00
|
|
|
return &grpcServer{
|
2019-10-21 15:24:45 -06:00
|
|
|
canAccessByKey: kitgrpc.NewServer(
|
2019-07-18 15:01:09 +02:00
|
|
|
kitot.TraceServer(tracer, "can_access")(canAccessEndpoint(svc)),
|
2019-10-21 15:24:45 -06:00
|
|
|
decodeCanAccessByKeyRequest,
|
2018-05-17 20:17:02 +02:00
|
|
|
encodeIdentityResponse,
|
|
|
|
),
|
2019-07-15 18:28:15 +02:00
|
|
|
canAccessByID: kitgrpc.NewServer(
|
|
|
|
canAccessByIDEndpoint(svc),
|
|
|
|
decodeCanAccessByIDRequest,
|
|
|
|
encodeEmptyResponse,
|
|
|
|
),
|
2021-02-22 19:41:59 +01:00
|
|
|
isChannelOwner: kitgrpc.NewServer(
|
|
|
|
isChannelOwnerEndpoint(svc),
|
|
|
|
decodeIsChannelOwnerRequest,
|
|
|
|
encodeEmptyResponse,
|
|
|
|
),
|
2018-05-17 20:17:02 +02:00
|
|
|
identify: kitgrpc.NewServer(
|
2019-07-18 15:01:09 +02:00
|
|
|
kitot.TraceServer(tracer, "identify")(identifyEndpoint(svc)),
|
2018-05-17 20:17:02 +02:00
|
|
|
decodeIdentifyRequest,
|
|
|
|
encodeIdentityResponse,
|
|
|
|
),
|
|
|
|
}
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2019-10-21 15:24:45 -06:00
|
|
|
func (gs *grpcServer) CanAccessByKey(ctx context.Context, req *mainflux.AccessByKeyReq) (*mainflux.ThingID, error) {
|
|
|
|
_, res, err := gs.canAccessByKey.ServeGRPC(ctx, req)
|
2018-05-10 23:53:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, encodeError(err)
|
|
|
|
}
|
2018-05-17 20:17:02 +02:00
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
return res.(*mainflux.ThingID), nil
|
2018-05-17 20:17:02 +02:00
|
|
|
}
|
|
|
|
|
2019-07-15 18:28:15 +02:00
|
|
|
func (gs *grpcServer) CanAccessByID(ctx context.Context, req *mainflux.AccessByIDReq) (*empty.Empty, error) {
|
|
|
|
_, res, err := gs.canAccessByID.ServeGRPC(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, encodeError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.(*empty.Empty), nil
|
|
|
|
}
|
|
|
|
|
2021-02-22 19:41:59 +01:00
|
|
|
func (gs *grpcServer) IsChannelOwner(ctx context.Context, req *mainflux.ChannelOwnerReq) (*empty.Empty, error) {
|
|
|
|
_, res, err := gs.isChannelOwner.ServeGRPC(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, encodeError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.(*empty.Empty), nil
|
|
|
|
}
|
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
func (gs *grpcServer) Identify(ctx context.Context, req *mainflux.Token) (*mainflux.ThingID, error) {
|
2018-05-17 20:17:02 +02:00
|
|
|
_, res, err := gs.identify.ServeGRPC(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, encodeError(err)
|
|
|
|
}
|
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
return res.(*mainflux.ThingID), nil
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2019-10-21 15:24:45 -06:00
|
|
|
func decodeCanAccessByKeyRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
|
|
|
|
req := grpcReq.(*mainflux.AccessByKeyReq)
|
|
|
|
return AccessByKeyReq{thingKey: req.GetToken(), chanID: req.GetChanID()}, nil
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2019-07-15 18:28:15 +02:00
|
|
|
func decodeCanAccessByIDRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
|
|
|
|
req := grpcReq.(*mainflux.AccessByIDReq)
|
|
|
|
return accessByIDReq{thingID: req.GetThingID(), chanID: req.GetChanID()}, nil
|
|
|
|
}
|
|
|
|
|
2021-02-22 19:41:59 +01:00
|
|
|
func decodeIsChannelOwnerRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
|
|
|
|
req := grpcReq.(*mainflux.ChannelOwnerReq)
|
|
|
|
return channelOwnerReq{owner: req.GetOwner(), chanID: req.GetChanID()}, nil
|
|
|
|
}
|
|
|
|
|
2018-05-17 20:17:02 +02:00
|
|
|
func decodeIdentifyRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
|
|
|
|
req := grpcReq.(*mainflux.Token)
|
2018-05-21 12:51:46 +02:00
|
|
|
return identifyReq{key: req.GetValue()}, nil
|
2018-05-17 20:17:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func encodeIdentityResponse(_ context.Context, grpcRes interface{}) (interface{}, error) {
|
|
|
|
res := grpcRes.(identityRes)
|
2020-12-29 23:02:35 +01:00
|
|
|
return &mainflux.ThingID{Value: res.id}, nil
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2019-07-15 18:28:15 +02:00
|
|
|
func encodeEmptyResponse(_ context.Context, grpcRes interface{}) (interface{}, error) {
|
|
|
|
res := grpcRes.(emptyRes)
|
|
|
|
return &empty.Empty{}, encodeError(res.err)
|
|
|
|
}
|
|
|
|
|
2018-05-10 23:53:25 +02:00
|
|
|
func encodeError(err error) error {
|
|
|
|
switch err {
|
2018-06-16 02:30:46 +02:00
|
|
|
case nil:
|
|
|
|
return nil
|
2022-01-27 17:03:57 +01:00
|
|
|
case errors.ErrMalformedEntity:
|
2018-05-10 23:53:25 +02:00
|
|
|
return status.Error(codes.InvalidArgument, "received invalid can access request")
|
2022-02-01 17:33:23 +01:00
|
|
|
case errors.ErrAuthentication:
|
|
|
|
return status.Error(codes.Unauthenticated, "missing or invalid credentials provided")
|
|
|
|
case errors.ErrAuthorization:
|
|
|
|
return status.Error(codes.PermissionDenied, "unauthorized access token provided")
|
2020-09-23 15:44:39 +02:00
|
|
|
case things.ErrEntityConnected:
|
|
|
|
return status.Error(codes.PermissionDenied, "entities are not connected")
|
2022-01-27 17:03:57 +01:00
|
|
|
case errors.ErrNotFound:
|
2020-09-23 15:44:39 +02:00
|
|
|
return status.Error(codes.NotFound, "entity does not exist")
|
2018-05-10 23:53:25 +02:00
|
|
|
default:
|
|
|
|
return status.Error(codes.Internal, "internal server error")
|
|
|
|
}
|
|
|
|
}
|