View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.ui.commons.panels;
20  
21  import java.net.URLEncoder;
22  import java.nio.charset.StandardCharsets;
23  import java.util.List;
24  import org.apache.syncope.client.ui.commons.BaseSession;
25  import org.apache.syncope.client.ui.commons.Constants;
26  import org.apache.syncope.client.ui.commons.SAML2SP4UIConstants;
27  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
28  import org.apache.syncope.common.lib.to.SAML2SP4UIIdPTO;
29  import org.apache.syncope.common.rest.api.service.SAML2SP4UIIdPService;
30  import org.apache.wicket.ajax.AjaxRequestTarget;
31  import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
32  import org.apache.wicket.markup.html.form.IChoiceRenderer;
33  import org.apache.wicket.model.IModel;
34  import org.apache.wicket.model.Model;
35  import org.apache.wicket.request.UrlUtils;
36  import org.apache.wicket.request.cycle.RequestCycle;
37  import org.apache.wicket.request.http.handler.RedirectRequestHandler;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  public abstract class AbstractSAMLSSOLoginFormPanel extends BaseSSOLoginFormPanel {
42  
43      private static final Logger LOG = LoggerFactory.getLogger(AbstractSAMLSSOLoginFormPanel.class);
44  
45      private static final long serialVersionUID = 1153528484703183466L;
46  
47      public AbstractSAMLSSOLoginFormPanel(final String id, final BaseSession session) {
48          super(id);
49  
50          List<SAML2SP4UIIdPTO> available = session.getAnonymousService(SAML2SP4UIIdPService.class).list();
51  
52          Model<SAML2SP4UIIdPTO> model = new Model<>();
53          AjaxDropDownChoicePanel<SAML2SP4UIIdPTO> idps =
54                  new AjaxDropDownChoicePanel<>("idps", "SAML 2.0", model, false);
55          idps.setChoices(available);
56          idps.setChoiceRenderer(new IChoiceRenderer<>() {
57  
58              private static final long serialVersionUID = 1814750973898916102L;
59  
60              @Override
61              public Object getDisplayValue(final SAML2SP4UIIdPTO object) {
62                  return object.getName();
63              }
64  
65              @Override
66              public String getIdValue(final SAML2SP4UIIdPTO object, final int index) {
67                  return object.getEntityID();
68              }
69  
70              @Override
71              public SAML2SP4UIIdPTO getObject(
72                  final String id, final IModel<? extends List<? extends SAML2SP4UIIdPTO>> choices) {
73  
74                  return choices.getObject().stream().
75                      filter(idp -> idp.getEntityID().equals(id)).findFirst().orElse(null);
76              }
77          });
78          idps.getField().add(new AjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
79  
80              private static final long serialVersionUID = -1107858522700306810L;
81  
82              @Override
83              protected void onUpdate(final AjaxRequestTarget target) {
84                  if (model.getObject() != null) {
85                      try {
86                          RequestCycle.get().scheduleRequestHandlerAfterCurrent(new RedirectRequestHandler(
87                                  UrlUtils.rewriteToContextRelative(SAML2SP4UIConstants.URL_CONTEXT + "/login?idp="
88                                          + URLEncoder.encode(
89                                                  model.getObject().getEntityID(), StandardCharsets.UTF_8),
90                                          RequestCycle.get())));
91                      } catch (Exception e) {
92                          LOG.error("Could not redirect to the selected IdP {}", model.getObject().getEntityID(), e);
93                      }
94                  }
95              }
96          });
97          idps.setOutputMarkupPlaceholderTag(true);
98          idps.setVisible(!available.isEmpty());
99          add(idps);
100     }
101 }