View Javadoc
1   /*
2    * Copyright 2012-2017 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.app.web.sso;
17  
18  import org.codelibs.fess.app.web.base.FessLoginAction;
19  import org.codelibs.fess.app.web.base.login.ActionResponseCredential;
20  import org.codelibs.fess.app.web.login.LoginAction;
21  import org.codelibs.fess.sso.SsoManager;
22  import org.codelibs.fess.util.ComponentUtil;
23  import org.lastaflute.web.Execute;
24  import org.lastaflute.web.login.credential.LoginCredential;
25  import org.lastaflute.web.login.exception.LoginFailureException;
26  import org.lastaflute.web.response.ActionResponse;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  public class SsoAction extends FessLoginAction {
31      // ===================================================================================
32      //                                                                            Constant
33      //
34      private static final Logger logger = LoggerFactory.getLogger(SsoAction.class);
35  
36      // ===================================================================================
37      //                                                                       Login Execute
38      //                                                                      ==============
39  
40      @Execute
41      public ActionResponse index() {
42          final SsoManager ssoManager = ComponentUtil.getSsoManager();
43          final LoginCredential loginCredential = ssoManager.getLoginCredential();
44          if (loginCredential == null) {
45              if (logger.isDebugEnabled()) {
46                  logger.debug("No user in SSO request.");
47              }
48              if (ssoManager.available()) {
49                  saveError(messages -> messages.addErrorsSsoLoginError(GLOBAL));
50              }
51              return redirect(LoginAction.class);
52          } else if (loginCredential instanceof ActionResponseCredential) {
53              return ((ActionResponseCredential) loginCredential).execute();
54          }
55          try {
56              return fessLoginAssist.loginRedirect(loginCredential, op -> {}, () -> {
57                  activityHelper.login(getUserBean());
58                  userInfoHelper.deleteUserCodeFromCookie(request);
59                  return getHtmlResponse();
60              });
61          } catch (final LoginFailureException lfe) {
62              if (logger.isDebugEnabled()) {
63                  logger.debug("SSO login failure.", lfe);
64              }
65              if (ssoManager.available()) {
66                  saveError(messages -> messages.addErrorsSsoLoginError(GLOBAL));
67              }
68              return redirect(LoginAction.class);
69          }
70      }
71  }