001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.jaas;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.InstanceFactory;
020    import com.liferay.portal.kernel.util.ServerDetector;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.util.PropsValues;
023    
024    import java.util.Map;
025    
026    import javax.security.auth.Subject;
027    import javax.security.auth.callback.CallbackHandler;
028    import javax.security.auth.login.LoginException;
029    import javax.security.auth.spi.LoginModule;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class PortalLoginModule implements LoginModule {
035    
036            public PortalLoginModule() {
037                    LoginModule loginModule = null;
038    
039                    if (Validator.isNotNull(PropsValues.PORTAL_JAAS_IMPL)) {
040                            try {
041                                    loginModule = (LoginModule)InstanceFactory.newInstance(
042                                            PropsValues.PORTAL_JAAS_IMPL);
043                            }
044                            catch (Exception e) {
045                                    _log.error(e, e);
046                            }
047                    }
048    
049                    if (loginModule == null) {
050                            if (ServerDetector.isJBoss()) {
051                                    loginModule =
052                                            new com.liferay.portal.security.jaas.ext.jboss.
053                                                    PortalLoginModule();
054                            }
055                            else if (ServerDetector.isJetty()) {
056                                    loginModule =
057                                            new com.liferay.portal.security.jaas.ext.jetty.
058                                                    PortalLoginModule();
059                            }
060                            else if (ServerDetector.isJOnAS()) {
061                                    loginModule =
062                                            new com.liferay.portal.security.jaas.ext.jonas.
063                                                    PortalLoginModule();
064                            }
065                            else if (ServerDetector.isResin()) {
066                                    loginModule =
067                                            new com.liferay.portal.security.jaas.ext.resin.
068                                                    PortalLoginModule();
069                            }
070                            else if (ServerDetector.isTomcat()) {
071                                    loginModule =
072                                            new com.liferay.portal.security.jaas.ext.tomcat.
073                                                    PortalLoginModule();
074                            }
075                            else if (ServerDetector.isWebLogic()) {
076                                    loginModule =
077                                            new com.liferay.portal.security.jaas.ext.weblogic.
078                                                    PortalLoginModule();
079                            }
080                    }
081    
082                    if (_log.isDebugEnabled()) {
083                            _log.debug(loginModule.getClass().getName());
084                    }
085    
086                    _loginModule = loginModule;
087            }
088    
089            @Override
090            public boolean abort() throws LoginException {
091                    return _loginModule.abort();
092            }
093    
094            @Override
095            public boolean commit() throws LoginException {
096                    return _loginModule.commit();
097            }
098    
099            @Override
100            public void initialize(
101                    Subject subject, CallbackHandler callbackHandler,
102                    Map<String, ?> sharedState, Map<String, ?> options) {
103    
104                    _loginModule.initialize(subject, callbackHandler, sharedState, options);
105            }
106    
107            @Override
108            public boolean login() throws LoginException {
109                    return _loginModule.login();
110            }
111    
112            @Override
113            public boolean logout() throws LoginException {
114                    return _loginModule.logout();
115            }
116    
117            private static final Log _log = LogFactoryUtil.getLog(
118                    PortalLoginModule.class);
119    
120            private final LoginModule _loginModule;
121    
122    }