001    /**
002     * Copyright (c) 2000-2013 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.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            @Override
038            public boolean commit() throws LoginException {
039                    boolean commitValue = super.commit();
040    
041                    if (commitValue) {
042                            Subject subject = getSubject();
043    
044                            Set<Principal> principals = subject.getPrincipals();
045    
046                            principals.add(getPrincipal());
047    
048                            Set<Object> privateCredentials = subject.getPrivateCredentials();
049    
050                            privateCredentials.add(getPassword());
051    
052                            try {
053                                    Principal group = (Principal)InstanceFactory.newInstance(
054                                            _JGROUP, String.class, "Roles");
055                                    Object role = InstanceFactory.newInstance(
056                                            _JROLE, String.class, "users");
057    
058                                    Method method = MethodCache.get(
059                                            _JGROUP, "addMember", new Class[] {role.getClass()});
060    
061                                    method.invoke(group, new Object[] {role});
062    
063                                    principals.add(group);
064                            }
065                            catch (Exception e) {
066                                    _log.error(e, e);
067                            }
068                    }
069    
070                    return commitValue;
071            }
072    
073            @Override
074            protected Principal getPortalPrincipal(String name) throws LoginException {
075                    try {
076                            return (Principal)InstanceFactory.newInstance(
077                                    _JPRINCIPAL, String.class, name);
078                    }
079                    catch (Exception e) {
080                            throw new LoginException(e.getMessage());
081                    }
082            }
083    
084            private static final String _JGROUP =
085                    "org.objectweb.jonas.security.auth.JGroup";
086    
087            private static final String _JPRINCIPAL =
088                    "org.objectweb.jonas.security.auth.JPrincipal";
089    
090            private static final String _JROLE =
091                    "org.objectweb.jonas.security.auth.JRole";
092    
093            private static Log _log = LogFactoryUtil.getLog(PortalLoginModule.class);
094    
095    }