Fix example

pull/55/head
Richard Körber 2017-08-13 14:16:36 +02:00
parent 3ce832d83e
commit dd548aaf4b
1 changed files with 29 additions and 37 deletions

View File

@ -88,11 +88,6 @@ public class ClientTest {
// If there is no account yet, create a new one. // If there is no account yet, create a new one.
Account acct = findOrRegisterAccount(session); Account acct = findOrRegisterAccount(session);
// Separately authorize every requested domain.
for (String domain : domains) {
authorize(acct, domain);
}
// Load or create a key pair for the domains. This should not be the userKeyPair! // Load or create a key pair for the domains. This should not be the userKeyPair!
KeyPair domainKeyPair = loadOrCreateDomainKeyPair(); KeyPair domainKeyPair = loadOrCreateDomainKeyPair();
@ -106,8 +101,16 @@ public class ClientTest {
csrb.write(out); csrb.write(out);
} }
// Now request a signed certificate. // Order the certificate
Order order = acct.orderCertificate(csrb.getEncoded(), null, null); Order order = acct.orderCertificate(csrb.getEncoded(), null, null);
// Perform all required authorizations
for (Authorization auth : order.getAuthorizations()) {
authorize(auth);
}
// Get the certificate
order.update();
Certificate certificate = order.getCertificate(); Certificate certificate = order.getCertificate();
LOG.info("Success! The certificate for domains " + domains + " has been generated!"); LOG.info("Success! The certificate for domains " + domains + " has been generated!");
@ -173,9 +176,10 @@ public class ClientTest {
* public key. If your key is not known to the server yet, a new account will be * public key. If your key is not known to the server yet, a new account will be
* created. * created.
* <p> * <p>
* This is a simple way of finding your {@link Account}. A better way is to get * This is a simple way of finding your {@link Account}. A better way is to get the
* the URL of your new account with {@link Account#getLocation()} and store * URL and KeyIdentifier of your new account with {@link Account#getLocation()}
* it somewhere. If you need to get access to your account later, reconnect to it via * {@link Session#getKeyIdentifier()} and store it somewhere. If you need to get
* access to your account later, reconnect to it via
* {@link Account#bind(Session, URI)} by using the stored location. * {@link Account#bind(Session, URI)} by using the stored location.
* *
* @param session * @param session
@ -198,33 +202,26 @@ public class ClientTest {
/** /**
* Authorize a domain. It will be associated with your account, so you will be able to * Authorize a domain. It will be associated with your account, so you will be able to
* retrieve a signed certificate for the domain later. * retrieve a signed certificate for the domain later.
* <p>
* You need separate authorizations for subdomains (e.g. "www" subdomain). Wildcard
* certificates are not currently supported.
* *
* @param acct * @param auth
* {@link Account} of your account * {@link Authorization} to perform
* @param domain
* Name of the domain to authorize
*/ */
private void authorize(Account acct, String domain) throws AcmeException { private void authorize(Authorization auth) throws AcmeException {
// Authorize the domain. LOG.info("Authorization for domain " + auth.getDomain());
Authorization auth = acct.preAuthorizeDomain(domain);
LOG.info("Authorization for domain " + domain);
// Find the desired challenge and prepare it. // Find the desired challenge and prepare it.
Challenge challenge = null; Challenge challenge = null;
switch (CHALLENGE_TYPE) { switch (CHALLENGE_TYPE) {
case HTTP: case HTTP:
challenge = httpChallenge(auth, domain); challenge = httpChallenge(auth);
break; break;
case DNS: case DNS:
challenge = dnsChallenge(auth, domain); challenge = dnsChallenge(auth);
break; break;
case TLSSNI: case TLSSNI:
challenge = tlsSniChallenge(auth, domain); challenge = tlsSniChallenge(auth);
break; break;
} }
@ -262,7 +259,8 @@ public class ClientTest {
// All reattempts are used up and there is still no valid authorization? // All reattempts are used up and there is still no valid authorization?
if (challenge.getStatus() != Status.VALID) { if (challenge.getStatus() != Status.VALID) {
throw new AcmeException("Failed to pass the challenge for domain " + domain + ", ... Giving up."); throw new AcmeException("Failed to pass the challenge for domain "
+ auth.getDomain() + ", ... Giving up.");
} }
} }
@ -278,11 +276,9 @@ public class ClientTest {
* *
* @param auth * @param auth
* {@link Authorization} to find the challenge in * {@link Authorization} to find the challenge in
* @param domain
* Domain name to be authorized
* @return {@link Challenge} to verify * @return {@link Challenge} to verify
*/ */
public Challenge httpChallenge(Authorization auth, String domain) throws AcmeException { public Challenge httpChallenge(Authorization auth) throws AcmeException {
// Find a single http-01 challenge // Find a single http-01 challenge
Http01Challenge challenge = auth.findChallenge(Http01Challenge.TYPE); Http01Challenge challenge = auth.findChallenge(Http01Challenge.TYPE);
if (challenge == null) { if (challenge == null) {
@ -291,7 +287,7 @@ public class ClientTest {
// Output the challenge, wait for acknowledge... // Output the challenge, wait for acknowledge...
LOG.info("Please create a file in your web server's base directory."); LOG.info("Please create a file in your web server's base directory.");
LOG.info("It must be reachable at: http://" + domain + "/.well-known/acme-challenge/" + challenge.getToken()); LOG.info("It must be reachable at: http://" + auth.getDomain() + "/.well-known/acme-challenge/" + challenge.getToken());
LOG.info("File name: " + challenge.getToken()); LOG.info("File name: " + challenge.getToken());
LOG.info("Content: " + challenge.getAuthorization()); LOG.info("Content: " + challenge.getAuthorization());
LOG.info("The file must not contain any leading or trailing whitespaces or line breaks!"); LOG.info("The file must not contain any leading or trailing whitespaces or line breaks!");
@ -299,7 +295,7 @@ public class ClientTest {
StringBuilder message = new StringBuilder(); StringBuilder message = new StringBuilder();
message.append("Please create a file in your web server's base directory.\n\n"); message.append("Please create a file in your web server's base directory.\n\n");
message.append("http://").append(domain).append("/.well-known/acme-challenge/").append(challenge.getToken()).append("\n\n"); message.append("http://").append(auth.getDomain()).append("/.well-known/acme-challenge/").append(challenge.getToken()).append("\n\n");
message.append("Content:\n\n"); message.append("Content:\n\n");
message.append(challenge.getAuthorization()); message.append(challenge.getAuthorization());
acceptChallenge(message.toString()); acceptChallenge(message.toString());
@ -317,11 +313,9 @@ public class ClientTest {
* *
* @param auth * @param auth
* {@link Authorization} to find the challenge in * {@link Authorization} to find the challenge in
* @param domain
* Domain name to be authorized
* @return {@link Challenge} to verify * @return {@link Challenge} to verify
*/ */
public Challenge dnsChallenge(Authorization auth, String domain) throws AcmeException { public Challenge dnsChallenge(Authorization auth) throws AcmeException {
// Find a single dns-01 challenge // Find a single dns-01 challenge
Dns01Challenge challenge = auth.findChallenge(Dns01Challenge.TYPE); Dns01Challenge challenge = auth.findChallenge(Dns01Challenge.TYPE);
if (challenge == null) { if (challenge == null) {
@ -330,12 +324,12 @@ public class ClientTest {
// Output the challenge, wait for acknowledge... // Output the challenge, wait for acknowledge...
LOG.info("Please create a TXT record:"); LOG.info("Please create a TXT record:");
LOG.info("_acme-challenge." + domain + ". IN TXT " + challenge.getDigest()); LOG.info("_acme-challenge." + auth.getDomain() + ". IN TXT " + challenge.getDigest());
LOG.info("If you're ready, dismiss the dialog..."); LOG.info("If you're ready, dismiss the dialog...");
StringBuilder message = new StringBuilder(); StringBuilder message = new StringBuilder();
message.append("Please create a TXT record:\n\n"); message.append("Please create a TXT record:\n\n");
message.append("_acme-challenge." + domain + ". IN TXT " + challenge.getDigest()); message.append("_acme-challenge." + auth.getDomain() + ". IN TXT " + challenge.getDigest());
acceptChallenge(message.toString()); acceptChallenge(message.toString());
return challenge; return challenge;
@ -352,11 +346,9 @@ public class ClientTest {
* *
* @param auth * @param auth
* {@link Authorization} to find the challenge in * {@link Authorization} to find the challenge in
* @param domain
* Domain name to be authorized
* @return {@link Challenge} to verify * @return {@link Challenge} to verify
*/ */
public Challenge tlsSniChallenge(Authorization auth, String domain) throws AcmeException { public Challenge tlsSniChallenge(Authorization auth) throws AcmeException {
// Find a single tls-sni-02 challenge // Find a single tls-sni-02 challenge
TlsSni02Challenge challenge = auth.findChallenge(TlsSni02Challenge.TYPE); TlsSni02Challenge challenge = auth.findChallenge(TlsSni02Challenge.TYPE);
if (challenge == null) { if (challenge == null) {