View Javadoc
1   /*
2    * Copyright 2012-2019 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.RootAction;
19  import org.codelibs.fess.app.web.base.FessLoginAction;
20  import org.codelibs.fess.app.web.base.login.ActionResponseCredential;
21  import org.codelibs.fess.app.web.login.LoginAction;
22  import org.codelibs.fess.sso.SsoManager;
23  import org.codelibs.fess.util.ComponentUtil;
24  import org.dbflute.optional.OptionalThing;
25  import org.lastaflute.web.Execute;
26  import org.lastaflute.web.login.credential.LoginCredential;
27  import org.lastaflute.web.login.exception.LoginFailureException;
28  import org.lastaflute.web.response.ActionResponse;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  public class SsoAction extends FessLoginAction {
33      // ===================================================================================
34      //                                                                            Constant
35      //
36      private static final Logger logger = LoggerFactory.getLogger(SsoAction.class);
37  
38      // ===================================================================================
39      //                                                                       Login Execute
40      //                                                                      ==============
41  
42      @Execute
43      public ActionResponse index() {
44          if (fessLoginAssist.getSavedUserBean().isPresent()) {
45              return redirect(RootAction.class);
46          }
47          final SsoManager ssoManager = ComponentUtil.getSsoManager();
48          final LoginCredential loginCredential = ssoManager.getLoginCredential();
49          if (loginCredential == null) {
50              if (logger.isDebugEnabled()) {
51                  logger.debug("No user in SSO request.");
52              }
53              if (ssoManager.available()) {
54                  saveError(messages -> messages.addErrorsSsoLoginError(GLOBAL));
55              }
56              return redirect(LoginAction.class);
57          } else if (loginCredential instanceof ActionResponseCredential) {
58              return ((ActionResponseCredential) loginCredential).execute();
59          }
60          try {
61              return fessLoginAssist.loginRedirect(loginCredential, op -> {}, () -> {
62                  activityHelper.login(getUserBean());
63                  userInfoHelper.deleteUserCodeFromCookie(request);
64                  return getHtmlResponse();
65              });
66          } catch (final LoginFailureException lfe) {
67              if (logger.isDebugEnabled()) {
68                  logger.debug("SSO login failure.", lfe);
69              }
70              if (ssoManager.available()) {
71                  saveError(messages -> messages.addErrorsSsoLoginError(GLOBAL));
72              }
73              activityHelper.loginFailure(OptionalThing.of(loginCredential));
74              return redirect(LoginAction.class);
75          }
76      }
77  }