42 lines
1.1 KiB
D
42 lines
1.1 KiB
D
|
/**
|
||
|
* Defines models for the Finnow API's authentication system.
|
||
|
*/
|
||
|
module auth.model;
|
||
|
|
||
|
/**
|
||
|
* The user is the basic authenticated entity representing someone who has one
|
||
|
* or more profiles.
|
||
|
*/
|
||
|
struct User {
|
||
|
const string username;
|
||
|
const string passwordHash;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Validates a username string.
|
||
|
* Params:
|
||
|
* username = The username to check.
|
||
|
* Returns: True if the username is valid.
|
||
|
*/
|
||
|
bool validateUsername(string username) {
|
||
|
import std.regex;
|
||
|
import std.uni : toLower;
|
||
|
if (username is null || username.length < 3) return false;
|
||
|
const string[] RESERVED_USERNAMES = ["user", "admin"];
|
||
|
static foreach (reservedUsername; RESERVED_USERNAMES) {
|
||
|
if (toLower(username) == reservedUsername) return false;
|
||
|
}
|
||
|
auto r = ctRegex!(`^[a-zA-Z]+[a-zA-Z0-9_]+$`);
|
||
|
return !matchFirst(username, r).empty;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Validates that a password is of sufficient complexity.
|
||
|
* Params:
|
||
|
* password = The password to check.
|
||
|
* Returns: True if the password is sufficiently complex.
|
||
|
*/
|
||
|
bool validatePassword(string password) {
|
||
|
return password !is null && password.length >= 8;
|
||
|
}
|