public String getAssertionConsumerURL(String sp) throws MetadataProviderException { EntityDescriptor entityDescriptor = metadataManager.getEntityDescriptor(sp); SPSSODescriptor spssoDescriptor = entityDescriptor.getSPSSODescriptor(SAMLConstants.SAML20P_NS); List<AssertionConsumerService> assertionConsumerServices = spssoDescriptor.getAssertionConsumerServices(); Optional<AssertionConsumerService> defaultService = assertionConsumerServices.stream().filter(acs -> acs.isDefault()).findFirst(); if (defaultService.isPresent()) { return defaultService.get().getLocation(); } else { return assertionConsumerServices.get(0).getLocation(); } }
@Test public void get_assertion_consumer_service_url() throws Exception { String entityID = "validEntityID"; EntityDescriptor entityDescriptor = mock(EntityDescriptor.class); when(metadataManager.getEntityDescriptor(eq(entityID))).thenReturn(entityDescriptor); SPSSODescriptor spssoDescriptor = mock(SPSSODescriptor.class); when(entityDescriptor.getSPSSODescriptor(eq(SAML20P_NS))).thenReturn(spssoDescriptor); AssertionConsumerService service = mock(AssertionConsumerService.class); when(service.getLocation()).thenReturn("service-location"); when(service.isDefault()).thenReturn(false); AssertionConsumerService defaultService = mock(AssertionConsumerService.class); when(defaultService.getLocation()).thenReturn("default-location"); when(defaultService.isDefault()).thenReturn(true); when(spssoDescriptor.getAssertionConsumerServices()).thenReturn(Arrays.asList(service, defaultService)); String url = controller.getAssertionConsumerURL(entityID); assertEquals("default-location", url); when(defaultService.isDefault()).thenReturn(false); url = controller.getAssertionConsumerURL(entityID); assertEquals("service-location", url); }
private void setAssertionConsumerUrl(SPSSODescriptor spssoDescriptor, SAMLSSOServiceProviderDO samlssoServiceProviderDO){ //Assertion Consumer URL //search for the url with the post binding, if there is no post binding select the default url List<AssertionConsumerService> assertionConsumerServices = spssoDescriptor.getAssertionConsumerServices(); if (assertionConsumerServices!=null && assertionConsumerServices.size()>0) { List<String> acs = new ArrayList<>(); boolean foundAssertionConsumerUrl = false; for (AssertionConsumerService assertionConsumerService : assertionConsumerServices) { acs.add(assertionConsumerService.getLocation()); if (assertionConsumerService.isDefault()) { samlssoServiceProviderDO.setDefaultAssertionConsumerUrl(assertionConsumerService.getLocation());//changed samlssoServiceProviderDO.setAssertionConsumerUrl(assertionConsumerService.getLocation());//changed foundAssertionConsumerUrl = true; } } samlssoServiceProviderDO.setAssertionConsumerUrls(acs); //select atleast one if (!foundAssertionConsumerUrl) { samlssoServiceProviderDO.setDefaultAssertionConsumerUrl(assertionConsumerServices.get(0).getLocation()); } } } private void setIssuer(EntityDescriptor entityDescriptor , SAMLSSOServiceProviderDO samlssoServiceProviderDO){
private void setAssertionConsumerUrl(SPSSODescriptor spssoDescriptor, SAMLSSOServiceProviderDO samlssoServiceProviderDO) throws InvalidMetadataException { //Assertion Consumer URL //search for the url with the post binding, if there is no post binding select the default url List<AssertionConsumerService> assertionConsumerServices = spssoDescriptor.getAssertionConsumerServices(); if (assertionConsumerServices != null && assertionConsumerServices.size() > 0) { List<String> acs = new ArrayList<>(); boolean foundAssertionConsumerUrl = false; for (AssertionConsumerService assertionConsumerService : assertionConsumerServices) { acs.add(assertionConsumerService.getLocation()); if (assertionConsumerService.isDefault()) { samlssoServiceProviderDO.setDefaultAssertionConsumerUrl(assertionConsumerService.getLocation());//changed samlssoServiceProviderDO.setAssertionConsumerUrl(assertionConsumerService.getLocation());//changed foundAssertionConsumerUrl = true; } } samlssoServiceProviderDO.setAssertionConsumerUrls(acs); //select atleast one if (!foundAssertionConsumerUrl) { samlssoServiceProviderDO.setDefaultAssertionConsumerUrl(assertionConsumerServices.get(0).getLocation()); } } else { throw new InvalidMetadataException("Invalid metadata content, no Assertion Consumer URL found"); } }
@Override public SAMLMessageContext sendMessage(SAMLMessageContext samlContext, boolean sign) throws SAMLException, MetadataProviderException, MessageEncodingException { Endpoint endpoint = samlContext.getPeerEntityEndpoint(); SAMLBinding binding = getBinding(endpoint); samlContext.setLocalEntityId(spConfiguration.getEntityId()); samlContext.getLocalEntityMetadata().setEntityID(spConfiguration.getEntityId()); samlContext.getPeerEntityEndpoint().setLocation(spConfiguration.getIdpSSOServiceURL()); SPSSODescriptor roleDescriptor = (SPSSODescriptor) samlContext.getLocalEntityMetadata().getRoleDescriptors().get(0); AssertionConsumerService assertionConsumerService = roleDescriptor.getAssertionConsumerServices().stream().filter(service -> service.isDefault()).findAny().orElseThrow(() -> new RuntimeException("No default ACS")); assertionConsumerService.setBinding(spConfiguration.getProtocolBinding()); assertionConsumerService.setLocation(spConfiguration.getAssertionConsumerServiceURL()); return super.sendMessage(samlContext, spConfiguration.isNeedsSigning(), binding); } }