use of javax.security.auth.callback.NameCallback in project jdk8u_jdk by JetBrains.
the class TestSampleLoginModule method login.
/*
* Authenticate the user by comparing the values of the java properties
* (username and password) against the values of the credentials.
* */
public boolean login() throws LoginException {
String credentials_username = null;
String credentials_password = null;
String authenticated_username = System.getProperty("susername");
String authenticated_password = System.getProperty("spassword");
System.out.println("TestSampleLoginModule::login: Start");
// First retreive the credentials {username, password} from
// the callback handler
Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("username");
callbacks[1] = new PasswordCallback("password", false);
try {
callbackHandler.handle(callbacks);
credentials_username = ((NameCallback) callbacks[0]).getName();
credentials_password = new String(((PasswordCallback) callbacks[1]).getPassword());
} catch (Exception e) {
throw new LoginException(e.toString());
}
System.out.println("TestSampleLoginModule::login: credentials username = " + credentials_username);
System.out.println("TestSampleLoginModule::login: credentials password = " + credentials_password);
System.out.println("TestSampleLoginModule::login: authenticated username = " + authenticated_username);
System.out.println("TestSampleLoginModule::login: authenticated password = " + authenticated_password);
if (credentials_username.equals(authenticated_username) && credentials_password.equals(authenticated_password)) {
System.out.println("TestSampleLoginModule::login: " + "Authentication should succeed");
return true;
} else {
System.out.println("TestSampleLoginModule::login: " + "Authentication should reject");
throw new LoginException("TestSampleLoginModule throws EXCEPTION");
}
}
use of javax.security.auth.callback.NameCallback in project jdk8u_jdk by JetBrains.
the class SampleCallbackHandler method handle.
public void handle(Callback[] callbacks) throws java.io.IOException, UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof NameCallback) {
NameCallback cb = (NameCallback) callbacks[i];
cb.setName(getInput(cb.getPrompt()));
} else if (callbacks[i] instanceof PasswordCallback) {
PasswordCallback cb = (PasswordCallback) callbacks[i];
String pw = getInput(cb.getPrompt());
char[] passwd = new char[pw.length()];
pw.getChars(0, passwd.length, passwd, 0);
cb.setPassword(passwd);
} else if (callbacks[i] instanceof RealmCallback) {
RealmCallback cb = (RealmCallback) callbacks[i];
cb.setText(getInput(cb.getPrompt()));
} else {
throw new UnsupportedCallbackException(callbacks[i]);
}
}
}
use of javax.security.auth.callback.NameCallback in project jdk8u_jdk by JetBrains.
the class CleanState method go.
void go() throws Exception {
Krb5LoginModule krb5 = new Krb5LoginModule();
final String name = OneKDC.USER;
final char[] password = OneKDC.PASS;
char[] badpassword = "hellokitty".toCharArray();
Map<String, String> map = new HashMap<>();
map.put("useTicketCache", "false");
map.put("doNotPrompt", "false");
map.put("tryFirstPass", "true");
Map<String, Object> shared = new HashMap<>();
shared.put("javax.security.auth.login.name", name);
shared.put("javax.security.auth.login.password", badpassword);
krb5.initialize(new Subject(), new CallbackHandler() {
@Override
public void handle(Callback[] callbacks) {
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
((NameCallback) callback).setName(name);
}
if (callback instanceof PasswordCallback) {
((PasswordCallback) callback).setPassword(password);
}
}
}
}, shared, map);
krb5.login();
}
use of javax.security.auth.callback.NameCallback in project druid by alibaba.
the class DruidDataSourceTest6 method setUp.
protected void setUp() throws Exception {
returnEmptyCount.set(0);
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setTestOnBorrow(true);
dataSource.setInitialSize(1);
dataSource.setValidationQuery("select 1");
dataSource.setValidationQueryTimeout(10);
dataSource.setQueryTimeout(100);
dataSource.setUserCallback(new NameCallback("xx") {
});
dataSource.setPasswordCallback(new DruidPasswordCallback() {
@Override
public char[] getPassword() {
return "xx".toCharArray();
}
});
dataSource.getProxyFilters().add(new FilterAdapter() {
public ResultSetProxy statement_executeQuery(FilterChain chain, StatementProxy statement, String sql) throws SQLException {
if (errorCount.get() > 0) {
errorCount.decrementAndGet();
throw new RuntimeException();
}
if (returnEmptyCount.get() > 0) {
returnEmptyCount.decrementAndGet();
return new ResultSetProxyImpl(statement, new MockResultSet(statement), 0, sql);
}
return chain.statement_executeQuery(statement, sql);
}
});
}
use of javax.security.auth.callback.NameCallback in project druid by alibaba.
the class DataSourceProxyImpl method connect.
public ConnectionProxy connect(Properties info) throws SQLException {
this.properties = info;
PasswordCallback passwordCallback = this.config.getPasswordCallback();
if (passwordCallback != null) {
char[] chars = passwordCallback.getPassword();
String password = new String(chars);
info.put("password", password);
}
NameCallback userCallback = this.config.getUserCallback();
if (userCallback != null) {
String user = userCallback.getName();
info.put("user", user);
}
FilterChain chain = new FilterChainImpl(this);
return chain.connection_connect(info);
}
Aggregations