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.enduser.pages;
20  
21  import java.security.AccessControlException;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.List;
25  import java.util.stream.Collectors;
26  import java.util.stream.Stream;
27  import org.apache.syncope.client.enduser.SyncopeEnduserSession;
28  import org.apache.syncope.client.enduser.SyncopeWebApplication;
29  import org.apache.syncope.client.ui.commons.BaseLogin;
30  import org.apache.syncope.client.ui.commons.BaseSession;
31  import org.apache.wicket.Component;
32  import org.apache.wicket.ajax.AjaxRequestTarget;
33  import org.apache.wicket.markup.html.link.BookmarkablePageLink;
34  import org.apache.wicket.markup.html.panel.Panel;
35  import org.apache.wicket.request.mapper.parameter.PageParameters;
36  
37  public class Login extends BaseLogin {
38  
39      private static final long serialVersionUID = 5889157642852559004L;
40  
41      private final BookmarkablePageLink<Void> selfPwdReset;
42  
43      private final BookmarkablePageLink<Void> selfRegistration;
44  
45      public Login(final PageParameters parameters) {
46          super(parameters);
47  
48          selfPwdReset = new BookmarkablePageLink<>("self-pwd-reset", SelfPasswordReset.class);
49          selfPwdReset.getPageParameters().add("domain", SyncopeEnduserSession.get().getDomain());
50          selfPwdReset.setVisible(SyncopeEnduserSession.get().getPlatformInfo().isPwdResetAllowed());
51          add(selfPwdReset.setOutputMarkupId(true).setOutputMarkupPlaceholderTag(true));
52  
53          selfRegistration = new BookmarkablePageLink<>("self-registration", SelfRegistration.class);
54          selfRegistration.getPageParameters().add("domain", SyncopeEnduserSession.get().getDomain());
55          selfRegistration.setVisible(SyncopeEnduserSession.get().getPlatformInfo().isSelfRegAllowed());
56          add(selfRegistration.setOutputMarkupId(true).setOutputMarkupPlaceholderTag(true));
57      }
58  
59      @Override
60      protected Collection<Component> getLanguageOnChangeComponents() {
61          return Stream.concat(
62                  super.getLanguageOnChangeComponents().stream(),
63                  List.of(selfRegistration, selfPwdReset).stream()).
64                  collect(Collectors.toList());
65      }
66  
67      @Override
68      protected BaseSession getBaseSession() {
69          return SyncopeEnduserSession.get();
70      }
71  
72      @Override
73      protected List<Panel> getSSOLoginFormPanels() {
74          List<Panel> ssoLoginFormPanels = new ArrayList<>();
75          SyncopeWebApplication.get().getLookup().getSSOLoginFormPanels().forEach(ssoLoginFormPanel -> {
76              try {
77                  ssoLoginFormPanels.add(ssoLoginFormPanel.getConstructor(String.class, BaseSession.class).newInstance(
78                          "ssoLogin", SyncopeEnduserSession.get()));
79              } catch (Exception e) {
80                  LOG.error("Could not initialize the provided SSO login form panel", e);
81              }
82          });
83          return ssoLoginFormPanels;
84      }
85  
86      @Override
87      protected void sendError(final String error) {
88          SyncopeEnduserSession.get().error(error);
89      }
90  
91      @Override
92      protected void authenticate(final String username, final String password, final AjaxRequestTarget target)
93              throws AccessControlException {
94  
95          if (SyncopeWebApplication.get().getAnonymousUser().equals(username)
96                  || SyncopeWebApplication.get().getAdminUser().equals(username)) {
97  
98              throw new AccessControlException("Illegal username");
99          }
100 
101         if (SyncopeEnduserSession.get().authenticate(username, password)) {
102             // If login has been called because the user was not yet logged in, than continue to the
103             // original destination, otherwise to the Home page
104             continueToOriginalDestination();
105             setResponsePage(getApplication().getHomePage());
106         } else {
107             SyncopeEnduserSession.get().error(getString("login-error"));
108             notificationPanel.refresh(target);
109         }
110     }
111 }