1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
Mainflux.mainflux/coap/adapter.go
b1ackd0t 7cccba91c9
NOISSUE - Reformat Things and Users Policies Endpoint (#1831)
* Reformat Policies Enpoint to Take Sub Obj

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Add Redirect to `policies`

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Make Delete Endpoint not to Contain Body

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Remove gRPC unused functions

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Remove Redirect

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Update CLI

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Use Switch Statement

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Uncomment Commented Parts

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Add Empty Line

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Remove Unused gRPC Req and Resp

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Fix Listing of Policies

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Rename Authorize Functions For Users and Things Service

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Add Authorize To CLI

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

---------

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
2023-07-28 14:39:13 +02:00

119 lines
3.3 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
// Package coap contains the domain concept definitions needed to support
// Mainflux CoAP adapter service functionality. All constant values are taken
// from RFC, and could be adjusted based on specific use case.
package coap
import (
"context"
"fmt"
"sync"
"github.com/mainflux/mainflux/pkg/errors"
"github.com/mainflux/mainflux/things/policies"
"github.com/mainflux/mainflux/pkg/messaging"
)
const chansPrefix = "channels"
// ErrUnsubscribe indicates an error to unsubscribe.
var ErrUnsubscribe = errors.New("unable to unsubscribe")
// Service specifies CoAP service API.
type Service interface {
// Publish publishes message to specified channel.
// Key is used to authorize publisher.
Publish(ctx context.Context, key string, msg *messaging.Message) error
// Subscribes to channel with specified id, subtopic and adds subscription to
// service map of subscriptions under given ID.
Subscribe(ctx context.Context, key, chanID, subtopic string, c Client) error
// Unsubscribe method is used to stop observing resource.
Unsubscribe(ctx context.Context, key, chanID, subptopic, token string) error
}
var _ Service = (*adapterService)(nil)
// Observers is a map of maps,.
type adapterService struct {
auth policies.AuthServiceClient
pubsub messaging.PubSub
obsLock sync.Mutex
}
// New instantiates the CoAP adapter implementation.
func New(auth policies.AuthServiceClient, pubsub messaging.PubSub) Service {
as := &adapterService{
auth: auth,
pubsub: pubsub,
obsLock: sync.Mutex{},
}
return as
}
func (svc *adapterService) Publish(ctx context.Context, key string, msg *messaging.Message) error {
ar := &policies.AuthorizeReq{
Subject: key,
Object: msg.Channel,
Action: policies.WriteAction,
EntityType: policies.ThingEntityType,
}
res, err := svc.auth.Authorize(ctx, ar)
if err != nil {
return errors.Wrap(errors.ErrAuthorization, err)
}
if !res.GetAuthorized() {
return errors.ErrAuthorization
}
msg.Publisher = res.GetThingID()
return svc.pubsub.Publish(ctx, msg.Channel, msg)
}
func (svc *adapterService) Subscribe(ctx context.Context, key, chanID, subtopic string, c Client) error {
ar := &policies.AuthorizeReq{
Subject: key,
Object: chanID,
Action: policies.ReadAction,
EntityType: policies.GroupEntityType,
}
res, err := svc.auth.Authorize(ctx, ar)
if err != nil {
return errors.Wrap(errors.ErrAuthorization, err)
}
if !res.GetAuthorized() {
return errors.ErrAuthorization
}
subject := fmt.Sprintf("%s.%s", chansPrefix, chanID)
if subtopic != "" {
subject = fmt.Sprintf("%s.%s", subject, subtopic)
}
return svc.pubsub.Subscribe(ctx, c.Token(), subject, c)
}
func (svc *adapterService) Unsubscribe(ctx context.Context, key, chanID, subtopic, token string) error {
ar := &policies.AuthorizeReq{
Subject: key,
Object: chanID,
Action: policies.ReadAction,
EntityType: policies.GroupEntityType,
}
res, err := svc.auth.Authorize(ctx, ar)
if err != nil {
return errors.Wrap(errors.ErrAuthorization, err)
}
if !res.GetAuthorized() {
return errors.ErrAuthorization
}
subject := fmt.Sprintf("%s.%s", chansPrefix, chanID)
if subtopic != "" {
subject = fmt.Sprintf("%s.%s", subject, subtopic)
}
return svc.pubsub.Unsubscribe(ctx, token, subject)
}