001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.security.jaas.ext.jonas;
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.MethodCache;
021    import com.liferay.portal.security.jaas.ext.BasicLoginModule;
022    
023    import java.lang.reflect.Method;
024    
025    import java.security.Principal;
026    
027    import java.util.Set;
028    
029    import javax.security.auth.Subject;
030    import javax.security.auth.login.LoginException;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class PortalLoginModule extends BasicLoginModule {
036    
037            public boolean commit() throws LoginException {
038                    boolean commitValue = super.commit();
039    
040                    if (commitValue) {
041                            Subject subject = getSubject();
042    
043                            Set<Principal> principals = subject.getPrincipals();
044    
045                            principals.add(getPrincipal());
046    
047                            Set<Object> privateCredentials = subject.getPrivateCredentials();
048    
049                            privateCredentials.add(getPassword());
050    
051                            try {
052                                    Principal group = (Principal)InstanceFactory.newInstance(
053                                            _JGROUP, String.class, "Roles");
054                                    Object role = InstanceFactory.newInstance(
055                                            _JROLE, String.class, "users");
056    
057                                    Method method = MethodCache.get(
058                                            _JGROUP, "addMember", new Class[] {role.getClass()});
059    
060                                    method.invoke(group, new Object[] {role});
061    
062                                    principals.add(group);
063                            }
064                            catch (Exception e) {
065                                    _log.error(e, e);
066                            }
067                    }
068    
069                    return commitValue;
070            }
071    
072            protected Principal getPortalPrincipal(String name) throws LoginException {
073                    try {
074                            return (Principal)InstanceFactory.newInstance(
075                                    _JPRINCIPAL, String.class, name);
076                    }
077                    catch (Exception e) {
078                            throw new LoginException(e.getMessage());
079                    }
080            }
081    
082            private static final String _JGROUP =
083                    "org.objectweb.jonas.security.auth.JGroup";
084    
085            private static final String _JPRINCIPAL =
086                    "org.objectweb.jonas.security.auth.JPrincipal";
087    
088            private static final String _JROLE =
089                    "org.objectweb.jonas.security.auth.JRole";
090    
091            private static Log _log = LogFactoryUtil.getLog(PortalLoginModule.class);
092    
093    }