View Javadoc
1   /*
2    * Copyright 2012-2021 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.apache.logging.log4j.LogManager;
19  import org.apache.logging.log4j.Logger;
20  import org.codelibs.fess.app.web.RootAction;
21  import org.codelibs.fess.app.web.base.FessLoginAction;
22  import org.codelibs.fess.app.web.base.login.ActionResponseCredential;
23  import org.codelibs.fess.app.web.login.LoginAction;
24  import org.codelibs.fess.exception.SsoMessageException;
25  import org.codelibs.fess.sso.SsoManager;
26  import org.codelibs.fess.sso.SsoResponseType;
27  import org.codelibs.fess.util.ComponentUtil;
28  import org.dbflute.optional.OptionalThing;
29  import org.lastaflute.web.Execute;
30  import org.lastaflute.web.login.credential.LoginCredential;
31  import org.lastaflute.web.login.exception.LoginFailureException;
32  import org.lastaflute.web.response.ActionResponse;
33  
34  public class SsoAction extends FessLoginAction {
35      // ===================================================================================
36      //                                                                            Constant
37      //
38      private static final Logger logger = LogManager.getLogger(SsoAction.class);
39  
40      // ===================================================================================
41      //                                                                       Login Execute
42      //                                                                      ==============
43  
44      @Execute
45      public ActionResponse index() {
46          if (fessLoginAssist.getSavedUserBean().isPresent()) {
47              return redirect(RootAction.class);
48          }
49          final SsoManager ssoManager = ComponentUtil.getSsoManager();
50          final LoginCredential loginCredential = ssoManager.getLoginCredential();
51          if (loginCredential == null) {
52              if (logger.isDebugEnabled()) {
53                  logger.debug("No user in SSO request.");
54              }
55              if (ssoManager.available()) {
56                  saveError(messages -> messages.addErrorsSsoLoginError(GLOBAL));
57              }
58              return redirect(LoginAction.class);
59          }
60          if (loginCredential instanceof ActionResponseCredential) {
61              return ((ActionResponseCredential) loginCredential).execute();
62          }
63          try {
64              return fessLoginAssist.loginRedirect(loginCredential, op -> {}, () -> {
65                  activityHelper.login(getUserBean());
66                  userInfoHelper.deleteUserCodeFromCookie(request);
67                  return getHtmlResponse();
68              });
69          } catch (final LoginFailureException lfe) {
70              if (logger.isDebugEnabled()) {
71                  logger.debug("SSO login failure.", lfe);
72              }
73              if (ssoManager.available()) {
74                  saveError(messages -> messages.addErrorsSsoLoginError(GLOBAL));
75              }
76              activityHelper.loginFailure(OptionalThing.of(loginCredential));
77              return redirect(LoginAction.class);
78          }
79      }
80  
81      @Execute
82      public ActionResponse metadata() {
83          final SsoManager ssoManager = ComponentUtil.getSsoManager();
84          try {
85              final ActionResponse actionResponse = ssoManager.getResponse(SsoResponseType.METADATA);
86              if (actionResponse == null) {
87                  throw responseManager.new400("Unsupported request type.");
88              }
89              return actionResponse;
90          } catch (final SsoMessageException e) {
91              if (e.getCause() == null) {
92                  if (logger.isDebugEnabled()) {
93                      logger.debug("Metadata response.", e);
94                  }
95                  saveInfo(e.getMessageCode());
96              } else {
97                  logger.warn("Failed to process metadata.", e);
98                  saveError(e.getMessageCode());
99              }
100             return redirect(LoginAction.class);
101         }
102     }
103 
104     @Execute
105     public ActionResponse logout() {
106         final SsoManager ssoManager = ComponentUtil.getSsoManager();
107         try {
108             final ActionResponse actionResponse = ssoManager.getResponse(SsoResponseType.LOGOUT);
109             if (actionResponse == null) {
110                 throw responseManager.new400("Unsupported request type.");
111             }
112             return actionResponse;
113         } catch (final SsoMessageException e) {
114             if (e.getCause() == null) {
115                 if (logger.isDebugEnabled()) {
116                     logger.debug("Logout response.", e);
117                 }
118                 saveInfo(e.getMessageCode());
119             } else {
120                 logger.warn("Failed to log out.", e);
121                 saveError(e.getMessageCode());
122             }
123             return redirect(LoginAction.class);
124         }
125     }
126 }