-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added schema validation for IdentityData
Signed-off-by: Mohd Kaif Siddique <[email protected]>
- Loading branch information
Mohd Kaif Siddique
committed
Nov 25, 2024
1 parent
ca033e8
commit eec6ef1
Showing
10 changed files
with
739 additions
and
3 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
23 changes: 23 additions & 0 deletions
23
...y-system/src/main/java/io/mosip/esignet/mock/identitysystem/validator/IdentitySchema.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 io.mosip.esignet.mock.identitysystem.validator; | ||
|
||
|
||
import io.mosip.esignet.mock.identitysystem.util.ErrorConstants; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.TYPE_USE, ElementType.FIELD, ElementType.PARAMETER}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = IdentitySchemaValidator.class) | ||
public @interface IdentitySchema { | ||
|
||
String message() default ErrorConstants.INVALID_IDENTITY_DATA; | ||
Class<?>[] groups() default {}; | ||
Class<? extends Payload>[] payload() default {}; | ||
|
||
boolean isCreate(); | ||
} |
109 changes: 109 additions & 0 deletions
109
...src/main/java/io/mosip/esignet/mock/identitysystem/validator/IdentitySchemaValidator.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,109 @@ | ||
package io.mosip.esignet.mock.identitysystem.validator; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.networknt.schema.JsonSchema; | ||
import com.networknt.schema.JsonSchemaFactory; | ||
import com.networknt.schema.SpecVersion; | ||
import com.networknt.schema.ValidationMessage; | ||
import io.mosip.esignet.mock.identitysystem.dto.IdentityData; | ||
import io.mosip.esignet.mock.identitysystem.dto.RequestWrapper; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.stereotype.Component; | ||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Set; | ||
|
||
@Component | ||
@Slf4j | ||
public class IdentitySchemaValidator implements ConstraintValidator<IdentitySchema, Object> { | ||
|
||
@Value("${mosip.mock.ida.identity.create.schema.url}") | ||
private String createSchemaUrl; | ||
|
||
@Value("${mosip.mock.ida.identity.update.schema.url}") | ||
private String updateSchemaUrl; | ||
private boolean isCreate; | ||
|
||
private volatile JsonSchema cachedCreateSchema; | ||
|
||
private volatile JsonSchema cachedUpdateSchema; | ||
|
||
@Autowired | ||
ObjectMapper objectMapper; | ||
|
||
@Autowired | ||
ResourceLoader resourceLoader; | ||
|
||
@Override | ||
public void initialize(IdentitySchema constraintAnnotation) { | ||
this.isCreate = constraintAnnotation.isCreate(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(Object object, ConstraintValidatorContext context) { | ||
if (!(object instanceof RequestWrapper)) { | ||
return false; | ||
} | ||
RequestWrapper wrapper= (RequestWrapper) object; | ||
Object requestObject = wrapper.getRequest(); | ||
if (!(requestObject instanceof IdentityData)) { | ||
context.disableDefaultConstraintViolation(); | ||
context.buildConstraintViolationWithTemplate("Invalid request object") | ||
.addPropertyNode("request") | ||
.addConstraintViolation(); | ||
return false; | ||
} | ||
IdentityData identityData=(IdentityData) requestObject; | ||
JsonNode identityJsonNode = objectMapper.valueToTree(identityData); | ||
Set<ValidationMessage> errors = isCreate | ||
? getCachedCreateSchema().validate(identityJsonNode) | ||
: getCachedUpdateSchema().validate(identityJsonNode); | ||
|
||
if (!errors.isEmpty()) { | ||
log.error("Validation failed for claims: {}", errors); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private JsonSchema getCachedCreateSchema() { | ||
if(cachedCreateSchema !=null ) return cachedCreateSchema; | ||
synchronized (this) { | ||
if (cachedCreateSchema == null) { | ||
InputStream schemaResponse = getResource(createSchemaUrl); | ||
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012); | ||
cachedCreateSchema = jsonSchemaFactory.getSchema(schemaResponse); | ||
} | ||
} | ||
return cachedCreateSchema; | ||
} | ||
|
||
private JsonSchema getCachedUpdateSchema() { | ||
if(cachedUpdateSchema !=null ) return cachedUpdateSchema; | ||
synchronized (this) { | ||
if (cachedUpdateSchema == null) { | ||
InputStream schemaResponse = getResource(updateSchemaUrl); | ||
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012); | ||
cachedUpdateSchema = jsonSchemaFactory.getSchema(schemaResponse); | ||
} | ||
} | ||
return cachedUpdateSchema; | ||
} | ||
|
||
private InputStream getResource(String url) { | ||
try{ | ||
Resource resource = resourceLoader.getResource(url); | ||
return resource.getInputStream(); | ||
}catch (IOException e){ | ||
log.error("Failed to parse data: {}", url, e); | ||
} | ||
throw new RuntimeException("invalid_configuration"); | ||
} | ||
} |
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
6 changes: 6 additions & 0 deletions
6
mock-identity-system/src/main/resources/application-local.properties
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
Oops, something went wrong.