Skip to content

Commit a4e4d85

Browse files
committed
Add support for basic and digest authentication
Enabled via CDP, but hidden behind an API we control.
1 parent 7bd9f31 commit a4e4d85

4 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.openqa.selenium;
2+
3+
public interface Credentials {
4+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.openqa.selenium;
2+
3+
import org.openqa.selenium.internal.Require;
4+
5+
import java.net.URI;
6+
import java.util.function.Predicate;
7+
import java.util.function.Supplier;
8+
9+
/**
10+
* Indicates that a driver supports authentication in some way.
11+
*/
12+
public interface HasAuthentication {
13+
14+
void register(Predicate<URI> whenThisMatches, Supplier<Credentials> useTheseCredentials);
15+
16+
default void register(Supplier<Credentials> alwaysUseTheseCredentials) {
17+
Require.nonNull("Credentials", alwaysUseTheseCredentials);
18+
19+
register(uri -> true, alwaysUseTheseCredentials);
20+
}
21+
22+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.openqa.selenium;
2+
3+
import org.openqa.selenium.internal.Require;
4+
5+
import java.util.function.Supplier;
6+
7+
public class UsernameAndPassword implements Credentials {
8+
9+
private final String username;
10+
private final String password;
11+
12+
public UsernameAndPassword(String username, String password) {
13+
this.username = Require.nonNull("User name", username);
14+
this.password = Require.nonNull("Password", password);
15+
}
16+
17+
public static Supplier<Credentials> of(String username, String password) {
18+
Require.nonNull("User name", username);
19+
Require.nonNull("Password", password);
20+
21+
Credentials creds = new UsernameAndPassword(username, password);
22+
23+
return () -> creds;
24+
}
25+
26+
public String username() {
27+
return username;
28+
}
29+
30+
public String password() {
31+
return password;
32+
}
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.openqa.selenium;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.openqa.selenium.testing.JUnit4TestBase;
6+
import org.openqa.selenium.testing.NoDriverAfterTest;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.assertj.core.api.Assumptions.assumeThat;
10+
11+
public class AuthenticationTest extends JUnit4TestBase {
12+
13+
@Before
14+
public void testRequiresAuthentication() {
15+
assumeThat(driver).isInstanceOf(HasAuthentication.class);
16+
}
17+
18+
@Test
19+
@NoDriverAfterTest
20+
public void canAccessUrlProtectedByBasicAuth() {
21+
((HasAuthentication) driver).register(UsernameAndPassword.of("test", "test"));
22+
23+
driver.get(appServer.whereIsWithCredentials("basicAuth", "test", "test"));
24+
assertThat(driver.findElement(By.tagName("h1")).getText()).isEqualTo("authorized");
25+
}
26+
27+
}

0 commit comments

Comments
 (0)