1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.extension.repository.http.internal; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
|
24 |
|
import org.apache.http.HttpException; |
25 |
|
import org.apache.http.HttpHost; |
26 |
|
import org.apache.http.HttpRequest; |
27 |
|
import org.apache.http.HttpRequestInterceptor; |
28 |
|
import org.apache.http.auth.AuthScheme; |
29 |
|
import org.apache.http.auth.AuthScope; |
30 |
|
import org.apache.http.auth.AuthState; |
31 |
|
import org.apache.http.auth.Credentials; |
32 |
|
import org.apache.http.client.CredentialsProvider; |
33 |
|
import org.apache.http.client.protocol.HttpClientContext; |
34 |
|
import org.apache.http.protocol.HttpContext; |
35 |
|
import org.apache.http.protocol.HttpCoreContext; |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
@version |
41 |
|
@since |
42 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 4 |
Complexity Density: 0.4 |
|
43 |
|
public class PreemptiveAuth implements HttpRequestInterceptor |
44 |
|
{ |
45 |
|
|
46 |
|
@link |
47 |
|
|
48 |
|
public static final PreemptiveAuth INSTANCE = new PreemptiveAuth(); |
49 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
|
50 |
0 |
@Override... |
51 |
|
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException |
52 |
|
{ |
53 |
0 |
AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE); |
54 |
|
|
55 |
|
|
56 |
0 |
if (authState.getAuthScheme() == null) { |
57 |
0 |
AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth"); |
58 |
0 |
CredentialsProvider credsProvider = |
59 |
|
(CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER); |
60 |
0 |
HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST); |
61 |
0 |
if (authScheme != null) { |
62 |
0 |
Credentials credentials = |
63 |
|
credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort())); |
64 |
0 |
if (credentials == null) { |
65 |
0 |
throw new HttpException("No credentials for preemptive authentication"); |
66 |
|
} |
67 |
0 |
authState.update(authScheme, credentials); |
68 |
|
} |
69 |
|
} |
70 |
|
|
71 |
|
} |
72 |
|
} |