protected void setPasswordCallbackValue(Object thePass, PasswordCallback passwdCallback) {
   String tmp;
   if (thePass instanceof String) {
     tmp = (String) thePass;
     passwdCallback.setPassword(tmp.toCharArray());
   } else if (thePass instanceof char[]) {
     passwdCallback.setPassword((char[]) thePass);
   } else if (thePass instanceof byte[]) {
     byte[] theBytes = (byte[]) thePass;
     passwdCallback.setPassword((new String(theBytes).toCharArray()));
   } else {
     throw PicketBoxMessages.MESSAGES.invalidPasswordType(
         thePass != null ? thePass.getClass() : null);
   }
 }
 /**
  * If the callback passed to the 'handle' method is an instance of PasswordCallback, the
  * JaasPasswordCallbackHandler will call,
  * callback.setPassword(authentication.getCredentials().toString()).
  *
  * @param callback
  * @param auth
  * @throws IOException
  * @throws UnsupportedCallbackException
  */
 public void handle(Callback callback, Authentication auth)
     throws IOException, UnsupportedCallbackException {
   if (callback instanceof PasswordCallback) {
     PasswordCallback pc = (PasswordCallback) callback;
     pc.setPassword(auth.getCredentials().toString().toCharArray());
   }
 }
示例#3
0
  protected AuthContext authenticate(
      String orgname, String username, String password, PrintWriter out) throws Exception {
    // Authenticate the user and obtain SSO Token
    AuthContext lc = new AuthContext(orgname);
    lc.login();
    while (lc.hasMoreRequirements()) {
      Callback[] callbacks = lc.getRequirements();
      for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof NameCallback) {
          NameCallback nc = (NameCallback) callbacks[i];
          nc.setName(username);
        } else if (callbacks[i] instanceof PasswordCallback) {
          PasswordCallback pc = (PasswordCallback) callbacks[i];
          pc.setPassword(password.toCharArray());
        } else {
          out.println("Unknow Callback: " + callbacks[i]);
          out.println("</body></html>");
          return null;
        }
      }
      lc.submitRequirements(callbacks);
    }

    if (lc.getStatus() != AuthContext.Status.SUCCESS) {
      out.println("Invalid credentials");
      out.println("</body></html>");
      return null;
    }

    return lc;
  }
示例#4
0
    @Override
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
      NameCallback nc = null;
      PasswordCallback pc = null;
      AuthorizeCallback ac = null;

      for (Callback callback : callbacks) {
        if (callback instanceof AuthorizeCallback) {
          ac = (AuthorizeCallback) callback;
        } else if (callback instanceof NameCallback) {
          nc = (NameCallback) callback;
        } else if (callback instanceof PasswordCallback) {
          pc = (PasswordCallback) callback;
        } else if (callback instanceof RealmCallback) {
          continue; // realm is ignored
        } else {
          throw new UnsupportedCallbackException(
              callback, "handle: Unrecognized SASL DIGEST-MD5 Callback");
        }
      }

      if (nc != null) {
        LOG.debug(
            "handle: SASL server DIGEST-MD5 callback: setting "
                + "username for client: "
                + userName);

        nc.setName(userName);
      }

      if (pc != null) {
        char[] password = SaslUtils.encodePassword(userPassword);

        LOG.debug(
            "handle: SASL server DIGEST-MD5 callback: setting "
                + "password for client: "
                + userPassword);

        pc.setPassword(password);
      }
      if (ac != null) {

        String authid = ac.getAuthenticationID();
        String authzid = ac.getAuthorizationID();

        if (authid.equals(authzid)) {
          ac.setAuthorized(true);
        } else {
          ac.setAuthorized(false);
        }

        if (ac.isAuthorized()) {
          LOG.debug(
              "handle: SASL server DIGEST-MD5 callback: setting "
                  + "canonicalized client ID: "
                  + userName);
          ac.setAuthorizedID(authzid);
        }
      }
    }
 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
   for (Callback current : callbacks) {
     if (current instanceof NameCallback) {
       NameCallback ncb = (NameCallback) current;
       ncb.setName(username);
     } else if (current instanceof PasswordCallback && password != null) {
       PasswordCallback pcb = (PasswordCallback) current;
       pcb.setPassword(password);
     } else if (current instanceof DigestHashCallback && hexURPHash != null) {
       DigestHashCallback dhc = (DigestHashCallback) current;
       dhc.setHexHash(hexURPHash);
     } else if (current instanceof RealmCallback) {
       RealmCallback rcb = (RealmCallback) current;
       if (realm == null) {
         String defaultText = rcb.getDefaultText();
         if (defaultText != null && defaultText.length() > 0) {
           rcb.setText(defaultText);
         }
       } else {
         rcb.setText(realm);
       }
     } else {
       throw new UnsupportedCallbackException(
           current, current.getClass().getSimpleName() + " not supported.");
     }
   }
 }
  public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {

    for (Callback c : callbacks) {
      if (c instanceof NameCallback) {
        String username = userIdent.getUserName();
        NameCallback nc = (NameCallback) c;
        nc.setName(username);
      } else if (c instanceof PasswordCallback) {
        PasswordCallback pc = (PasswordCallback) c;
        char[] password = userIdent.getPassword().toCharArray();
        pc.setPassword(password);
      } else if (c instanceof UserIdentificationInfoCallback) {
        UserIdentificationInfoCallback uic = (UserIdentificationInfoCallback) c;
        uic.setUserInfo(userIdent);
      } else if (c.getClass()
          .getName()
          .equals("org.jboss.security.auth.callback.SecurityAssociationCallback")) {
        // we do nothing but do not raise error

      } else {
        throw new UnsupportedCallbackException(
            c, "Unrecognized Callback:" + c.getClass().getName());
      }
    }
  }
  // TODO:V3 trying to read system properties here
  protected void handleSupportedCallbacks(Callback[] callbacks)
      throws IOException, UnsupportedCallbackException {

    // this variable is set to true if we have used the older jaas
    // mechanisms to process the callbacks - and we will not need
    // to process further as the inside loop, just takes care
    // of processing all callbacks
    boolean processedSomeAppclientCallbacks = false;

    int i = 0;
    while (i < callbacks.length) {
      if (!processedSomeAppclientCallbacks) {
        if ((callbacks[i] instanceof NameCallback)
            || (callbacks[i] instanceof PasswordCallback)
            || (callbacks[i] instanceof ChoiceCallback)) {

          String loginName = UsernamePasswordStore.getUsername();
          char[] password = UsernamePasswordStore.getPassword();
          boolean doSet = false;
          if (loginName == null) {
            loginName = System.getProperty(LOGIN_NAME);
            doSet = true;
          }
          if (password == null) {
            password = System.getProperty(LOGIN_PASSWORD).toCharArray();
            doSet = true;
          }
          if (doSet) {
            UsernamePasswordStore.set(loginName, password);
          }
          // TODO: V3 CallbackHandler callbackHandler = AppContainer.getCallbackHandler();
          CallbackHandler callbackHandler = SecurityServicesUtil.getInstance().getCallbackHandler();
          if (loginName != null && password != null) {
            // username/password set already
            for (Callback callback : callbacks) {
              if (callback instanceof NameCallback) {
                NameCallback nc = (NameCallback) callback;
                nc.setName(loginName);
              } else if (callback instanceof PasswordCallback) {
                PasswordCallback pc = (PasswordCallback) callback;
                pc.setPassword(password);
              }
            }
          } else {
            // once this is called all callbacks will be handled by
            // callbackHandler and then we dont have to check for
            // NameCallback PasswordCallback and ChoiceCallback
            // again.
            // Let control flow to the callback processors
            callbackHandler.handle(callbacks);
          }
          processedSomeAppclientCallbacks = true;
          break;
        }
      }
      processCallback(callbacks[i]);
      i++;
    }
  }
  public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    List<Callback> toRespondTo = new LinkedList<Callback>();

    String userName = null;
    boolean userFound = false;

    // A single pass may be sufficient but by using a two pass approach the Callbackhandler will not
    // fail if an unexpected order is encountered.

    // First Pass - is to double check no unsupported callbacks and to retrieve
    // information from the callbacks passing in information.
    Properties users = getUsersProperties();
    for (Callback current : callbacks) {

      if (current instanceof AuthorizeCallback) {
        toRespondTo.add(current);
      } else if (current instanceof NameCallback) {
        NameCallback nameCallback = (NameCallback) current;
        userName = nameCallback.getDefaultName();
        userFound = users.containsKey(userName);
      } else if (current instanceof PasswordCallback && plainText) {
        toRespondTo.add(current);
      } else if (current instanceof DigestHashCallback && plainText == false) {
        toRespondTo.add(current);
      } else if (current instanceof RealmCallback) {
        String realm = ((RealmCallback) current).getDefaultText();
        if (this.realm.equals(realm) == false) {
          throw new IllegalStateException(
              "Invalid Realm '" + realm + "' expected '" + this.realm + "'");
        }
      } else {
        throw new UnsupportedCallbackException(current);
      }
    }

    // Second Pass - Now iterate the Callback(s) requiring a response.
    for (Callback current : toRespondTo) {
      if (current instanceof AuthorizeCallback) {
        AuthorizeCallback authorizeCallback = (AuthorizeCallback) current;
        // Don't support impersonating another identity
        authorizeCallback.setAuthorized(
            authorizeCallback.getAuthenticationID().equals(authorizeCallback.getAuthorizationID()));
      } else if (current instanceof PasswordCallback) {
        if (userFound == false) {
          throw new UserNotFoundException(userName);
        }
        String password = users.get(userName).toString();
        ((PasswordCallback) current).setPassword(password.toCharArray());
      } else if (current instanceof DigestHashCallback) {
        if (userFound == false) {
          throw new UserNotFoundException(userName);
        }
        String hash = users.get(userName).toString();
        ((DigestHashCallback) current).setHexHash(hash);
      }
    }
  }
示例#9
0
 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
   for (Callback callback : callbacks) {
     if (callback instanceof NameCallback) {
       ((NameCallback) callback).setName(login);
     }
     if (callback instanceof PasswordCallback) {
       ((PasswordCallback) callback).setPassword(password.toCharArray());
     }
   }
 }
示例#10
0
 public Collection<Callback> get() {
   Collection<Callback> callbacks = new ArrayList<Callback>();
   NameCallback nameCallback = new NameCallback(NAME, GUEST);
   nameCallback.setName(GUEST);
   callbacks.add(nameCallback);
   PasswordCallback passwordCallback = new PasswordCallback(PASSWORD, true);
   passwordCallback.setPassword(GUEST.toCharArray());
   callbacks.add(passwordCallback);
   return callbacks;
 }
示例#11
0
    public void setPassword(Principal principal, PasswordCallback callback)
        throws IOException, AccountNotFoundException {
      // Let the read DB set the password
      _realPricipalDatabase.setPassword(principal, callback);

      // Retrieve the setpassword
      char[] plainPassword = callback.getPassword();

      char[] hexPassword = toHex(plainPassword);

      callback.setPassword(hexPassword);
    }
示例#12
0
    /** {@inheritDoc} */
    @Override
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
      NameCallback nc = null;
      PasswordCallback pc = null;
      AuthorizeCallback ac = null;
      for (Callback callback : callbacks) {
        if (callback instanceof AuthorizeCallback) {
          ac = (AuthorizeCallback) callback;
        } else if (callback instanceof NameCallback) {
          nc = (NameCallback) callback;
        } else if (callback instanceof PasswordCallback) {
          pc = (PasswordCallback) callback;
        } else if (callback instanceof RealmCallback) {
          continue; // realm is ignored
        } else {
          throw new UnsupportedCallbackException(
              callback, "handle: Unrecognized SASL DIGEST-MD5 Callback");
        }
      }
      if (pc != null) {
        JobTokenIdentifier tokenIdentifier = getIdentifier(nc.getDefaultName(), secretManager);
        char[] password = encodePassword(secretManager.retrievePassword(tokenIdentifier));

        if (LOG.isDebugEnabled()) {
          LOG.debug(
              "handle: SASL server DIGEST-MD5 callback: setting "
                  + "password for client: "
                  + tokenIdentifier.getUser());
        }
        pc.setPassword(password);
      }
      if (ac != null) {
        String authid = ac.getAuthenticationID();
        String authzid = ac.getAuthorizationID();
        if (authid.equals(authzid)) {
          ac.setAuthorized(true);
        } else {
          ac.setAuthorized(false);
        }
        if (ac.isAuthorized()) {
          if (LOG.isDebugEnabled()) {
            String username = getIdentifier(authzid, secretManager).getUser().getUserName();
            if (LOG.isDebugEnabled()) {
              LOG.debug(
                  "handle: SASL server DIGEST-MD5 callback: setting "
                      + "canonicalized client ID: "
                      + username);
            }
          }
          ac.setAuthorizedID(authzid);
        }
      }
    }
    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());
        }
      }
    }
示例#14
0
 @Override
 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
   for (Callback callback : callbacks) {
     if (callback instanceof NameCallback) {
       NameCallback nameCallback = (NameCallback) callback;
       nameCallback.setName(getUserName());
     } else if (callback instanceof PasswordCallback) {
       PasswordCallback passwordCallback = (PasswordCallback) callback;
       passwordCallback.setPassword(getPassword().toCharArray());
     } else {
       throw new UnsupportedCallbackException(callback);
     }
   }
 }
 @Override
 public void handle(final Callback[] cb) throws IOException, UnsupportedCallbackException {
   for (final Callback element : cb) {
     if (element instanceof NameCallback) {
       final NameCallback nc = (NameCallback) element;
       nc.setName(this.username);
     } else if (element instanceof PasswordCallback) {
       final PasswordCallback pc = (PasswordCallback) element;
       pc.setPassword(this.password == null ? null : this.password.toCharArray());
     } else {
       throw new UnsupportedCallbackException(element, "UsernamePasswordCallbackHandler");
     }
   }
 }
示例#16
0
 public void handle(Callback[] callbacks)
     throws java.io.IOException, UnsupportedCallbackException {
   for (int i = 0; i < callbacks.length; i++) {
     if (callbacks[i] instanceof NameCallback) {
       NameCallback nc = (NameCallback) callbacks[i];
       nc.setName(username);
     } else if (callbacks[i] instanceof PasswordCallback) {
       PasswordCallback pc = (PasswordCallback) callbacks[i];
       pc.setPassword(password.toCharArray());
     } else {
       throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
     }
   }
 }
 @Override
 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
   for (Callback callback : callbacks) {
     if (callback instanceof NameCallback) {
       ((NameCallback) callback).setName(login);
     } else if (callback instanceof PasswordCallback) {
       ((PasswordCallback) callback).setPassword(password.toCharArray());
     } else if (callback instanceof RealmCallback) {
       ((RealmCallback) callback).setText(realm);
     } else {
       throw new UnsupportedCallbackException(callback);
     }
   }
 }
示例#18
0
 public void handle(Callback[] callbacks) {
   String user = OneKDC.USER;
   char[] pass = OneKDC.PASS;
   for (Callback callback : callbacks) {
     if (callback instanceof NameCallback) {
       System.out.println("Callback for name: " + user);
       ((NameCallback) callback).setName(user);
     }
     if (callback instanceof PasswordCallback) {
       System.out.println("Callback for pass: " + new String(pass));
       ((PasswordCallback) callback).setPassword(pass);
     }
   }
 }
示例#19
0
 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
   for (Callback cb : callbacks) {
     if (cb instanceof TextOutputCallback) {
       // Not implementing this one yet...
     } else if (cb instanceof NameCallback) {
       NameCallback nc = (NameCallback) cb;
       nc.setName(username);
     } else if (cb instanceof PasswordCallback) {
       PasswordCallback pc = (PasswordCallback) cb;
       pc.setPassword(password);
     } else {
       throw new UnsupportedCallbackException(cb);
     }
   }
 }
 /** {@inheritDoc} */
 @Override
 public void handle(final Callback[] callbacks)
     throws IOException, UnsupportedCallbackException {
   for (Callback cb : callbacks) {
     if (cb instanceof NameCallback) {
       // if user is null, the authzId will be used as it's the default name
       ((NameCallback) cb).setName(user != null ? user : ((NameCallback) cb).getDefaultName());
     } else if (cb instanceof PasswordCallback) {
       ((PasswordCallback) cb).setPassword(pass);
     } else if (cb instanceof RealmCallback) {
       ((RealmCallback) cb).setText(((RealmCallback) cb).getDefaultText());
     } else if (cb instanceof RealmChoiceCallback) {
       ((RealmChoiceCallback) cb).setSelectedIndex(0);
     }
   }
 }
 @Override
 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
   for (Callback current : callbacks) {
     if (current instanceof NameCallback) {
       NameCallback ncb = (NameCallback) current;
       ncb.setName(this.userName);
     } else if (current instanceof PasswordCallback) {
       PasswordCallback pcb = (PasswordCallback) current;
       pcb.setPassword(this.password.toCharArray());
     } else if (current instanceof RealmCallback) {
       RealmCallback rcb = (RealmCallback) current;
       rcb.setText(rcb.getDefaultText());
     } else {
       throw new UnsupportedCallbackException(current);
     }
   }
 }
    /**
     * The handler sets for instances of {@link NameCallBack} the given {@link #name} and for
     * instances of {@link PasswordCallBack} the given {@link #password}. {@link TextOutputCallBack}
     * instances are ignored.
     *
     * @param _callbacks callback instances to handle
     * @throws UnsupportedCallbackException for all {@link Callback} instances which are not {@link
     *     NameCallBack}, {@link PasswordCallBack} or {@link TextOutputCallBack}.
     */
    public void handle(final Callback[] _callbacks)
        throws IOException, UnsupportedCallbackException {

      for (int i = 0; i < _callbacks.length; i++) {
        if (_callbacks[i] instanceof TextOutputCallback) {
          // do nothing, TextOutputCallBack's are ignored!
        } else if (_callbacks[i] instanceof NameCallback) {
          NameCallback nc = (NameCallback) _callbacks[i];
          nc.setName(this.name);
        } else if (_callbacks[i] instanceof PasswordCallback) {
          PasswordCallback pc = (PasswordCallback) _callbacks[i];
          pc.setPassword(this.password.toCharArray());
        } else {
          throw new UnsupportedCallbackException(_callbacks[i], "Unrecognized Callback");
        }
      }
    }
示例#23
0
 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
   for (int i = 0; i < callbacks.length; i++) {
     if (callbacks[i] instanceof NameCallback) {
       NameCallback ncb = (NameCallback) callbacks[i];
       ncb.setName(authenticationId);
     } else if (callbacks[i] instanceof PasswordCallback) {
       PasswordCallback pcb = (PasswordCallback) callbacks[i];
       pcb.setPassword(password.toCharArray());
     } else if (callbacks[i] instanceof RealmCallback) {
       RealmCallback rcb = (RealmCallback) callbacks[i];
       rcb.setText(hostname);
     } else if (callbacks[i] instanceof RealmChoiceCallback) {
       // unused
       // RealmChoiceCallback rccb = (RealmChoiceCallback)callbacks[i];
     } else {
       throw new UnsupportedCallbackException(callbacks[i]);
     }
   }
 }
 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
   for (Callback current : callbacks) {
     if (current instanceof RealmCallback) {
       RealmCallback rcb = (RealmCallback) current;
       String defaultText = rcb.getDefaultText();
       rcb.setText(defaultText); // For now just use the realm suggested.
     } else if (current instanceof RealmChoiceCallback) {
       throw new UnsupportedCallbackException(current, "Realm choice not currently supported.");
     } else if (current instanceof NameCallback) {
       NameCallback ncb = (NameCallback) current;
       ncb.setName(serverName);
     } else if (current instanceof PasswordCallback) {
       PasswordCallback pcb = (PasswordCallback) current;
       pcb.setPassword(new String(authKey).toCharArray());
     } else {
       throw new UnsupportedCallbackException(current);
     }
   }
 }
示例#25
0
    @Override
    public void handle(Callback[] callbacks) throws UnsupportedCallbackException {

      for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
          NameCallback nameCallback = (NameCallback) callback;

          nameCallback.setName(_name);
        } else if (callback instanceof PasswordCallback) {
          String password = GetterUtil.getString(_password);

          PasswordCallback passwordCallback = (PasswordCallback) callback;

          passwordCallback.setPassword(password.toCharArray());
        } else {
          throw new UnsupportedCallbackException(callback);
        }
      }
    }
示例#26
0
  @Override
  public byte[] evaluateResponse(byte[] response) throws SaslException {
    if (completed) {
      throw new IllegalStateException("PLAIN authentication has completed");
    }
    if (response == null) {
      throw new IllegalArgumentException("Received null response");
    }
    try {
      String payload;
      try {
        payload = new String(response, "UTF-8");
      } catch (Exception e) {
        throw new IllegalArgumentException("Received corrupt response", e);
      }
      // [ authz, authn, password ]
      String[] parts = payload.split("\u0000", 3);
      if (parts.length != 3) {
        throw new IllegalArgumentException("Received corrupt response");
      }
      if (parts[0].isEmpty()) { // authz = authn
        parts[0] = parts[1];
      }

      NameCallback nc = new NameCallback("SASL PLAIN");
      nc.setName(parts[1]);
      PasswordCallback pc = new PasswordCallback("SASL PLAIN", false);
      pc.setPassword(parts[2].toCharArray());
      AuthorizeCallback ac = new AuthorizeCallback(parts[1], parts[0]);
      cbh.handle(new Callback[] {nc, pc, ac});
      if (ac.isAuthorized()) {
        authz = ac.getAuthorizedID();
      }
    } catch (Exception e) {
      throw new SaslException("PLAIN auth failed: " + e.toString(), e);
    } finally {
      completed = true;
    }
    return null;
  }
示例#27
0
 @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);
     }
   }
 }
示例#28
0
  /*
   * process callbacks
   */
  private void processRequirements(
      String xml,
      AuthContextLocal authContext,
      AuthXMLResponse authResponse,
      String params,
      HttpServletRequest servletRequest) {
    String[] paramArray = null;
    StringTokenizer paramsSet = null;
    if (params != null) {
      paramsSet = new StringTokenizer(params, ISAuthConstants.PIPE_SEPARATOR);
    }
    boolean allCallbacksAreSet = true;
    String param;

    while (authContext.hasMoreRequirements()) {
      Callback[] reqdCallbacks = authContext.getRequirements();

      for (int i = 0; i < reqdCallbacks.length; i++) {
        if (reqdCallbacks[i] instanceof X509CertificateCallback) {
          X509CertificateCallback certCallback = (X509CertificateCallback) reqdCallbacks[i];
          LoginState loginState = AuthUtils.getLoginState(authContext);
          if (loginState != null) {
            X509Certificate cert = loginState.getX509Certificate(servletRequest);
            if (cert != null) {
              certCallback.setCertificate(cert);
              certCallback.setReqSignature(false);
            } else {
              allCallbacksAreSet = false;
            }
          }
        } else {
          param = null;

          if (reqdCallbacks[i] instanceof NameCallback) {
            param = getNextParam(paramsSet);

            if (param != null) {
              NameCallback nc = (NameCallback) reqdCallbacks[i];
              nc.setName(param);

              if (messageEnabled) {
                debug.message("Name callback set to " + param);
              }
            } else {
              allCallbacksAreSet = false;
              break;
            }
          } else if (reqdCallbacks[i] instanceof PasswordCallback) {
            param = getNextParam(paramsSet);

            if (param != null) {
              PasswordCallback pc = (PasswordCallback) reqdCallbacks[i];
              pc.setPassword(param.toCharArray());
              if (messageEnabled) {
                debug.message("Password callback is set");
              }
            } else {
              allCallbacksAreSet = false;
              break;
            }
          } else {
            if (params == null) {
              allCallbacksAreSet = false;
            }
          }
          // add more callbacks if required
        }
      }

      if (getNextParam(paramsSet) != null) {
        allCallbacksAreSet = false;
      }

      if (allCallbacksAreSet) {
        if (messageEnabled) {
          debug.message("submit callbacks with passed in params");
        }
        authContext.submitRequirements(reqdCallbacks);
      } else {
        authResponse.setReqdCallbacks(reqdCallbacks);
        break;
      }
    }
    if (!authContext.hasMoreRequirements()) {
      AuthContext.Status loginStatus = authContext.getStatus();
      if (messageEnabled) {
        debug.message(" Status: " + loginStatus);
      }
      authResponse.setLoginStatus(loginStatus);
    }
  }
示例#29
0
  @Override
  public synchronized void handle(Callback[] callbacks)
      throws IOException, UnsupportedCallbackException {
    final Console console = System.console();
    if (console == null) throw new IOException("Console not available");

    for (Callback callback : callbacks) {
      if (callback instanceof PasswordCallback) {
        final PasswordCallback password = (PasswordCallback) callback;
        final String prompt = getDefault(password.getPrompt(), "Enter password");

        char[] answer = console.readPassword("\u001b[34m%s\u001b[0m: ", prompt);
        if (answer == null) throw new EOFException();
        password.setPassword(answer);

      } else if (callback instanceof NameCallback) {
        final NameCallback name = (NameCallback) callback;
        final String prompt = getDefault(name.getPrompt(), "Enter name");
        final String defaultName = name.getDefaultName();

        if (defaultName == null)
          while (true) {
            final String answer = console.readLine("\u001b[34m%s\u001b[0m: ", prompt);
            if (answer == null) throw new EOFException();
            if (answer.length() == 0) continue;
            name.setName(answer);
            break;
          }
        else {
          final String answer =
              console.readLine(
                  "\u001b[34m%s\u001b[0m [\u001b[36m%s\u001b[0m]: ", prompt, defaultName);
          if (answer == null) throw new EOFException();
          if (answer.length() == 0) name.setName(defaultName);
          else name.setName(answer);
        }

      } else if (callback instanceof TextInputCallback) {
        final TextInputCallback input = (TextInputCallback) callback;
        final String prompt = getDefault(input.getPrompt(), "Enter value");
        final String defaultText = input.getDefaultText();

        if (defaultText == null)
          while (true) {
            final String answer = console.readLine("\u001b[34m%s\u001b[0m: ", prompt);
            if (answer == null) throw new EOFException();
            if (answer.length() == 0) continue;
            input.setText(answer);
            break;
          }
        else {
          final String answer =
              console.readLine(
                  "\u001b[34m%s\u001b[0m [\u001b[36m%s\u001b[0m]: ", prompt, defaultText);
          if (answer == null) throw new EOFException();
          if (answer.length() == 0) input.setText(defaultText);
          else input.setText(answer);
        }

      } else if (callback instanceof TextOutputCallback) {
        final TextOutputCallback output = (TextOutputCallback) callback;
        final String message = output.getMessage();
        switch (output.getMessageType()) {
          case TextOutputCallback.INFORMATION:
            console.format("\u001b[32m%s\u001b[0m\n", message);
            break;
          case TextOutputCallback.WARNING:
            console.format("\u001b[33mWARNING: %s\u001b[0m\n", message);
            break;
          case TextOutputCallback.ERROR:
            console.format("\u001b[31mERROR: %s\u001b[0m\n", message);
            break;
          default:
            console.format("\u001b[35m??? [%d]: %s\u001b[0m\n", output.getMessageType(), message);
            break;
        }

      } else {
        throw new UnsupportedCallbackException(callback, "Callback type not supported");
      }
    }
  }
    /**
     * Describe <code>handle</code> method here.
     *
     * @param callbacks a <code>Callback[]</code> value
     * @exception IOException if an error occurs
     * @exception UnsupportedCallbackException if an error occurs
     */
    @Override
    public void handle(final Callback[] callbacks)
        throws IOException, UnsupportedCallbackException {
      BareJID jid = null;

      for (int i = 0; i < callbacks.length; i++) {
        if (log.isLoggable(Level.FINEST)) {
          log.finest("Callback: " + callbacks[i].getClass().getSimpleName());
        }

        if (callbacks[i] instanceof RealmCallback) {
          RealmCallback rc = (RealmCallback) callbacks[i];
          String realm = (String) options.get(REALM_KEY);

          if (realm != null) {
            rc.setText(realm);
          } // end of if (realm == null)

          if (log.isLoggable(Level.FINEST)) {
            log.finest("RealmCallback: " + realm);
          }
        } else {
          if (callbacks[i] instanceof NameCallback) {
            NameCallback nc = (NameCallback) callbacks[i];
            String user_name = nc.getName();

            if (user_name == null) {
              user_name = nc.getDefaultName();
            } // end of if (name == null)

            jid = BareJID.bareJIDInstanceNS(user_name, (String) options.get(REALM_KEY));
            options.put(USER_ID_KEY, jid);

            if (log.isLoggable(Level.FINEST)) {
              log.finest("NameCallback: " + user_name);
            }
          } else {
            if (callbacks[i] instanceof PasswordCallback) {
              PasswordCallback pc = (PasswordCallback) callbacks[i];

              try {
                String passwd = getPassword(jid);

                pc.setPassword(passwd.toCharArray());

                if (log.isLoggable(Level.FINEST)) {
                  log.finest("PasswordCallback: " + passwd);
                }
              } catch (Exception e) {
                throw new IOException("Password retrieving problem.", e);
              } // end of try-catch
            } else {
              if (callbacks[i] instanceof AuthorizeCallback) {
                AuthorizeCallback authCallback = ((AuthorizeCallback) callbacks[i]);
                String authenId = authCallback.getAuthenticationID();
                String authorId = authCallback.getAuthorizationID();

                if (log.isLoggable(Level.FINEST)) {
                  log.finest("AuthorizeCallback: authenId: " + authenId);
                  log.finest("AuthorizeCallback: authorId: " + authorId);
                }

                // if (authenId.equals(authorId)) {
                authCallback.setAuthorized(true);

                // } // end of if (authenId.equals(authorId))
              } else {
                throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
              }
            }
          }
        }
      }
    }