From bf961e753c92bb2ee8bc82990a3fd01332c86855 Mon Sep 17 00:00:00 2001 From: alifgiant Date: Tue, 17 Jun 2025 22:35:34 +0800 Subject: [PATCH] add AUTH command to redis connection --- neat_cache/lib/src/providers/redis.dart | 28 ++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/neat_cache/lib/src/providers/redis.dart b/neat_cache/lib/src/providers/redis.dart index b5dbf649..9d827a0d 100644 --- a/neat_cache/lib/src/providers/redis.dart +++ b/neat_cache/lib/src/providers/redis.dart @@ -77,9 +77,35 @@ class RedisCacheProvider extends CacheProvider> { ).timeout(_connectTimeLimit); socket.setOption(SocketOption.tcpNoDelay, true); + final client = RespClient(socket, socket); + + final userInfo = + _connectionString.userInfo; // "user:password" or ":password" + if (userInfo.isNotEmpty) { + final List parts = userInfo.split(':'); + String? password; + if (parts.length == 2) { + // If it's user:password + password = parts[1]; + } else if (parts.length == 1 && userInfo.startsWith(':')) { + // If it's :password + password = parts[0].substring(1); // Remove the leading colon + } + + if (password != null && password.isNotEmpty) { + _log.info('Authenticating with Redis...'); + + final authResponse = await client + .command(['AUTH', password]).timeout(_commandTimeLimit); + if (authResponse != 'OK') { + _log.severe('Redis authentication unsuccessful.'); + } + } + } + // Create context return _RedisContext( - client: RespClient(socket, socket), + client: client, // Use the client after potential authentication ); } on RedisConnectionException { throw IntermittentCacheException('connection failed');