001/* 002 * acme4j - Java ACME client 003 * 004 * Copyright (C) 2015 Richard "Shred" Körber 005 * http://acme4j.shredzone.org 006 * 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 013 */ 014package org.shredzone.acme4j; 015 016import static java.util.stream.Collectors.toUnmodifiableList; 017 018import java.io.Serial; 019import java.net.URL; 020import java.time.Duration; 021import java.time.Instant; 022import java.util.EnumSet; 023import java.util.List; 024import java.util.Optional; 025 026import org.shredzone.acme4j.challenge.Challenge; 027import org.shredzone.acme4j.exception.AcmeException; 028import org.shredzone.acme4j.exception.AcmeProtocolException; 029import org.shredzone.acme4j.toolbox.AcmeUtils; 030import org.shredzone.acme4j.toolbox.JSON.Value; 031import org.shredzone.acme4j.toolbox.JSONBuilder; 032import org.slf4j.Logger; 033import org.slf4j.LoggerFactory; 034 035/** 036 * Represents an authorization request at the ACME server. 037 */ 038public class Authorization extends AcmeJsonResource implements PollableResource { 039 @Serial 040 private static final long serialVersionUID = -3116928998379417741L; 041 private static final Logger LOG = LoggerFactory.getLogger(Authorization.class); 042 043 protected Authorization(Login login, URL location) { 044 super(login, location); 045 } 046 047 /** 048 * Gets the {@link Identifier} to be authorized. 049 * <p> 050 * For wildcard domain orders, the domain itself (without wildcard prefix) is returned 051 * here. To find out if this {@link Authorization} is related to a wildcard domain 052 * order, check the {@link #isWildcard()} method. 053 * 054 * @since 2.3 055 */ 056 public Identifier getIdentifier() { 057 return getJSON().get("identifier").asIdentifier(); 058 } 059 060 /** 061 * Gets the authorization status. 062 * <p> 063 * Possible values are: {@link Status#PENDING}, {@link Status#VALID}, 064 * {@link Status#INVALID}, {@link Status#DEACTIVATED}, {@link Status#EXPIRED}, 065 * {@link Status#REVOKED}. 066 */ 067 @Override 068 public Status getStatus() { 069 return getJSON().get("status").asStatus(); 070 } 071 072 /** 073 * Gets the expiry date of the authorization, if set by the server. 074 */ 075 public Optional<Instant> getExpires() { 076 return getJSON().get("expires") 077 .map(Value::asString) 078 .map(AcmeUtils::parseTimestamp); 079 } 080 081 /** 082 * Returns {@code true} if this {@link Authorization} is related to a wildcard domain, 083 * {@code false} otherwise. 084 */ 085 public boolean isWildcard() { 086 return getJSON().get("wildcard") 087 .map(Value::asBoolean) 088 .orElse(false); 089 } 090 091 /** 092 * Returns {@code true} if certificates for subdomains can be issued according to 093 * RFC9444. 094 * 095 * @since 3.3.0 096 */ 097 public boolean isSubdomainAuthAllowed() { 098 return getJSON().get("subdomainAuthAllowed") 099 .map(Value::asBoolean) 100 .orElse(false); 101 } 102 103 /** 104 * Gets a list of all challenges offered by the server, in no specific order. 105 */ 106 public List<Challenge> getChallenges() { 107 var login = getLogin(); 108 109 return getJSON().get("challenges") 110 .asArray() 111 .stream() 112 .map(Value::asObject) 113 .map(login::createChallenge) 114 .collect(toUnmodifiableList()); 115 } 116 117 /** 118 * Finds a {@link Challenge} of the given type. Responding to this {@link Challenge} 119 * is sufficient for authorization. 120 * <p> 121 * {@link Authorization#findChallenge(Class)} should be preferred, as this variant 122 * is not type safe. 123 * 124 * @param type 125 * Challenge name (e.g. "http-01") 126 * @return {@link Challenge} matching that name, or empty if there is no such 127 * challenge, or if the challenge alone is not sufficient for authorization. 128 * @throws ClassCastException 129 * if the type does not match the expected Challenge class type 130 */ 131 @SuppressWarnings("unchecked") 132 public <T extends Challenge> Optional<T> findChallenge(final String type) { 133 return (Optional<T>) getChallenges().stream() 134 .filter(ch -> type.equals(ch.getType())) 135 .reduce((a, b) -> { 136 throw new AcmeProtocolException("Found more than one challenge of type " + type); 137 }); 138 } 139 140 /** 141 * Finds a {@link Challenge} of the given class type. Responding to this {@link 142 * Challenge} is sufficient for authorization. 143 * 144 * @param type 145 * Challenge type (e.g. "Http01Challenge.class") 146 * @return {@link Challenge} of that type, or empty if there is no such 147 * challenge, or if the challenge alone is not sufficient for authorization. 148 * @since 2.8 149 */ 150 public <T extends Challenge> Optional<T> findChallenge(Class<T> type) { 151 return getChallenges().stream() 152 .filter(type::isInstance) 153 .map(type::cast) 154 .reduce((a, b) -> { 155 throw new AcmeProtocolException("Found more than one challenge of type " + type.getName()); 156 }); 157 } 158 159 /** 160 * Waits until the authorization is completed. 161 * <p> 162 * It is completed if it reaches either {@link Status#VALID} or 163 * {@link Status#INVALID}. 164 * <p> 165 * This method is synchronous and blocks the current thread. 166 * 167 * @param timeout 168 * Timeout until a terminal status must have been reached 169 * @return Status that was reached 170 * @since 3.4.0 171 */ 172 public Status waitForCompletion(Duration timeout) 173 throws AcmeException, InterruptedException { 174 return waitForStatus(EnumSet.of(Status.VALID, Status.INVALID), timeout); 175 } 176 177 /** 178 * Permanently deactivates the {@link Authorization}. 179 */ 180 public void deactivate() throws AcmeException { 181 LOG.debug("deactivate"); 182 try (var conn = getSession().connect()) { 183 var claims = new JSONBuilder(); 184 claims.put("status", "deactivated"); 185 186 conn.sendSignedRequest(getLocation(), claims, getLogin()); 187 setJSON(conn.readJsonResponse()); 188 } 189 } 190 191}