-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
15 changed files
with
202 additions
and
10 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
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
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
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
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
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
package com.wultra.security.powerauth.fido2.controller.request; | ||
|
||
import com.wultra.security.powerauth.fido2.controller.validation.EmailConditional; | ||
import jakarta.validation.constraints.NotBlank; | ||
|
||
/** | ||
|
@@ -26,7 +27,7 @@ | |
* @author Jan Pesek, [email protected] | ||
*/ | ||
public record RegistrationOptionsRequest( | ||
@NotBlank | ||
@NotBlank @EmailConditional | ||
String userId, | ||
|
||
@NotBlank | ||
|
43 changes: 43 additions & 0 deletions
43
...main/java/com/wultra/security/powerauth/fido2/controller/validation/EmailConditional.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,43 @@ | ||
/* | ||
* PowerAuth test and related software components | ||
* Copyright (C) 2024 Wultra s.r.o. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.wultra.security.powerauth.fido2.controller.validation; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Validation annotation to validate email address. Allow null or empty values. | ||
* | ||
* @author Jan Pesek, [email protected] | ||
*/ | ||
@Constraint(validatedBy = EmailConditionalValidator.class) | ||
@Target({ ElementType.FIELD, ElementType.PARAMETER }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface EmailConditional { | ||
String message() default "Invalid email address."; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
50 changes: 50 additions & 0 deletions
50
.../com/wultra/security/powerauth/fido2/controller/validation/EmailConditionalValidator.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,50 @@ | ||
/* | ||
* PowerAuth test and related software components | ||
* Copyright (C) 2024 Wultra s.r.o. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.wultra.security.powerauth.fido2.controller.validation; | ||
|
||
import com.wultra.security.powerauth.fido2.configuration.PowerAuthFido2TestsConfigProperties; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import lombok.AllArgsConstructor; | ||
import org.hibernate.validator.internal.constraintvalidators.AbstractEmailValidator; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Validator to validate email address. Allow null or empty values. | ||
* | ||
* @author Jan Pesek, [email protected] | ||
*/ | ||
@AllArgsConstructor | ||
public class EmailConditionalValidator extends AbstractEmailValidator<EmailConditional> { | ||
|
||
private static final Pattern GENERIC_EMAIL_PATTERN = Pattern.compile("[^@\\s]+@[^@\\s]+\\.[^@\\s]+"); | ||
|
||
private final PowerAuthFido2TestsConfigProperties powerAuthFido2TestsConfigProperties; | ||
|
||
@Override | ||
public boolean isValid(final CharSequence value, final ConstraintValidatorContext context) { | ||
if (!StringUtils.hasLength(value) || !powerAuthFido2TestsConfigProperties.isEmailAddressRequired()) { | ||
return true; | ||
} | ||
|
||
return super.isValid(value, context) && GENERIC_EMAIL_PATTERN.matcher(value).matches(); | ||
} | ||
|
||
} |
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
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
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
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
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
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
75 changes: 75 additions & 0 deletions
75
.../wultra/security/powerauth/fido2/controller/validation/EmailConditionalValidatorTest.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,75 @@ | ||
/* | ||
* PowerAuth test and related software components | ||
* Copyright (C) 2024 Wultra s.r.o. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.wultra.security.powerauth.fido2.controller.validation; | ||
|
||
import com.wultra.security.powerauth.fido2.configuration.PowerAuthFido2TestsConfigProperties; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.NullAndEmptySource; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* Test of {@link EmailConditionalValidator}. | ||
* | ||
* @author Jan Pesek, [email protected] | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
class EmailConditionalValidatorTest { | ||
|
||
@Mock(strictness = Mock.Strictness.LENIENT) | ||
private PowerAuthFido2TestsConfigProperties properties; | ||
|
||
@InjectMocks | ||
private EmailConditionalValidator tested; | ||
|
||
@ParameterizedTest | ||
@NullAndEmptySource | ||
@ValueSource(strings = {"[email protected]", "[email protected]"}) | ||
void testValidation_emailRequired_validExamples(final String input) { | ||
when(properties.isEmailAddressRequired()).thenReturn(true); | ||
assertTrue(isValid(input)); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"abcd@abc", " "}) | ||
void testValidation_emailRequired_invalidExamples(final String input) { | ||
when(properties.isEmailAddressRequired()).thenReturn(true); | ||
assertFalse(isValid(input)); | ||
} | ||
|
||
@ParameterizedTest | ||
@NullAndEmptySource | ||
@ValueSource(strings = {"username", " ", "[email protected]"}) | ||
void testValidation_emailNotRequired(final String input) { | ||
when(properties.isEmailAddressRequired()).thenReturn(false); | ||
assertTrue(isValid(input)); | ||
} | ||
|
||
private boolean isValid(final String parameter) { | ||
return tested.isValid(parameter, null); | ||
} | ||
|
||
} |