Skip to content

Commit 4d24739

Browse files
committed
Enable AppId extension in demo server
1 parent 3921558 commit 4d24739

File tree

5 files changed

+45
-14
lines changed

5 files changed

+45
-14
lines changed

webauthn-server-demo/src/main/java/demo/App.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import javax.ws.rs.core.Application;
44

5+
import com.yubico.webauthn.extension.appid.InvalidAppIdException;
56
import demo.webauthn.WebAuthnRestResource;
6-
import java.net.MalformedURLException;
77
import java.util.Arrays;
88
import java.util.HashSet;
99
import java.util.Set;
@@ -19,9 +19,13 @@ public Set<Class<?>> getClasses() {
1919

2020
@Override
2121
public Set<Object> getSingletons() {
22-
return new HashSet<>(Arrays.asList(
23-
new WebAuthnRestResource()
24-
));
22+
try {
23+
return new HashSet<>(Arrays.asList(
24+
new WebAuthnRestResource()
25+
));
26+
} catch (InvalidAppIdException e) {
27+
throw new RuntimeException(e);
28+
}
2529
}
2630

2731
}

webauthn-server-demo/src/main/java/demo/webauthn/Config.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package demo.webauthn;
22

33
import com.yubico.webauthn.data.RelyingPartyIdentity;
4+
import com.yubico.webauthn.extension.appid.AppId;
5+
import com.yubico.webauthn.extension.appid.InvalidAppIdException;
46
import java.net.MalformedURLException;
57
import java.net.URL;
68
import java.util.Arrays;
@@ -22,20 +24,24 @@ public class Config {
2224
private final List<String> origins;
2325
private final int port;
2426
private final RelyingPartyIdentity rpIdentity;
27+
private final Optional<AppId> appId;
2528

26-
private Config(List<String> origins, int port, RelyingPartyIdentity rpIdentity) {
29+
private Config(List<String> origins, int port, RelyingPartyIdentity rpIdentity, Optional<AppId> appId) {
2730
this.origins = Collections.unmodifiableList(origins);
2831
this.port = port;
2932
this.rpIdentity = rpIdentity;
33+
this.appId = appId;
3034
}
3135

3236
private static Config instance;
3337
private static Config getInstance() {
3438
if (instance == null) {
3539
try {
36-
instance = new Config(computeOrigins(), computePort(), computeRpIdentity());
40+
instance = new Config(computeOrigins(), computePort(), computeRpIdentity(), computeAppId());
3741
} catch (MalformedURLException e) {
3842
throw new RuntimeException(e);
43+
} catch (InvalidAppIdException e) {
44+
throw new RuntimeException(e);
3945
}
4046
}
4147
return instance;
@@ -53,6 +59,10 @@ public static RelyingPartyIdentity getRpIdentity() {
5359
return getInstance().rpIdentity;
5460
}
5561

62+
public static Optional<AppId> getAppId() {
63+
return getInstance().appId;
64+
}
65+
5666
private static List<String> computeOrigins() {
5767
final String origins = System.getenv("YUBICO_WEBAUTHN_ALLOWED_ORIGINS");
5868

@@ -113,4 +123,15 @@ private static RelyingPartyIdentity computeRpIdentity() throws MalformedURLExcep
113123
return result;
114124
}
115125

126+
private static Optional<AppId> computeAppId() throws InvalidAppIdException {
127+
final String appId = System.getenv("YUBICO_WEBAUTHN_U2F_APPID");
128+
logger.debug("U2F AppId: {}", appId);
129+
130+
if (appId == null) {
131+
return Optional.empty();
132+
} else {
133+
return Optional.of(new AppId(appId));
134+
}
135+
}
136+
116137
}

webauthn-server-demo/src/main/java/demo/webauthn/WebAuthnRestResource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.yubico.webauthn.data.ByteArray;
2525
import com.yubico.webauthn.data.exception.Base64UrlException;
2626
import com.yubico.internal.util.WebAuthnCodecs;
27+
import com.yubico.webauthn.extension.appid.InvalidAppIdException;
2728
import com.yubico.webauthn.meta.VersionInfo;
2829
import demo.webauthn.data.AssertionRequest;
2930
import demo.webauthn.data.RegistrationRequest;
@@ -47,7 +48,7 @@ public class WebAuthnRestResource {
4748
private final ObjectMapper jsonMapper = WebAuthnCodecs.json();
4849
private final JsonNodeFactory jsonFactory = JsonNodeFactory.instance;
4950

50-
public WebAuthnRestResource() {
51+
public WebAuthnRestResource() throws InvalidAppIdException {
5152
this(new WebAuthnServer());
5253
}
5354

webauthn-server-demo/src/main/java/demo/webauthn/WebAuthnServer.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.google.common.io.CharStreams;
99
import com.google.common.io.Closeables;
1010
import com.yubico.internal.util.CertificateParser;
11+
import com.yubico.internal.util.WebAuthnCodecs;
1112
import com.yubico.util.Either;
1213
import com.yubico.webauthn.ChallengeGenerator;
1314
import com.yubico.webauthn.FinishAssertionOptions;
@@ -16,7 +17,6 @@
1617
import com.yubico.webauthn.RelyingParty;
1718
import com.yubico.webauthn.StartAssertionOptions;
1819
import com.yubico.webauthn.StartRegistrationOptions;
19-
import com.yubico.internal.util.WebAuthnCodecs;
2020
import com.yubico.webauthn.attestation.MetadataResolver;
2121
import com.yubico.webauthn.attestation.MetadataService;
2222
import com.yubico.webauthn.attestation.StandardMetadataService;
@@ -32,6 +32,8 @@
3232
import com.yubico.webauthn.data.UserIdentity;
3333
import com.yubico.webauthn.exception.AssertionFailedException;
3434
import com.yubico.webauthn.exception.RegistrationFailedException;
35+
import com.yubico.webauthn.extension.appid.AppId;
36+
import com.yubico.webauthn.extension.appid.InvalidAppIdException;
3537
import demo.webauthn.data.AssertionRequest;
3638
import demo.webauthn.data.AssertionResponse;
3739
import demo.webauthn.data.CredentialRegistration;
@@ -78,11 +80,11 @@ public class WebAuthnServer {
7880

7981
private final RelyingParty rp;
8082

81-
public WebAuthnServer() {
82-
this(new InMemoryRegistrationStorage(), newCache(), newCache(), Config.getRpIdentity(), Config.getOrigins());
83+
public WebAuthnServer() throws InvalidAppIdException {
84+
this(new InMemoryRegistrationStorage(), newCache(), newCache(), Config.getRpIdentity(), Config.getOrigins(), Config.getAppId());
8385
}
8486

85-
public WebAuthnServer(RegistrationStorage userStorage, Cache<ByteArray, RegistrationRequest> registerRequestStorage, Cache<ByteArray, AssertionRequest> assertRequestStorage, RelyingPartyIdentity rpIdentity, List<String> origins) {
87+
public WebAuthnServer(RegistrationStorage userStorage, Cache<ByteArray, RegistrationRequest> registerRequestStorage, Cache<ByteArray, AssertionRequest> assertRequestStorage, RelyingPartyIdentity rpIdentity, List<String> origins, Optional<AppId> appId) throws InvalidAppIdException {
8688
this.userStorage = userStorage;
8789
this.registerRequestStorage = registerRequestStorage;
8890
this.assertRequestStorage = assertRequestStorage;
@@ -99,6 +101,7 @@ public WebAuthnServer(RegistrationStorage userStorage, Cache<ByteArray, Registra
99101
.allowUntrustedAttestation(true)
100102
.validateSignatureCounter(true)
101103
.validateTypeAttribute(false)
104+
.appId(appId)
102105
.build();
103106
}
104107

webauthn-server-demo/src/test/scala/demo/webauthn/WebAuthnServerSpec.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.yubico.webauthn.data.PublicKeyCredentialDescriptor
2222
import com.yubico.webauthn.data.AttestationType
2323
import com.yubico.webauthn.data.CollectedClientData
2424
import com.yubico.webauthn.data.ByteArray
25+
import com.yubico.webauthn.extension.appid.AppId
2526
import demo.webauthn.data.CredentialRegistration
2627
import demo.webauthn.data.RegistrationRequest
2728
import demo.webauthn.data.RegistrationResponse
@@ -47,6 +48,7 @@ class WebAuthnServerSpec extends FunSpec with Matchers {
4748
private val requestId = ByteArray.fromBase64Url("request1")
4849
private val rpId = RelyingPartyIdentity.builder().id("localhost").name("Test party").build()
4950
private val origins = List("localhost").asJava
51+
private val appId = Optional.empty[AppId]
5052

5153
describe("WebAuthnServer") {
5254

@@ -143,7 +145,7 @@ class WebAuthnServerSpec extends FunSpec with Matchers {
143145
.build()
144146
).asJava)
145147

146-
new WebAuthnServer(userStorage, newCache(), assertionRequests, rpId, origins)
148+
new WebAuthnServer(userStorage, newCache(), assertionRequests, rpId, origins, appId)
147149
}
148150
}
149151

@@ -154,7 +156,7 @@ class WebAuthnServerSpec extends FunSpec with Matchers {
154156
private def newServerWithUser(testData: RegistrationTestData) = {
155157
val userStorage: RegistrationStorage = makeUserStorage(testData)
156158

157-
new WebAuthnServer(userStorage, newCache(), newCache(), rpId, origins)
159+
new WebAuthnServer(userStorage, newCache(), newCache(), rpId, origins, appId)
158160
}
159161

160162
private def makeUserStorage(testData: RegistrationTestData) = {
@@ -197,7 +199,7 @@ class WebAuthnServerSpec extends FunSpec with Matchers {
197199
testData.request
198200
))
199201

200-
new WebAuthnServer(new InMemoryRegistrationStorage, registrationRequests, newCache(), rpId, origins)
202+
new WebAuthnServer(new InMemoryRegistrationStorage, registrationRequests, newCache(), rpId, origins, appId)
201203
}
202204

203205
private def newCache[K <: Object, V <: Object](): Cache[K, V] =

0 commit comments

Comments
 (0)