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                            Class<?> clazz = loginModule.getClass();
084    
085                            _log.debug(clazz.getName());
086                    }
087    
088                    _loginModule = loginModule;
089            }
090    
091            @Override
092            public boolean abort() throws LoginException {
093                    return _loginModule.abort();
094            }
095    
096            @Override
097            public boolean commit() throws LoginException {
098                    return _loginModule.commit();
099            }
100    
101            @Override
102            public void initialize(
103                    Subject subject, CallbackHandler callbackHandler,
104                    Map<String, ?> sharedState, Map<String, ?> options) {
105    
106                    _loginModule.initialize(subject, callbackHandler, sharedState, options);
107            }
108    
109            @Override
110            public boolean login() throws LoginException {
111                    return _loginModule.login();
112            }
113    
114            @Override
115            public boolean logout() throws LoginException {
116                    return _loginModule.logout();
117            }
118    
119            private static final Log _log = LogFactoryUtil.getLog(
120                    PortalLoginModule.class);
121    
122            private final LoginModule _loginModule;
123    
124    }