Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for displaying specific language name in federation Metadata #640

Merged
merged 4 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.opensaml.common.xml.SAMLConstants;
import org.opensaml.saml2.metadata.EntityDescriptor;
import org.opensaml.saml2.metadata.IDPSSODescriptor;
import org.opensaml.saml2.metadata.LocalizedString;
import org.opensaml.saml2.metadata.provider.MetadataProvider;
import org.opensaml.saml2.metadata.provider.MetadataProviderException;
import org.opensaml.saml2.metadata.provider.ObservableMetadataProvider;
Expand All @@ -49,7 +51,8 @@

@Component
@Profile("saml")
public class DefaultMetadataLookupService implements MetadataLookupService, ObservableMetadataProvider.Observer {
public class DefaultMetadataLookupService
implements MetadataLookupService, ObservableMetadataProvider.Observer {

private static final int MAX_RESULTS = 20;
private static final Logger LOG = LoggerFactory.getLogger(DefaultMetadataLookupService.class);
Expand All @@ -70,7 +73,7 @@ private void initializeMetadataSet() throws MetadataProviderException {

final Instant startTime = Instant.now();
LOG.debug("Initializing IdP descriptor list from metadata");

Set<IdpDescription> newDescriptions = new HashSet<>();

for (String idpName : metadataManager.getIDPEntityNames()) {
Expand Down Expand Up @@ -107,6 +110,8 @@ private IdpDescription descriptionFromMetadata(EntityDescriptor descriptor) {

if (!uiInfo.getDisplayNames().isEmpty()) {
result.setOrganizationName(uiInfo.getDisplayNames().get(0).getName().getLocalString());
result
.setDisplayNames(uiInfo.getDisplayNames().stream().map(dn -> dn.getName()).toList());
}
}
}
Expand Down Expand Up @@ -140,6 +145,17 @@ private Optional<List<IdpDescription>> lookupByEntityId(String text) {
public List<IdpDescription> lookupIdp(String text) {

List<IdpDescription> result = new ArrayList<>();
String textToFind = text.toLowerCase();

Predicate<IdpDescription> filterForDescriptions = description -> {
if (description.getDisplayNames() != null) {
return description.getDisplayNames()
.stream()
.anyMatch(name -> name.getLocalString().toLowerCase().contains(textToFind));
} else {
return description.getEntityId().toLowerCase().contains(textToFind);
}
};

lookupByEntityId(text).ifPresent(result::addAll);

Expand All @@ -149,9 +165,24 @@ public List<IdpDescription> lookupIdp(String text) {

try {
lock.readLock().lock();

return descriptions.stream()
.filter(p -> p.getOrganizationName().toLowerCase().contains(text.toLowerCase()))
.filter(filterForDescriptions)
.limit(MAX_RESULTS)
.map(description -> {
List<LocalizedString> displayNames = description.getDisplayNames();
if (displayNames != null) {

for (LocalizedString displayName : displayNames) {
String localString = displayName.getLocalString();
if (localString.toLowerCase().contains(textToFind)) {
description.setOrganizationName(localString);
break;
}
}
}
return description;
})
.collect(Collectors.toList());
} finally {
lock.readLock().unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@
*/
package it.infn.mw.iam.authn.saml.model;

import java.util.List;

import org.opensaml.saml2.metadata.LocalizedString;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

@JsonInclude(Include.NON_EMPTY)
public class IdpDescription {
public class IdpDescription {

private String entityId;
private String organizationName;
private String imageUrl;
private List<LocalizedString> displayNames;

public String getEntityId() {
return entityId;
Expand All @@ -49,9 +54,18 @@ public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}

public List<LocalizedString> getDisplayNames() {
return displayNames;
}

public void setDisplayNames(List<LocalizedString> displayNames) {
this.displayNames = displayNames;
}

@Override
public String toString() {
return "IdpDescription [entityId=" + entityId + ", organizationName=" + organizationName
+ ", imageUrl=" + imageUrl + "]";
+ ", imageUrl=" + imageUrl + ", displayNames=" + displayNames + "]";
}

}
federicaagostini marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ public class MetadataLookupServiceTests {
public static final String IDP2_ENTITY_ID = "urn:test:idp2";
public static final String IDP3_ENTITY_ID = "urn:test:idp3";
public static final String IDP4_ENTITY_ID = "urn:test:idp4";

public static final String IDP1_ORGANIZATION_NAME = "IDP1 organization";
public static final String IDP2_ORGANIZATION_NAME = "IDP2 organization";

public static final String IDP4_ORGANIZATION_NAME = "IDP4 organization";

@Mock
MetadataManager manager;

Expand All @@ -75,23 +76,26 @@ public class MetadataLookupServiceTests {
UIInfo idp1UIInfo, idp2UIInfo, idp4UIInfo;

@Mock
DisplayName idp1DisplayName, idp2DisplayName, idp4DisplayName;
DisplayName idp1DisplayName, idp1ItDisplayName, idp2DisplayName, idp4DisplayName;

@Mock
LocalizedString idp1LocalizedString, idp2LocalizedString, idp4LocalizedString;
LocalizedString idp1LocalizedString, idp1ItLocalizedString, idp2LocalizedString,
idp4LocalizedString;

@Before
public void setup() throws MetadataProviderException {

when(idp1LocalizedString.getLocalString()).thenReturn(IDP1_ORGANIZATION_NAME);
when(idp1ItLocalizedString.getLocalString()).thenReturn("IDP1 organizzazione");
when(idp1DisplayName.getName()).thenReturn(idp1LocalizedString);
when(idp1UIInfo.getDisplayNames()).thenReturn(asList(idp1DisplayName));
when(idp1ItDisplayName.getName()).thenReturn(idp1ItLocalizedString);
when(idp1UIInfo.getDisplayNames()).thenReturn(asList(idp1DisplayName, idp1ItDisplayName));

when(idp2LocalizedString.getLocalString()).thenReturn(IDP2_ORGANIZATION_NAME);
when(idp2DisplayName.getName()).thenReturn(idp2LocalizedString);
when(idp2UIInfo.getDisplayNames()).thenReturn(asList(idp2DisplayName));

when(idp4LocalizedString.getLocalString()).thenReturn("");
when(idp4LocalizedString.getLocalString()).thenReturn(IDP4_ORGANIZATION_NAME);
when(idp4DisplayName.getName()).thenReturn(idp4LocalizedString);
when(idp4UIInfo.getDisplayNames()).thenReturn(asList(idp4DisplayName));

Expand All @@ -102,7 +106,7 @@ public void setup() throws MetadataProviderException {
.thenReturn(asList(idp2UIInfo));

when(idp4SsoExtensions.getUnknownXMLObjects(UIInfo.DEFAULT_ELEMENT_NAME))
.thenReturn(asList(idp4UIInfo));
.thenReturn(asList(idp4UIInfo));

when(idp1SsoDesc.getExtensions()).thenReturn(idp1SsoExtensions);

Expand All @@ -115,7 +119,7 @@ public void setup() throws MetadataProviderException {

when(idp2Desc.getEntityID()).thenReturn(IDP2_ENTITY_ID);
when(idp2Desc.getIDPSSODescriptor(SAMLConstants.SAML20P_NS)).thenReturn(idp2SsoDesc);

when(idp3Desc.getEntityID()).thenReturn(IDP3_ENTITY_ID);

when(idp4Desc.getEntityID()).thenReturn(IDP4_ENTITY_ID);
Expand All @@ -126,8 +130,8 @@ public void setup() throws MetadataProviderException {
when(manager.getEntityDescriptor(IDP3_ENTITY_ID)).thenReturn(idp3Desc);
when(manager.getEntityDescriptor(IDP4_ENTITY_ID)).thenReturn(idp4Desc);

when(manager.getIDPEntityNames()).thenReturn(Sets.newHashSet(IDP1_ENTITY_ID, IDP2_ENTITY_ID,
IDP3_ENTITY_ID, IDP4_ENTITY_ID));
when(manager.getIDPEntityNames())
.thenReturn(Sets.newHashSet(IDP1_ENTITY_ID, IDP2_ENTITY_ID, IDP3_ENTITY_ID, IDP4_ENTITY_ID));
}


Expand All @@ -138,72 +142,86 @@ public void testServiceInitialization() throws MetadataProviderException {

assertNotNull(service.listIdps());
List<IdpDescription> idps = service.listIdps();

assertThat(idps, hasSize(4));

assertThat(idps, hasItem(allOf(hasProperty("entityId", is(IDP1_ENTITY_ID)),
hasProperty("organizationName", is(IDP1_ORGANIZATION_NAME)))));

assertThat(idps, hasItem(allOf(hasProperty("entityId", is(IDP2_ENTITY_ID)),
hasProperty("organizationName", is(IDP2_ORGANIZATION_NAME)))));

assertThat(idps, hasItem(allOf(hasProperty("entityId", is(IDP3_ENTITY_ID)),
hasProperty("organizationName", is(IDP3_ENTITY_ID)))));

assertThat(idps, hasItem(allOf(hasProperty("entityId", is(IDP4_ENTITY_ID)),
hasProperty("organizationName", is(IDP4_ENTITY_ID)))));
hasProperty("organizationName", is(IDP4_ORGANIZATION_NAME)))));
}


@Test
public void testEmptyMetadataInitialization() {
when(manager.getIDPEntityNames()).thenReturn(emptySet());
DefaultMetadataLookupService service = new DefaultMetadataLookupService(manager);

assertThat(service.listIdps(), hasSize(0));
}

@Test
public void testLookupByOrganizationNameWorks() {
public void testEmptyTextToFind() {
DefaultMetadataLookupService service = new DefaultMetadataLookupService(manager);

List<IdpDescription> idps = service.lookupIdp(IDP1_ORGANIZATION_NAME);
assertThat(idps, hasSize(1));

assertThat(idps, hasItem(allOf(hasProperty("entityId", is(IDP1_ENTITY_ID)),
List<IdpDescription> idps = service.lookupIdp("noMatchOnTextToFind");
assertThat(idps, hasSize(0));
}

@Test
public void testLookupByOrganizationNameWorks() {
DefaultMetadataLookupService service = new DefaultMetadataLookupService(manager);

List<IdpDescription> idpsIt = service.lookupIdp("organizz");
assertThat(idpsIt, hasSize(1));

assertThat(idpsIt, hasItem(allOf(hasProperty("entityId", is(IDP1_ENTITY_ID)),
hasProperty("organizationName", is("IDP1 organizzazione")))));

List<IdpDescription> idpsEn = service.lookupIdp(IDP1_ORGANIZATION_NAME);
assertThat(idpsEn, hasSize(1));

assertThat(idpsEn, hasItem(allOf(hasProperty("entityId", is(IDP1_ENTITY_ID)),
hasProperty("organizationName", is(IDP1_ORGANIZATION_NAME)))));
}

@Test
public void testPartialLookupWorks() {
DefaultMetadataLookupService service = new DefaultMetadataLookupService(manager);

List<IdpDescription> idps = service.lookupIdp("idp");
assertThat(idps, hasSize(4));

assertThat(idps, hasItem(allOf(hasProperty("entityId", is(IDP1_ENTITY_ID)),
hasProperty("organizationName", is(IDP1_ORGANIZATION_NAME)))));

assertThat(idps, hasItem(allOf(hasProperty("entityId", is(IDP2_ENTITY_ID)),
hasProperty("organizationName", is(IDP2_ORGANIZATION_NAME)))));

assertThat(idps, hasItem(allOf(hasProperty("entityId", is(IDP3_ENTITY_ID)),
hasProperty("organizationName", is(IDP3_ENTITY_ID)))));

assertThat(idps, hasItem(allOf(hasProperty("entityId", is(IDP4_ENTITY_ID)),
hasProperty("organizationName", is(IDP4_ENTITY_ID)))));
hasProperty("organizationName", is(IDP4_ORGANIZATION_NAME)))));
}

@Test
public void testEntityIdLookupWorks() {

DefaultMetadataLookupService service = new DefaultMetadataLookupService(manager);
List<IdpDescription> idps = service.lookupIdp(IDP1_ENTITY_ID);
assertThat(idps, hasSize(1));

assertThat(idps, hasItem(allOf(hasProperty("entityId", is(IDP1_ENTITY_ID)),
hasProperty("organizationName", is(IDP1_ORGANIZATION_NAME)))));

idps = service.lookupIdp("unknown");
assertThat(idps, hasSize(0));
}
Expand Down