Add method to get available profiles

This commit is contained in:
Richard Körber
2025-01-18 12:07:13 +01:00
parent 83d6f38ec7
commit 36363adfe2
2 changed files with 28 additions and 3 deletions

View File

@@ -20,6 +20,7 @@ import java.net.URL;
import java.time.Duration;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import org.shredzone.acme4j.exception.AcmeNotSupportedException;
import org.shredzone.acme4j.toolbox.JSON;
@@ -135,6 +136,9 @@ public class Metadata {
* Returns whether the CA supports the profile feature.
*
* @since 3.5.0
* @draft This method is currently based on RFC draft draft-aaron-acme-profiles. It
* may be changed or removed without notice to reflect future changes to the draft.
* SemVer rules do not apply here.
*/
public boolean isProfileAllowed() {
return meta.get("profiles").isPresent();
@@ -146,13 +150,29 @@ public class Metadata {
* Also returns {@code false} if profiles are not allowed in general.
*
* @since 3.5.0
* @draft This method is currently based on RFC draft draft-aaron-acme-profiles. It
* may be changed or removed without notice to reflect future changes to the draft.
* SemVer rules do not apply here.
*/
public boolean isProfileAllowed(String profile) {
return meta.get("profiles").optional()
return getProfiles().contains(profile);
}
/**
* Returns all profiles supported by the CA. May be empty if the CA does not support
* profiles.
*
* @since 3.5.0
* @draft This method is currently based on RFC draft draft-aaron-acme-profiles. It
* may be changed or removed without notice to reflect future changes to the draft.
* SemVer rules do not apply here.
*/
public Set<String> getProfiles() {
return meta.get("profiles")
.optional()
.map(Value::asObject)
.orElseGet(JSON::empty)
.get(profile)
.isPresent();
.keySet();
}
/**
@@ -162,6 +182,9 @@ public class Metadata {
* Empty if the profile is not allowed.
*
* @since 3.5.0
* @draft This method is currently based on RFC draft draft-aaron-acme-profiles. It
* may be changed or removed without notice to reflect future changes to the draft.
* SemVer rules do not apply here.
*/
public Optional<String> getProfileDescription(String profile) {
return meta.get("profiles").optional()

View File

@@ -189,6 +189,7 @@ public class SessionTest {
softly.assertThat(meta.isProfileAllowed("invalid")).isFalse();
softly.assertThat(meta.getProfileDescription("classic")).contains("The profile you're accustomed to");
softly.assertThat(meta.getProfileDescription("custom")).contains("Some other profile");
softly.assertThat(meta.getProfiles()).contains("classic", "custom");
softly.assertThat(meta.getProfileDescription("invalid")).isEmpty();
softly.assertThat(meta.isExternalAccountRequired()).isTrue();
softly.assertThat(meta.isSubdomainAuthAllowed()).isTrue();
@@ -245,6 +246,7 @@ public class SessionTest {
softly.assertThat(meta.isProfileAllowed()).isFalse();
softly.assertThat(meta.isProfileAllowed("classic")).isFalse();
softly.assertThat(meta.getProfileDescription("classic")).isEmpty();
softly.assertThat(meta.getProfiles()).isEmpty();
}
}