1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-05-02 22:17:10 +08:00
Dušan Borovčanin ea3a891c91
MF-1190 - Add pkg for library packages (#1191)
* Move messaging to pkg

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Move errors to pkg

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Move Transformers to pkg

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Move SDK to pkg

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Remove Transformers from root

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Fix make proto

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Add copyrights header

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Fix CI

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Move Auth client to pkg

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Fix dependencies

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Update dependencies and vendors

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Fix CI

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
2020-06-03 15:16:19 +02:00

66 lines
1.3 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package auth
import (
"context"
"github.com/go-redis/redis"
"github.com/mainflux/mainflux"
)
// Client represents Auth cache.
type Client interface {
Authorize(chanID, thingID string) error
Identify(thingKey string) (string, error)
}
const (
chanPrefix = "channel"
keyPrefix = "thing_key"
)
type client struct {
redisClient *redis.Client
thingsClient mainflux.ThingsServiceClient
}
// New returns redis channel cache implementation.
func New(redisClient *redis.Client, thingsClient mainflux.ThingsServiceClient) Client {
return client{
redisClient: redisClient,
thingsClient: thingsClient,
}
}
func (c client) Identify(thingKey string) (string, error) {
tkey := keyPrefix + ":" + thingKey
thingID, err := c.redisClient.Get(tkey).Result()
if err != nil {
t := &mainflux.Token{
Value: string(thingKey),
}
thid, err := c.thingsClient.Identify(context.TODO(), t)
if err != nil {
return "", err
}
return thid.GetValue(), nil
}
return thingID, nil
}
func (c client) Authorize(chanID, thingID string) error {
if c.redisClient.SIsMember(chanPrefix+":"+chanID, thingID).Val() {
return nil
}
ar := &mainflux.AccessByIDReq{
ThingID: thingID,
ChanID: chanID,
}
_, err := c.thingsClient.CanAccessByID(context.TODO(), ar)
return err
}