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"
|
|
|
|
|
2018-05-10 23:53:25 +02:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
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) {
|
2019-10-21 15:24:45 -06:00
|
|
|
req := request.(AccessByKeyReq)
|
2018-05-10 23:53:25 +02:00
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-21 15:24:45 -06:00
|
|
|
id, err := svc.CanAccessByKey(ctx, req.chanID, req.thingKey)
|
2018-05-10 23:53:25 +02:00
|
|
|
if err != nil {
|
2020-12-29 23:02:35 +01:00
|
|
|
return identityRes{}, err
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
2020-12-29 23:02:35 +01:00
|
|
|
return identityRes{id: id}, nil
|
2018-05-17 20:17:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
err := svc.CanAccessByID(ctx, req.chanID, req.thingID)
|
2019-07-15 18:28:15 +02:00
|
|
|
return emptyRes{err: err}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 19:41:59 +01:00
|
|
|
func isChannelOwnerEndpoint(svc things.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(channelOwnerReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-24 13:23:50 +01:00
|
|
|
err := svc.IsChannelOwner(ctx, req.owner, req.chanID)
|
2021-02-22 19:41:59 +01:00
|
|
|
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)
|
2019-07-18 15:01:09 +02:00
|
|
|
id, err := svc.Identify(ctx, req.key)
|
2018-05-17 20:17:02 +02:00
|
|
|
if err != nil {
|
2020-12-29 23:02:35 +01:00
|
|
|
return identityRes{}, err
|
2018-05-17 20:17:02 +02:00
|
|
|
}
|
2020-12-29 23:02:35 +01:00
|
|
|
return identityRes{id: id}, nil
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|
|
|
|
}
|