001    /**
002     * Copyright (c) 2000-2012 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.ClassResolverUtil;
020    import com.liferay.portal.kernel.util.InstanceFactory;
021    import com.liferay.portal.kernel.util.MethodKey;
022    import com.liferay.portal.security.jaas.ext.BasicLoginModule;
023    
024    import java.lang.reflect.Method;
025    
026    import java.security.Principal;
027    
028    import java.util.Set;
029    
030    import javax.security.auth.Subject;
031    import javax.security.auth.login.LoginException;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class PortalLoginModule extends BasicLoginModule {
037    
038            @Override
039            public boolean commit() throws LoginException {
040                    boolean commitValue = super.commit();
041    
042                    if (commitValue) {
043                            Subject subject = getSubject();
044    
045                            Set<Principal> principals = subject.getPrincipals();
046    
047                            principals.add(getPrincipal());
048    
049                            Set<Object> privateCredentials = subject.getPrivateCredentials();
050    
051                            privateCredentials.add(getPassword());
052    
053                            try {
054                                    Principal group = (Principal)InstanceFactory.newInstance(
055                                            _JGROUP, String.class, "Roles");
056                                    Object role = InstanceFactory.newInstance(
057                                            _JROLE, String.class, "users");
058    
059                                    MethodKey methodKey = new MethodKey(
060                                            ClassResolverUtil.resolveByContextClassLoader(_JGROUP),
061                                            "addMember", role.getClass());
062    
063                                    Method method = methodKey.getMethod();
064    
065                                    method.invoke(group, new Object[] {role});
066    
067                                    principals.add(group);
068                            }
069                            catch (Exception e) {
070                                    _log.error(e, e);
071                            }
072                    }
073    
074                    return commitValue;
075            }
076    
077            @Override
078            protected Principal getPortalPrincipal(String name) throws LoginException {
079                    try {
080                            return (Principal)InstanceFactory.newInstance(
081                                    _JPRINCIPAL, String.class, name);
082                    }
083                    catch (Exception e) {
084                            throw new LoginException(e.getMessage());
085                    }
086            }
087    
088            private static final String _JGROUP =
089                    "org.objectweb.jonas.security.auth.JGroup";
090    
091            private static final String _JPRINCIPAL =
092                    "org.objectweb.jonas.security.auth.JPrincipal";
093    
094            private static final String _JROLE =
095                    "org.objectweb.jonas.security.auth.JRole";
096    
097            private static Log _log = LogFactoryUtil.getLog(PortalLoginModule.class);
098    
099    }