2018-05-10 23:53:25 +02:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
kitgrpc "github.com/go-kit/kit/transport/grpc"
|
|
|
|
"github.com/mainflux/mainflux"
|
2018-05-15 17:13:09 +02:00
|
|
|
"github.com/mainflux/mainflux/things"
|
2018-05-10 23:53:25 +02:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
"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 {
|
2018-05-17 20:17:02 +02:00
|
|
|
canAccess kitgrpc.Handler
|
|
|
|
identify kitgrpc.Handler
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
// NewServer returns new ThingsServiceServer instance.
|
|
|
|
func NewServer(svc things.Service) mainflux.ThingsServiceServer {
|
2018-05-17 20:17:02 +02:00
|
|
|
return &grpcServer{
|
|
|
|
canAccess: kitgrpc.NewServer(
|
|
|
|
canAccessEndpoint(svc),
|
|
|
|
decodeCanAccessRequest,
|
|
|
|
encodeIdentityResponse,
|
|
|
|
),
|
|
|
|
identify: kitgrpc.NewServer(
|
|
|
|
identifyEndpoint(svc),
|
|
|
|
decodeIdentifyRequest,
|
|
|
|
encodeIdentityResponse,
|
|
|
|
),
|
|
|
|
}
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
func (gs *grpcServer) CanAccess(ctx context.Context, req *mainflux.AccessReq) (*mainflux.ThingID, error) {
|
2018-05-17 20:17:02 +02:00
|
|
|
_, res, err := gs.canAccess.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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func decodeCanAccessRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
|
|
|
|
req := grpcReq.(*mainflux.AccessReq)
|
2018-05-21 12:51:46 +02:00
|
|
|
return accessReq{thingKey: req.GetToken(), chanID: req.GetChanID()}, nil
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2018-05-21 12:51:46 +02:00
|
|
|
return &mainflux.ThingID{Value: res.id}, 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
|
2018-05-15 17:13:09 +02:00
|
|
|
case things.ErrMalformedEntity:
|
2018-05-10 23:53:25 +02:00
|
|
|
return status.Error(codes.InvalidArgument, "received invalid can access request")
|
2018-05-15 17:13:09 +02:00
|
|
|
case things.ErrUnauthorizedAccess:
|
2018-05-14 13:11:29 +02:00
|
|
|
return status.Error(codes.PermissionDenied, "missing or invalid credentials provided")
|
2018-05-10 23:53:25 +02:00
|
|
|
default:
|
|
|
|
return status.Error(codes.Internal, "internal server error")
|
|
|
|
}
|
|
|
|
}
|