Skip to content

Commit ce0b6f1

Browse files
authored
[java] [cdp] Simplify Augmentation for Basic Auth (#11601)
1 parent 2917d05 commit ce0b6f1

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.remote;
19+
20+
import static org.openqa.selenium.remote.Browser.CHROME;
21+
import static org.openqa.selenium.remote.Browser.EDGE;
22+
import static org.openqa.selenium.remote.Browser.OPERA;
23+
24+
import org.openqa.selenium.Capabilities;
25+
import org.openqa.selenium.HasAuthentication;
26+
import org.openqa.selenium.WebDriver;
27+
import org.openqa.selenium.devtools.DevTools;
28+
import org.openqa.selenium.devtools.HasDevTools;
29+
import org.openqa.selenium.internal.Require;
30+
31+
import java.util.function.Predicate;
32+
import java.util.logging.Logger;
33+
34+
public class AddHasAuthentication
35+
implements AugmenterProvider<HasAuthentication> {
36+
37+
private static final Logger logger = Logger.getLogger(AddHasAuthentication.class.getName());
38+
private static final Predicate<String> IS_CHROMIUM_BROWSER = name ->
39+
CHROME.is(name) ||
40+
EDGE.is(name) ||
41+
OPERA.is(name);
42+
43+
@Override
44+
public Predicate<Capabilities> isApplicable() {
45+
return caps -> IS_CHROMIUM_BROWSER.test(caps.getBrowserName());
46+
}
47+
48+
@Override
49+
public Class<HasAuthentication> getDescribedInterface() {
50+
return HasAuthentication.class;
51+
}
52+
53+
@Override
54+
public HasAuthentication getImplementation(Capabilities capabilities, ExecuteMethod executeMethod) {
55+
return (whenThisMatches, useTheseCredentials) -> {
56+
Require.nonNull("Check to use to see how we should authenticate", whenThisMatches);
57+
Require.nonNull("Credentials to use when authenticating", useTheseCredentials);
58+
59+
if (((RemoteExecuteMethod) executeMethod).getWrappedDriver() instanceof HasDevTools) {
60+
WebDriver driver = ((RemoteExecuteMethod) executeMethod).getWrappedDriver();
61+
DevTools devTools = ((HasDevTools) driver).getDevTools();
62+
devTools.createSessionIfThereIsNotOne();
63+
devTools.getDomains().network().addAuthHandler(whenThisMatches, useTheseCredentials);
64+
}
65+
// Todo: Similarly add for BiDi once BiDi supports the same functionality
66+
};
67+
}
68+
}

java/src/org/openqa/selenium/remote/Augmenter.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.openqa.selenium.Beta;
2828
import org.openqa.selenium.Capabilities;
29+
import org.openqa.selenium.HasAuthentication;
2930
import org.openqa.selenium.HasCapabilities;
3031
import org.openqa.selenium.ImmutableCapabilities;
3132
import org.openqa.selenium.WebDriver;
@@ -36,6 +37,7 @@
3637

3738
import java.lang.reflect.Field;
3839
import java.lang.reflect.Modifier;
40+
import java.util.ArrayList;
3941
import java.util.HashSet;
4042
import java.util.List;
4143
import java.util.ServiceLoader;
@@ -158,6 +160,12 @@ public WebDriver augment(WebDriver driver) {
158160
.filter(augmentation -> augmentation.whenMatches.test(caps))
159161
.collect(Collectors.toList());
160162

163+
return this.augment(driver, matchingAugmenters);
164+
}
165+
166+
private WebDriver augment(WebDriver driver, List<Augmentation<?>> matchingAugmenters) {
167+
Capabilities caps = ImmutableCapabilities.copyOf(((HasCapabilities) driver).getCapabilities());
168+
161169
if (matchingAugmenters.isEmpty()) {
162170
return driver;
163171
}
@@ -193,12 +201,36 @@ public WebDriver augment(WebDriver driver) {
193201

194202
copyFields(driver.getClass(), driver, toReturn);
195203

204+
toReturn = addDependentAugmentations(toReturn);
205+
196206
return toReturn;
197207
} catch (ReflectiveOperationException e) {
198208
throw new IllegalStateException("Unable to create new proxy", e);
199209
}
200210
}
201211

212+
private WebDriver addDependentAugmentations(WebDriver driver) {
213+
List<Augmentation<?>> augmentationList = new ArrayList<>();
214+
215+
WebDriver toReturn = driver;
216+
217+
// add interfaces that need to use the augmented driver
218+
if (!(driver instanceof HasAuthentication)) {
219+
augmentationList.add(createAugmentation(new AddHasAuthentication()));
220+
}
221+
222+
if (!augmentationList.isEmpty()) {
223+
Capabilities caps = ImmutableCapabilities.copyOf(((HasCapabilities) driver).getCapabilities());
224+
225+
List<Augmentation<?>> matchingAugmenters = augmentationList.stream()
226+
.filter(augmentation -> augmentation.whenMatches.test(caps))
227+
.collect(Collectors.toList());
228+
229+
toReturn = this.augment(driver, matchingAugmenters);
230+
}
231+
return toReturn;
232+
}
233+
202234
private RemoteWebDriver extractRemoteWebDriver(WebDriver driver) {
203235
Require.nonNull("WebDriver", driver);
204236

0 commit comments

Comments
 (0)