This repository has been archived by the owner on May 31, 2022. It is now read-only.
/
AbstractTestDefaultTokenServices.java
209 lines (183 loc) · 9.15 KB
/
AbstractTestDefaultTokenServices.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package org.springframework.security.oauth2.provider.token;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
import java.util.Date;
import org.junit.Before;
import org.junit.Test;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.security.oauth2.provider.BaseClientDetails;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
/**
* @author Dave Syer
*
*/
public abstract class AbstractTestDefaultTokenServices {
private DefaultTokenServices services;
private TokenStore tokenStore;
@Before
public void setUp() throws Exception {
tokenStore = createTokenStore();
services = new DefaultTokenServices();
getTokenServices().setTokenStore(tokenStore);
getTokenServices().afterPropertiesSet();
getTokenServices().setSupportRefreshToken(true);
}
protected abstract TokenStore createTokenStore();
@Test
public void testTokenEnhancerUpdatesStoredTokens() throws Exception {
OAuth2Authentication expectedAuthentication = new OAuth2Authentication(new AuthorizationRequest("id",
Collections.singleton("read"), null, null), new TestAuthentication("test2", false));
DefaultTokenServices tokenServices = getTokenServices();
tokenServices.setTokenEnhancer(new TokenEnhancer() {
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
ExpiringOAuth2RefreshToken refreshToken = new DefaultExpiringOAuth2RefreshToken("testToken", new Date(
System.currentTimeMillis() + 100000));
result.setRefreshToken(refreshToken);
return result;
}
});
OAuth2AccessToken original = tokenServices.createAccessToken(expectedAuthentication);
OAuth2AccessToken result = tokenStore.getAccessToken(expectedAuthentication);
assertEquals(original, result);
}
@Test
public void testRefreshedTokenHasScopes() throws Exception {
ExpiringOAuth2RefreshToken expectedExpiringRefreshToken = new DefaultExpiringOAuth2RefreshToken("testToken",
new Date(System.currentTimeMillis() + 100000));
OAuth2Authentication expectedAuthentication = new OAuth2Authentication(new AuthorizationRequest("id",
Collections.singleton("read"), null, null), new TestAuthentication("test2", false));
tokenStore.storeRefreshToken(expectedExpiringRefreshToken, expectedAuthentication);
OAuth2AccessToken refreshedAccessToken = getTokenServices().refreshAccessToken(
expectedExpiringRefreshToken.getValue(), null);
assertEquals("[read]", refreshedAccessToken.getScope().toString());
}
@Test
public void testUnlimitedTokenExpiry() throws Exception {
getTokenServices().setAccessTokenValiditySeconds(0);
OAuth2Authentication expectedAuthentication = new OAuth2Authentication(new AuthorizationRequest("id",
Collections.singleton("read"), null, null), new TestAuthentication("test2", false));
OAuth2AccessToken accessToken = getTokenServices().createAccessToken(expectedAuthentication);
assertEquals(0, accessToken.getExpiresIn());
assertEquals(null, accessToken.getExpiration());
}
@Test
public void testDefaultTokenExpiry() throws Exception {
getTokenServices().setAccessTokenValiditySeconds(100);
OAuth2Authentication expectedAuthentication = new OAuth2Authentication(new AuthorizationRequest("id",
Collections.singleton("read"), null, null), new TestAuthentication("test2", false));
OAuth2AccessToken accessToken = getTokenServices().createAccessToken(expectedAuthentication);
assertTrue(100 >= accessToken.getExpiresIn());
}
@Test
public void testClientSpecificTokenExpiry() throws Exception {
getTokenServices().setAccessTokenValiditySeconds(1000);
getTokenServices().setClientDetailsService(new ClientDetailsService() {
public ClientDetails loadClientByClientId(String clientId) throws OAuth2Exception {
BaseClientDetails client = new BaseClientDetails();
client.setAccessTokenValiditySeconds(100);
return client;
}
});
OAuth2Authentication expectedAuthentication = new OAuth2Authentication(new AuthorizationRequest("id",
Collections.singleton("read"), null, null), new TestAuthentication("test2", false));
OAuth2AccessToken accessToken = getTokenServices().createAccessToken(expectedAuthentication);
assertTrue(100 >= accessToken.getExpiresIn());
}
@Test
public void testClientSpecificRefreshTokenExpiry() throws Exception {
getTokenServices().setRefreshTokenValiditySeconds(1000);
getTokenServices().setClientDetailsService(new ClientDetailsService() {
public ClientDetails loadClientByClientId(String clientId) throws OAuth2Exception {
BaseClientDetails client = new BaseClientDetails();
client.setRefreshTokenValiditySeconds(100);
return client;
}
});
OAuth2Authentication expectedAuthentication = new OAuth2Authentication(new AuthorizationRequest("id",
Collections.singleton("read"), null, null), new TestAuthentication("test2", false));
OAuth2AccessToken accessToken = getTokenServices().createAccessToken(expectedAuthentication);
DefaultExpiringOAuth2RefreshToken refreshToken = (DefaultExpiringOAuth2RefreshToken) accessToken
.getRefreshToken();
Date expectedExpiryDate = new Date(System.currentTimeMillis() + 102 * 1000L);
assertTrue(expectedExpiryDate.after(refreshToken.getExpiration()));
}
@Test
public void testOneAccessTokenPerAuthentication() throws Exception {
OAuth2Authentication authentication = new OAuth2Authentication(new AuthorizationRequest("id",
Collections.singleton("read"), null, null), new TestAuthentication("test2", false));
OAuth2AccessToken first = getTokenServices().createAccessToken(authentication);
assertEquals(1, getAccessTokenCount());
assertEquals(1, getRefreshTokenCount());
OAuth2AccessToken second = getTokenServices().createAccessToken(authentication);
assertEquals(first, second);
assertEquals(1, getAccessTokenCount());
assertEquals(1, getRefreshTokenCount());
}
@Test
public void testOneAccessTokenPerUniqueAuthentication() throws Exception {
getTokenServices().createAccessToken(
new OAuth2Authentication(new AuthorizationRequest("id", Collections.singleton("read"), null, null),
new TestAuthentication("test2", false)));
assertEquals(1, getAccessTokenCount());
getTokenServices().createAccessToken(
new OAuth2Authentication(new AuthorizationRequest("id", Collections.singleton("write"), null, null),
new TestAuthentication("test2", false)));
assertEquals(2, getAccessTokenCount());
}
@Test
public void testRefreshTokenMaintainsState() throws Exception {
OAuth2Authentication expectedAuthentication = new OAuth2Authentication(new AuthorizationRequest("id",
Collections.singleton("read"), null, null), new TestAuthentication("test2", false));
getTokenServices().setSupportRefreshToken(true);
OAuth2AccessToken accessToken = getTokenServices().createAccessToken(expectedAuthentication);
OAuth2RefreshToken expectedExpiringRefreshToken = accessToken.getRefreshToken();
OAuth2AccessToken refreshedAccessToken = getTokenServices().refreshAccessToken(
expectedExpiringRefreshToken.getValue(), null);
assertNotNull(refreshedAccessToken);
assertEquals(1, getAccessTokenCount());
}
@Test
public void testNotReuseRefreshTokenMaintainsState() throws Exception {
OAuth2Authentication expectedAuthentication = new OAuth2Authentication(new AuthorizationRequest("id",
Collections.singleton("read"), null, null), new TestAuthentication("test2", false));
getTokenServices().setSupportRefreshToken(true);
getTokenServices().setReuseRefreshToken(false);
OAuth2AccessToken accessToken = getTokenServices().createAccessToken(expectedAuthentication);
OAuth2RefreshToken expectedExpiringRefreshToken = accessToken.getRefreshToken();
OAuth2AccessToken refreshedAccessToken = getTokenServices().refreshAccessToken(
expectedExpiringRefreshToken.getValue(), null);
assertNotNull(refreshedAccessToken);
assertEquals(1, getRefreshTokenCount());
}
protected abstract int getAccessTokenCount();
protected abstract int getRefreshTokenCount();
protected DefaultTokenServices getTokenServices() {
return services;
}
protected static class TestAuthentication extends AbstractAuthenticationToken {
private String principal;
public TestAuthentication(String name, boolean authenticated) {
super(null);
setAuthenticated(authenticated);
this.principal = name;
}
public Object getCredentials() {
return null;
}
public Object getPrincipal() {
return this.principal;
}
}
}