1 package sparklr.common;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertNull;
6 import static org.junit.Assert.assertTrue;
7 import static org.junit.Assert.fail;
8
9 import java.io.IOException;
10 import java.util.Arrays;
11
12 import org.junit.Test;
13 import org.springframework.http.HttpHeaders;
14 import org.springframework.http.HttpStatus;
15 import org.springframework.http.client.ClientHttpResponse;
16 import org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration;
17 import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider;
18 import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
19 import org.springframework.security.oauth2.common.OAuth2AccessToken;
20 import org.springframework.web.client.DefaultResponseErrorHandler;
21 import org.springframework.web.client.ResponseErrorHandler;
22
23
24
25
26
27 public abstract class AbstractClientCredentialsProviderTests extends AbstractIntegrationTests {
28
29 private HttpHeaders responseHeaders;
30
31 private HttpStatus responseStatus;
32
33
34
35
36 @Test
37 @OAuth2ContextConfiguration(ClientCredentials.class)
38 public void testPostForToken() throws Exception {
39 OAuth2AccessToken token = context.getAccessToken();
40 assertNull(token.getRefreshToken());
41 }
42
43
44
45
46 @Test
47 @OAuth2ContextConfiguration(NoScopeClientCredentials.class)
48 public void testPostForTokenWithNoScopes() throws Exception {
49 OAuth2AccessToken token = context.getAccessToken();
50 assertFalse("Wrong scope: " + token.getScope(), token.getScope().isEmpty());
51 }
52
53 @Test
54 @OAuth2ContextConfiguration(resource = InvalidClientCredentials.class, initialize = false)
55 public void testInvalidCredentials() throws Exception {
56 context.setAccessTokenProvider(new ClientCredentialsAccessTokenProvider() {
57 @Override
58 protected ResponseErrorHandler getResponseErrorHandler() {
59 return new DefaultResponseErrorHandler() {
60 public void handleError(ClientHttpResponse response) throws IOException {
61 responseHeaders = response.getHeaders();
62 responseStatus = response.getStatusCode();
63 }
64 };
65 }
66 });
67 try {
68 context.getAccessToken();
69 fail("Expected ResourceAccessException");
70 }
71 catch (Exception e) {
72
73 }
74
75 String header = responseHeaders.getFirst("WWW-Authenticate");
76 assertTrue("Wrong header: " + header, header.contains("Basic realm"));
77 assertEquals(HttpStatus.UNAUTHORIZED, responseStatus);
78 }
79
80 protected static class ClientCredentials extends ClientCredentialsResourceDetails {
81
82 public ClientCredentials(Object target) {
83 setClientId("my-client-with-secret");
84 setClientSecret("secret");
85 setScope(Arrays.asList("read"));
86 setId(getClientId());
87 }
88 }
89
90 static class InvalidClientCredentials extends ClientCredentials {
91 public InvalidClientCredentials(Object target) {
92 super(target);
93 setClientId("my-client-with-secret");
94 setClientSecret("wrong");
95 }
96 }
97
98 static class NoScopeClientCredentials extends ClientCredentialsResourceDetails {
99 public NoScopeClientCredentials(Object target) {
100 setClientId("my-client-with-secret");
101 setClientSecret("secret");
102 setId(getClientId());
103 }
104 }
105
106
107 }