2020-12-29 23:02:35 +01:00
|
|
|
// Copyright (c) Mainflux
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-kit/kit/endpoint"
|
|
|
|
"github.com/mainflux/mainflux/auth"
|
|
|
|
)
|
|
|
|
|
|
|
|
func issueEndpoint(svc auth.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(issueReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return issueRes{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key := auth.Key{
|
|
|
|
Type: req.keyType,
|
|
|
|
Subject: req.email,
|
|
|
|
IssuerID: req.id,
|
|
|
|
IssuedAt: time.Now().UTC(),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, secret, err := svc.Issue(ctx, "", key)
|
|
|
|
if err != nil {
|
|
|
|
return issueRes{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return issueRes{secret}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func identifyEndpoint(svc auth.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(identityReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return identityRes{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := svc.Identify(ctx, req.token)
|
|
|
|
if err != nil {
|
|
|
|
return identityRes{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := identityRes{
|
|
|
|
id: id.ID,
|
|
|
|
email: id.Email,
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func authorizeEndpoint(svc auth.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(authReq)
|
|
|
|
|
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return authorizeRes{}, err
|
|
|
|
}
|
|
|
|
|
2021-10-27 00:38:28 +02:00
|
|
|
err := svc.Authorize(ctx, auth.PolicyReq{Subject: req.Sub, Object: req.Obj, Relation: req.Act})
|
2020-12-29 23:02:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return authorizeRes{}, err
|
|
|
|
}
|
2021-10-27 00:38:28 +02:00
|
|
|
return authorizeRes{authorized: true}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addPolicyEndpoint(svc auth.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
2022-03-03 17:13:46 +01:00
|
|
|
req := request.(policyReq)
|
2021-10-27 00:38:28 +02:00
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return addPolicyRes{}, err
|
|
|
|
}
|
2020-12-29 23:02:35 +01:00
|
|
|
|
2021-10-27 00:38:28 +02:00
|
|
|
err := svc.AddPolicy(ctx, auth.PolicyReq{Subject: req.Sub, Object: req.Obj, Relation: req.Act})
|
2020-12-29 23:02:35 +01:00
|
|
|
if err != nil {
|
2021-10-27 00:38:28 +02:00
|
|
|
return addPolicyRes{}, err
|
2020-12-29 23:02:35 +01:00
|
|
|
}
|
2021-10-27 00:38:28 +02:00
|
|
|
return addPolicyRes{authorized: true}, err
|
|
|
|
}
|
|
|
|
}
|
2020-12-29 23:02:35 +01:00
|
|
|
|
2021-10-27 00:38:28 +02:00
|
|
|
func deletePolicyEndpoint(svc auth.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
2022-03-03 17:13:46 +01:00
|
|
|
req := request.(policyReq)
|
2021-10-27 00:38:28 +02:00
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return deletePolicyRes{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err := svc.DeletePolicy(ctx, auth.PolicyReq{Subject: req.Sub, Object: req.Obj, Relation: req.Act})
|
|
|
|
if err != nil {
|
|
|
|
return deletePolicyRes{}, err
|
|
|
|
}
|
|
|
|
return deletePolicyRes{deleted: true}, nil
|
2020-12-29 23:02:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 16:32:38 +03:00
|
|
|
func listPoliciesEndpoint(svc auth.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(listPoliciesReq)
|
|
|
|
|
|
|
|
page, err := svc.ListPolicies(ctx, auth.PolicyReq{Subject: req.Sub, Object: req.Obj, Relation: req.Act})
|
|
|
|
if err != nil {
|
|
|
|
return deletePolicyRes{}, err
|
|
|
|
}
|
|
|
|
return listPoliciesRes{policies: page.Policies}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 23:02:35 +01:00
|
|
|
func assignEndpoint(svc auth.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(assignReq)
|
|
|
|
|
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return emptyRes{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := svc.Identify(ctx, req.token)
|
|
|
|
if err != nil {
|
|
|
|
return emptyRes{}, err
|
|
|
|
}
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
err = svc.Assign(ctx, req.token, req.memberID, req.groupID, req.groupType)
|
2020-12-29 23:02:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return emptyRes{}, err
|
|
|
|
}
|
|
|
|
return emptyRes{}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func membersEndpoint(svc auth.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(membersReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
|
|
return membersRes{}, err
|
|
|
|
}
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
pm := auth.PageMetadata{
|
|
|
|
Offset: req.offset,
|
|
|
|
Limit: req.limit,
|
2020-12-29 23:02:35 +01:00
|
|
|
}
|
2021-03-04 10:29:03 +01:00
|
|
|
mp, err := svc.ListMembers(ctx, req.token, req.groupID, req.memberType, pm)
|
2020-12-29 23:02:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return membersRes{}, err
|
|
|
|
}
|
2021-03-04 10:29:03 +01:00
|
|
|
var members []string
|
2020-12-29 23:02:35 +01:00
|
|
|
for _, m := range mp.Members {
|
2021-03-04 10:29:03 +01:00
|
|
|
members = append(members, m.ID)
|
2020-12-29 23:02:35 +01:00
|
|
|
}
|
|
|
|
return membersRes{
|
|
|
|
offset: req.offset,
|
|
|
|
limit: req.limit,
|
|
|
|
total: mp.PageMetadata.Total,
|
2021-03-04 10:29:03 +01:00
|
|
|
members: members,
|
2020-12-29 23:02:35 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|