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.sso;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.codelibs.fess.util.ComponentUtil;
22  import org.lastaflute.web.login.credential.LoginCredential;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  public class SsoManager {
27      private static final Logger logger = LoggerFactory.getLogger(SsoManager.class);
28  
29      protected static final String SSO_TYPE = "sso.type";
30  
31      protected static final String NONE = "none";
32  
33      protected final List<SsoAuthenticator> authenticatorList = new ArrayList<>();
34  
35      public boolean available() {
36          final String ssoType = getSsoType();
37          if (logger.isDebugEnabled()) {
38              logger.debug("sso.type: {}", ssoType);
39          }
40          return !NONE.equals(ssoType);
41      }
42  
43      public LoginCredential getLoginCredential() {
44          if (available()) {
45              final SsoAuthenticator authenticator = ComponentUtil.getComponent(getSsoType() + "Authenticator");
46              return authenticator.getLoginCredential();
47          }
48          return null;
49      }
50  
51      protected String getSsoType() {
52          return ComponentUtil.getFessConfig().getSystemProperty(SSO_TYPE, NONE);
53      }
54  
55      public SsoAuthenticator[] getAuthenticators() {
56          return authenticatorList.toArray(new SsoAuthenticator[authenticatorList.size()]);
57      }
58  
59      public void register(final SsoAuthenticator authenticator) {
60          if (logger.isInfoEnabled()) {
61              logger.info("Load " + authenticator.getClass().getSimpleName());
62          }
63          authenticatorList.add(authenticator);
64      }
65  }