2024-07-31 17:20:17 +00:00
|
|
|
/// API endpoints for authentication-related functions, like registration and login.
|
|
|
|
module auth.api;
|
|
|
|
|
|
|
|
import handy_httpd;
|
|
|
|
import handy_httpd.components.optional;
|
|
|
|
import slf4d;
|
|
|
|
|
|
|
|
import auth.model;
|
2024-08-01 17:01:50 +00:00
|
|
|
import auth.data;
|
2024-07-31 17:20:17 +00:00
|
|
|
import auth.service;
|
2024-08-01 17:01:50 +00:00
|
|
|
import auth.data_impl_fs;
|
2024-09-11 20:15:53 +00:00
|
|
|
import util.json;
|
2024-07-31 17:20:17 +00:00
|
|
|
|
|
|
|
void postLogin(ref HttpRequestContext ctx) {
|
2024-09-11 20:15:53 +00:00
|
|
|
LoginCredentials loginCredentials = readJsonPayload!LoginCredentials(ctx);
|
2024-07-31 17:20:17 +00:00
|
|
|
if (!validateUsername(loginCredentials.username)) {
|
|
|
|
ctx.response.status = HttpStatus.UNAUTHORIZED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UserRepository userRepo = new FileSystemUserRepository();
|
|
|
|
Optional!User optionalUser = userRepo.findByUsername(loginCredentials.username);
|
|
|
|
if (optionalUser.isNull) {
|
|
|
|
ctx.response.status = HttpStatus.UNAUTHORIZED;
|
2024-09-11 20:15:53 +00:00
|
|
|
return;
|
2024-07-31 17:20:17 +00:00
|
|
|
}
|
|
|
|
import botan.passhash.bcrypt : checkBcrypt;
|
2024-09-11 20:15:53 +00:00
|
|
|
|
2024-07-31 17:20:17 +00:00
|
|
|
if (!checkBcrypt(loginCredentials.password, optionalUser.value.passwordHash)) {
|
|
|
|
ctx.response.status = HttpStatus.UNAUTHORIZED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
string token = generateAccessToken(optionalUser.value);
|
2024-09-11 20:15:53 +00:00
|
|
|
writeJsonBody(ctx, TokenResponse(token));
|
2024-07-31 17:20:17 +00:00
|
|
|
}
|
|
|
|
|
2024-09-11 20:15:53 +00:00
|
|
|
void getUsernameAvailability(ref HttpRequestContext ctx) {
|
|
|
|
Optional!string username = ctx.request.queryParams.getFirst("username");
|
|
|
|
if (username.isNull) {
|
2024-07-31 17:20:17 +00:00
|
|
|
ctx.response.status = HttpStatus.BAD_REQUEST;
|
2024-09-11 20:15:53 +00:00
|
|
|
ctx.response.writeBodyString("Missing username parameter.");
|
2024-07-31 17:20:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-09-11 20:15:53 +00:00
|
|
|
UserRepository userRepo = new FileSystemUserRepository();
|
|
|
|
bool available = userRepo.findByUsername(username.value).isNull;
|
|
|
|
writeJsonBody(ctx, UsernameAvailabilityResponse(available));
|
|
|
|
}
|
|
|
|
|
|
|
|
void postRegister(ref HttpRequestContext ctx) {
|
|
|
|
RegistrationData registrationData = readJsonPayload!RegistrationData(ctx);
|
2024-07-31 17:20:17 +00:00
|
|
|
if (!validateUsername(registrationData.username)) {
|
|
|
|
ctx.response.status = HttpStatus.BAD_REQUEST;
|
|
|
|
ctx.response.writeBodyString("Invalid username.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!validatePassword(registrationData.password)) {
|
|
|
|
ctx.response.status = HttpStatus.BAD_REQUEST;
|
|
|
|
ctx.response.writeBodyString("Invalid password.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UserRepository userRepo = new FileSystemUserRepository();
|
|
|
|
if (!userRepo.findByUsername(registrationData.username).isNull) {
|
|
|
|
ctx.response.status = HttpStatus.BAD_REQUEST;
|
|
|
|
ctx.response.writeBodyString("Username is taken.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
import botan.passhash.bcrypt : generateBcrypt;
|
|
|
|
import botan.rng.auto_rng;
|
2024-09-11 20:15:53 +00:00
|
|
|
|
2024-07-31 17:20:17 +00:00
|
|
|
RandomNumberGenerator rng = new AutoSeededRNG();
|
|
|
|
string passwordHash = generateBcrypt(registrationData.password, rng, 12);
|
2024-09-11 20:15:53 +00:00
|
|
|
User user = userRepo.createUser(registrationData.username, passwordHash);
|
2024-07-31 17:20:17 +00:00
|
|
|
infoF!"Created user: %s"(registrationData.username);
|
2024-09-11 20:15:53 +00:00
|
|
|
string token = generateAccessToken(user);
|
|
|
|
writeJsonBody(ctx, TokenResponse(token));
|
2024-07-31 17:20:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void getMyUser(ref HttpRequestContext ctx) {
|
|
|
|
AuthContext auth = getAuthContext(ctx);
|
|
|
|
ctx.response.writeBodyString(auth.user.username);
|
|
|
|
}
|