mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +08:00

* Update WS tests Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Use require in all writer tests Refactor code. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Ignore Mainflux generated pb.go files Ignore *.pb.go files generated by Mainflux, but don't ignore vendored generated code. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Return an exported ErrNotFound instead of the unexported one Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update mocks to match the actual behaviour Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update mocks error message Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Add auth service unavailable error test Since this error is caused by gRPC server returning codes.Internal, this behaviour is simulated using specific token. When that token is passed as an auth header, the mock gRPC client returns aforementioned error. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Use require package for postgres tests Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove redundant error checks in tests Refactor tests. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Rename error flag token Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
80 lines
2.2 KiB
Go
80 lines
2.2 KiB
Go
package grpc
|
|
|
|
import (
|
|
kitgrpc "github.com/go-kit/kit/transport/grpc"
|
|
"github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/things"
|
|
"golang.org/x/net/context"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
var _ mainflux.ThingsServiceServer = (*grpcServer)(nil)
|
|
|
|
type grpcServer struct {
|
|
canAccess kitgrpc.Handler
|
|
identify kitgrpc.Handler
|
|
}
|
|
|
|
// NewServer returns new ThingsServiceServer instance.
|
|
func NewServer(svc things.Service) mainflux.ThingsServiceServer {
|
|
return &grpcServer{
|
|
canAccess: kitgrpc.NewServer(
|
|
canAccessEndpoint(svc),
|
|
decodeCanAccessRequest,
|
|
encodeIdentityResponse,
|
|
),
|
|
identify: kitgrpc.NewServer(
|
|
identifyEndpoint(svc),
|
|
decodeIdentifyRequest,
|
|
encodeIdentityResponse,
|
|
),
|
|
}
|
|
}
|
|
|
|
func (gs *grpcServer) CanAccess(ctx context.Context, req *mainflux.AccessReq) (*mainflux.ThingID, error) {
|
|
_, res, err := gs.canAccess.ServeGRPC(ctx, req)
|
|
if err != nil {
|
|
return nil, encodeError(err)
|
|
}
|
|
|
|
return res.(*mainflux.ThingID), nil
|
|
}
|
|
|
|
func (gs *grpcServer) Identify(ctx context.Context, req *mainflux.Token) (*mainflux.ThingID, error) {
|
|
_, res, err := gs.identify.ServeGRPC(ctx, req)
|
|
if err != nil {
|
|
return nil, encodeError(err)
|
|
}
|
|
|
|
return res.(*mainflux.ThingID), nil
|
|
}
|
|
|
|
func decodeCanAccessRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
|
|
req := grpcReq.(*mainflux.AccessReq)
|
|
return accessReq{thingKey: req.GetToken(), chanID: req.GetChanID()}, nil
|
|
}
|
|
|
|
func decodeIdentifyRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
|
|
req := grpcReq.(*mainflux.Token)
|
|
return identifyReq{key: req.GetValue()}, nil
|
|
}
|
|
|
|
func encodeIdentityResponse(_ context.Context, grpcRes interface{}) (interface{}, error) {
|
|
res := grpcRes.(identityRes)
|
|
return &mainflux.ThingID{Value: res.id}, encodeError(res.err)
|
|
}
|
|
|
|
func encodeError(err error) error {
|
|
switch err {
|
|
case nil:
|
|
return nil
|
|
case things.ErrMalformedEntity:
|
|
return status.Error(codes.InvalidArgument, "received invalid can access request")
|
|
case things.ErrUnauthorizedAccess:
|
|
return status.Error(codes.PermissionDenied, "missing or invalid credentials provided")
|
|
default:
|
|
return status.Error(codes.Internal, "internal server error")
|
|
}
|
|
}
|