From 125e311eda76b9e93bf52cf850b32ae29ab8ac08 Mon Sep 17 00:00:00 2001 From: dusan Date: Wed, 8 Apr 2026 18:47:49 +0200 Subject: [PATCH] NOISSUE - Fix token revoked erro on refresh Signed-off-by: dusan --- auth/cache/tokens.go | 18 +++++------------- auth/cache/tokens_test.go | 2 +- cmd/auth/main.go | 2 +- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/auth/cache/tokens.go b/auth/cache/tokens.go index 4e64a8c21..7dddcf8b3 100644 --- a/auth/cache/tokens.go +++ b/auth/cache/tokens.go @@ -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, }) diff --git a/auth/cache/tokens_test.go b/auth/cache/tokens_test.go index 1f6bee96b..7e94708e8 100644 --- a/auth/cache/tokens_test.go +++ b/auth/cache/tokens_test.go @@ -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) } diff --git a/cmd/auth/main.go b/cmd/auth/main.go index 4a03d36fe..79afda012 100644 --- a/cmd/auth/main.go +++ b/cmd/auth/main.go @@ -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 }