Search in sources :

Example 51 with NameCallback

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");
    }
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) LoginException(javax.security.auth.login.LoginException) LoginException(javax.security.auth.login.LoginException)

Example 52 with NameCallback

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]);
        }
    }
}
Also used : NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RealmCallback(javax.security.sasl.RealmCallback)

Example 53 with NameCallback

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();
}
Also used : Krb5LoginModule(com.sun.security.auth.module.Krb5LoginModule) CallbackHandler(javax.security.auth.callback.CallbackHandler) HashMap(java.util.HashMap) Subject(javax.security.auth.Subject) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback)

Example 54 with NameCallback

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);
        }
    });
}
Also used : SQLException(java.sql.SQLException) FilterChain(com.alibaba.druid.filter.FilterChain) FilterAdapter(com.alibaba.druid.filter.FilterAdapter) MockResultSet(com.alibaba.druid.mock.MockResultSet) ResultSetProxyImpl(com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) NameCallback(javax.security.auth.callback.NameCallback) StatementProxy(com.alibaba.druid.proxy.jdbc.StatementProxy) DruidPasswordCallback(com.alibaba.druid.util.DruidPasswordCallback) ResultSetProxy(com.alibaba.druid.proxy.jdbc.ResultSetProxy)

Example 55 with NameCallback

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);
}
Also used : FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl) NameCallback(javax.security.auth.callback.NameCallback) FilterChain(com.alibaba.druid.filter.FilterChain) PasswordCallback(javax.security.auth.callback.PasswordCallback)

Aggregations

NameCallback (javax.security.auth.callback.NameCallback)203 PasswordCallback (javax.security.auth.callback.PasswordCallback)161 Callback (javax.security.auth.callback.Callback)140 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)101 IOException (java.io.IOException)60 LoginException (javax.security.auth.login.LoginException)51 CallbackHandler (javax.security.auth.callback.CallbackHandler)27 ChoiceCallback (javax.security.auth.callback.ChoiceCallback)22 ConfirmationCallback (javax.security.auth.callback.ConfirmationCallback)22 RealmCallback (javax.security.sasl.RealmCallback)22 Subject (javax.security.auth.Subject)19 FailedLoginException (javax.security.auth.login.FailedLoginException)19 LoginContext (javax.security.auth.login.LoginContext)18 AuthorizeCallback (javax.security.sasl.AuthorizeCallback)18 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)17 Test (org.testng.annotations.Test)15 HashMap (java.util.HashMap)14 Test (org.junit.Test)14 IdRepoException (com.sun.identity.idm.IdRepoException)13 TextOutputCallback (javax.security.auth.callback.TextOutputCallback)11