1 module server.accounts.redis; 2 3 import vibe.vibe; 4 import server.accounts.base : BesterDataStore; 5 import utils.debugging : debugPrint; 6 7 /** 8 * This represents a Redis datastore for the Bester 9 * server's account management system. 10 */ 11 public final class RedisDataStore : BesterDataStore 12 { 13 14 /** 15 * Redis client. 16 */ 17 private RedisClient redisClient; 18 19 /** 20 * Redis database with the account information 21 */ 22 private RedisDatabase redisDatabase; 23 24 this(string address, ushort port) 25 { 26 /* Opens a connection to the redis server */ 27 initializeRedis(address, port); 28 } 29 30 private void initializeRedis(string address, ushort port) 31 { 32 redisClient = new RedisClient(address, port); 33 redisDatabase = redisClient.getDatabase(0); 34 // createAccount("deavmi", "poes"); 35 36 } 37 38 override public bool userExists(string username) 39 { 40 /* TODO: Implement me */ 41 return redisDatabase.exists(username); 42 // return true; 43 } 44 45 override public bool authenticate(string username, string password) 46 { 47 debugPrint(redisClient.info()); 48 debugPrint(redisDatabase.keys("*")); 49 /* Check if a key exists with the `username` */ 50 bool accountExists = redisDatabase.exists(username); 51 debugPrint(accountExists); 52 if(accountExists) 53 { 54 /** 55 * Check within the key if the subkey and value pair exists. 56 * `(username) [password: <password>], ...` 57 */ 58 if(redisDatabase.hexists(username, "password")) 59 { 60 /* Get the password sub-field */ 61 string passwordDB = redisDatabase.hget(username, "password"); 62 if(cmp(password, passwordDB) == 0) 63 { 64 return true; 65 } 66 else 67 { 68 return false; 69 } 70 } 71 else 72 { 73 /* TODO: Raise exception for missing password sub-key */ 74 } 75 } 76 else 77 { 78 /* TODO: Raise exception for non-existent account */ 79 } 80 81 /* TODO: Remove */ 82 return false; 83 } 84 85 override public void createAccount(string username, string password) 86 { 87 /* TODO: Implement me */ 88 89 /* Check if a key exists with the `username` */ 90 bool accountExists = redisDatabase.exists(username); 91 92 if(!accountExists) 93 { 94 /** 95 * Create the new account. 96 * This involves creating a new key named `username` 97 * with a field named `"password"` matching to the value 98 * of `password`. 99 */ 100 redisDatabase.hset(username, "password", password); 101 } 102 else 103 { 104 /* TODO: Raise exception for an already existing account */ 105 } 106 } 107 108 public void f() 109 { 110 111 } 112 113 override public void shutdown() 114 { 115 /* TODO: Should we shutdown the server? */ 116 redisClient.shutdown(); 117 } 118 }