Skip to content

Commit 4c568fe

Browse files
nsatragnoshs96c
authored andcommitted
Add support for creating and removing virtual authenticators (#7760)
This patch adds support for the Virtual Authenticators WebDriver API, allowing developers to write E2E tests that exercise the WebAuthn API. At the moment, the API is only supported on Chrome 79 onwards. Fixes #7753 [1] https://w3c.github.io/webauthn#sctn-automation
1 parent 3950338 commit 4c568fe

File tree

10 files changed

+350
-1
lines changed

10 files changed

+350
-1
lines changed

java/client/src/org/openqa/selenium/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ java_export(
1717
"logging/**/*.java",
1818
"mobile/*.java",
1919
"net/*.java",
20+
"virtualauthenticator/*.java",
2021
]),
2122
maven_coordinates = "org.seleniumhq.selenium:selenium-api:%s" % SE_VERSION,
2223
pom_template = ":template-pom",

java/client/src/org/openqa/selenium/remote/DriverCommand.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,4 +318,9 @@ static CommandPayload SET_CURRENT_WINDOW_SIZE(Dimension targetSize) {
318318
// Mobile API
319319
String GET_NETWORK_CONNECTION = "getNetworkConnection";
320320
String SET_NETWORK_CONNECTION = "setNetworkConnection";
321+
322+
// Virtual Authenticator API
323+
// http://w3c.github.io/webauthn#sctn-automation
324+
String ADD_VIRTUAL_AUTHENTICATOR = "addVirtualAuthenticator";
325+
String REMOVE_VIRTUAL_AUTHENTICATOR = "removeVirtualAuthenticator";
321326
}

java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
import org.openqa.selenium.logging.Logs;
6868
import org.openqa.selenium.logging.NeedsLocalLogs;
6969
import org.openqa.selenium.remote.internal.WebElementToJsonConverter;
70+
import org.openqa.selenium.virtualauthenticator.HasVirtualAuthenticator;
71+
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticator;
72+
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticatorOptions;
7073

7174
import java.net.URL;
7275
import java.util.Collection;
@@ -88,7 +91,8 @@
8891
public class RemoteWebDriver implements WebDriver, JavascriptExecutor,
8992
FindsById, FindsByClassName, FindsByLinkText, FindsByName,
9093
FindsByCssSelector, FindsByTagName, FindsByXPath,
91-
HasInputDevices, HasCapabilities, Interactive, TakesScreenshot {
94+
HasInputDevices, HasCapabilities, Interactive, TakesScreenshot,
95+
HasVirtualAuthenticator {
9296

9397
// TODO(dawagner): This static logger should be unified with the per-instance localLogs
9498
private static final Logger logger = Logger.getLogger(RemoteWebDriver.class.getName());
@@ -661,6 +665,19 @@ public Mouse getMouse() {
661665
return mouse;
662666
}
663667

668+
@Override
669+
public VirtualAuthenticator addVirtualAuthenticator(VirtualAuthenticatorOptions options) {
670+
String authenticatorId = (String)
671+
execute(DriverCommand.ADD_VIRTUAL_AUTHENTICATOR, options.toMap()).getValue();
672+
return new VirtualAuthenticator(authenticatorId);
673+
}
674+
675+
@Override
676+
public void removeVirtualAuthenticator(VirtualAuthenticator authenticator) {
677+
execute(DriverCommand.REMOVE_VIRTUAL_AUTHENTICATOR,
678+
ImmutableMap.of("authenticatorId", authenticator.getId()));
679+
}
680+
664681
/**
665682
* Override this to be notified at key points in the execution of a command.
666683
*

java/client/src/org/openqa/selenium/remote/codec/AbstractHttpCommandCodec.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static java.nio.charset.StandardCharsets.UTF_8;
2727
import static org.openqa.selenium.json.Json.MAP_TYPE;
2828
import static org.openqa.selenium.remote.DriverCommand.ADD_COOKIE;
29+
import static org.openqa.selenium.remote.DriverCommand.ADD_VIRTUAL_AUTHENTICATOR;
2930
import static org.openqa.selenium.remote.DriverCommand.CLEAR_ELEMENT;
3031
import static org.openqa.selenium.remote.DriverCommand.CLICK_ELEMENT;
3132
import static org.openqa.selenium.remote.DriverCommand.CLOSE;
@@ -76,6 +77,7 @@
7677
import static org.openqa.selenium.remote.DriverCommand.NEW_SESSION;
7778
import static org.openqa.selenium.remote.DriverCommand.QUIT;
7879
import static org.openqa.selenium.remote.DriverCommand.REFRESH;
80+
import static org.openqa.selenium.remote.DriverCommand.REMOVE_VIRTUAL_AUTHENTICATOR;
7981
import static org.openqa.selenium.remote.DriverCommand.SCREENSHOT;
8082
import static org.openqa.selenium.remote.DriverCommand.SEND_KEYS_TO_ELEMENT;
8183
import static org.openqa.selenium.remote.DriverCommand.SET_ALERT_CREDENTIALS;
@@ -214,6 +216,11 @@ public AbstractHttpCommandCodec() {
214216
defineCommand(SWITCH_TO_CONTEXT, post("/session/:sessionId/context"));
215217
defineCommand(GET_CURRENT_CONTEXT_HANDLE, get("/session/:sessionId/context"));
216218
defineCommand(GET_CONTEXT_HANDLES, get("/session/:sessionId/contexts"));
219+
220+
// Virtual Authenticator API
221+
defineCommand(ADD_VIRTUAL_AUTHENTICATOR, post("/session/:sessionId/webauthn/authenticator"));
222+
defineCommand(REMOVE_VIRTUAL_AUTHENTICATOR,
223+
delete("/session/:sessionId/webauthn/authenticator/:authenticatorId"));
217224
}
218225

219226
@Override
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.virtualauthenticator;
19+
20+
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticator;
21+
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticatorOptions;
22+
23+
/**
24+
* Interface implemented by each driver that allows access to the virtual authenticator API.
25+
*/
26+
public interface HasVirtualAuthenticator {
27+
public VirtualAuthenticator addVirtualAuthenticator(VirtualAuthenticatorOptions options);
28+
29+
public void removeVirtualAuthenticator(VirtualAuthenticator authenticator);
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.virtualauthenticator;
19+
20+
import java.util.Objects;
21+
22+
/**
23+
* Represents a virtual authenticator.
24+
*/
25+
public class VirtualAuthenticator {
26+
27+
private final String id;
28+
29+
public VirtualAuthenticator(final String id) {
30+
this.id = Objects.requireNonNull(id);
31+
}
32+
33+
public String getId() {
34+
return id;
35+
}
36+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.virtualauthenticator;
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
/**
24+
* Options for the creation of virtual authenticators.
25+
* @see http://w3c.github.io/webauthn/#sctn-automation
26+
*/
27+
public class VirtualAuthenticatorOptions {
28+
29+
enum Protocol {
30+
CTAP2("ctap2"),
31+
U2F("ctap1/u2f");
32+
33+
public final String id;
34+
35+
private Protocol(String id) {
36+
this.id = id;
37+
}
38+
}
39+
40+
enum Transport {
41+
BLE("ble"),
42+
INTERNAL("internal"),
43+
NFC("nfc"),
44+
USB("usb");
45+
46+
public final String id;
47+
48+
private Transport(String id) {
49+
this.id = id;
50+
}
51+
}
52+
53+
private Protocol protocol = Protocol.CTAP2;
54+
private Transport transport = Transport.USB;
55+
private boolean hasResidentKey = false;
56+
private boolean hasUserVerification = false;
57+
private boolean isUserConsenting = true;
58+
private boolean isUserVerified = false;
59+
60+
public VirtualAuthenticatorOptions() { }
61+
62+
public VirtualAuthenticatorOptions setProtocol(Protocol protocol) {
63+
this.protocol = protocol;
64+
return this;
65+
}
66+
67+
public VirtualAuthenticatorOptions setTransport(Transport transport) {
68+
this.transport = transport;
69+
return this;
70+
}
71+
72+
public VirtualAuthenticatorOptions setHasResidentKey(boolean hasResidentKey) {
73+
this.hasResidentKey = hasResidentKey;
74+
return this;
75+
}
76+
77+
public VirtualAuthenticatorOptions setHasUserVerification(boolean hasUserVerification) {
78+
this.hasUserVerification = hasUserVerification;
79+
return this;
80+
}
81+
82+
public VirtualAuthenticatorOptions setIsUserConsenting(boolean isUserConsenting) {
83+
this.isUserConsenting = isUserConsenting;
84+
return this;
85+
}
86+
87+
public VirtualAuthenticatorOptions setIsUserVerified(boolean isUserVerified) {
88+
this.isUserVerified = isUserVerified;
89+
return this;
90+
}
91+
92+
public Map<String, Object> toMap() {
93+
HashMap<String, Object> map = new HashMap();
94+
map.put("protocol", protocol.id);
95+
map.put("transport", transport.id);
96+
map.put("hasResidentKey", hasResidentKey);
97+
map.put("hasUserVerification", hasUserVerification);
98+
map.put("isUserConsenting", isUserConsenting);
99+
map.put("isUserVerified", isUserVerified);
100+
return map;
101+
}
102+
}

java/client/test/org/openqa/selenium/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ java_library(
8989
"logging/*.java",
9090
"net/*Test.java",
9191
"os/*Test.java",
92+
"virtualauthenticator/*Test.java",
9293
]) + [
9394
"interactions/touch/TouchTestBase.java",
9495
],
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
load("@rules_jvm_external//:defs.bzl", "artifact")
2+
load("//java:defs.bzl", "java_selenium_test_suite")
3+
4+
java_selenium_test_suite(
5+
name = "LargeTests",
6+
size = "large",
7+
srcs = glob(["*.java"]),
8+
browsers = ["chrome", "edge", "ie", "safari"],
9+
deps = [
10+
"//java/client/src/org/openqa/selenium:core",
11+
"//java/client/src/org/openqa/selenium/remote",
12+
"//java/client/test/org/openqa/selenium/environment",
13+
"//java/client/test/org/openqa/selenium/testing:annotations",
14+
"//java/client/test/org/openqa/selenium/testing:test-base",
15+
"//java/client/test/org/openqa/selenium/testing/drivers",
16+
artifact("junit:junit"),
17+
artifact("org.assertj:assertj-core"),
18+
],
19+
)

0 commit comments

Comments
 (0)