finnow/finnow-api/source/auth/dto.d

46 lines
1.2 KiB
D

/// Defines data-transfer objects for the API's authentication mechanisms.
module auth.dto;
import handy_httpd;
import std.json;
struct LoginCredentials {
string username;
string password;
static LoginCredentials parse(JSONValue obj) {
if (
obj.type != JSONType.OBJECT ||
"username" !in obj.object ||
"password" !in obj.object ||
obj.object["username"].type != JSONType.STRING ||
obj.object["password"].type != JSONType.STRING
) {
throw new HttpStatusException(HttpStatus.BAD_REQUEST, "Malformed login credentials.");
}
return LoginCredentials(
obj.object["username"].str,
obj.object["password"].str
);
}
}
struct TokenResponse {
string token;
string toJson() {
JSONValue obj = JSONValue.emptyObject;
obj.object["token"] = JSONValue(token);
return obj.toString();
}
}
struct RegistrationData {
string username;
string password;
static RegistrationData parse(JSONValue obj) {
LoginCredentials lc = LoginCredentials.parse(obj);
return RegistrationData(lc.username, lc.password);
}
}