2018-08-26 13:15:48 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2018-05-10 23:53:25 +02:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-kit/kit/endpoint"
|
2018-05-15 17:13:09 +02:00
|
|
|
"github.com/mainflux/mainflux/things"
|
2018-05-10 23:53:25 +02:00
|
|
|
context "golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
func canAccessEndpoint(svc things.Service) endpoint.Endpoint {
|
2018-05-10 23:53:25 +02:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(accessReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
id, err := svc.CanAccess(req.chanID, req.thingKey)
|
2018-05-10 23:53:25 +02:00
|
|
|
if err != nil {
|
2018-12-05 13:09:25 +01:00
|
|
|
return identityRes{err: err}, err
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
2018-05-17 20:17:02 +02:00
|
|
|
return identityRes{id: id, err: nil}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-15 18:28:15 +02:00
|
|
|
func canAccessByIDEndpoint(svc things.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(accessByIDReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err := svc.CanAccessByID(req.chanID, req.thingID)
|
|
|
|
return emptyRes{err: err}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 20:17:02 +02:00
|
|
|
func identifyEndpoint(svc things.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(identifyReq)
|
|
|
|
id, err := svc.Identify(req.key)
|
|
|
|
if err != nil {
|
2018-12-05 13:09:25 +01:00
|
|
|
return identityRes{err: err}, err
|
2018-05-17 20:17:02 +02:00
|
|
|
}
|
|
|
|
return identityRes{id: id, err: nil}, nil
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
}
|