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 1 commit
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 Down Expand Up @@ -107,6 +109,7 @@ 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 +143,16 @@ 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,10 +162,25 @@ public List<IdpDescription> lookupIdp(String text) {

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

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

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 @@ -58,6 +58,7 @@ public class MetadataLookupServiceTests {

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 Down Expand Up @@ -91,7 +92,7 @@ public void setup() throws MetadataProviderException {
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 Down Expand Up @@ -151,7 +152,7 @@ public void testServiceInitialization() throws MetadataProviderException {
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)))));
}


Expand Down Expand Up @@ -191,7 +192,7 @@ public void testPartialLookupWorks() {
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
Expand Down