Skip to content

Commit

Permalink
Refactor TokenServices tests so JWT fits in better
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Syer committed Apr 17, 2014
1 parent 8d1e947 commit 61e7720
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 141 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

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.Arrays;
Expand All @@ -13,10 +25,8 @@
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.InvalidGrantException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.ClientDetails;
Expand All @@ -28,9 +38,9 @@

/**
* @author Dave Syer
*
*
*/
public abstract class AbstractTestDefaultTokenServices {
public abstract class AbstractDefaultTokenServicesTests {

private DefaultTokenServices services;

Expand All @@ -40,92 +50,55 @@ public abstract class AbstractTestDefaultTokenServices {
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 {
final ExpiringOAuth2RefreshToken refreshToken = new DefaultExpiringOAuth2RefreshToken("testToken", new Date(
System.currentTimeMillis() + 100000));
getTokenServices().setTokenEnhancer(new TokenEnhancer() {
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
result.setRefreshToken(refreshToken);
return result;
}
});
OAuth2Authentication authentication = createAuthentication();
OAuth2AccessToken original = getTokenServices().createAccessToken(authentication);
assertTrue(original.getRefreshToken().equals(refreshToken));
OAuth2AccessToken result = tokenStore.getAccessToken(authentication);
assertEquals(original, result);
assertEquals(refreshToken, result.getRefreshToken());
assertEquals(refreshToken, tokenStore.readRefreshToken(refreshToken.getValue()));
configureTokenServices(services);
}

@Test
public void testTokenRevoked() throws Exception {
OAuth2Authentication authentication = createAuthentication();
OAuth2AccessToken original = getTokenServices().createAccessToken(authentication);
tokenStore.removeAccessToken(original);
assertEquals(0, tokenStore.findTokensByClientId(authentication.getOAuth2Request().getClientId()).size());
}

@Test
public void testRefreshedTokenIsEnhanced() throws Exception {
getTokenServices().setTokenEnhancer(new TokenEnhancer() {
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
result.setValue("I'mEnhanced");
return result;
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);
client.setAuthorizedGrantTypes(Arrays.asList("authorization_code", "refresh_token"));
return client;
}
});

OAuth2AccessToken accessToken = getTokenServices().createAccessToken(createAuthentication());
assertTrue(accessToken.getValue().startsWith("I'mEnhanced"));
TokenRequest tokenRequest = new TokenRequest(Collections.singletonMap("client_id", "id"), "id", null, null);
OAuth2AccessToken refreshedAccessToken = getTokenServices().refreshAccessToken(
accessToken.getRefreshToken().getValue(), tokenRequest);
assertTrue(refreshedAccessToken.getValue().startsWith("I'mEnhanced"));
DefaultExpiringOAuth2RefreshToken refreshToken = (DefaultExpiringOAuth2RefreshToken) accessToken
.getRefreshToken();
Date expectedExpiryDate = new Date(System.currentTimeMillis() + 102 * 1000L);
assertTrue(expectedExpiryDate.after(refreshToken.getExpiration()));
}

@Test
public void testRefreshedTokenHasScopes() throws Exception {
ExpiringOAuth2RefreshToken expectedExpiringRefreshToken = new DefaultExpiringOAuth2RefreshToken("testToken",
new Date(System.currentTimeMillis() + 100000));
tokenStore.storeRefreshToken(expectedExpiringRefreshToken, createAuthentication());
TokenRequest tokenRequest = new TokenRequest(Collections.singletonMap("client_id", "id"), "id", null, null);
@Test(expected = InvalidGrantException.class)
public void testRefreshedTokenInvalidWithWrongClient() throws Exception {
ExpiringOAuth2RefreshToken expectedExpiringRefreshToken = (ExpiringOAuth2RefreshToken) getTokenServices()
.createAccessToken(createAuthentication()).getRefreshToken();
TokenRequest tokenRequest = new TokenRequest(Collections.singletonMap("client_id", "wrong"), "wrong", null,
null);
OAuth2AccessToken refreshedAccessToken = getTokenServices().refreshAccessToken(
expectedExpiringRefreshToken.getValue(), tokenRequest);
assertEquals("[read, write]", refreshedAccessToken.getScope().toString());
assertEquals("[read]", refreshedAccessToken.getScope().toString());
}

@Test
public void testRefreshedTokenHasNarrowedScopes() throws Exception {
ExpiringOAuth2RefreshToken expectedExpiringRefreshToken = new DefaultExpiringOAuth2RefreshToken("testToken",
new Date(System.currentTimeMillis() + 100000));
tokenStore.storeRefreshToken(expectedExpiringRefreshToken, createAuthentication());
ExpiringOAuth2RefreshToken expectedExpiringRefreshToken = (ExpiringOAuth2RefreshToken) getTokenServices()
.createAccessToken(createAuthentication()).getRefreshToken();
TokenRequest tokenRequest = new TokenRequest(Collections.singletonMap("client_id", "id"), "id",
Collections.singleton("read"), null);
OAuth2AccessToken refreshedAccessToken = getTokenServices().refreshAccessToken(
expectedExpiringRefreshToken.getValue(), tokenRequest);
assertEquals("[read]", refreshedAccessToken.getScope().toString());
}

@Test(expected = InvalidGrantException.class)
public void testRefreshedTokenInvalidWithWrongClient() throws Exception {
ExpiringOAuth2RefreshToken expectedExpiringRefreshToken = new DefaultExpiringOAuth2RefreshToken("testToken",
new Date(System.currentTimeMillis() + 100000));
tokenStore.storeRefreshToken(expectedExpiringRefreshToken, createAuthentication());
TokenRequest tokenRequest = new TokenRequest(Collections.singletonMap("client_id", "wrong"), "wrong", null,
null);
OAuth2AccessToken refreshedAccessToken = getTokenServices().refreshAccessToken(
expectedExpiringRefreshToken.getValue(), tokenRequest);
assertEquals("[read]", refreshedAccessToken.getScope().toString());
@Test
public void testTokenRevoked() throws Exception {
OAuth2Authentication authentication = createAuthentication();
OAuth2AccessToken original = getTokenServices().createAccessToken(authentication);
getTokenStore().removeAccessToken(original);
assertEquals(0, getTokenStore().findTokensByClientId(authentication.getOAuth2Request().getClientId()).size());
}

@Test
Expand Down Expand Up @@ -158,85 +131,32 @@ public ClientDetails loadClientByClientId(String clientId) throws OAuth2Exceptio
}

@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);
client.setAuthorizedGrantTypes(Arrays.asList("authorization_code", "refresh_token"));
return client;
}
});
OAuth2AccessToken accessToken = getTokenServices().createAccessToken(createAuthentication());
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 = createAuthentication();
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(RequestTokenFactory.createOAuth2Request("id", false,
Collections.singleton("read")), new TestAuthentication("test2",
false)));
assertEquals(1, getAccessTokenCount());
getTokenServices()
.createAccessToken(
new OAuth2Authentication(RequestTokenFactory.createOAuth2Request("id", false,
Collections.singleton("write")), new TestAuthentication(
"test2", false)));
assertEquals(2, getAccessTokenCount());
}

@Test
public void testRefreshTokenMaintainsState() throws Exception {
getTokenServices().setSupportRefreshToken(true);
OAuth2AccessToken accessToken = getTokenServices().createAccessToken(createAuthentication());
OAuth2RefreshToken expectedExpiringRefreshToken = accessToken.getRefreshToken();
public void testRefreshedTokenHasScopes() throws Exception {
ExpiringOAuth2RefreshToken expectedExpiringRefreshToken = (ExpiringOAuth2RefreshToken) getTokenServices()
.createAccessToken(createAuthentication()).getRefreshToken();
TokenRequest tokenRequest = new TokenRequest(Collections.singletonMap("client_id", "id"), "id", null, null);
OAuth2AccessToken refreshedAccessToken = getTokenServices().refreshAccessToken(
expectedExpiringRefreshToken.getValue(), tokenRequest);
assertNotNull(refreshedAccessToken);
assertEquals(1, getAccessTokenCount());
assertEquals("[read, write]", refreshedAccessToken.getScope().toString());
}

@Test
public void testNotReuseRefreshTokenMaintainsState() throws Exception {
getTokenServices().setSupportRefreshToken(true);
getTokenServices().setReuseRefreshToken(false);
OAuth2AccessToken accessToken = getTokenServices().createAccessToken(createAuthentication());
OAuth2RefreshToken expectedExpiringRefreshToken = accessToken.getRefreshToken();
TokenRequest tokenRequest = new TokenRequest(Collections.singletonMap("client_id", "id"), "id", null, null);
OAuth2AccessToken refreshedAccessToken = getTokenServices().refreshAccessToken(
expectedExpiringRefreshToken.getValue(), tokenRequest);
assertNotNull(refreshedAccessToken);
assertEquals(1, getRefreshTokenCount());
protected void configureTokenServices(DefaultTokenServices services) throws Exception {
services.setTokenStore(tokenStore);
services.setSupportRefreshToken(true);
services.afterPropertiesSet();
}

private OAuth2Authentication createAuthentication() {
protected abstract TokenStore createTokenStore();

protected OAuth2Authentication createAuthentication() {
return new OAuth2Authentication(RequestTokenFactory.createOAuth2Request(null, "id", null, false,
new LinkedHashSet<String>(Arrays.asList("read", "write")), null, null, null, null),
new TestAuthentication("test2", false));
}

protected abstract int getAccessTokenCount();

protected abstract int getRefreshTokenCount();
protected TokenStore getTokenStore() {
return tokenStore;
}

protected DefaultTokenServices getTokenServices() {
return services;
Expand All @@ -262,5 +182,5 @@ public Object getPrincipal() {
return this.principal;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
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.Test;
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.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.RequestTokenFactory;
import org.springframework.security.oauth2.provider.TokenRequest;

/**
* @author Dave Syer
*
*/
public abstract class AbstractPersistentDefaultTokenServicesTests extends AbstractDefaultTokenServicesTests {

@Test
public void testTokenEnhancerUpdatesStoredTokens() throws Exception {
final ExpiringOAuth2RefreshToken refreshToken = new DefaultExpiringOAuth2RefreshToken("testToken", new Date(
System.currentTimeMillis() + 100000));
getTokenServices().setTokenEnhancer(new TokenEnhancer() {
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
result.setRefreshToken(refreshToken);
return result;
}
});
OAuth2Authentication authentication = createAuthentication();
OAuth2AccessToken original = getTokenServices().createAccessToken(authentication);
assertTrue(original.getRefreshToken().equals(refreshToken));
OAuth2AccessToken result = getTokenStore().getAccessToken(authentication);
assertEquals(original, result);
assertEquals(refreshToken, result.getRefreshToken());
assertEquals(refreshToken, getTokenStore().readRefreshToken(refreshToken.getValue()));
}

@Test
public void testRefreshedTokenIsEnhanced() throws Exception {
getTokenServices().setTokenEnhancer(new TokenEnhancer() {
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
result.setValue("I'mEnhanced");
return result;
}
});

OAuth2AccessToken accessToken = getTokenServices().createAccessToken(createAuthentication());
assertTrue(accessToken.getValue().startsWith("I'mEnhanced"));
TokenRequest tokenRequest = new TokenRequest(Collections.singletonMap("client_id", "id"), "id", null, null);
OAuth2AccessToken refreshedAccessToken = getTokenServices().refreshAccessToken(
accessToken.getRefreshToken().getValue(), tokenRequest);
assertTrue(refreshedAccessToken.getValue().startsWith("I'mEnhanced"));
}

@Test
public void testOneAccessTokenPerAuthentication() throws Exception {
OAuth2Authentication authentication = createAuthentication();
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(RequestTokenFactory.createOAuth2Request("id", false,
Collections.singleton("read")), new TestAuthentication("test2",
false)));
assertEquals(1, getAccessTokenCount());
getTokenServices()
.createAccessToken(
new OAuth2Authentication(RequestTokenFactory.createOAuth2Request("id", false,
Collections.singleton("write")), new TestAuthentication(
"test2", false)));
assertEquals(2, getAccessTokenCount());
}

@Test
public void testRefreshTokenMaintainsState() throws Exception {
getTokenServices().setSupportRefreshToken(true);
OAuth2AccessToken accessToken = getTokenServices().createAccessToken(createAuthentication());
OAuth2RefreshToken expectedExpiringRefreshToken = accessToken.getRefreshToken();
TokenRequest tokenRequest = new TokenRequest(Collections.singletonMap("client_id", "id"), "id", null, null);
OAuth2AccessToken refreshedAccessToken = getTokenServices().refreshAccessToken(
expectedExpiringRefreshToken.getValue(), tokenRequest);
assertNotNull(refreshedAccessToken);
assertEquals(1, getAccessTokenCount());
}

@Test
public void testNotReuseRefreshTokenMaintainsState() throws Exception {
getTokenServices().setSupportRefreshToken(true);
getTokenServices().setReuseRefreshToken(false);
OAuth2AccessToken accessToken = getTokenServices().createAccessToken(createAuthentication());
OAuth2RefreshToken expectedExpiringRefreshToken = accessToken.getRefreshToken();
TokenRequest tokenRequest = new TokenRequest(Collections.singletonMap("client_id", "id"), "id", null, null);
OAuth2AccessToken refreshedAccessToken = getTokenServices().refreshAccessToken(
expectedExpiringRefreshToken.getValue(), tokenRequest);
assertNotNull(refreshedAccessToken);
assertEquals(1, getRefreshTokenCount());
}

protected abstract int getAccessTokenCount();

protected abstract int getRefreshTokenCount();

}
Loading

0 comments on commit 61e7720

Please sign in to comment.