Skip to content

Commit

Permalink
Fixing SAML 2.0 SP metadata download from Console from different loca…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
ilgrosso committed Jul 21, 2023
1 parent 9ac16a3 commit 789a5d7
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import javax.ws.rs.core.Response;
import org.apache.cxf.jaxrs.client.Client;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.syncope.client.console.SyncopeConsoleSession;
import org.apache.syncope.common.lib.to.ProvisioningReport;
import org.apache.syncope.common.lib.to.PullTaskTO;
import org.apache.syncope.common.lib.to.PushTaskTO;
Expand Down Expand Up @@ -59,7 +58,7 @@ public Response push(final AnyQuery anyQuery, final CSVPushSpec spec) {

Response response = service.push(anyQuery, spec);

SyncopeConsoleSession.get().resetClient(ReconciliationService.class);
resetClient(ReconciliationService.class);

return response;
}
Expand All @@ -72,7 +71,7 @@ public ArrayList<ProvisioningReport> pull(final CSVPullSpec spec, final InputStr
ArrayList<ProvisioningReport> result = service.pull(spec, csv).stream().
collect(Collectors.toCollection(ArrayList::new));

SyncopeConsoleSession.get().resetClient(ReconciliationService.class);
resetClient(ReconciliationService.class);

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.impl.MetadataMap;
import org.apache.syncope.client.console.SyncopeConsoleSession;
import org.apache.syncope.common.lib.to.BpmnProcess;
import org.apache.syncope.common.rest.api.RESTHeaders;
import org.apache.syncope.common.rest.api.service.BpmnProcessService;
Expand All @@ -52,14 +51,14 @@ private BpmnProcessService getService(final MediaType mediaType) {

public InputStream getDefinition(final MediaType mediaType, final String key) {
Response response = getService(mediaType).get(key);
SyncopeConsoleSession.get().resetClient(BpmnProcessService.class);
resetClient(BpmnProcessService.class);

return (InputStream) response.getEntity();
}

public void setDefinition(final MediaType mediaType, final String key, final String definition) {
getService(mediaType).set(key, definition);
SyncopeConsoleSession.get().resetClient(BpmnProcessService.class);
resetClient(BpmnProcessService.class);
}

public byte[] getDiagram(final String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.syncope.client.console.resources.saml2sp4ui.ConsoleAssertionConsumerResource;
import org.apache.syncope.client.console.resources.saml2sp4ui.ConsoleLogoutResource;
import org.apache.syncope.client.console.rest.SAML2IdPsRestClient;
import org.apache.syncope.client.console.rest.SAML2SPRestClient;
import org.apache.syncope.client.ui.commons.resources.saml2sp4ui.LoginResource;
import org.apache.syncope.client.ui.commons.resources.saml2sp4ui.MetadataResource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand All @@ -36,6 +37,12 @@ public SAML2IdPsRestClient saml2IdPsRestClient() {
return new SAML2IdPsRestClient();
}

@ConditionalOnMissingBean
@Bean
public SAML2SPRestClient saml2SPRestClient() {
return new SAML2SPRestClient();
}

@ConditionalOnMissingBean
@Bean
public MetadataResource metadataResource() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.syncope.client.console.panels.SAML2IdPsDirectoryPanel;
import org.apache.syncope.client.console.panels.SAML2SPPanel;
import org.apache.syncope.client.console.rest.SAML2IdPsRestClient;
import org.apache.syncope.client.console.rest.SAML2SPRestClient;
import org.apache.syncope.client.ui.commons.annotations.ExtPage;
import org.apache.syncope.common.lib.types.SAML2SP4UIEntitlement;
import org.apache.wicket.extensions.markup.html.tabs.AbstractTab;
Expand All @@ -44,6 +45,9 @@ public class SAML2SP4UI extends BaseExtPage {
@SpringBean
protected SAML2IdPsRestClient saml2IdPsRestClient;

@SpringBean
protected SAML2SPRestClient saml2SPRestClient;

public SAML2SP4UI(final PageParameters parameters) {
super(parameters);

Expand All @@ -56,8 +60,7 @@ public SAML2SP4UI(final PageParameters parameters) {
}

protected List<ITab> buildTabList() {

final List<ITab> tabs = new ArrayList<>(2);
List<ITab> tabs = new ArrayList<>();

tabs.add(new AbstractTab(new ResourceModel("idps")) {

Expand All @@ -75,7 +78,7 @@ public Panel getPanel(final String panelId) {

@Override
public Panel getPanel(final String panelId) {
return new SAML2SPPanel(panelId);
return new SAML2SPPanel(panelId, saml2SPRestClient);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
*/
package org.apache.syncope.client.console.panels;

import javax.ws.rs.client.ClientBuilder;
import org.apache.commons.lang3.StringUtils;
import org.apache.syncope.client.console.SyncopeConsoleSession;
import org.apache.syncope.client.ui.commons.HttpResourceStream;
import org.apache.syncope.client.console.rest.SAML2SPRestClient;
import org.apache.syncope.client.ui.commons.SAML2SP4UIConstants;
import org.apache.syncope.client.ui.commons.rest.ResponseHolder;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.Url;
import org.apache.wicket.request.UrlUtils;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler;
import org.apache.wicket.request.resource.ContentDisposition;
import org.apache.wicket.util.resource.IResourceStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,7 +39,7 @@ public class SAML2SPPanel extends Panel {

protected static final Logger LOG = LoggerFactory.getLogger(SAML2SPPanel.class);

public SAML2SPPanel(final String id) {
public SAML2SPPanel(final String id, final SAML2SPRestClient restClient) {
super(id);

add(new Link<Void>("downloadMetadata") {
Expand All @@ -49,16 +49,15 @@ public SAML2SPPanel(final String id) {
@Override
public void onClick() {
try {
HttpResourceStream stream = new HttpResourceStream(new ResponseHolder(ClientBuilder.newClient().
target(RequestCycle.get().getUrlRenderer().renderFullUrl(Url.parse(
UrlUtils.rewriteToContextRelative(SAML2SP4UIConstants.URL_CONTEXT + "/metadata",
RequestCycle.get())))).
request().get()));
String spEntityID = StringUtils.substringBefore(
RequestCycle.get().getUrlRenderer().renderFullUrl(
Url.parse(UrlUtils.rewriteToContextRelative(
SAML2SP4UIConstants.URL_CONTEXT, RequestCycle.get()))),
SAML2SP4UIConstants.URL_CONTEXT);
IResourceStream stream = restClient.getMetadata(spEntityID);

ResourceStreamRequestHandler rsrh = new ResourceStreamRequestHandler(stream);
rsrh.setFileName(stream.getFilename() == null
? SyncopeConsoleSession.get().getDomain() + "-SAML-SP-Metadata.xml"
: stream.getFilename());
rsrh.setFileName(SyncopeConsoleSession.get().getDomain() + "-SAML-SP-Metadata.xml");
rsrh.setContentDisposition(ContentDisposition.ATTACHMENT);

getRequestCycle().scheduleRequestHandlerAfterCurrent(rsrh);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import javax.ws.rs.core.MediaType;
import org.apache.cxf.jaxrs.client.Client;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.syncope.client.console.SyncopeConsoleSession;
import org.apache.syncope.common.lib.to.SAML2SP4UIIdPTO;
import org.apache.syncope.common.rest.api.service.SAML2SP4UIIdPService;

Expand All @@ -42,7 +41,7 @@ public void importIdPs(final InputStream input) {

service.importFromMetadata(input);

SyncopeConsoleSession.get().resetClient(SAML2SP4UIIdPService.class);
resetClient(SAML2SP4UIIdPService.class);
}

public SAML2SP4UIIdPTO read(final String key) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.syncope.client.console.rest;

import java.io.IOException;
import java.io.InputStream;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.syncope.client.console.SyncopeConsoleSession;
import org.apache.syncope.client.ui.commons.SAML2SP4UIConstants;
import org.apache.syncope.common.rest.api.service.SAML2SP4UIService;
import org.apache.wicket.util.io.IOUtils;
import org.apache.wicket.util.resource.AbstractResourceStream;
import org.apache.wicket.util.resource.IResourceStream;
import org.apache.wicket.util.resource.ResourceStreamNotFoundException;

public class SAML2SPRestClient extends BaseRestClient {

private static final long serialVersionUID = -5084300184764037527L;

public IResourceStream getMetadata(final String spEntityID) {
SAML2SP4UIService service = SyncopeConsoleSession.get().getAnonymousService(SAML2SP4UIService.class);
WebClient.client(service).accept(MediaType.APPLICATION_XML_TYPE).type(MediaType.APPLICATION_XML_TYPE);
Response metadataResponse = service.getMetadata(spEntityID, SAML2SP4UIConstants.URL_CONTEXT);
WebClient.client(service).reset();

InputStream inputStream = (InputStream) metadataResponse.getEntity();

return new AbstractResourceStream() {

private static final long serialVersionUID = -2268011115723452312L;

@Override
public InputStream getInputStream() throws ResourceStreamNotFoundException {
return inputStream;
}

@Override
public void close() throws IOException {
IOUtils.closeQuietly(inputStream);
}
};
}
}

0 comments on commit 789a5d7

Please sign in to comment.