Skip to content

Commit d1ebdd8

Browse files
security work
1 parent 909d72d commit d1ebdd8

2 files changed

Lines changed: 27 additions & 40 deletions

File tree

‎src/test/java/org/rest/client/AbstractClientRESTTemplate.java‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ protected final HttpHeaders createContentTypeHeaders() {
130130

131131
public abstract String getURI();
132132

133-
public abstract IClientTemplate<T> givenAuthenticated();
133+
public final IClientTemplate<T> givenAuthenticated() {
134+
if (isBasicAuth()) {
135+
basicAuth();
136+
} else {
137+
digestAuth();
138+
}
139+
140+
return this;
141+
}
142+
143+
protected abstract boolean isBasicAuth();
144+
145+
protected abstract void basicAuth();
146+
147+
protected abstract void digestAuth();
134148

135149
}
Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
package org.rest.sec.client.template.newer;
22

3-
import org.apache.http.HttpHost;
43
import org.apache.http.auth.AuthScope;
54
import org.apache.http.auth.UsernamePasswordCredentials;
6-
import org.apache.http.client.AuthCache;
7-
import org.apache.http.client.protocol.ClientContext;
8-
import org.apache.http.impl.auth.DigestScheme;
9-
import org.apache.http.impl.client.BasicAuthCache;
105
import org.apache.http.impl.client.DefaultHttpClient;
11-
import org.apache.http.protocol.BasicHttpContext;
126
import org.rest.client.AbstractClientRESTTemplate;
13-
import org.rest.client.IClientTemplate;
147
import org.rest.sec.client.ExamplePaths;
158
import org.rest.sec.model.dto.User;
169
import org.rest.sec.util.SecurityConstants;
17-
import org.rest.security.DigestHttpComponentsClientHttpRequestFactory;
1810
import org.springframework.beans.factory.annotation.Autowired;
1911
import org.springframework.beans.factory.annotation.Value;
2012
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
@@ -30,8 +22,6 @@ public class UserClientRESTTemplate extends AbstractClientRESTTemplate<User> {
3022
private String host;
3123
@Value("${http.port}")
3224
private int port;
33-
@Value("${http.protocol}")
34-
private String protocol;
3525

3626
@Value("${sec.auth.basic}")
3727
private boolean basicAuth;
@@ -40,8 +30,6 @@ public UserClientRESTTemplate() {
4030
super(User.class);
4131
}
4232

43-
//
44-
4533
// template method
4634

4735
@Override
@@ -50,35 +38,20 @@ public final String getURI() {
5038
}
5139

5240
@Override
53-
public final IClientTemplate<User> givenAuthenticated() {
54-
if (basicAuth) {
55-
final HttpComponentsClientHttpRequestFactory requestFactory = (HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();
56-
final DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient();
57-
httpClient.getCredentialsProvider().setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM), new UsernamePasswordCredentials(SecurityConstants.ADMIN_USERNAME, SecurityConstants.ADMIN_PASSWORD));
58-
} else {
59-
final HttpHost targetHost = new HttpHost(host, port, protocol);
60-
61-
// Create AuthCache instance
62-
final AuthCache authCache = new BasicAuthCache();
63-
// Generate DIGEST scheme object, initialize it and add it to the local auth cache
64-
final DigestScheme digestAuth = new DigestScheme();
65-
// Suppose we already know the realm name
66-
digestAuth.overrideParamter("realm", "Contacts Realm via Digest Authentication");
67-
// Suppose we already know the expected nonce value
68-
digestAuth.overrideParamter("nonce", "acegi");
69-
authCache.put(targetHost, digestAuth);
70-
71-
// Add AuthCache to the execution context
72-
final BasicHttpContext localcontext = new BasicHttpContext();
73-
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
41+
protected boolean isBasicAuth() {
42+
return basicAuth;
43+
}
7444

75-
final DigestHttpComponentsClientHttpRequestFactory requestFactory = (DigestHttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();
76-
requestFactory.setHttpContext(localcontext);
77-
final DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient();
78-
httpClient.getCredentialsProvider().setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM), new UsernamePasswordCredentials(SecurityConstants.ADMIN_USERNAME, SecurityConstants.ADMIN_PASSWORD));
79-
}
45+
@Override
46+
protected void basicAuth() {
47+
final HttpComponentsClientHttpRequestFactory requestFactory = (HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();
48+
final DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient();
49+
httpClient.getCredentialsProvider().setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM), new UsernamePasswordCredentials(SecurityConstants.ADMIN_USERNAME, SecurityConstants.ADMIN_PASSWORD));
50+
}
8051

81-
return this;
52+
@Override
53+
protected void digestAuth() {
54+
throw new UnsupportedOperationException();
8255
}
8356

8457
}

0 commit comments

Comments
 (0)