AuthProvider

- Added provider interface

DummyProvider

- Added a dummy authentication provider

AuthManager

- Implemented an initial authentication manager
This commit is contained in:
Tristan B. Velloza Kildaire 2023-11-19 14:37:00 +02:00
parent 8415fd7c9f
commit bfc5467f28
1 changed files with 108 additions and 0 deletions

View File

@ -90,4 +90,112 @@ unittest
u.setUsername("gustav");
assert(u.getUsername(), "gustav");
}
public interface AuthProvider
{
public bool authenticate(string username, string password, ref User user);
}
public class DummyProvider : AuthProvider
{
public bool authenticate(string username, string password, ref User user)
{
user = User(username);
return true;
}
}
import renaissance.server.server : Server;
import renaissance.logging;
// Should handle all users authenticated and
// act as an information base for the current
// users
public class AuthManager
{
private Server server;
// TODO: Need an AuthProvider here
private AuthProvider provider;
/**
* TODO: We need to find a way to easily
* manage User* mapped to by a string (username)
* and how updating the username (key) would
* work (including the allocated value)
* then
*
* Update: We won't expose this User*
* to the public API as that means
* you can manipulate the user
* there (that is fine) but ALSO
* replace the entire user there
*
* Nah, forget the above we should discern
* between username (never changing)
* and nick
*
* UPDATE2: We will STILL need to index (somehow)
* on that then, perhaps a seperate
*
* What is the point of usernames? for
* auth but then nick is what people _should_
* see when you `membershipList()`.
*
* So we would need to update
* ChannelManager code to do that
*
*/
private User[string] users;
private Mutex usersLock;
private this(AuthProvider provider)
{
this.usersLock = new Mutex();
this.provider = provider;
}
private User* getUser(string username)
{
User* foundUser;
// Lock
this.usersLock.lock();
foundUser = username in this.users;
// Unlock
this.usersLock.unlock();
return foundUser;
}
public bool authenticate(string username, string password)
{
logger.dbg("Authentication request for user '"~username~"' with password '"~password~"'");
bool status;
User potentialUser = User("");
status = this.provider.authenticate(username, password, potentialUser);
if(status)
{
logger.info("Authenticated user '"~username~"'");
}
else
{
logger.error("Authentication failed for user '"~username~"'");
}
return status;
}
public static AuthManager create(Server server, AuthProvider provider = new DummyProvider())
{
AuthManager manager = new AuthManager(provider);
manager.server = server;
return manager;
}
}