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

feat: Return a sorted IdP list #2980

Merged
merged 8 commits into from
Aug 16, 2024
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 @@ -10,6 +10,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -450,12 +451,13 @@ private void updateLoginPageModel(
boolean fieldUsernameShow,
boolean linkCreateAccountShow
) {
Comparator<SamlIdentityProviderDefinition> sortingByLinkText = Comparator.comparing(SamlIdentityProviderDefinition::getLinkText, String.CASE_INSENSITIVE_ORDER);
model.addAttribute(LINK_CREATE_ACCOUNT_SHOW, linkCreateAccountShow);
model.addAttribute(FIELD_USERNAME_SHOW, fieldUsernameShow);
model.addAttribute(IDP_DEFINITIONS, samlIdentityProviders.values());
model.addAttribute(IDP_DEFINITIONS, samlIdentityProviders.values().stream().sorted(sortingByLinkText).toList());
Map<String, String> oauthLinks = new HashMap<>();
ofNullable(oauthIdentityProviders).orElse(emptyMap()).entrySet().stream()
.filter(e -> e.getValue().isShowLinkText())
.filter(e -> e.getValue() != null && e.getValue().isShowLinkText() && e.getKey() != null)
.forEach(e ->
oauthLinks.put(
externalOAuthProviderConfigurator.getIdpAuthenticationUrl(
Expand All @@ -465,7 +467,7 @@ private void updateLoginPageModel(
e.getValue().getLinkText()
)
);
model.addAttribute(OAUTH_LINKS, oauthLinks);
model.addAttribute(OAUTH_LINKS, oauthLinks.entrySet().stream().sorted(Map.Entry.comparingByValue(String::compareToIgnoreCase)).toList());
model.addAttribute("clientName", clientName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -827,18 +827,20 @@ void allowedIdpsforClientOIDCProvider() throws Exception {
when(clientDetailsService.loadClientByClientId("client-id", "uaa")).thenReturn(clientDetails);

List<IdentityProvider> clientAllowedIdps = new LinkedList<>();
clientAllowedIdps.add(createOIDCIdentityProvider("my-OIDC-idp1"));
clientAllowedIdps.add(createOIDCIdentityProvider("my-OIDC-idp2"));
clientAllowedIdps.add(createOIDCIdentityProvider("my-OIDC-idp3"));
clientAllowedIdps.add(createOIDCIdentityProvider("my-OIDC-idp2"));
clientAllowedIdps.add(createOIDCIdentityProvider("my-OIDC-idp1"));

when(mockIdentityProviderProvisioning.retrieveAll(eq(true), anyString())).thenReturn(clientAllowedIdps);

LoginInfoEndpoint endpoint = getEndpoint(IdentityZoneHolder.get(), clientDetailsService);

endpoint.loginForHtml(extendedModelMap, null, request, singletonList(MediaType.TEXT_HTML));

Map<String, AbstractExternalOAuthIdentityProviderDefinition> idpDefinitions = (Map<String, AbstractExternalOAuthIdentityProviderDefinition>) extendedModelMap.asMap().get("oauthLinks");
Collection<Map<String, String>> idpDefinitions = (Collection<Map<String, String>>) extendedModelMap.asMap().get("oauthLinks");
assertEquals(2, idpDefinitions.size());
// Expect this always on top of list because of sorting
assertEquals("my-OIDC-idp1", ((Map.Entry<String, String>) idpDefinitions.iterator().next()).getValue());
}

@Test
Expand Down Expand Up @@ -1243,7 +1245,7 @@ public void testInvalidLoginHintLoginPageReturnsList() throws Exception {

endpoint.loginForHtml(extendedModelMap, null, mockHttpServletRequest, Collections.singletonList(MediaType.TEXT_HTML));

assertFalse(((Map)extendedModelMap.get("oauthLinks")).isEmpty());
assertFalse(((Collection<Map<String, String>>)extendedModelMap.get("oauthLinks")).isEmpty());
}

@Test
Expand Down Expand Up @@ -1701,7 +1703,7 @@ void allowedProvidersLoginHintDoesKeepExternalProviders() throws Exception {
assertEquals("{\"origin\":\"uaa\"}", extendedModelMap.get("login_hint"));
assertEquals("login", redirect);

Map<String, String> oauthLinks = (Map<String, String>) extendedModelMap.get("oauthLinks");
Collection<Map<String, String>> oauthLinks = (Collection<Map<String, String>>) extendedModelMap.get("oauthLinks");
assertEquals(1, oauthLinks.size());
}

Expand Down Expand Up @@ -1812,6 +1814,7 @@ private static IdentityProvider createOIDCIdentityProvider(String originKey) thr
OIDCIdentityProviderDefinition definition = new OIDCIdentityProviderDefinition();
definition.setAuthUrl(new URL("https://" + originKey + ".com"));
definition.setRelyingPartySecret("client-secret");
definition.setLinkText(originKey);
oidcIdentityProvider.setConfig(definition);

return oidcIdentityProvider;
Expand Down
Loading