Search in sources :

Example 71 with PasswordCallback

use of javax.security.auth.callback.PasswordCallback in project async-http-client by AsyncHttpClient.

the class NamePasswordCallbackHandler method handle.

public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (int i = 0; i < callbacks.length; i++) {
        Callback callback = callbacks[i];
        if (handleCallback(callback)) {
            continue;
        } else if (callback instanceof NameCallback) {
            ((NameCallback) callback).setName(username);
        } else if (callback instanceof PasswordCallback) {
            PasswordCallback pwCallback = (PasswordCallback) callback;
            pwCallback.setPassword(password.toCharArray());
        } else if (!invokePasswordCallback(callback)) {
            String errorMsg = "Unsupported callback type " + callbacks[i].getClass().getName();
            log.info(errorMsg);
            throw new UnsupportedCallbackException(callbacks[i], errorMsg);
        }
    }
}
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) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 72 with PasswordCallback

use of javax.security.auth.callback.PasswordCallback in project ranger by apache.

the class ConsolePromptCallbackHandler method handle.

@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    for (Callback cb : callbacks) {
        if (cb instanceof NameCallback) {
            NameCallback nc = (NameCallback) cb;
            System.out.print(nc.getPrompt());
            System.out.flush();
            String line = null;
            while (line == null) {
                line = reader.readLine();
            }
            nc.setName(line);
        } else if (cb instanceof PasswordCallback) {
            PasswordCallback pc = (PasswordCallback) cb;
            System.out.print(pc.getPrompt());
            System.out.flush();
            String line = null;
            while (line == null) {
                line = reader.readLine();
            }
            pc.setPassword(line.toCharArray());
        } else {
            System.out.println("Unknown callback [" + cb.getClass().getName() + "]");
        }
    }
}
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) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) PasswordCallback(javax.security.auth.callback.PasswordCallback)

Example 73 with PasswordCallback

use of javax.security.auth.callback.PasswordCallback in project keycloak by keycloak.

the class KerberosUsernamePasswordAuthenticator method createJaasCallbackHandler.

protected CallbackHandler createJaasCallbackHandler(final String principal, final String password) {
    return new CallbackHandler() {

        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback callback : callbacks) {
                if (callback instanceof NameCallback) {
                    NameCallback nameCallback = (NameCallback) callback;
                    nameCallback.setName(principal);
                } else if (callback instanceof PasswordCallback) {
                    PasswordCallback passwordCallback = (PasswordCallback) callback;
                    passwordCallback.setPassword(password.toCharArray());
                } else {
                    throw new UnsupportedCallbackException(callback, "Unsupported callback: " + callback.getClass().getCanonicalName());
                }
            }
        }
    };
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) 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) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 74 with PasswordCallback

use of javax.security.auth.callback.PasswordCallback in project apache-kafka-on-k8s by banzaicloud.

the class SaslClientCallbackHandler method handle.

@Override
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
            NameCallback nc = (NameCallback) callback;
            if (!isKerberos && subject != null && !subject.getPublicCredentials(String.class).isEmpty()) {
                nc.setName(subject.getPublicCredentials(String.class).iterator().next());
            } else
                nc.setName(nc.getDefaultName());
        } else if (callback instanceof PasswordCallback) {
            if (!isKerberos && subject != null && !subject.getPrivateCredentials(String.class).isEmpty()) {
                char[] password = subject.getPrivateCredentials(String.class).iterator().next().toCharArray();
                ((PasswordCallback) callback).setPassword(password);
            } else {
                String errorMessage = "Could not login: the client is being asked for a password, but the Kafka" + " client code does not currently support obtaining a password from the user.";
                if (isKerberos) {
                    errorMessage += " Make sure -Djava.security.auth.login.config property passed to JVM and" + " the client is configured to use a ticket cache (using" + " the JAAS configuration setting 'useTicketCache=true)'. Make sure you are using" + " FQDN of the Kafka broker you are trying to connect to.";
                }
                throw new UnsupportedCallbackException(callback, errorMessage);
            }
        } else if (callback instanceof RealmCallback) {
            RealmCallback rc = (RealmCallback) callback;
            rc.setText(rc.getDefaultText());
        } else if (callback instanceof AuthorizeCallback) {
            AuthorizeCallback ac = (AuthorizeCallback) callback;
            String authId = ac.getAuthenticationID();
            String authzId = ac.getAuthorizationID();
            ac.setAuthorized(authId.equals(authzId));
            if (ac.isAuthorized())
                ac.setAuthorizedID(authzId);
        } else if (callback instanceof ScramExtensionsCallback) {
            ScramExtensionsCallback sc = (ScramExtensionsCallback) callback;
            if (!isKerberos && subject != null && !subject.getPublicCredentials(Map.class).isEmpty()) {
                sc.extensions((Map<String, String>) subject.getPublicCredentials(Map.class).iterator().next());
            }
        } else {
            throw new UnsupportedCallbackException(callback, "Unrecognized SASL ClientCallback");
        }
    }
}
Also used : RealmCallback(javax.security.sasl.RealmCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) ScramExtensionsCallback(org.apache.kafka.common.security.scram.ScramExtensionsCallback) AuthorizeCallback(javax.security.sasl.AuthorizeCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) ScramExtensionsCallback(org.apache.kafka.common.security.scram.ScramExtensionsCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) Map(java.util.Map) AuthorizeCallback(javax.security.sasl.AuthorizeCallback) RealmCallback(javax.security.sasl.RealmCallback)

Example 75 with PasswordCallback

use of javax.security.auth.callback.PasswordCallback in project xades4j by luisgoncalves.

the class KeyStoreKeyingDataProvider method ensureInitialized.

private void ensureInitialized() throws UnexpectedJCAException {
    synchronized (this.lockObj) {
        if (!this.initialized) {
            try {
                KeyStore.CallbackHandlerProtection storeLoadProtec = null;
                if (storePasswordProvider != null)
                    // Create the load protection with callback.
                    storeLoadProtec = new KeyStore.CallbackHandlerProtection(new CallbackHandler() {

                        @Override
                        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                            PasswordCallback c = (PasswordCallback) callbacks[0];
                            c.setPassword(storePasswordProvider.getPassword());
                        }
                    });
                else
                    // If no load password provider is supplied is because it shouldn't
                    // be needed. Create a dummy protection because the keystore
                    // builder needs it to be non-null.
                    storeLoadProtec = new KeyStore.CallbackHandlerProtection(new CallbackHandler() {

                        @Override
                        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                            throw new UnsupportedOperationException("No KeyStorePasswordProvider");
                        }
                    });
                this.keyStore = builderCreator.getBuilder(storeLoadProtec).getKeyStore();
            } catch (KeyStoreException ex) {
                throw new UnexpectedJCAException("The keystore couldn't be initialized", ex);
            }
            this.initialized = true;
        }
    }
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore) PasswordCallback(javax.security.auth.callback.PasswordCallback) Callback(javax.security.auth.callback.Callback) UnexpectedJCAException(xades4j.verification.UnexpectedJCAException) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Aggregations

PasswordCallback (javax.security.auth.callback.PasswordCallback)316 NameCallback (javax.security.auth.callback.NameCallback)255 Callback (javax.security.auth.callback.Callback)207 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)167 IOException (java.io.IOException)102 LoginException (javax.security.auth.login.LoginException)72 CallbackHandler (javax.security.auth.callback.CallbackHandler)66 LoginContext (javax.security.auth.login.LoginContext)39 FailedLoginException (javax.security.auth.login.FailedLoginException)35 RealmCallback (javax.security.sasl.RealmCallback)35 Subject (javax.security.auth.Subject)31 Test (org.junit.Test)28 ConfirmationCallback (javax.security.auth.callback.ConfirmationCallback)26 AuthorizeCallback (javax.security.sasl.AuthorizeCallback)26 ChoiceCallback (javax.security.auth.callback.ChoiceCallback)24 Principal (java.security.Principal)21 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)18 TextOutputCallback (javax.security.auth.callback.TextOutputCallback)18 HashMap (java.util.HashMap)16 Test (org.testng.annotations.Test)15