package org.apache.sshd.server.jaas;
import java.util.Map;
import java.util.HashMap;
import java.io.IOException;
import javax.security.auth.login.Configuration;
import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;
import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import static org.junit.Assert.*;
@Before
Configuration config = new Configuration() {
return new AppConfigurationEntry[] {
new AppConfigurationEntry(DummyLoginModule.class.getName(),
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
new HashMap<String,Object>())
};
}
}
};
Configuration.setConfiguration(config);
}
@After
Configuration.setConfiguration(null);
}
@Test
JaasPasswordAuthenticator auth = new JaasPasswordAuthenticator();
assertNull(auth.getDomain());
auth.setDomain("domain");
assertEquals("domain", auth.getDomain());
assertTrue(auth.authenticate("sshd", "sshd"));
assertFalse(auth.authenticate("sshd", "dummy"));
}
private Subject subject;
private CallbackHandler callbackHandler;
}
public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) {
this.subject = subject;
this.callbackHandler = callbackHandler;
}
public boolean login()
throws LoginException {
Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("Username: ");
callbacks[1] = new PasswordCallback("Password: ", false);
try {
callbackHandler.handle(callbacks);
} catch (IOException ioe) {
throw new LoginException(ioe.getMessage());
} catch (UnsupportedCallbackException uce) {
throw new LoginException(uce.getMessage() + " not available to obtain information from user");
}
String user = ((NameCallback) callbacks[0]).getName();
char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();
return user.equals(new String(tmpPassword));
}
public boolean commit()
throws LoginException {
return true;
}
public boolean abort()
throws LoginException {
return true;
}
public boolean logout()
throws LoginException {
return true;
}
}
}