Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #787 from codeborne/rosolko-784
- Loading branch information
Showing
24 changed files
with
587 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/com/codeborne/selenide/AuthenticationType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,25 @@ | |||
package com.codeborne.selenide; | |||
|
|||
/** | |||
* Authentication schemes. | |||
* | |||
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#Authentication_schemes">Web HTTP reference</a> | |||
*/ | |||
public enum AuthenticationType { | |||
BASIC("Basic"), | |||
BEARER("Bearer"), | |||
DIGEST("Digest"), | |||
HOBA("HOBA"), | |||
MUTUAL("Mutual"), | |||
AWS4_HMAC_SHA256("AWS4-HMAC-SHA256"); | |||
|
|||
private final String value; | |||
|
|||
AuthenticationType(String value) { | |||
this.value = value; | |||
} | |||
|
|||
public String getValue() { | |||
return value; | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,34 @@ | |||
package com.codeborne.selenide; | |||
|
|||
import java.util.Base64; | |||
|
|||
import static java.nio.charset.StandardCharsets.UTF_8; | |||
|
|||
public final class Credentials { | |||
public final String login; | |||
public final String password; | |||
|
|||
public Credentials(String login, String password) { | |||
this.login = login; | |||
this.password = password; | |||
} | |||
|
|||
/** | |||
* The resulting string is base64 encoded (YWxhZGRpbjpvcGVuc2VzYW1l). | |||
* | |||
* @return encoded string | |||
*/ | |||
public String encode() { | |||
byte[] credentialsBytes = combine().getBytes(UTF_8); | |||
return Base64.getEncoder().encodeToString(credentialsBytes); | |||
} | |||
|
|||
private String combine() { | |||
return String.format("%s:%s", login, password); | |||
} | |||
|
|||
@Override | |||
public String toString() { | |||
return combine(); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/com/codeborne/selenide/proxy/AuthenticationFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,35 @@ | |||
package com.codeborne.selenide.proxy; | |||
|
|||
import com.codeborne.selenide.AuthenticationType; | |||
import com.codeborne.selenide.Credentials; | |||
import io.netty.handler.codec.http.HttpHeaders; | |||
import io.netty.handler.codec.http.HttpRequest; | |||
import io.netty.handler.codec.http.HttpResponse; | |||
import net.lightbody.bmp.filters.RequestFilter; | |||
import net.lightbody.bmp.util.HttpMessageContents; | |||
import net.lightbody.bmp.util.HttpMessageInfo; | |||
|
|||
public class AuthenticationFilter implements RequestFilter { | |||
private AuthenticationType authenticationType; | |||
private Credentials credentials; | |||
|
|||
@Override | |||
public HttpResponse filterRequest(HttpRequest request, HttpMessageContents contents, HttpMessageInfo messageInfo) { | |||
if (authenticationType != null) { | |||
String authorization = String.format("%s %s", authenticationType.getValue(), credentials.encode()); | |||
HttpHeaders headers = request.headers(); | |||
headers.add("Authorization", authorization); | |||
headers.add("Proxy-Authorization", authorization); | |||
} | |||
return null; | |||
} | |||
|
|||
public void setAuthentication(AuthenticationType authenticationType, Credentials credentials) { | |||
this.authenticationType = authenticationType; | |||
this.credentials = credentials; | |||
} | |||
|
|||
public void removeAuthentication() { | |||
setAuthentication(null, null); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.