Skip to content

Commit

Permalink
relying party overrides validation against JSON schema
Browse files Browse the repository at this point in the history
  • Loading branch information
dima767 committed Oct 15, 2018
1 parent e6c5320 commit 76206ce
Show file tree
Hide file tree
Showing 6 changed files with 466 additions and 440 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package edu.internet2.tier.shibboleth.admin.ui.jsonschema

/**
* Indicates JSON schema validation failure. Encapsulates a list of error messages produced by JSON schema validator
* component.
*
* @author Dmitriy Kopylenko
*/
class JsonSchemaValidationFailedException extends RuntimeException {

def errors

JsonSchemaValidationFailedException(List<String> errors) {
this.errors = errors
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package edu.internet2.tier.shibboleth.admin.ui.jsonschema

import com.fasterxml.jackson.databind.ObjectMapper
import edu.internet2.tier.shibboleth.admin.ui.domain.frontend.EntityDescriptorRepresentation
import mjson.Json
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.core.MethodParameter
import org.springframework.http.HttpInputMessage
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.http.converter.HttpMessageConverter
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.context.request.WebRequest
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter

import java.lang.reflect.Type
Expand All @@ -16,9 +25,31 @@ import java.lang.reflect.Type
@ControllerAdvice
class RelyingPartyOverridesJsonSchemaValidatingControllerAdvice extends RequestBodyAdviceAdapter {

@Autowired
MetadataSourcesJsonSchemaResourceLocation schemaLocation

@Autowired
ObjectMapper jacksonMapper

@Override
boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
def cls = targetType.typeName
print('cx')
targetType.typeName == EntityDescriptorRepresentation.typeName
}

@Override
Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
def relyingPartyOverrides = EntityDescriptorRepresentation.cast(body).relyingPartyOverrides
def relyingPartyOverridesJson = Json.make([relyingPartyOverrides: relyingPartyOverrides])
def schema = Json.schema(this.schemaLocation.uri)
def validationResult = schema.validate(relyingPartyOverridesJson)
if (!validationResult.at('ok')) {
throw new JsonSchemaValidationFailedException(validationResult.at('errors').asList())
}
body
}

@ExceptionHandler(JsonSchemaValidationFailedException)
final ResponseEntity<?> handleUserNotFoundException(JsonSchemaValidationFailedException ex, WebRequest request) {
new ResponseEntity<>([errors: ex.errors], HttpStatus.BAD_REQUEST)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,4 @@ public class JsonSchemaValidationComponentsConfiguration {
public MetadataSourcesJsonSchemaResourceLocation metadataSourcesJsonSchemaResourceLocation(ResourceLoader resourceLoader) {
return new MetadataSourcesJsonSchemaResourceLocation(resourceLoader);
}

@Bean
public RelyingPartyOverridesJsonSchemaValidatingControllerAdvice relyingPartyOverridesJsonSchemaValidatingControllerAdvice() {
return new RelyingPartyOverridesJsonSchemaValidatingControllerAdvice();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public MetadataSourcesJsonSchemaResourceLocation(ResourceLoader resourceLoader)
this.resourceLoader = resourceLoader;
}

public void setMetadataSourcesUiSchemaLocation(String metadataSourcesUiSchemaLocation) {
this.metadataSourcesUiSchemaLocation = metadataSourcesUiSchemaLocation;
}

public URL getUrl() {
return this.jsonSchemaUrl;
}
Expand Down
Loading

0 comments on commit 76206ce

Please sign in to comment.