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;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.security.jaas.PortalPrincipal;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.service.UserLocalServiceUtil;
023    
024    import java.io.IOException;
025    
026    import java.security.Principal;
027    
028    import java.util.Map;
029    import java.util.Set;
030    
031    import javax.security.auth.Subject;
032    import javax.security.auth.callback.Callback;
033    import javax.security.auth.callback.CallbackHandler;
034    import javax.security.auth.callback.NameCallback;
035    import javax.security.auth.callback.PasswordCallback;
036    import javax.security.auth.callback.UnsupportedCallbackException;
037    import javax.security.auth.login.LoginException;
038    import javax.security.auth.spi.LoginModule;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class BasicLoginModule implements LoginModule {
044    
045            public boolean abort() {
046                    return true;
047            }
048    
049            @SuppressWarnings("unused")
050            public boolean commit() throws LoginException {
051                    Principal principal = getPrincipal();
052    
053                    if (principal != null) {
054                            Subject subject = getSubject();
055    
056                            Set<Principal> principals = subject.getPrincipals();
057    
058                            principals.add(getPrincipal());
059    
060                            return true;
061                    }
062                    else {
063                            return false;
064                    }
065            }
066    
067            public void initialize(
068                    Subject subject, CallbackHandler callbackHandler,
069                    Map<String, ?> sharedState, Map<String, ?> options) {
070    
071                    _subject = subject;
072                    _callbackHandler = callbackHandler;
073            }
074    
075            public boolean login() throws LoginException {
076                    String[] credentials = null;
077    
078                    try {
079                            credentials = authenticate();
080                    }
081                    catch (Exception e) {
082                            _log.error(e.getMessage());
083    
084                            throw new LoginException();
085                    }
086    
087                    if ((credentials != null) && (credentials.length == 2)) {
088                            setPrincipal(getPortalPrincipal(credentials[0]));
089                            setPassword(credentials[1]);
090    
091                            return true;
092                    }
093                    else {
094                            throw new LoginException();
095                    }
096            }
097    
098            public boolean logout() {
099                    Subject subject = getSubject();
100    
101                    Set<Principal> principals = subject.getPrincipals();
102    
103                    principals.clear();
104    
105                    return true;
106            }
107    
108            protected String[] authenticate()
109                    throws IOException, UnsupportedCallbackException {
110    
111                    NameCallback nameCallback = new NameCallback("name: ");
112                    PasswordCallback passwordCallback = new PasswordCallback(
113                            "password: ", false);
114    
115                    _callbackHandler.handle(
116                            new Callback[] {nameCallback, passwordCallback});
117    
118                    String name = nameCallback.getName();
119    
120                    String password = null;
121                    char[] passwordChar = passwordCallback.getPassword();
122    
123                    if (passwordChar != null) {
124                            password = new String(passwordChar);
125                    }
126    
127                    if (name == null) {
128                            return new String[] {StringPool.BLANK, StringPool.BLANK};
129                    }
130    
131                    try {
132                            long userId = GetterUtil.getLong(name);
133    
134                            if (UserLocalServiceUtil.authenticateForJAAS(userId, password)) {
135                                    return new String[] {name, password};
136                            }
137                    }
138                    catch (Exception e) {
139                            _log.error(e, e);
140                    }
141    
142                    return null;
143            }
144    
145            protected String getPassword() {
146                    return _password;
147            }
148    
149            @SuppressWarnings("unused")
150            protected Principal getPortalPrincipal(String name) throws LoginException {
151                    return new PortalPrincipal(name);
152            }
153    
154            protected Principal getPrincipal() {
155                    return _principal;
156            }
157    
158            protected Subject getSubject() {
159                    return _subject;
160            }
161    
162            protected void setPassword(String password) {
163                    _password = password;
164            }
165    
166            protected void setPrincipal(Principal principal) {
167                    _principal = principal;
168            }
169    
170            private static Log _log = LogFactoryUtil.getLog(BasicLoginModule.class);
171    
172            private CallbackHandler _callbackHandler;
173            private String _password;
174            private Principal _principal;
175            private Subject _subject;
176    
177    }