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.markup.html.form.AjaxDropDownChoicePanel;
27  import org.apache.syncope.common.lib.to.OIDCC4UIProviderTO;
28  import org.apache.syncope.common.rest.api.service.OIDCC4UIProviderService;
29  import org.apache.wicket.ajax.AjaxRequestTarget;
30  import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
31  import org.apache.wicket.markup.html.form.IChoiceRenderer;
32  import org.apache.wicket.model.IModel;
33  import org.apache.wicket.model.Model;
34  import org.apache.wicket.request.UrlUtils;
35  import org.apache.wicket.request.cycle.RequestCycle;
36  import org.apache.wicket.request.http.handler.RedirectRequestHandler;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  public abstract class AbstractOIDCSSOLoginFormPanel extends BaseSSOLoginFormPanel {
41  
42      private static final Logger LOG = LoggerFactory.getLogger(AbstractOIDCSSOLoginFormPanel.class);
43  
44      private static final long serialVersionUID = -7756088163755119603L;
45  
46      public AbstractOIDCSSOLoginFormPanel(final String id, final BaseSession session) {
47          super(id);
48  
49          List<OIDCC4UIProviderTO> available = session.getAnonymousService(OIDCC4UIProviderService.class).list();
50  
51          final Model<OIDCC4UIProviderTO> model = new Model<>();
52          AjaxDropDownChoicePanel<OIDCC4UIProviderTO> ops =
53                  new AjaxDropDownChoicePanel<>("ops", "OpenID Connect", model, false);
54          ops.setChoices(available);
55          ops.setChoiceRenderer(new IChoiceRenderer<>() {
56  
57              private static final long serialVersionUID = 1814750973898916102L;
58  
59              @Override
60              public Object getDisplayValue(final OIDCC4UIProviderTO object) {
61                  return object.getName();
62              }
63  
64              @Override
65              public String getIdValue(final OIDCC4UIProviderTO object, final int index) {
66                  return object.getName();
67              }
68  
69              @Override
70              public OIDCC4UIProviderTO getObject(final String id,
71                                                  final IModel<? extends List<? extends OIDCC4UIProviderTO>> choices) {
72  
73                  return choices.getObject().stream().
74                      filter(object -> object.getName().equals(id)).findFirst().orElse(null);
75              }
76          });
77  
78          ops.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(OIDCC4UIConstants.URL_CONTEXT + "/login?op="
88                                          + URLEncoder.encode(model.getObject().getName(), StandardCharsets.UTF_8),
89                                          RequestCycle.get())));
90                      } catch (Exception e) {
91                          LOG.error("Could not redirect to the selected OP {}", model.getObject().getName(), e);
92                      }
93                  }
94              }
95          });
96          ops.setOutputMarkupPlaceholderTag(true);
97          ops.setVisible(!available.isEmpty());
98          add(ops);
99      }
100 }