Skip to content

Commit

Permalink
[SHIBUI-1226]
Browse files Browse the repository at this point in the history
update backing service
update controller to use newly implemented service method
ignore update test for now
  • Loading branch information
jj committed Feb 13, 2019
1 parent 880100b commit 2396bd7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,10 @@ public ResponseEntity<?> update(@RequestBody EntityDescriptorRepresentation edRe
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
}

EntityDescriptor updatedEd =
EntityDescriptor.class.cast(entityDescriptorService.createDescriptorFromRepresentation(edRepresentation));
entityDescriptorService.updateDescriptorFromRepresentation(existingEd, edRepresentation);
existingEd = entityDescriptorRepository.save(existingEd);

updatedEd.setAudId(existingEd.getAudId());
updatedEd.setResourceId(existingEd.getResourceId());
updatedEd.setCreatedDate(existingEd.getCreatedDate());

updatedEd = entityDescriptorRepository.save(updatedEd);

return ResponseEntity.ok().body(entityDescriptorService.createRepresentationFromDescriptor(updatedEd));
return ResponseEntity.ok().body(entityDescriptorService.createRepresentationFromDescriptor(existingEd));
} else {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new ErrorResponse(HttpStatus.FORBIDDEN,
"You are not authorized to perform the requested operation."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,23 @@ public JPAEntityDescriptorServiceImpl(OpenSamlObjects openSamlObjects, EntitySer
this.userService = userService;
}

@Override
public void updateDescriptorFromRepresentation(org.opensaml.saml.saml2.metadata.EntityDescriptor entityDescriptor, EntityDescriptorRepresentation representation) {
if (!(entityDescriptor instanceof EntityDescriptor)) {
throw new UnsupportedOperationException("not yet implemented");
}
buildDescriptorFromRepresentation((EntityDescriptor) entityDescriptor, representation);
}

@Override
public EntityDescriptor createDescriptorFromRepresentation(final EntityDescriptorRepresentation representation) {
EntityDescriptor ed = openSamlObjects.buildDefaultInstanceOfType(EntityDescriptor.class);
ed.setEntityID(representation.getEntityId());
/*
User user = userService.getCurrentUser();
if (user != null) {
ed.setCreatedBy(user.getUsername());
} else {
LOGGER.warn("Current user was null! Who is logged in?");
}
*/

return buildDescriptorFromRepresentation(ed, representation);
}

private EntityDescriptor buildDescriptorFromRepresentation(final EntityDescriptor ed, final EntityDescriptorRepresentation representation) {
// setup SPSSODescriptor
if (representation.getServiceProviderSsoDescriptor() != null) {
SPSSODescriptor spssoDescriptor = getSPSSODescriptorFromEntityDescriptor(ed);
Expand All @@ -123,6 +127,8 @@ public EntityDescriptor createDescriptorFromRepresentation(final EntityDescripto
spssoDescriptor.getNameIDFormats().add(nameIDFormat);
}
}
} else {
ed.setRoleDescriptors(null);
}

ed.setServiceProviderName(representation.getServiceProviderName());
Expand All @@ -149,6 +155,8 @@ public EntityDescriptor createDescriptorFromRepresentation(final EntityDescripto
organization.getURLs().add(organizationURL);

ed.setOrganization(organization);
} else {
ed.setOrganization(null);
}

// set up contacts
Expand All @@ -168,6 +176,8 @@ public EntityDescriptor createDescriptorFromRepresentation(final EntityDescripto

ed.addContactPerson(contactPerson);
}
} else {
ed.getContactPersons().clear();
}

// set up mdui
Expand All @@ -179,27 +189,43 @@ public EntityDescriptor createDescriptorFromRepresentation(final EntityDescripto
getUIInfo(ed).addDisplayName(displayName);
displayName.setValue(mduiRepresentation.getDisplayName());
displayName.setXMLLang("en");
} else {
if (getUIInfo(ed).getXMLObjects(DisplayName.DEFAULT_ELEMENT_NAME).size() > 0) {
getUIInfo(ed).getXMLObjects().remove(getUIInfo(ed).getXMLObjects(DisplayName.DEFAULT_ELEMENT_NAME).get(0));
}
}

if (!Strings.isNullOrEmpty(mduiRepresentation.getInformationUrl())) {
InformationURL informationURL = openSamlObjects.buildDefaultInstanceOfType(InformationURL.class);
getUIInfo(ed).addInformationURL(informationURL);
informationURL.setValue(mduiRepresentation.getInformationUrl());
informationURL.setXMLLang("en");
} else {
if (getUIInfo(ed).getXMLObjects(InformationURL.DEFAULT_ELEMENT_NAME).size() > 0) {
getUIInfo(ed).getXMLObjects().remove(getUIInfo(ed).getXMLObjects(InformationURL.DEFAULT_ELEMENT_NAME).get(0));
}
}

if (!Strings.isNullOrEmpty(mduiRepresentation.getPrivacyStatementUrl())) {
PrivacyStatementURL privacyStatementURL = openSamlObjects.buildDefaultInstanceOfType(PrivacyStatementURL.class);
getUIInfo(ed).addPrivacyStatementURL(privacyStatementURL);
privacyStatementURL.setValue(mduiRepresentation.getPrivacyStatementUrl());
privacyStatementURL.setXMLLang("en");
} else {
if (getUIInfo(ed).getXMLObjects(PrivacyStatementURL.DEFAULT_ELEMENT_NAME).size() > 0) {
getUIInfo(ed).getXMLObjects().remove(getUIInfo(ed).getXMLObjects(PrivacyStatementURL.DEFAULT_ELEMENT_NAME).get(0));
}
}

if (!Strings.isNullOrEmpty(mduiRepresentation.getDescription())) {
Description description = openSamlObjects.buildDefaultInstanceOfType(Description.class);
getUIInfo(ed).addDescription(description);
description.setValue(mduiRepresentation.getDescription());
description.setXMLLang("en");
} else {
if (getUIInfo(ed).getXMLObjects(Description.DEFAULT_ELEMENT_NAME).size() > 0) {
getUIInfo(ed).getXMLObjects().remove(getUIInfo(ed).getXMLObjects(Description.DEFAULT_ELEMENT_NAME).get(0));
}
}

if (!Strings.isNullOrEmpty(mduiRepresentation.getLogoUrl())) {
Expand All @@ -209,7 +235,13 @@ public EntityDescriptor createDescriptorFromRepresentation(final EntityDescripto
logo.setHeight(mduiRepresentation.getLogoHeight());
logo.setWidth(mduiRepresentation.getLogoWidth());
logo.setXMLLang("en");
} else {
if (getUIInfo(ed).getXMLObjects(Description.DEFAULT_ELEMENT_NAME).size() > 0) {
getUIInfo(ed).getXMLObjects().remove(getUIInfo(ed).getXMLObjects(Description.DEFAULT_ELEMENT_NAME).get(0));
}
}
} else {
removeUIInfo(ed);
}

// setup security
Expand All @@ -227,6 +259,8 @@ public EntityDescriptor createDescriptorFromRepresentation(final EntityDescripto
getSPSSODescriptorFromEntityDescriptor(ed).addKeyDescriptor(keyDescriptor);
}
}
} else {
// TODO: implement
}

// setup ACSs
Expand All @@ -240,6 +274,8 @@ public EntityDescriptor createDescriptorFromRepresentation(final EntityDescripto
assertionConsumerService.setBinding(acsRepresentation.getBinding());
assertionConsumerService.setLocation(acsRepresentation.getLocationUrl());
}
} else {
// TODO: implement
}

// setup logout
Expand All @@ -251,16 +287,28 @@ public EntityDescriptor createDescriptorFromRepresentation(final EntityDescripto

getSPSSODescriptorFromEntityDescriptor(ed).getSingleLogoutServices().add(singleLogoutService);
}
} else {
// TODO: implement
}

if (representation.getRelyingPartyOverrides() != null || (representation.getAttributeRelease() != null && representation.getAttributeRelease().size() > 0)) {
// TODO: fix implementation
getEntityAttributes(ed).getAttributes().addAll(entityService.getAttributeListFromEntityRepresentation(representation));
} else {
EntityAttributes entityAttributes = getEntityAttributes(ed, false);
if (entityAttributes != null) {
entityAttributes.getAttributes().clear();
}
}
return ed;
}

private SPSSODescriptor getSPSSODescriptorFromEntityDescriptor(EntityDescriptor entityDescriptor) {
if (entityDescriptor.getSPSSODescriptor("") == null) {
private SPSSODescriptor getSPSSODescriptorFromEntityDescriptor(EntityDescriptor entityDescriptor) {
return getSPSSODescriptorFromEntityDescriptor(entityDescriptor, true);
}

private SPSSODescriptor getSPSSODescriptorFromEntityDescriptor(EntityDescriptor entityDescriptor, boolean create) {
if (entityDescriptor.getSPSSODescriptor("") == null && create) {
SPSSODescriptor spssoDescriptor = openSamlObjects.buildDefaultInstanceOfType(SPSSODescriptor.class);
entityDescriptor.getRoleDescriptors().add(spssoDescriptor);
}
Expand Down Expand Up @@ -324,7 +372,14 @@ private KeyDescriptor createKeyDescriptor(String name, String type, String value
}

private EntityAttributes getEntityAttributes(EntityDescriptor ed) {
return getEntityAttributes(ed, true);
}

private EntityAttributes getEntityAttributes(EntityDescriptor ed, boolean create) {
Extensions extensions = ed.getExtensions();
if (extensions == null && !create) {
return null;
}
if (extensions == null) {
extensions = openSamlObjects.buildDefaultInstanceOfType(Extensions.class);
ed.setExtensions(extensions);
Expand Down Expand Up @@ -357,6 +412,19 @@ private UIInfo getUIInfo(EntityDescriptor ed) {
return uiInfo;
}

private void removeUIInfo(EntityDescriptor ed) {
SPSSODescriptor spssoDescriptor = getSPSSODescriptorFromEntityDescriptor(ed, false);
if (spssoDescriptor != null) {
Extensions extensions = spssoDescriptor.getExtensions();
if (extensions == null) {
return;
}
if (extensions.getUnknownXMLObjects(UIInfo.DEFAULT_ELEMENT_NAME).size() > 0) {
extensions.getUnknownXMLObjects().remove(extensions.getUnknownXMLObjects(UIInfo.DEFAULT_ELEMENT_NAME).get(0));
}
}
}

//TODO: implement
@Override
public EntityDescriptorRepresentation createRepresentationFromDescriptor(org.opensaml.saml.saml2.metadata.EntityDescriptor entityDescriptor) {
Expand Down Expand Up @@ -584,12 +652,4 @@ public List<String> getAttributeReleaseListFromAttributeList(List<Attribute> att
public Map<String, Object> getRelyingPartyOverridesRepresentationFromAttributeList(List<Attribute> attributeList) {
return ModelRepresentationConversions.getRelyingPartyOverridesRepresentationFromAttributeList(attributeList);
}



@Override
public void updateDescriptorFromRepresentation(org.opensaml.saml.saml2.metadata.EntityDescriptor entityDescriptor, EntityDescriptorRepresentation representation) {
// TODO: implement
throw new UnsupportedOperationException("not yet implemented");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import org.xmlunit.builder.DiffBuilder
import org.xmlunit.builder.Input
import org.xmlunit.diff.DefaultNodeMatcher
import org.xmlunit.diff.ElementSelectors
import spock.lang.Ignore
import spock.lang.Specification

@ContextConfiguration(classes=[CoreShibUiConfiguration, CustomPropertiesConfiguration])
Expand Down Expand Up @@ -764,6 +765,7 @@ class JPAEntityDescriptorServiceImplTests extends Specification {
testRunIndex << (1..5)
}

@Ignore
def "updateDescriptorFromRepresentation throws expected exception"() {
given:
def randomEntityDescriptor = generateRandomEntityDescriptor()
Expand Down

0 comments on commit 2396bd7

Please sign in to comment.