Initial commit for site for forms

This commit is contained in:
2024-01-13 16:24:54 +01:00
commit 2b908b4e94
39 changed files with 1649 additions and 0 deletions
@@ -0,0 +1,84 @@
package com.alttd.forms.verify_mail;
import com.alttd.forms.database.DatabaseConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
public class FormQuery {
private static final Logger logger = LoggerFactory.getLogger(FormQuery.class);
public static Optional<Integer> getFormId(Connection connection, int verificationCode, String eMail) throws SQLException {
String sql = "SELECT formId FROM verify_form WHERE verification_code = ? AND e_mail = ?";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setInt(1, verificationCode);
stmt.setString(2, eMail);
ResultSet resultSet = stmt.executeQuery();
if (!resultSet.next()) {
logger.warn("A user tried to enter an invalid code: " + verificationCode + " with email: " + eMail);
return Optional.empty();
}
return Optional.of(resultSet.getInt("formId"));
} catch (SQLException e) {
logger.error("Failed select form query for verification code: " + verificationCode + " with e-mail " + eMail, e);
throw e;
}
}
private static Optional<String> getFormForId(Connection connection, int formId) throws SQLException {
String sql = "SELECT form_json FROM form WHERE formId = ?";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setInt(1, formId);
ResultSet resultSet = stmt.executeQuery();
if (!resultSet.next()) {
logger.warn("Could not find form with id: " + formId);
return Optional.empty();
}
return Optional.of(resultSet.getString("form_json"));
} catch (SQLException e) {
logger.error("Failed select form query for form with id: " + formId, e);
throw e;
}
}
public static CompletableFuture<FormQueryResult> getFormForCode(String verificationCode, String eMail) {
Connection connection = DatabaseConnection.getConnection();
int code;
try {
code = Integer.parseInt(verificationCode);
} catch (NumberFormatException e) {
return CompletableFuture.completedFuture(new FormQueryResult(Optional.empty(), "Invalid code (not a number)"));
}
return CompletableFuture.supplyAsync(() -> {
Optional<Integer> formId;
try {
formId = getFormId(connection, code, eMail);
} catch (SQLException e) {
throw new RuntimeException(e);
}
if (formId.isEmpty()) {
return new FormQueryResult(Optional.empty(), "Unable to find form for a user with this code and e-mail");
}
try {
return getFormForId(connection, formId.get())
.map(formJson -> new FormQueryResult(Optional.of(formJson), "Success"))
.orElse(new FormQueryResult(Optional.empty(), "Unable to find your form"));
} catch (SQLException e) {
throw new RuntimeException(e);
}
});
}
}
@@ -0,0 +1,6 @@
package com.alttd.forms.verify_mail;
import java.util.Optional;
public record FormQueryResult(Optional<String> formJson, String failReason) {
}
@@ -0,0 +1,28 @@
package com.alttd.forms.verify_mail;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
public class VerificationData {
public VerificationData(String code, String eMail) {
this.code = code;
this.eMail = eMail;
}
@NotBlank(message = "You must provide a valid code")
String code;
@NotBlank(message = "You must provide an email")
@Email(regexp = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)])",
message = "This is not a valid e-mail address")
String eMail;
@Override
public String toString() {
return "VerificationData{" +
"code='" + code + '\'' +
", eMail='" + eMail + '\'' +
'}';
}
}
@@ -0,0 +1,33 @@
package com.alttd.forms.verify_mail;
import com.alttd.forms.mail.mail_forms.MailForm;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.CompletableFuture;
@RestController
@RequestMapping("/api/verify_email")
public class VerifyController {
private static final Logger logger = LoggerFactory.getLogger(VerifyController.class);
@PostMapping("/form")
public CompletableFuture<ResponseEntity<String>> validateEmailFromForm(@Valid @RequestBody VerificationData verificationData) {
logger.debug(verificationData.toString());
return FormQuery.getFormForCode(verificationData.code, verificationData.eMail).thenApply(form -> form.formJson()
.map(body -> {
MailForm.sendForm("[email protected]", body);
return ResponseEntity.ok(body);
})
.orElse(ResponseEntity.ok(form.failReason()))
).exceptionally(throwable -> ResponseEntity.internalServerError()
.body("The server was unable to process your request, if this issue persists please contact [email protected]"));
}
}