Skip to content

Commit 192d97c

Browse files
committed
Read all version info from manifest
1 parent ece6c03 commit 192d97c

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

test-dependent-projects/java-dep-webauthn-server-core/src/test/java/com/yubico/webauthn/meta/VersionInfoTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public void specPropertiesAreSet() {
2727
public void implementationPropertiesAreSet() {
2828
final Implementation impl = versionInfo.getImplementation();
2929
assertTrue(impl.getSourceCodeUrl().toExternalForm().startsWith("https://"));
30-
assertTrue(impl.getVersion().get().matches("^\\d+\\.\\d+\\.\\d+(-.*)?"));
30+
assertTrue(impl.getVersion().matches("^\\d+\\.\\d+\\.\\d+(-.*)?"));
3131
}
3232

3333
@Test
3434
public void majorVersionIsAtLeast1() {
35-
final String version = versionInfo.getImplementation().getVersion().get();
35+
final String version = versionInfo.getImplementation().getVersion();
3636
String[] splits = version.split("\\.");
3737
final int majorVersion = Integer.parseInt(splits[0]);
3838
assertTrue(majorVersion >= 1);

webauthn-server-core/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,17 @@ jar {
4444
'Specification-Title': 'Web Authentication: An API for accessing Public Key Credentials',
4545
'Specification-Version': 'Level 1 Recommendation 2019-03-04',
4646
'Specification-Vendor': 'World Wide Web Consortium',
47+
48+
'Specification-Url': 'https://www.w3.org/TR/2019/REC-webauthn-1-20190304/',
49+
'Specification-Url-Latest': 'https://www.w3.org/TR/webauthn/',
50+
'Specification-W3c-Status': 'recommendation',
51+
'Specification-Release-Date': '2019-03-04',
52+
4753
'Implementation-Id': 'java-webauthn-server',
4854
'Implementation-Title': 'Yubico Web Authentication server library',
4955
'Implementation-Version': project.version,
5056
'Implementation-Vendor': 'Yubico',
57+
'Implementation-Source-Url': 'https://github.com/Yubico/java-webauthn-server',
5158
])
5259
}
5360
}

webauthn-server-core/src/main/java/com/yubico/webauthn/meta/DocumentStatus.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
2828
import com.yubico.internal.util.json.JsonStringSerializable;
2929
import com.yubico.internal.util.json.JsonStringSerializer;
30+
import java.util.Optional;
31+
import java.util.stream.Stream;
3032
import lombok.AllArgsConstructor;
33+
import lombok.NonNull;
3134

3235
/**
3336
* A representation of Web Authentication specification document statuses.
@@ -62,6 +65,10 @@ public enum DocumentStatus implements JsonStringSerializable {
6265

6366
private final String id;
6467

68+
static Optional<DocumentStatus> fromString(@NonNull String id) {
69+
return Stream.of(values()).filter(v -> v.id.equals(id)).findAny();
70+
}
71+
6572
/**
6673
* Used by JSON serializer.
6774
*/

webauthn-server-core/src/main/java/com/yubico/webauthn/meta/Implementation.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class Implementation {
4242
/**
4343
* The version number of this release of the library.
4444
*/
45+
@NonNull
4546
private final String version;
4647

4748
/**
@@ -50,8 +51,4 @@ public class Implementation {
5051
@NonNull
5152
private final URL sourceCodeUrl;
5253

53-
public Optional<String> getVersion() {
54-
return Optional.ofNullable(version);
55-
}
56-
5754
}

webauthn-server-core/src/main/java/com/yubico/webauthn/meta/VersionInfo.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@
2929
import java.net.URL;
3030
import java.time.LocalDate;
3131
import java.util.Enumeration;
32+
import java.util.NoSuchElementException;
3233
import java.util.Optional;
3334
import java.util.jar.Manifest;
34-
import lombok.AccessLevel;
35-
import lombok.AllArgsConstructor;
3635
import lombok.Value;
3736
import lombok.extern.slf4j.Slf4j;
3837

@@ -64,35 +63,34 @@ public static VersionInfo getInstance() {
6463
* Represents the specification this implementation is based on
6564
*/
6665
private final Specification specification = Specification.builder()
67-
.url(new URL("https://www.w3.org/TR/2019/REC-webauthn-1-20190304/"))
68-
.latestVersionUrl(new URL("https://www.w3.org/TR/webauthn/"))
69-
.status(DocumentStatus.RECOMMENDATION)
70-
.releaseDate(LocalDate.parse("2019-03-04"))
66+
.url(new URL(findValueInManifest("Specification-Url")))
67+
.latestVersionUrl(new URL(findValueInManifest("Specification-Url-Latest")))
68+
.status(DocumentStatus.fromString(findValueInManifest("Specification-W3c-Status")).get())
69+
.releaseDate(LocalDate.parse(findValueInManifest("Specification-Release-Date")))
7170
.build();
7271

7372
/**
7473
* Description of this version of this library
7574
*/
7675
private final Implementation implementation = new Implementation(
77-
findImplementationVersionInManifest().orElse(null),
78-
new URL("https://github.com/Yubico/java-webauthn-server")
76+
findValueInManifest("Implementation-Version"),
77+
new URL(findValueInManifest("Implementation-Source-Url"))
7978
);
8079

8180
private VersionInfo() throws IOException {
8281
}
8382

84-
private Optional<String> findImplementationVersionInManifest() throws IOException {
83+
private String findValueInManifest(String key) throws IOException {
8584
final Enumeration<URL> resources = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");
8685

8786
while (resources.hasMoreElements()) {
8887
final URL resource = resources.nextElement();
8988
final Manifest manifest = new Manifest(resource.openStream());
9089
if ("java-webauthn-server".equals(manifest.getMainAttributes().getValue("Implementation-Id"))) {
91-
return Optional.ofNullable(manifest.getMainAttributes().getValue("Implementation-Version"));
90+
return manifest.getMainAttributes().getValue(key);
9291
}
9392
}
94-
95-
return Optional.empty();
93+
throw new NoSuchElementException("Could not find \"" + key + "\" in manifest.");
9694
}
9795

9896
}

0 commit comments

Comments
 (0)