예제 #1
0
 private boolean tryWith(AuthMethod meth) throws UserAuthException, TransportException {
   currentMethod = meth;
   result.clear();
   meth.init(this);
   meth.request();
   return result.get(timeout, TimeUnit.SECONDS);
 }
예제 #2
0
 private void gotFailure(SSHPacket buf) throws UserAuthException, TransportException {
   allowed.clear();
   allowed.addAll(Arrays.<String>asList(buf.readString().split(",")));
   partialSuccess |= buf.readBoolean();
   if (allowed.contains(currentMethod.getName()) && currentMethod.shouldRetry())
     currentMethod.request();
   else {
     saveException(currentMethod.getName() + " auth failed");
     result.set(false);
   }
 }
예제 #3
0
  private void gotUnknown(Message msg, SSHPacket buf) throws SSHException {
    if (currentMethod == null || result == null) {
      trans.sendUnimplemented();
      return;
    }

    log.debug("Asking {} method to handle {} packet", currentMethod.getName(), msg);
    try {
      currentMethod.handle(msg, buf);
    } catch (UserAuthException e) {
      result.error(e);
    }
  }
예제 #4
0
  @Override
  public synchronized void authenticate(
      String username, Service nextService, Iterable<AuthMethod> methods)
      throws UserAuthException, TransportException {
    clearState();

    this.username = username;
    this.nextService = nextService;

    // Request "ssh-userauth" service (if not already active)
    request();

    if (firstAttempt) { // Assume all allowed
      for (AuthMethod meth : methods) allowed.add(meth.getName());
      firstAttempt = false;
    }

    try {

      for (AuthMethod meth : methods)
        if (allowed.contains(meth.getName())) {

          log.info("Trying `{}` auth...", meth.getName());

          boolean success = false;
          try {
            success = tryWith(meth);
          } catch (UserAuthException e) {
            // Give other method a shot
            saveException(e);
          }

          if (success) {
            log.info("`{}` auth successful", meth.getName());
            return;
          } else log.info("`{}` auth failed", meth.getName());

        } else saveException(meth.getName() + " auth not allowed by server");

    } finally {
      currentMethod = null;
    }

    log.debug("Had {} saved exception(s)", savedEx.size());
    throw new UserAuthException("Exhausted available authentication methods", savedEx.peek());
  }