1
0

auth.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #define REDISMODULE_EXPERIMENTAL_API
  2. #include "redismodule.h"
  3. // A simple global user
  4. static RedisModuleUser *global = NULL;
  5. static long long client_change_delta = 0;
  6. void UserChangedCallback(uint64_t client_id, void *privdata) {
  7. REDISMODULE_NOT_USED(privdata);
  8. REDISMODULE_NOT_USED(client_id);
  9. client_change_delta++;
  10. }
  11. int Auth_CreateModuleUser(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  12. REDISMODULE_NOT_USED(argv);
  13. REDISMODULE_NOT_USED(argc);
  14. if (global) {
  15. RedisModule_FreeModuleUser(global);
  16. }
  17. global = RedisModule_CreateModuleUser("global");
  18. RedisModule_SetModuleUserACL(global, "allcommands");
  19. RedisModule_SetModuleUserACL(global, "allkeys");
  20. RedisModule_SetModuleUserACL(global, "on");
  21. return RedisModule_ReplyWithSimpleString(ctx, "OK");
  22. }
  23. int Auth_AuthModuleUser(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  24. REDISMODULE_NOT_USED(argv);
  25. REDISMODULE_NOT_USED(argc);
  26. uint64_t client_id;
  27. RedisModule_AuthenticateClientWithUser(ctx, global, UserChangedCallback, NULL, &client_id);
  28. return RedisModule_ReplyWithLongLong(ctx, (uint64_t) client_id);
  29. }
  30. int Auth_AuthRealUser(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  31. if (argc != 2) return RedisModule_WrongArity(ctx);
  32. size_t length;
  33. uint64_t client_id;
  34. RedisModuleString *user_string = argv[1];
  35. const char *name = RedisModule_StringPtrLen(user_string, &length);
  36. if (RedisModule_AuthenticateClientWithACLUser(ctx, name, length,
  37. UserChangedCallback, NULL, &client_id) == REDISMODULE_ERR) {
  38. return RedisModule_ReplyWithError(ctx, "Invalid user");
  39. }
  40. return RedisModule_ReplyWithLongLong(ctx, (uint64_t) client_id);
  41. }
  42. int Auth_ChangeCount(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  43. REDISMODULE_NOT_USED(argv);
  44. REDISMODULE_NOT_USED(argc);
  45. long long result = client_change_delta;
  46. client_change_delta = 0;
  47. return RedisModule_ReplyWithLongLong(ctx, result);
  48. }
  49. /* This function must be present on each Redis module. It is used in order to
  50. * register the commands into the Redis server. */
  51. int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  52. REDISMODULE_NOT_USED(argv);
  53. REDISMODULE_NOT_USED(argc);
  54. if (RedisModule_Init(ctx,"testacl",1,REDISMODULE_APIVER_1)
  55. == REDISMODULE_ERR) return REDISMODULE_ERR;
  56. if (RedisModule_CreateCommand(ctx,"auth.authrealuser",
  57. Auth_AuthRealUser,"no-auth",0,0,0) == REDISMODULE_ERR)
  58. return REDISMODULE_ERR;
  59. if (RedisModule_CreateCommand(ctx,"auth.createmoduleuser",
  60. Auth_CreateModuleUser,"",0,0,0) == REDISMODULE_ERR)
  61. return REDISMODULE_ERR;
  62. if (RedisModule_CreateCommand(ctx,"auth.authmoduleuser",
  63. Auth_AuthModuleUser,"no-auth",0,0,0) == REDISMODULE_ERR)
  64. return REDISMODULE_ERR;
  65. if (RedisModule_CreateCommand(ctx,"auth.changecount",
  66. Auth_ChangeCount,"",0,0,0) == REDISMODULE_ERR)
  67. return REDISMODULE_ERR;
  68. return REDISMODULE_OK;
  69. }