mirror of
https://github.com/absmach/magistrala.git
synced 2026-06-23 04:10:28 +00:00
NOISSUE - Fix token revoked erro on refresh
Signed-off-by: dusan <borovcanindusan1@gmail.com>
This commit is contained in:
Vendored
+5
-13
@@ -9,7 +9,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/absmach/magistrala/auth"
|
||||
"github.com/absmach/magistrala/pkg/errors"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
@@ -22,30 +21,23 @@ const (
|
||||
var _ auth.UserActiveTokensCache = (*tokensCache)(nil)
|
||||
|
||||
type tokensCache struct {
|
||||
client *redis.Client
|
||||
keyDuration time.Duration
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
// NewUserActiveTokensCache returns redis auth cache implementation.
|
||||
func NewUserActiveTokensCache(client *redis.Client, duration time.Duration) (auth.UserActiveTokensCache, error) {
|
||||
if duration == 0 {
|
||||
return nil, errors.New("token cache duration must not be zero")
|
||||
}
|
||||
return &tokensCache{
|
||||
client: client,
|
||||
keyDuration: duration,
|
||||
}, nil
|
||||
func NewUserActiveTokensCache(client *redis.Client) (auth.UserActiveTokensCache, error) {
|
||||
return &tokensCache{client: client}, nil
|
||||
}
|
||||
|
||||
// SaveActive saves an active refresh token ID for a user with optional description.
|
||||
func (tc *tokensCache) SaveActive(ctx context.Context, userID, tokenID, description string, expiry time.Time) error {
|
||||
ttl := min(tc.keyDuration, time.Until(expiry))
|
||||
ttl := time.Until(expiry)
|
||||
|
||||
pipe := tc.client.TxPipeline()
|
||||
|
||||
pipe.Set(ctx, tokenKey(tokenID), description, ttl)
|
||||
pipe.ZAdd(ctx, userTokensKey(userID), redis.Z{
|
||||
Score: float64(time.Now().Add(ttl).Unix()),
|
||||
Score: float64(expiry.Unix()),
|
||||
Member: tokenID,
|
||||
})
|
||||
|
||||
|
||||
Vendored
+1
-1
@@ -29,7 +29,7 @@ func TestMain(m *testing.M) {
|
||||
}
|
||||
|
||||
func setupRedisTokensClient() auth.UserActiveTokensCache {
|
||||
tc, err := cache.NewUserActiveTokensCache(storeClient, 10*time.Minute)
|
||||
tc, err := cache.NewUserActiveTokensCache(storeClient)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
+1
-1
@@ -293,7 +293,7 @@ func validateKeyConfig(isSymmetric bool, cfg config, l *slog.Logger) error {
|
||||
|
||||
func newService(db *sqlx.DB, tracer trace.Tracer, cfg config, dbConfig pgclient.Config, logger *slog.Logger, spicedbClient *authzed.ClientWithExperimental, cacheClient *redis.Client, keyDuration time.Duration, tokenizer auth.Tokenizer, idProvider magistrala.IDProvider) (auth.Service, error) {
|
||||
patsCache := cache.NewPatsCache(cacheClient, keyDuration)
|
||||
tokensCache, err := cache.NewUserActiveTokensCache(cacheClient, keyDuration)
|
||||
tokensCache, err := cache.NewUserActiveTokensCache(cacheClient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user