forked from spring-attic/spring-security-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor TokenServices tests so JWT fits in better
- Loading branch information
Dave Syer
committed
Apr 17, 2014
1 parent
8d1e947
commit 61e7720
Showing
6 changed files
with
209 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...framework/security/oauth2/provider/token/AbstractPersistentDefaultTokenServicesTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
Oops, something went wrong.