Skip to content

Commit

Permalink
[ES-1424] (#846)
Browse files Browse the repository at this point in the history
Signed-off-by: Venkata Saidurga Polamraju <[email protected]>
  • Loading branch information
pvsaidurga authored Aug 14, 2024
1 parent 23c1629 commit f068bec
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ public class ErrorConstants {
public static final String INVALID_PATH_FRAGMENT = "invalid_path_fragment";
public static final String INVALID_VERIFICATION = "invalid_verification";
public static final String INVALID_VERIFIED_CLAIMS = "invalid_verified_claims";
public static final String INVALID_PURPOSE="invalid_purpose";
}
19 changes: 19 additions & 0 deletions esignet-integration-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

<properties>
<java.version>11</java.version>
<junit.version>4.13.2</junit.version>
<mockito.version>2.22.0</mockito.version>
</properties>

<dependencies>
Expand All @@ -40,6 +42,23 @@
<artifactId>jsonld-common-java</artifactId>
<version>0.5.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.mosip.esignet.api.dto.claim;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.mosip.esignet.api.validator.Purpose;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -23,6 +24,7 @@ public class ClaimDetail extends VerifiedClaimDetail implements Serializable {
private String[] values;
private boolean essential;

@Purpose
private String purpose;

public ClaimDetail(String value, String[] values, boolean essential) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.mosip.esignet.api.validator;

import io.mosip.esignet.api.util.ErrorConstants;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target({FIELD, TYPE_USE})
@Retention(RUNTIME)
@Constraint(validatedBy = PurposeValidator.class)
@Documented
public @interface Purpose {
String message() default ErrorConstants.INVALID_PURPOSE;

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.mosip.esignet.api.validator;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class PurposeValidator implements ConstraintValidator<Purpose, String> {

@Value("${mosip.esignet.claim-detail.purpose.min-length}")
private int minLength;

@Value("${mosip.esignet.claim-detail.purpose.max-length}")
private int maxLength;

@Override
public boolean isValid(String purpose, ConstraintValidatorContext constraintValidatorContext) {
int length = StringUtils.hasText(purpose) ? purpose.length() : 0;
return length >= minLength && length <= maxLength;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.mosip.esignet.api.validator;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.util.ReflectionTestUtils;

import javax.validation.ConstraintValidatorContext;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

@RunWith(MockitoJUnitRunner.class)
public class ValidatorTest {

@InjectMocks
private PurposeValidator purposeValidator;

@Mock
private ConstraintValidatorContext constraintValidatorContext;

@Before
public void setUp() {
ReflectionTestUtils.setField(purposeValidator, "minLength", 3);
ReflectionTestUtils.setField(purposeValidator, "maxLength", 300);
}

@Test
public void testIsValid_WithValidPurpose_thenPass() {
String purpose = "Purpose";
boolean isValid = purposeValidator.isValid(purpose, constraintValidatorContext);
assertTrue(isValid);
}

@Test
public void testIsValid_WithPurposeWithInvalidLength_thenFail() {
String purpose = "In";
boolean isValid = purposeValidator.isValid(purpose, constraintValidatorContext);
assertFalse(isValid);
}

@Test
public void testIsValid_WithNullPurpose_theFail() {
String purpose = null;
boolean isValid = purposeValidator.isValid(purpose, constraintValidatorContext);
assertFalse(isValid);
}

@Test
public void testIsValid_WithEmptyPurpose_thenFail() {
String purpose = "";
boolean isValid = purposeValidator.isValid(purpose, constraintValidatorContext);
assertFalse(isValid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ mosip.esignet.auth-challenge.IDT.format=base64url-encoded-json
mosip.esignet.auth-challenge.IDT.min-length=20
mosip.esignet.auth-challenge.IDT.max-length=2000

mosip.esignet.claim-detail.purpose.min-length=3
mosip.esignet.claim-detail.purpose.max-length=300


## Endpoints required to have oauth-details-hash and oauth-details-key HTTP header
mosip.esignet.header-filter.paths-to-validate={'${server.servlet.path}/authorization/send-otp', \
'${server.servlet.path}/authorization/authenticate', \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ mosip.esignet.auth-challenge.PIN.format=number
mosip.esignet.auth-challenge.PIN.min-length=4
mosip.esignet.auth-challenge.PIN.max-length=5

mosip.esignet.claim-detail.purpose.min-length=3
mosip.esignet.claim-detail.purpose.max-length=300

mosip.esignet.header-filter.paths-to-validate={'${server.servlet.path}/authorization/send-otp', \
'${server.servlet.path}/authorization/authenticate', \
'${server.servlet.path}/authorization/auth-code'}
Expand Down

0 comments on commit f068bec

Please sign in to comment.