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.sso;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.logging.log4j.LogManager;
22  import org.apache.logging.log4j.Logger;
23  import org.codelibs.fess.mylasta.action.FessUserBean;
24  import org.codelibs.fess.util.ComponentUtil;
25  import org.lastaflute.web.login.credential.LoginCredential;
26  import org.lastaflute.web.response.ActionResponse;
27  
28  public class SsoManager {
29      private static final Logger logger = LogManager.getLogger(SsoManager.class);
30  
31      protected static final String SSO_TYPE = "sso.type";
32  
33      protected static final String NONE = "none";
34  
35      protected final List<SsoAuthenticator> authenticatorList = new ArrayList<>();
36  
37      public boolean available() {
38          final String ssoType = getSsoType();
39          if (logger.isDebugEnabled()) {
40              logger.debug("sso.type: {}", ssoType);
41          }
42          return !NONE.equals(ssoType);
43      }
44  
45      public LoginCredential getLoginCredential() {
46          if (available()) {
47              final SsoAuthenticator authenticator = getAuthenticator();
48              if (authenticator != null) {
49                  return authenticator.getLoginCredential();
50              }
51          }
52          return null;
53      }
54  
55      public ActionResponse getResponse(final SsoResponseType responseType) {
56          if (available()) {
57              final SsoAuthenticator authenticator = getAuthenticator();
58              if (authenticator != null) {
59                  return authenticator.getResponse(responseType);
60              }
61          }
62          return null;
63      }
64  
65      public String logout(final FessUserBean user) {
66          if (available()) {
67              final SsoAuthenticator authenticator = getAuthenticator();
68              if (authenticator != null) {
69                  return authenticator.logout(user);
70              }
71          }
72          return null;
73      }
74  
75      protected SsoAuthenticator getAuthenticator() {
76          final String name = getSsoType() + "Authenticator";
77          if (ComponentUtil.hasComponent(name)) {
78              return ComponentUtil.getComponent(name);
79          }
80          return null;
81      }
82  
83      protected String getSsoType() {
84          return ComponentUtil.getFessConfig().getSystemProperty(SSO_TYPE, NONE);
85      }
86  
87      public SsoAuthenticator[] getAuthenticators() {
88          return authenticatorList.toArray(new SsoAuthenticator[authenticatorList.size()]);
89      }
90  
91      public void register(final SsoAuthenticator authenticator) {
92          if (logger.isInfoEnabled()) {
93              logger.info("Load {}", authenticator.getClass().getSimpleName());
94          }
95          authenticatorList.add(authenticator);
96      }
97  }