2022-03-02 17:01:30 +01:00
|
|
|
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package pgp.wkd.test_suite;
|
|
|
|
|
|
|
|
import pgp.wkd.WKDAddress;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
|
|
|
public abstract class WkdDirectoryStructure {
|
|
|
|
|
|
|
|
protected final String domain;
|
|
|
|
protected final File rootDir;
|
|
|
|
protected final File wellKnown;
|
|
|
|
protected final File openpgpkey;
|
|
|
|
|
|
|
|
public WkdDirectoryStructure(File rootDirectory, String domain) {
|
|
|
|
this.domain = domain;
|
|
|
|
this.rootDir = rootDirectory;
|
|
|
|
wellKnown = new File(rootDirectory, ".well-known");
|
|
|
|
openpgpkey = new File(wellKnown, "openpgpkey");
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract Path getRelativeCertificatePath(String mailAddress);
|
|
|
|
|
|
|
|
public abstract void mkdirs() throws IOException;
|
|
|
|
|
|
|
|
protected void mkdir(File dir) throws IOException {
|
|
|
|
if (!dir.exists() && !dir.mkdirs()) {
|
|
|
|
throw new IOException("Cannot create directory '" + dir.getAbsolutePath() + "'.");
|
|
|
|
}
|
|
|
|
if (dir.isFile()) {
|
|
|
|
throw new IOException("Cannot create directory '" + dir.getAbsolutePath() + "': Is a file.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 16:03:11 +01:00
|
|
|
protected void touch(File file) throws IOException {
|
|
|
|
if (!file.exists() && !file.createNewFile()) {
|
|
|
|
throw new IOException("Cannot create file '" + file.getAbsolutePath() + "'.");
|
|
|
|
}
|
|
|
|
if (!file.isFile()) {
|
|
|
|
throw new IOException("Cannot create file '" + file.getAbsolutePath() + "': Is not a file.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 17:01:30 +01:00
|
|
|
public abstract URI getAddress(String mail);
|
|
|
|
|
|
|
|
public abstract File resolve(Path path);
|
|
|
|
|
|
|
|
public static class DirectMethod extends WkdDirectoryStructure {
|
|
|
|
|
|
|
|
private final File hu;
|
2022-03-21 16:03:11 +01:00
|
|
|
private final File policy;
|
2022-03-02 17:01:30 +01:00
|
|
|
|
|
|
|
public DirectMethod(File rootDirectory, String domain) {
|
|
|
|
super(rootDirectory, domain);
|
|
|
|
this.hu = new File(openpgpkey, "hu");
|
2022-03-21 16:03:11 +01:00
|
|
|
this.policy = new File(openpgpkey, "policy");
|
2022-03-02 17:01:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Path getRelativeCertificatePath(String mailAddress) {
|
|
|
|
WKDAddress address = WKDAddress.fromEmail(mailAddress);
|
|
|
|
String path = address.getDirectMethodURI().getPath();
|
|
|
|
String fileName = path.substring(path.lastIndexOf('/') + 1);
|
2022-04-02 17:26:42 +02:00
|
|
|
return rootDir.toPath().relativize(new File(hu, fileName).toPath());
|
2022-03-02 17:01:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void mkdirs() throws IOException {
|
|
|
|
mkdir(rootDir);
|
|
|
|
mkdir(wellKnown);
|
|
|
|
mkdir(openpgpkey);
|
|
|
|
mkdir(hu);
|
2022-03-21 16:03:11 +01:00
|
|
|
|
|
|
|
touch(policy);
|
2022-03-02 17:01:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public URI getAddress(String mail) {
|
|
|
|
return WKDAddress.fromEmail(mail).getDirectMethodURI();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public File resolve(Path path) {
|
|
|
|
return rootDir.toPath().resolve(path).toFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class AdvancedMethod extends WkdDirectoryStructure {
|
|
|
|
|
|
|
|
private final File domainFile;
|
|
|
|
private final File hu;
|
2022-03-21 16:03:11 +01:00
|
|
|
private final File policy;
|
2022-03-02 17:01:30 +01:00
|
|
|
|
|
|
|
public AdvancedMethod(File rootDir, String domain) {
|
|
|
|
super(rootDir, domain);
|
|
|
|
this.domainFile = new File(openpgpkey, domain);
|
|
|
|
this.hu = new File(domainFile, "hu");
|
2022-03-21 16:03:11 +01:00
|
|
|
this.policy = new File(domainFile, "policy");
|
2022-03-02 17:01:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Path getRelativeCertificatePath(String mailAddress) {
|
|
|
|
WKDAddress address = WKDAddress.fromEmail(mailAddress);
|
|
|
|
String path = address.getAdvancedMethodURI().getPath();
|
|
|
|
String fileName = path.substring(path.lastIndexOf('/') + 1);
|
2022-04-02 17:26:42 +02:00
|
|
|
return rootDir.toPath().relativize(new File(hu, fileName).toPath());
|
2022-03-02 17:01:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void mkdirs() throws IOException {
|
|
|
|
mkdir(rootDir);
|
|
|
|
mkdir(wellKnown);
|
|
|
|
mkdir(openpgpkey);
|
|
|
|
mkdir(domainFile);
|
|
|
|
mkdir(hu);
|
2022-03-21 16:03:11 +01:00
|
|
|
|
|
|
|
touch(policy);
|
2022-03-02 17:01:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public URI getAddress(String mail) {
|
|
|
|
return WKDAddress.fromEmail(mail).getAdvancedMethodURI();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public File resolve(Path path) {
|
|
|
|
return rootDir.toPath().resolve(path).toFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|