besterd/source/server/accounts/redis.d

118 lines
3.0 KiB
D
Raw Normal View History

2020-05-08 14:16:56 +02:00
module server.accounts.redis;
import vibe.vibe;
2020-05-08 14:16:56 +02:00
import server.accounts.base : BesterDataStore;
2020-05-08 15:42:28 +02:00
import utils.debugging : debugPrint;
/**
* This represents a Redis datastore for the Bester
* server's account management system.
*/
2020-05-08 14:16:56 +02:00
public final class RedisDataStore : BesterDataStore
{
/**
* Redis client.
*/
private RedisClient redisClient;
/**
* Redis database with the account information
*/
private RedisDatabase redisDatabase;
this(string address, ushort port)
{
/* Opens a connection to the redis server */
initializeRedis(address, port);
}
private void initializeRedis(string address, ushort port)
{
redisClient = new RedisClient(address, port);
redisDatabase = redisClient.getDatabase(0);
2020-05-08 14:16:56 +02:00
// createAccount("deavmi", "poes");
2020-05-08 15:42:28 +02:00
2020-05-08 14:16:56 +02:00
}
override public bool userExists(string username)
{
/* TODO: Implement me */
2020-05-09 14:30:39 +02:00
return redisDatabase.exists(username);
// return true;
}
2020-05-08 13:40:47 +02:00
override public bool authenticate(string username, string password)
{
2020-05-08 15:42:28 +02:00
debugPrint(redisClient.info());
debugPrint(redisDatabase.keys("*"));
2020-05-08 13:40:47 +02:00
/* Check if a key exists with the `username` */
bool accountExists = redisDatabase.exists(username);
2020-05-08 15:42:28 +02:00
debugPrint(accountExists);
2020-05-08 13:40:47 +02:00
if(accountExists)
{
/**
* Check within the key if the subkey and value pair exists.
* `(username) [password: <password>], ...`
*/
if(redisDatabase.hexists(username, "password"))
{
/* Get the password sub-field */
string passwordDB = redisDatabase.hget(username, "password");
if(cmp(password, passwordDB) == 0)
{
return true;
}
else
{
return false;
}
2020-05-08 13:40:47 +02:00
}
else
{
/* TODO: Raise exception for missing password sub-key */
}
}
else
{
/* TODO: Raise exception for non-existent account */
}
/* TODO: Remove */
return false;
2020-05-08 13:40:47 +02:00
}
override public void createAccount(string username, string password)
{
/* TODO: Implement me */
2020-05-08 13:40:47 +02:00
/* Check if a key exists with the `username` */
bool accountExists = redisDatabase.exists(username);
if(!accountExists)
{
/**
* Create the new account.
* This involves creating a new key named `username`
* with a field named `"password"` matching to the value
* of `password`.
*/
redisDatabase.hset(username, "password", password);
}
else
{
/* TODO: Raise exception for an already existing account */
}
}
public void f()
{
}
override public void shutdown()
{
/* TODO: Should we shutdown the server? */
redisClient.shutdown();
}
}