-
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.
Merge branch 'master' into feature/SHIBUI-1364-QA
- Loading branch information
Showing
31 changed files
with
1,427 additions
and
170 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
90 changes: 90 additions & 0 deletions
90
...oovy/edu/internet2/tier/shibboleth/admin/ui/jsonschema/LowLevelJsonSchemaValidator.groovy
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,90 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.jsonschema | ||
|
||
import mjson.Json | ||
import org.springframework.http.HttpInputMessage | ||
|
||
import static edu.internet2.tier.shibboleth.admin.ui.jsonschema.JsonSchemaLocationLookup.dynamicHttpMetadataProviderSchema | ||
import static edu.internet2.tier.shibboleth.admin.ui.jsonschema.JsonSchemaLocationLookup.entityAttributesFiltersSchema | ||
import static edu.internet2.tier.shibboleth.admin.ui.jsonschema.JsonSchemaLocationLookup.filesystemMetadataProviderSchema | ||
import static edu.internet2.tier.shibboleth.admin.ui.jsonschema.JsonSchemaLocationLookup.localDynamicMetadataProviderSchema | ||
import static edu.internet2.tier.shibboleth.admin.ui.jsonschema.JsonSchemaLocationLookup.nameIdFormatFilterSchema | ||
|
||
/** | ||
* Currently uses mjson library. | ||
* | ||
* @author Dmitriy Kopylenko | ||
*/ | ||
class LowLevelJsonSchemaValidator { | ||
|
||
static HttpInputMessage validatePayloadAgainstSchema(HttpInputMessage inputMessage, URI schemaUri) { | ||
def origInput = [inputMessage.body.bytes, inputMessage.headers] | ||
def json = extractJsonPayload(origInput) | ||
def schema = Json.schema(schemaUri) | ||
doValidate(origInput, schema, json) | ||
} | ||
|
||
static HttpInputMessage validateMetadataResolverTypePayloadAgainstSchema(HttpInputMessage inputMessage, | ||
JsonSchemaResourceLocationRegistry schemaRegistry) { | ||
|
||
def origInput = [inputMessage.body.bytes, inputMessage.headers] | ||
def json = extractJsonPayload(origInput) | ||
def schemaUri = null | ||
switch (json.asMap()['@type']) { | ||
case 'LocalDynamicMetadataResolver': | ||
schemaUri = localDynamicMetadataProviderSchema(schemaRegistry).uri | ||
break | ||
case 'DynamicHttpMetadataResolver': | ||
schemaUri = dynamicHttpMetadataProviderSchema(schemaRegistry).uri | ||
break | ||
case 'FilesystemMetadataResolver': | ||
schemaUri = filesystemMetadataProviderSchema(schemaRegistry).uri | ||
break | ||
default: | ||
break | ||
} | ||
if (!schemaUri) { | ||
return newInputMessage(origInput) | ||
} | ||
doValidate(origInput, Json.schema(schemaUri), json) | ||
} | ||
|
||
static HttpInputMessage validateMetadataFilterTypePayloadAgainstSchema(HttpInputMessage inputMessage, | ||
JsonSchemaResourceLocationRegistry schemaRegistry) { | ||
def origInput = [inputMessage.body.bytes, inputMessage.headers] | ||
def json = extractJsonPayload(origInput) | ||
def schemaUri = null | ||
switch (json.asMap()['@type']) { | ||
case 'EntityAttributes': | ||
schemaUri = entityAttributesFiltersSchema(schemaRegistry).uri | ||
break | ||
case 'NameIDFormat': | ||
schemaUri = nameIdFormatFilterSchema(schemaRegistry).uri | ||
break | ||
default: | ||
break | ||
} | ||
if (!schemaUri) { | ||
return newInputMessage(origInput) | ||
} | ||
doValidate(origInput, Json.schema(schemaUri), json) | ||
} | ||
|
||
private static Json extractJsonPayload(List origInput) { | ||
Json.read(new ByteArrayInputStream(origInput[0]).getText()) | ||
} | ||
|
||
private static HttpInputMessage doValidate(List origInput, Json.Schema schema, Json json) { | ||
def validationResult = schema.validate(json) | ||
if (!validationResult.at('ok')) { | ||
throw new JsonSchemaValidationFailedException(validationResult.at('errors').asList()) | ||
} | ||
newInputMessage(origInput) | ||
} | ||
|
||
private static newInputMessage(origInput) { | ||
[ | ||
getBody : { new ByteArrayInputStream(origInput[0]) }, | ||
getHeaders: { origInput[1] } | ||
] as HttpInputMessage | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...ier/shibboleth/admin/ui/jsonschema/MetadataFiltersSchemaValidatingControllerAdvice.groovy
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,40 @@ | ||
package edu.internet2.tier.shibboleth.admin.ui.jsonschema | ||
|
||
import edu.internet2.tier.shibboleth.admin.ui.domain.filters.MetadataFilter | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.core.MethodParameter | ||
import org.springframework.http.HttpInputMessage | ||
import org.springframework.http.converter.HttpMessageConverter | ||
import org.springframework.web.bind.annotation.ControllerAdvice | ||
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter | ||
|
||
import java.lang.reflect.Type | ||
|
||
import static edu.internet2.tier.shibboleth.admin.ui.jsonschema.LowLevelJsonSchemaValidator.validateMetadataFilterTypePayloadAgainstSchema | ||
|
||
/** | ||
* Controller advice implementation for validating metadata filters payload coming from UI layer | ||
* against pre-defined JSON schema for their respected types. Choosing of the appropriate schema based on incoming | ||
* resolver types is delegated to @{LowLevelJsonSchemaValidator}. | ||
* | ||
* @author Dmitriy Kopylenko | ||
*/ | ||
@ControllerAdvice | ||
class MetadataFiltersSchemaValidatingControllerAdvice extends RequestBodyAdviceAdapter { | ||
|
||
@Autowired | ||
JsonSchemaResourceLocationRegistry jsonSchemaResourceLocationRegistry | ||
|
||
@Override | ||
boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { | ||
targetType.typeName == MetadataFilter.typeName | ||
} | ||
|
||
@Override | ||
HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, | ||
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) | ||
throws IOException { | ||
|
||
validateMetadataFilterTypePayloadAgainstSchema(inputMessage, jsonSchemaResourceLocationRegistry) | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...r/shibboleth/admin/ui/jsonschema/MetadataResolversSchemaValidatingControllerAdvice.groovy
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 edu.internet2.tier.shibboleth.admin.ui.jsonschema | ||
|
||
|
||
import edu.internet2.tier.shibboleth.admin.ui.domain.resolvers.MetadataResolver | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.core.MethodParameter | ||
import org.springframework.http.HttpInputMessage | ||
import org.springframework.http.converter.HttpMessageConverter | ||
import org.springframework.web.bind.annotation.ControllerAdvice | ||
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter | ||
|
||
import java.lang.reflect.Type | ||
|
||
import static edu.internet2.tier.shibboleth.admin.ui.jsonschema.LowLevelJsonSchemaValidator.validateMetadataResolverTypePayloadAgainstSchema | ||
|
||
/** | ||
* Controller advice implementation for validating metadata resolvers payload coming from UI layer | ||
* against pre-defined JSON schema for their respected types. Choosing of the appropriate schema based on incoming | ||
* resolver types is delegated to @{LowLevelJsonSchemaValidator}. | ||
* | ||
* @author Dmitriy Kopylenko | ||
*/ | ||
@ControllerAdvice | ||
class MetadataResolversSchemaValidatingControllerAdvice extends RequestBodyAdviceAdapter { | ||
|
||
@Autowired | ||
JsonSchemaResourceLocationRegistry jsonSchemaResourceLocationRegistry | ||
|
||
@Override | ||
boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { | ||
targetType.typeName == MetadataResolver.typeName | ||
} | ||
|
||
@Override | ||
HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, | ||
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) | ||
throws IOException { | ||
|
||
validateMetadataResolverTypePayloadAgainstSchema(inputMessage, jsonSchemaResourceLocationRegistry) | ||
} | ||
} |
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
Oops, something went wrong.