-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added username and password validations
- Loading branch information
Showing
8 changed files
with
172 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...end/src/main/java/com/llm_service/llm_service/constraint/PasswordConstraintValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.llm_service.llm_service.constraint; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import java.util.Arrays; | ||
import org.passay.*; | ||
|
||
public class PasswordConstraintValidator implements ConstraintValidator<ValidPassword, String> { | ||
|
||
@Override | ||
public void initialize(ValidPassword arg0) {} | ||
|
||
@Override | ||
public boolean isValid(String password, ConstraintValidatorContext context) { | ||
PasswordValidator validator = new PasswordValidator(Arrays.asList( | ||
new LengthRule(8, 30), | ||
new CharacterRule(EnglishCharacterData.UpperCase, 1), | ||
new CharacterRule(EnglishCharacterData.LowerCase, 1), | ||
new CharacterRule(EnglishCharacterData.Digit, 1), | ||
new CharacterRule(EnglishCharacterData.Special, 1), | ||
new WhitespaceRule())); | ||
|
||
RuleResult result = validator.validate(new PasswordData(password)); | ||
if (result.isValid()) { | ||
return true; | ||
} | ||
context.disableDefaultConstraintViolation(); | ||
context.buildConstraintViolationWithTemplate(String.join(" ", validator.getMessages(result))) | ||
.addConstraintViolation(); | ||
return false; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...end/src/main/java/com/llm_service/llm_service/constraint/UsernameConstraintValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.llm_service.llm_service.constraint; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class UsernameConstraintValidator implements ConstraintValidator<ValidUsername, String> { | ||
@Override | ||
public void initialize(ValidUsername constraintAnnotation) {} | ||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
if (value == null) { | ||
return false; | ||
} | ||
|
||
Set<String> errorMessages = new HashSet<>(); | ||
|
||
if (!value.matches("^[A-Za-z].*")) { | ||
errorMessages.add("Username must start with a letter."); | ||
} | ||
|
||
if (!value.matches(".{8,30}")) { | ||
errorMessages.add("Username must be between 8 and 30 characters long."); | ||
} | ||
|
||
if (!value.matches("[A-Za-z0-9_]*")) { | ||
errorMessages.add("Username must consist of letters, digits, or underscores only."); | ||
} | ||
|
||
if (!errorMessages.isEmpty()) { | ||
context.disableDefaultConstraintViolation(); | ||
context.buildConstraintViolationWithTemplate(String.join(" ", errorMessages)) | ||
.addConstraintViolation(); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
backend/src/main/java/com/llm_service/llm_service/constraint/ValidPassword.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.llm_service.llm_service.constraint; | ||
|
||
import static java.lang.annotation.ElementType.*; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
@Documented | ||
@Constraint(validatedBy = PasswordConstraintValidator.class) | ||
@Target({TYPE, FIELD, ANNOTATION_TYPE}) | ||
@Retention(RUNTIME) | ||
public @interface ValidPassword { | ||
|
||
String message() default "Invalid Password"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
22 changes: 22 additions & 0 deletions
22
backend/src/main/java/com/llm_service/llm_service/constraint/ValidUsername.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.llm_service.llm_service.constraint; | ||
|
||
import static java.lang.annotation.ElementType.*; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
@Documented | ||
@Constraint(validatedBy = UsernameConstraintValidator.class) | ||
@Target({FIELD, METHOD, PARAMETER, ANNOTATION_TYPE}) | ||
@Retention(RUNTIME) | ||
public @interface ValidUsername { | ||
String message() default "Invalid username"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 7 additions & 4 deletions
11
backend/src/main/java/com/llm_service/llm_service/controller/user/UserRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...nd/src/main/java/com/llm_service/llm_service/controller/user/ValidationErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.llm_service.llm_service.controller.user; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import lombok.Builder; | ||
import lombok.extern.jackson.Jacksonized; | ||
|
||
@Builder | ||
@Jacksonized | ||
public record ValidationErrorResponse(Map<String, String> errors) { | ||
@JsonCreator | ||
public ValidationErrorResponse(@JsonProperty("errors") Map<String, String> errors) { | ||
this.errors = errors != null ? new HashMap<>(errors) : new HashMap<>(); | ||
} | ||
|
||
public void addError(String field, String message) { | ||
errors.put(field, message); | ||
} | ||
} |