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.console.pages;
20  
21  import java.security.AccessControlException;
22  import java.util.ArrayList;
23  import java.util.List;
24  import org.apache.syncope.client.console.SyncopeConsoleSession;
25  import org.apache.syncope.client.console.SyncopeWebApplication;
26  import org.apache.syncope.client.ui.commons.BaseLogin;
27  import org.apache.syncope.client.ui.commons.BaseSession;
28  import org.apache.syncope.client.ui.commons.panels.BaseSSOLoginFormPanel;
29  import org.apache.wicket.ajax.AjaxRequestTarget;
30  import org.apache.wicket.authentication.IAuthenticationStrategy;
31  import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
32  import org.apache.wicket.markup.html.panel.Panel;
33  import org.apache.wicket.request.mapper.parameter.PageParameters;
34  
35  public class Login extends BaseLogin {
36  
37      private static final long serialVersionUID = 5889157642852559004L;
38  
39      public Login(final PageParameters parameters) {
40          super(parameters);
41      }
42  
43      @Override
44      protected BaseSession getBaseSession() {
45          return SyncopeConsoleSession.get();
46      }
47  
48      @Override
49      protected List<Panel> getSSOLoginFormPanels() {
50          List<Panel> ssoLoginFormPanels = new ArrayList<>();
51          SyncopeWebApplication.get().getLookup().getClasses(BaseSSOLoginFormPanel.class).forEach(ssoLoginFormPanel -> {
52              try {
53                  ssoLoginFormPanels.add(ssoLoginFormPanel.getConstructor(String.class, BaseSession.class).newInstance(
54                          "ssoLogin", SyncopeConsoleSession.get()));
55              } catch (Exception e) {
56                  LOG.error("Could not initialize the provided SSO login form panel", e);
57              }
58          });
59          return ssoLoginFormPanels;
60      }
61  
62      @Override
63      protected void sendError(final String error) {
64          SyncopeConsoleSession.get().error(error);
65      }
66  
67      @Override
68      protected void authenticate(final String username, final String password, final AjaxRequestTarget target)
69              throws AccessControlException {
70  
71          if (SyncopeWebApplication.get().getAnonymousUser().equals(username)) {
72              throw new AccessControlException("Illegal username");
73          }
74  
75          IAuthenticationStrategy strategy = getApplication().getSecuritySettings().getAuthenticationStrategy();
76  
77          if (AuthenticatedWebSession.get().signIn(username, password)) {
78              // If login has been called because the user was not yet logged in, than continue to the
79              // original destination, otherwise to the Home page
80              continueToOriginalDestination();
81              setResponsePage(getApplication().getHomePage());
82          } else {
83              SyncopeConsoleSession.get().error(getString("login-error"));
84              notificationPanel.refresh(target);
85          }
86          strategy.remove();
87      }
88  }