1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-28 13:48:49 +08:00
Dušan Borovčanin 516c02bebe
MF-1378 - Update dependencies (#1379)
* Update dependencies

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

* Fix compose files and configs

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

* Upgrade image versions

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

* Update Postgres version

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

* Update test dependencies

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

* Fix fkey error handling

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
2021-05-20 20:53:56 +02:00

66 lines
1.4 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package auth
import (
"context"
"github.com/go-redis/redis/v8"
"github.com/mainflux/mainflux"
)
// Client represents Auth cache.
type Client interface {
Authorize(ctx context.Context, chanID, thingID string) error
Identify(ctx context.Context, 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(ctx context.Context, thingKey string) (string, error) {
tkey := keyPrefix + ":" + thingKey
thingID, err := c.redisClient.Get(ctx, 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(ctx context.Context, chanID, thingID string) error {
if c.redisClient.SIsMember(ctx, chanPrefix+":"+chanID, thingID).Val() {
return nil
}
ar := &mainflux.AccessByIDReq{
ThingID: thingID,
ChanID: chanID,
}
_, err := c.thingsClient.CanAccessByID(ctx, ar)
return err
}