/
AuthenticationFilter.java
35 lines (30 loc) · 1.27 KB
/
AuthenticationFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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);
}
}