mirror of
https://codeberg.org/PGPainless/wkd-java.git
synced 2024-11-21 23:02:05 +01:00
Change uri construction to string formatting and add policy uris
This commit is contained in:
parent
f1505083c4
commit
cb996733fb
2 changed files with 67 additions and 9 deletions
|
@ -26,13 +26,6 @@ import java.util.regex.Pattern;
|
||||||
*/
|
*/
|
||||||
public final class WKDAddress {
|
public final class WKDAddress {
|
||||||
|
|
||||||
private static final String SCHEME = "https://";
|
|
||||||
private static final String ADV_SUBDOMAIN = "openpgpkey.";
|
|
||||||
private static final String DIRECT_WELL_KNOWN = "/.well-known/openpgpkey/hu/";
|
|
||||||
private static String ADV_WELL_KNOWN(@Nonnull String domain) {
|
|
||||||
return "/.well-known/openpgpkey/" + domain + "/hu/";
|
|
||||||
}
|
|
||||||
|
|
||||||
// RegExs for Email Addresses.
|
// RegExs for Email Addresses.
|
||||||
// https://www.baeldung.com/java-email-validation-regex#regular-expression-by-rfc-5322-for-email-validation
|
// https://www.baeldung.com/java-email-validation-regex#regular-expression-by-rfc-5322-for-email-validation
|
||||||
// Modified by adding capture groups '()' for local and domain part
|
// Modified by adding capture groups '()' for local and domain part
|
||||||
|
@ -117,6 +110,24 @@ public final class WKDAddress {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a {@link URI} pointing to the policy document for the given {@link DiscoveryMethod}.
|
||||||
|
*
|
||||||
|
* @param method discovery method
|
||||||
|
* @return policy uri
|
||||||
|
*/
|
||||||
|
@Nonnull
|
||||||
|
public URI getPolicyUri(@Nonnull DiscoveryMethod method) {
|
||||||
|
switch (method) {
|
||||||
|
case advanced:
|
||||||
|
return getAdvancedMethodPolicyURI();
|
||||||
|
case direct:
|
||||||
|
return getDirectMethodPolicyURI();
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Invalid discovery method: " + method);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the email address from which the {@link WKDAddress} was created.
|
* Return the email address from which the {@link WKDAddress} was created.
|
||||||
*
|
*
|
||||||
|
@ -141,7 +152,9 @@ public final class WKDAddress {
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public URI getDirectMethodURI() {
|
public URI getDirectMethodURI() {
|
||||||
return URI.create(SCHEME + domainPart + DIRECT_WELL_KNOWN + zbase32LocalPart + "?l=" + percentEncodedLocalPart);
|
String urlString = String.format("https://%s/.well-known/openpgpkey/hu/%s?l=%s",
|
||||||
|
domainPart, zbase32LocalPart, percentEncodedLocalPart);
|
||||||
|
return URI.create(urlString);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -158,7 +171,32 @@ public final class WKDAddress {
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public URI getAdvancedMethodURI() {
|
public URI getAdvancedMethodURI() {
|
||||||
return URI.create(SCHEME + ADV_SUBDOMAIN + domainPart + ADV_WELL_KNOWN(domainPart) + zbase32LocalPart + "?l=" + percentEncodedLocalPart);
|
String urlString = String.format("https://openpgpkey.%s/.well-known/openpgpkey/%s/hu/%s?l=%s",
|
||||||
|
domainPart, domainPart, zbase32LocalPart, percentEncodedLocalPart);
|
||||||
|
return URI.create(urlString);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the policy uri for the direct discovery method.
|
||||||
|
*
|
||||||
|
* @return direct discovery policy uri
|
||||||
|
*/
|
||||||
|
@Nonnull
|
||||||
|
public URI getDirectMethodPolicyURI() {
|
||||||
|
String urlString = String.format("https://%s/.well-known/openpgpkey/policy", domainPart);
|
||||||
|
return URI.create(urlString);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the policy uri for the advanced discovery method.
|
||||||
|
*
|
||||||
|
* @return advanced discovery policy uri
|
||||||
|
*/
|
||||||
|
@Nonnull
|
||||||
|
public URI getAdvancedMethodPolicyURI() {
|
||||||
|
String urlString = String.format("https://openpgpkey.%s/.well-known/openpgpkey/%s/policy",
|
||||||
|
domainPart, domainPart);
|
||||||
|
return URI.create(urlString);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -11,6 +11,7 @@ import java.net.URI;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import pgp.wkd.discovery.DiscoveryMethod;
|
||||||
import pgp.wkd.exception.MalformedUserIdException;
|
import pgp.wkd.exception.MalformedUserIdException;
|
||||||
|
|
||||||
public class WKDAddressTest {
|
public class WKDAddressTest {
|
||||||
|
@ -93,4 +94,23 @@ public class WKDAddressTest {
|
||||||
assertThrows(MalformedUserIdException.class, () -> WKDAddress.fromEmail(brokenEmail));
|
assertThrows(MalformedUserIdException.class, () -> WKDAddress.fromEmail(brokenEmail));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDirectPolicyUri() {
|
||||||
|
WKDAddress address = WKDAddress.fromEmail("alice@pgpainless.org");
|
||||||
|
assertEquals("https://pgpainless.org/.well-known/openpgpkey/policy", address.getDirectMethodPolicyURI().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAdvancedPolicyUri() {
|
||||||
|
WKDAddress address = WKDAddress.fromEmail("alice@pgpainless.org");
|
||||||
|
assertEquals("https://openpgpkey.pgpainless.org/.well-known/openpgpkey/pgpainless.org/policy", address.getAdvancedMethodPolicyURI().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPolicyUriByMethod() {
|
||||||
|
WKDAddress address = WKDAddress.fromEmail("bob@example.com");
|
||||||
|
assertEquals(address.getAdvancedMethodPolicyURI(), address.getPolicyUri(DiscoveryMethod.advanced));
|
||||||
|
assertEquals(address.getDirectMethodPolicyURI(), address.getPolicyUri(DiscoveryMethod.direct));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue