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

      for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof NameCallback) {
          // prompt the user for a username
          NameCallback nc = (NameCallback) callbacks[i];
          nc.setName(userid);
        } else if (callbacks[i] instanceof PasswordCallback) {
          PasswordCallback pc = (PasswordCallback) callbacks[i];
          pc.setPassword(password.toCharArray());
        }
      }
    }
  public void initialize(
      Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) {
    this.subject = subject;

    try {
      TextInputCallback textCallback = new TextInputCallback("prompt");
      NameCallback nameCallback = new NameCallback("prompt");
      PasswordCallback passwordCallback = new PasswordCallback("prompt", false);

      callbackHandler.handle(new Callback[] {textCallback, nameCallback, passwordCallback});

      passwordFromLoginPage = new String(passwordCallback.getPassword());
      userFromLoginPage = nameCallback.getName();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
 @Override
 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
   for (Callback callback : callbacks) {
     if (callback instanceof NameCallback) {
       NameCallback nameCallback = (NameCallback) callback;
       nameCallback.setName("user");
     } else if (callback instanceof PasswordCallback) {
       PasswordCallback passwordCallback = (PasswordCallback) callback;
       passwordCallback.setPassword(password.toCharArray());
     } else if (callback instanceof AuthorizeCallback) {
       AuthorizeCallback authorizeCallback = (AuthorizeCallback) callback;
       authorizeCallback.setAuthorized(
           authorizeCallback
               .getAuthenticationID()
               .equals(authorizeCallback.getAuthorizationID()));
     } else if (callback instanceof RealmCallback) {
       RealmCallback realmCallback = (RealmCallback) callback;
       realmCallback.setText(REALM);
     } else {
       throw new UnsupportedCallbackException(callback);
     }
   }
 }
Example #4
0
  /**
   * Loads the keystore using the given <code>KeyStore.LoadStoreParameter</code>.
   *
   * <p>Note that if this KeyStore has already been loaded, it is reinitialized and loaded again
   * from the given parameter.
   *
   * @param param the <code>KeyStore.LoadStoreParameter</code> that specifies how to load the
   *     keystore, which may be <code>null</code>
   * @exception IllegalArgumentException if the given <code>KeyStore.LoadStoreParameter</code> input
   *     is not recognized
   * @exception IOException if there is an I/O or format problem with the keystore data. If the
   *     error is due to an incorrect <code>ProtectionParameter</code> (e.g. wrong password) the
   *     {@link Throwable#getCause cause} of the <code>IOException</code> should be an <code>
   *     UnrecoverableKeyException</code>
   * @exception NoSuchAlgorithmException if the algorithm used to check the integrity of the
   *     keystore cannot be found
   * @exception CertificateException if any of the certificates in the keystore could not be loaded
   * @since 1.5
   */
  public void engineLoad(KeyStore.LoadStoreParameter param)
      throws IOException, NoSuchAlgorithmException, CertificateException {

    if (param == null) {
      engineLoad((InputStream) null, (char[]) null);
      return;
    }

    if (param instanceof KeyStore.SimpleLoadStoreParameter) {
      ProtectionParameter protection = param.getProtectionParameter();
      char[] password;
      if (protection instanceof PasswordProtection) {
        password = ((PasswordProtection) protection).getPassword();
      } else if (protection instanceof CallbackHandlerProtection) {
        CallbackHandler handler = ((CallbackHandlerProtection) protection).getCallbackHandler();
        PasswordCallback callback = new PasswordCallback("Password: "******"Could not obtain password", e);
        }
        password = callback.getPassword();
        callback.clearPassword();
        if (password == null) {
          throw new NoSuchAlgorithmException("No password provided");
        }
      } else {
        throw new NoSuchAlgorithmException(
            "ProtectionParameter must" + " be PasswordProtection or CallbackHandlerProtection");
      }
      engineLoad(null, password);
      return;
    }

    throw new UnsupportedOperationException();
  }
 private void handlePassword(PasswordCallback callback) {
   char[] passwordChars = _password.toCharArray();
   callback.setPassword(passwordChars);
 }