Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ES-936] Handled captcha token null error #647

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mosip.esignet.header-filter.paths-to-validate={'${server.servlet.path}/authoriza

#This property is used for captcha validation and allowed values are send-otp and pwd.
#captcha validation is enabled for send-otp and pwd.
mosip.esignet.captcha.required=send-otp,pwd
mosip.esignet.captcha.required=pwd

## ------------------------------------------ e-Signet binding ---------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,14 @@ public class AuthorizationHelperService {
private List<String> credentialScopes;

protected void validateSendOtpCaptchaToken(String captchaToken) {
if(captchaRequired.contains("send-otp")) {
if(!captchaRequired.contains("send-otp")) {
log.warn("captcha validation is disabled for send-otp request!");
return;
}
if(!StringUtils.hasText(captchaToken)) {
log.error("Captcha token is Null or Empty");
throw new EsignetException(ErrorConstants.INVALID_CAPTCHA);
}
validateCaptchaToken(captchaToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,39 @@ public class AuthorizationHelperServiceTest {

@Mock
private CaptchaValidator captchaValidator;

@Test
public void validateSendOtpCaptchaToken_withEmptyToken_thenFail() {
ReflectionTestUtils.setField(authorizationHelperService, "captchaRequired", List.of("send-otp"));
try {
authorizationHelperService.validateSendOtpCaptchaToken("");
} catch(EsignetException e) {
Assert.assertEquals(ErrorConstants.INVALID_CAPTCHA, e.getErrorCode());
}
}

@Test
public void validateSendOtpCaptchaToken_withValidToken_thenFail() {
ReflectionTestUtils.setField(authorizationHelperService, "captchaRequired", List.of("send-otp"));
ReflectionTestUtils.setField(authorizationHelperService, "captchaValidator", captchaValidator);
Mockito.when(captchaValidator.validateCaptcha(Mockito.anyString())).thenReturn(false);
try {
authorizationHelperService.validateSendOtpCaptchaToken("captcha-token");
} catch(EsignetException e) {
Assert.assertEquals(ErrorConstants.INVALID_CAPTCHA, e.getErrorCode());
}
}

@Test
public void validateSendOtpCaptchaToken_withValidToken_thenPass() {
ReflectionTestUtils.setField(authorizationHelperService, "captchaRequired", List.of("send-otp"));
ReflectionTestUtils.setField(authorizationHelperService, "captchaValidator", captchaValidator);
Mockito.when(captchaValidator.validateCaptcha(Mockito.anyString())).thenReturn(true);
authorizationHelperService.validateSendOtpCaptchaToken("captcha-token");
}

@Test
public void validateCaptchaToken_withNoValidator_thenFail() {
ReflectionTestUtils.setField(authorizationHelperService, "captchaRequired", List.of("send-otp"));
ReflectionTestUtils.setField(authorizationHelperService, "captchaValidator", null);
try {
authorizationHelperService.validateCaptchaToken("captcha-token");
Expand All @@ -83,7 +112,6 @@ public void validateCaptchaToken_withNoValidator_thenFail() {

@Test
public void validateCaptchaToken_withInvalidToken_thenFail() {
ReflectionTestUtils.setField(authorizationHelperService, "captchaRequired", List.of("send-otp"));
ReflectionTestUtils.setField(authorizationHelperService, "captchaValidator", captchaValidator);
Mockito.when(captchaValidator.validateCaptcha(Mockito.anyString())).thenReturn(false);
try {
Expand All @@ -93,10 +121,9 @@ public void validateCaptchaToken_withInvalidToken_thenFail() {
Assert.assertEquals(ErrorConstants.INVALID_CAPTCHA, e.getErrorCode());
}
}

@Test
public void validateCaptchaToken_withValidToken_thenPass() {
ReflectionTestUtils.setField(authorizationHelperService, "captchaRequired", List.of("send-otp"));
ReflectionTestUtils.setField(authorizationHelperService, "captchaValidator", captchaValidator);
Mockito.when(captchaValidator.validateCaptcha(Mockito.anyString())).thenReturn(true);
authorizationHelperService.validateCaptchaToken("captcha-token");
Expand Down
Loading