org.shredzone.acme4j.challenge.Challenge Java Examples

The following examples show how to use org.shredzone.acme4j.challenge.Challenge. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ChallengeManager.java    From acme_client with MIT License 5 votes vote down vote up
public ChallengeManager(Challenge challenge, Login login) throws AcmeException {
    this.challenge = challenge;
    try {
        challenge.rebind(login);
    } catch (Exception ex) {
        LOG.warn("Cannot rebind challenge: " + challenge.getLocation() + " to login: " +
                login.getAccountLocation().toString(), ex);
    }
}
 
Example #2
Source File: LetsEncryptReloadLifecycle.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
private boolean authorize(final Authorization authorization) throws AcmeException {
    final Challenge challenge = httpChallenge(authorization);
    if (challenge == null) {
        throw new AcmeException("HTTP challenge is null");
    }
    if (challenge.getStatus() == Status.VALID) {
        return false;
    }

    challenge.trigger();

    try {
        int attempts = config.getRetryCount();
        while (challenge.getStatus() != Status.VALID && attempts-- > 0) {
            if (challenge.getStatus() == Status.INVALID) {
                throw new AcmeException("Invalid challenge status, exiting refresh iteration");
            }

            Thread.sleep(config.getRetryTimeoutMs());
            challenge.update();
        }
    } catch (final InterruptedException ex) {
        Thread.currentThread().interrupt();
    }

    if (challenge.getStatus() != Status.VALID) {
        throw new AcmeException("Challenge for domain " + authorization.getDomain() + ", is invalid, exiting iteration");
    }
    return true;
}
 
Example #3
Source File: LetsEncryptReloadLifecycle.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
private Challenge httpChallenge(final Authorization auth) throws AcmeException {
    final Http01Challenge challenge = auth.findChallenge(Http01Challenge.TYPE);
    if (challenge == null) {
        throw new AcmeException("Challenge is null");
    }

    challengeUpdater.accept("/.well-known/acme-challenge/" + challenge.getToken(), challenge.getAuthorization());
    return challenge;
}
 
Example #4
Source File: CertGenerator.java    From spring-boot-starter-acme with Apache License 2.0 4 votes vote down vote up
/**
 * Authorize a domain. It will be associated with your account, so you will be able to
 * retrieve a signed certificate for the domain later.
 * <p>
 * You need separate authorizations for subdomains (e.g. "www" subdomain). Wildcard
 * certificates are currently not supported.
 *
 * @param aRegistration
 *            {@link Registration} of your account
 * @param aDomain
 *            Name of the domain to authorize
 */
private void authorize (Registration aRegistration, String aDomain) throws AcmeException {
  // Authorize the domain.
  Authorization auth = aRegistration.authorizeDomain(aDomain);
  logger.info("Authorization for domain " + aDomain);

  // Find the desired challenge and prepare it.
  Challenge challenge = httpChallenge(auth, aDomain);

  // If the challenge is already verified, there's no need to execute it again.
  if (challenge.getStatus() == Status.VALID) {
    return;
  }

  // Now trigger the challenge.
  challenge.trigger();

  // Poll for the challenge to complete.
  try {
    int attempts = 10;
    while (challenge.getStatus() != Status.VALID && attempts-- > 0) {
      // Did the authorization fail?
      if (challenge.getStatus() == Status.INVALID) {
        throw new AcmeException("Challenge failed... Giving up.");
      }

      // Wait for a few seconds
      Thread.sleep(3000L);

      // Then update the status
      challenge.update();
    }
  } catch (InterruptedException ex) {
    logger.error("interrupted", ex);
  }

  // All reattempts are used up and there is still no valid authorization?
  if (challenge.getStatus() != Status.VALID) {
    throw new AcmeException("Failed to pass the challenge for domain " + aDomain + ", ... Giving up.");
  }
  
}
 
Example #5
Source File: AcmeClient.java    From r2cloud with Apache License 2.0 4 votes vote down vote up
private void authorize(Registration reg, String domain) throws AcmeException, IOException {
	messages.add("authorizing domain: " + domain, LOG);
	Authorization auth = reg.authorizeDomain(domain);
	messages.add("find http challenge", LOG);
	Http01Challenge challenge1 = auth.findChallenge(Http01Challenge.TYPE);
	if (challenge1 == null) {
		throw new AcmeException("Found no " + Http01Challenge.TYPE + " challenge, don't know what to do...");
	}
	messages.add("saving challenge request", LOG);
	try (FileOutputStream fos = new FileOutputStream(new File(challengePath, challenge1.getToken()))) {
		fos.write(challenge1.getAuthorization().getBytes(StandardCharsets.UTF_8));
	}

	Challenge challenge = challenge1;
	if (challenge.getStatus() == Status.VALID) {
		messages.add("challenge already successeded", LOG);
		return;
	}
	messages.add("trigger challenge", LOG);
	challenge.trigger();

	// Poll for the challenge to complete.
	long retryTimeout = INITIAL_RETRY;
	while (challenge.getStatus() != Status.VALID && !Thread.currentThread().isInterrupted()) {
		// Did the authorization fail?
		if (challenge.getStatus() == Status.INVALID) {
			messages.add("Authorization failed: " + challenge.getError().getDetail());
			throw new AcmeException("Challenge failed...");
		}

		try {
			Thread.sleep(retryTimeout);
		} catch (InterruptedException ex) {
			Thread.currentThread().interrupt();
			break;
		}

		try {
			messages.add("update challenge", LOG);
			challenge.update();
		} catch (AcmeRetryAfterException e) {
			retryTimeout = e.getRetryAfter().toEpochMilli() - System.currentTimeMillis();
			messages.add("not ready. retry after: " + retryTimeout + " millis", LOG);
		}
	}

	// All reattempts are used up and there is still no valid authorization?
	if (challenge.getStatus() != Status.VALID) {
		throw new AcmeException("Failed to pass the challenge for domain " + domain + ", ... Giving up.");
	}
}
 
Example #6
Source File: ChallengeManager.java    From acme_client with MIT License 4 votes vote down vote up
public ChallengeManager(Challenge challenge) {
    this.challenge = challenge;
}
 
Example #7
Source File: ChallengeManager.java    From acme_client with MIT License 4 votes vote down vote up
public Challenge getChallenge() {
    return this.challenge;
}
 
Example #8
Source File: AuthorizationManager.java    From acme_client with MIT License 4 votes vote down vote up
public Collection<Challenge> getChallenges(){
    return authorization.getChallenges();
}
 
Example #9
Source File: CertGenerator.java    From spring-boot-starter-acme with Apache License 2.0 3 votes vote down vote up
/**
 * Prepares a HTTP challenge.
 * <p>
 * The verification of this challenge expects a file with a certain content to be
 * reachable at a given path under the domain to be tested.
 * </p>
 *
 * @param aAuthorization
 *            {@link Authorization} to find the challenge in
 * @param aDomainName
 *            Domain name to be authorized
 * @return {@link Challenge} to verify
 */
private Challenge httpChallenge(Authorization aAuthorization, String aDomainName) throws AcmeException {
  // Find a single http-01 challenge
  Http01Challenge challenge = aAuthorization.findChallenge(Http01Challenge.TYPE);
  
  if (challenge == null) {
    throw new AcmeException("Found no " + Http01Challenge.TYPE + " challenge, don't know what to do...");
  }
  
  challengeStore.put(challenge.getToken(), challenge.getAuthorization());

  return challenge;
}