001 /*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020 package org.crsh.auth;
021
022 import org.crsh.plugin.CRaSHPlugin;
023 import org.crsh.plugin.PropertyDescriptor;
024
025 import javax.security.auth.Subject;
026 import javax.security.auth.callback.Callback;
027 import javax.security.auth.callback.CallbackHandler;
028 import javax.security.auth.callback.NameCallback;
029 import javax.security.auth.callback.PasswordCallback;
030 import javax.security.auth.callback.UnsupportedCallbackException;
031 import javax.security.auth.login.LoginContext;
032 import java.io.IOException;
033 import java.util.Collections;
034
035 public class JaasAuthenticationPlugin extends CRaSHPlugin<AuthenticationPlugin> implements AuthenticationPlugin {
036
037 /** . */
038 static final PropertyDescriptor<String> JAAS_DOMAIN = PropertyDescriptor.create("auth.jaas.domain", (String)null, "The JAAS domain name used for authentication");
039
040 public String getName() {
041 return "jaas";
042 }
043
044 @Override
045 protected Iterable<PropertyDescriptor<?>> createConfigurationCapabilities() {
046 return Collections.<PropertyDescriptor<?>>singletonList(JAAS_DOMAIN);
047 }
048
049 public boolean authenticate(final String username, final String password) throws Exception {
050 String domain = getContext().getProperty(JAAS_DOMAIN);
051 if (domain != null) {
052 log.debug("Will use the JAAS domain '" + domain + "' for authenticating user " + username);
053 LoginContext loginContext = new LoginContext(domain, new Subject(), new CallbackHandler() {
054 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
055 for (Callback c : callbacks) {
056 if (c instanceof NameCallback) {
057 ((NameCallback)c).setName(username);
058 }
059 else if (c instanceof PasswordCallback) {
060 ((PasswordCallback)c).setPassword(password.toCharArray());
061 }
062 else {
063 throw new UnsupportedCallbackException(c);
064 }
065 }
066 }
067 });
068
069 //
070 try {
071 loginContext.login();
072 loginContext.logout();
073 log.debug("Authenticated user " + username + " against the JAAS domain '" + domain + "'");
074 return true;
075 }
076 catch (Exception e) {
077 if (log.isDebugEnabled()) log.error("Exception when authenticating user " + username + " to JAAS domain '" + domain + "'", e);
078 return false;
079 }
080 }
081 else {
082 log.warn("The JAAS domain property '" + JAAS_DOMAIN.name + "' was not found");
083 return false;
084 }
085 }
086
087 @Override
088 public AuthenticationPlugin getImplementation() {
089 return this;
090 }
091 }