Example usage for javax.security.auth.callback PasswordCallback setPassword

List of usage examples for javax.security.auth.callback PasswordCallback setPassword

Introduction

In this page you can find the example usage for javax.security.auth.callback PasswordCallback setPassword.

Prototype

public void setPassword(char[] password) 

Source Link

Document

Set the retrieved password.

Usage

From source file:org.forgerock.openam.forgerockrest.authn.callbackhandlers.RestAuthPasswordCallbackHandler.java

/**
 * Checks the request for the presence of a header "X-OpenAM-Password", if set and not an empty string then
 * sets the header value on the Callback and returns true. Otherwise does nothing and returns false.
 *
 * {@inheritDoc}//from w w  w .jav  a2s .c om
 */
boolean doUpdateCallbackFromRequest(HttpHeaders headers, HttpServletRequest request,
        HttpServletResponse response, PasswordCallback callback)
        throws RestAuthCallbackHandlerResponseException {

    List<String> passwordHeader = headers.getRequestHeader(PASSWORD_HEADER_KEY);
    String password = passwordHeader != null && passwordHeader.size() > 0 ? passwordHeader.get(0) : null;

    if (StringUtils.isEmpty(password)) {
        DEBUG.message("password not set in request.");
        return false;
    }

    callback.setPassword(password.toCharArray());
    return true;
}

From source file:org.forgerock.openam.forgerockrest.authn.callbackhandlers.RestAuthPasswordCallbackHandler.java

/**
 * {@inheritDoc}// w  w w. j  a va  2 s  . c o  m
 */
public PasswordCallback convertFromJson(PasswordCallback callback, JsonValue jsonCallback) {

    validateCallbackType(CALLBACK_NAME, jsonCallback);

    JsonValue input = jsonCallback.get("input");

    if (input.size() != 1) {
        throw new JsonException("JSON Callback does not include a input field");
    }

    JsonValue inputField = input.get(0);
    String value = inputField.get("value").asString();
    callback.setPassword(value.toCharArray());

    return callback;
}

From source file:org.hyperic.hq.plugin.weblogic.WeblogicAuth.java

public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {

    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof TextOutputCallback) {

            TextOutputCallback toc = (TextOutputCallback) callbacks[i];

            switch (toc.getMessageType()) {
            case TextOutputCallback.INFORMATION:
                log.info(toc.getMessage());
                break;

            case TextOutputCallback.ERROR:
                log.error(toc.getMessage());
                break;

            case TextOutputCallback.WARNING:
                log.warn(toc.getMessage());
                break;

            default:
                throw new IOException("Unsupported message type: " + toc.getMessageType());
            }// ww w  .  j  a v  a 2s  .  c om
        } else if (callbacks[i] instanceof NameCallback) {
            NameCallback nc = (NameCallback) callbacks[i];
            nc.setName(this.username);
        } else if (callbacks[i] instanceof URLCallback) {
            URLCallback uc = (URLCallback) callbacks[i];
            uc.setURL(this.url);
        } else if (callbacks[i] instanceof PasswordCallback) {
            PasswordCallback pc = (PasswordCallback) callbacks[i];
            pc.setPassword(this.passwordArray);
        } else {
            throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
        }
    }
}

From source file:org.kalypso.test.bsu.wfs.SingleSignonTest.java

public void testSigngleSignon() throws Exception {
    try {// ww w.j ava 2s  .  c  o m
        copy(new File("D:/eclipse3.1/tmp/web_FlowsAStestLogin.html"));
        LoginContext loginContext = null;
        System.setProperty("java.security.auth.login.config", "D:/eclipse3.1/tmp/jaasConf.txt");
        // Login-Kontext fr die Konfiguration "Demo" erzeugen
        // loginContext = new LoginContext( "Demo" );
        loginContext = new LoginContext("Demo", new CallbackHandler() {

            public void handle(Callback[] callbacks) {
                for (int i = 0; i < callbacks.length; i++) {
                    Callback callback = callbacks[i];
                    if (callback instanceof NameCallback) {
                        final NameCallback nCall = (NameCallback) callback;
                        System.out.println(nCall.getPrompt());
                        nCall.setName("Flowsad");
                    } else if (callback instanceof PasswordCallback) {
                        final PasswordCallback call = (PasswordCallback) callback;
                        System.out.println(call.getPrompt());
                        call.setPassword(new char[] { ' ', ' ', });
                    } else
                        System.out.println("unknown Callback: " + callback.getClass().getName());
                }
            }

        });
        // Durchfhrung des Logins
        loginContext.login();
        System.out.println("authentication succeeded");

        // Die Principals ermitteln...
        Set principals = loginContext.getSubject().getPrincipals();
        // ...und in einer Iteration ausgeben
        Iterator it = principals.iterator();
        Principal p;
        while (it.hasNext()) {
            p = (Principal) it.next();
            System.out.println(p);
        }
        System.out.println("logging out...");
        copy(new File("D:/eclipse3.1/tmp/web_FlowsAdmitLogin.html"));

        loginContext.logout();
    } catch (Exception e) {
        System.out.println("authentication failed");
        throw e;
    }
}

From source file:org.opensc.test.pkcs11.PINEntry.java

public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        if (callback instanceof PasswordCallback) {
            PasswordCallback pwCb = (PasswordCallback) callback;

            log.info("Starting PIN entry...");
            char pin[] = this.getPIN(pwCb.getPrompt());

            pwCb.setPassword(pin);
            log.info("PIN has successfully been entered.");
        } else if (callback instanceof PKCS11EventCallback) {
            PKCS11EventCallback evCb = (PKCS11EventCallback) callback;

            log.info("Received event [" + evCb + "].");
            this.label.setText(evCb.toString());
        } else/*from  w w  w. j  ava 2s  .  com*/
            throw new UnsupportedCallbackException(callback,
                    "Only PasswordCallback or PKCS11EventCallback is supported.");

    }
}

From source file:org.wso2.carbon.identity.application.authenticator.iwa.IWAAuthenticationUtil.java

/**
 * Create call back handler using given username and password
 *
 * @param username/*w ww  .  j a  v a 2  s  . c om*/
 * @param password
 * @return CallbackHandler
 */
private static CallbackHandler getUserNamePasswordCallbackHandler(final String username,
        final char[] password) {

    return new CallbackHandler() {
        public void handle(final Callback[] callback) {
            for (Callback currentCallBack : callback) {
                if (currentCallBack instanceof NameCallback) {
                    final NameCallback nameCallback = (NameCallback) currentCallBack;
                    nameCallback.setName(username);
                } else if (currentCallBack instanceof PasswordCallback) {
                    final PasswordCallback passCallback = (PasswordCallback) currentCallBack;
                    passCallback.setPassword(password);
                } else {
                    log.error("Unsupported Callback class = " + currentCallBack.getClass().getName());
                }
            }
        }
    };
}

From source file:org.wso2.carbon.identity.recovery.password.NotificationPasswordRecoveryManager.java

public void updatePassword(String code, char[] newPassword) throws IdentityRecoveryException {
    UserRecoveryDataStore userRecoveryDataStore = JDBCRecoveryDataStore.getInstance();
    UserRecoveryData userRecoveryData = userRecoveryDataStore.loadByCode(code);
    //if return data from load method, it means the code is validated. Otherwise it returns exceptions

    if (!RecoverySteps.UPDATE_PASSWORD.equals(userRecoveryData.getRecoveryStep())) {
        throw Utils.handleClientException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_INVALID_CODE,
                null);//www . ja va 2s  .co  m
    }

    try {
        PasswordCallback passwordCallback = new PasswordCallback("password", false);
        passwordCallback.setPassword(newPassword);
        IdentityStore identityStore = IdentityRecoveryServiceDataHolder.getInstance().getRealmService()
                .getIdentityStore();
        identityStore.updateUserCredentials(userRecoveryData.getUserUniqueId(),
                Collections.singletonList(passwordCallback));

    } catch (UserNotFoundException e) {
        throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_UNEXPECTED, null,
                e);
    } catch (IdentityStoreException e) {
        throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_UNEXPECTED, null,
                e);
    }

    userRecoveryDataStore.invalidateByCode(code);
    boolean isNotificationInternallyManaged = IdentityRecoveryServiceDataHolder.getInstance()
            .getRecoveryLinkConfig().isNotificationInternallyManage();
    boolean isNotificationSendWhenSuccess = IdentityRecoveryServiceDataHolder.getInstance()
            .getRecoveryLinkConfig().isSendRecoveryNotificationSuccess();

    if (isNotificationInternallyManaged && isNotificationSendWhenSuccess) {
        try {
            triggerNotification(userRecoveryData.getUserUniqueId(),
                    IdentityRecoveryConstants.NOTIFICATION_TYPE_PASSWORD_RESET_SUCCESS, null);
        } catch (Exception e) {
            log.warn("Error while sending password reset success notification to user :"
                    + userRecoveryData.getUserUniqueId());
        }
    }
}

From source file:sernet.gs.ui.rcp.main.security.VeriniceSecurityProvider.java

private void setupSunPKCS11Provider() {
    // Prevents installing the provider twice.
    if (Security.getProvider("SunPKCS11-verinice") != null) {
        return;//from  w w w. jav  a  2s.  c om
    }
    // If the user enabled anything PKCS#11 related we need to lead the PKCS#11 library and add its
    // provider.
    String configFile = createPKCS11ConfigFile();
    if (configFile != null) {
        // The availability of this class in an OSGi environment depends on a system property. If
        // get errors of this class not being available check that you have
        // -Dosgi.parentClassloader=ext
        // in your VM arguments.
        if (LOG.isDebugEnabled()) {
            LOG.debug("Setup SunPKCS11 AuthProvider with config file: " + configFile);
        }
        SunPKCS11 p = new SunPKCS11(configFile);
        p.setCallbackHandler(new Helper() {
            @Override
            protected void handle(PasswordCallback cb) {
                cb.setPassword(getTokenPIN());
            }
        });
        Security.addProvider(p);
    }
}