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.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                            return false;
044                    }
045    
046                    Subject subject = getSubject();
047    
048                    Set<Principal> principals = subject.getPrincipals();
049    
050                    principals.add(getPrincipal());
051    
052                    Set<Object> privateCredentials = subject.getPrivateCredentials();
053    
054                    privateCredentials.add(getPassword());
055    
056                    try {
057                            Principal group = (Principal)InstanceFactory.newInstance(
058                                    _JGROUP, String.class, "Roles");
059                            Object role = InstanceFactory.newInstance(
060                                    _JROLE, String.class, "users");
061    
062                            MethodKey methodKey = new MethodKey(
063                                    ClassResolverUtil.resolveByContextClassLoader(_JGROUP),
064                                    "addMember", role.getClass());
065    
066                            Method method = methodKey.getMethod();
067    
068                            method.invoke(group, new Object[] {role});
069    
070                            principals.add(group);
071                    }
072                    catch (Exception e) {
073                            _log.error(e, e);
074                    }
075    
076                    return commitValue;
077            }
078    
079            @Override
080            protected Principal getPortalPrincipal(String name) throws LoginException {
081                    try {
082                            return (Principal)InstanceFactory.newInstance(
083                                    _JPRINCIPAL, String.class, name);
084                    }
085                    catch (Exception e) {
086                            throw new LoginException(e.getMessage());
087                    }
088            }
089    
090            private static final String _JGROUP =
091                    "org.objectweb.jonas.security.auth.JGroup";
092    
093            private static final String _JPRINCIPAL =
094                    "org.objectweb.jonas.security.auth.JPrincipal";
095    
096            private static final String _JROLE =
097                    "org.objectweb.jonas.security.auth.JRole";
098    
099            private static Log _log = LogFactoryUtil.getLog(PortalLoginModule.class);
100    
101    }