I want to add a directive my.AUTH to forward the acl account to AUTH. Disconnect other connections with the same username after successful auth. Can it be done without modifying the redis source code?
#include "redismodule.h"
#include <string.h>
int yhAuthCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
{
if (argc != 2 && argc != 3)
{
return RedisModule_WrongArity(ctx);
}
const char *username;
const char *password = RedisModule_StringPtrLen(argv[argc - 1], NULL);
if (argc == 3)
{
username = RedisModule_StringPtrLen(argv[1], NULL);
}
else
{
username = "default";
}
RedisModuleCallReply *authReply = RedisModule_Call(ctx, "AUTH", "ss", username, password);
if (authReply == NULL || RedisModule_CallReplyType(authReply) == REDISMODULE_REPLY_ERROR)
{
RedisModule_ReplyWithError(ctx, "ERR Failed to forward AUTH command");
return REDISMODULE_ERR;
}
RedisModule_ReplyWithCallReply(ctx, authReply);
RedisModule_FreeCallReply(authReply);
// todo Disconnect other connections with the same username after auth
// CLIENT KILL SKIPME yes ;CLIENT KILL USER username
return REDISMODULE_OK;
}
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
{
if (RedisModule_Init(ctx, "yhAuth", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR)
{
return REDISMODULE_ERR;
}
if (RedisModule_CreateCommand(ctx, "yh.auth", yhAuthCommand, "write", 1, 1, 1) == REDISMODULE_ERR)
{
return REDISMODULE_ERR;
}
return REDISMODULE_OK;
}
Comment From: hpatro
@joyanhui Won't you be able to do the same via MULTI/EXEC ?
MULTI
AUTH username pwd
CLIENT KILL SKIPME YES
CLIENT KILL USER username
EXEC