|
1 | 1 | package mertz.security.oauth2.provider.token.store.cassandra; |
2 | 2 |
|
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.util.Collection; |
| 6 | +import java.util.Date; |
| 7 | +import java.util.UUID; |
| 8 | + |
3 | 9 | import org.junit.Before; |
| 10 | +import org.junit.Test; |
4 | 11 | import org.junit.runner.RunWith; |
5 | 12 | import org.springframework.beans.factory.annotation.Autowired; |
6 | 13 | import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer; |
7 | 14 | import org.springframework.context.annotation.ComponentScan; |
8 | 15 | import org.springframework.context.annotation.Configuration; |
9 | 16 | import org.springframework.data.cassandra.core.CassandraOperations; |
10 | 17 | import org.springframework.data.cassandra.mapping.CassandraMappingContext; |
| 18 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
| 19 | +import org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken; |
| 20 | +import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; |
| 21 | +import org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken; |
| 22 | +import org.springframework.security.oauth2.common.OAuth2AccessToken; |
| 23 | +import org.springframework.security.oauth2.common.OAuth2RefreshToken; |
| 24 | +import org.springframework.security.oauth2.provider.OAuth2Authentication; |
| 25 | +import org.springframework.security.oauth2.provider.OAuth2Request; |
| 26 | +import org.springframework.security.oauth2.provider.RequestTokenFactory; |
11 | 27 | import org.springframework.security.oauth2.provider.token.TokenStore; |
12 | 28 | import org.springframework.security.oauth2.provider.token.store.TokenStoreBaseTests; |
13 | 29 | import org.springframework.test.context.ActiveProfiles; |
@@ -44,4 +60,86 @@ public static class SpringConfig { |
44 | 60 |
|
45 | 61 | } |
46 | 62 |
|
| 63 | + @Test |
| 64 | + public void testExpiringRefreshToken() throws InterruptedException { |
| 65 | + String refreshToken = "refreshToken-" + UUID.randomUUID(); |
| 66 | + DefaultOAuth2RefreshToken expectedExpiringRefreshToken = new DefaultExpiringOAuth2RefreshToken(refreshToken, new Date(System.currentTimeMillis() + 2000)); |
| 67 | + OAuth2Authentication expectedAuthentication = new OAuth2Authentication(RequestTokenFactory.createOAuth2Request("id", false), new TestAuthentication("test2", false)); |
| 68 | + getTokenStore().storeRefreshToken(expectedExpiringRefreshToken, expectedAuthentication); |
| 69 | + OAuth2RefreshToken actualExpiringRefreshToken = getTokenStore().readRefreshToken(refreshToken); |
| 70 | + assertEquals(expectedExpiringRefreshToken, actualExpiringRefreshToken); |
| 71 | + assertEquals(expectedAuthentication, getTokenStore().readAuthenticationForRefreshToken(expectedExpiringRefreshToken)); |
| 72 | + // let the token expire |
| 73 | + Thread.sleep(5000); |
| 74 | + // now it should be gone |
| 75 | + assertNull(getTokenStore().readRefreshToken(refreshToken)); |
| 76 | + assertNull(getTokenStore().readAuthenticationForRefreshToken(expectedExpiringRefreshToken)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testExpiringAccessToken() throws InterruptedException { |
| 81 | + String accessToken = "accessToken-" + UUID.randomUUID(); |
| 82 | + OAuth2Authentication expectedAuthentication = new OAuth2Authentication(RequestTokenFactory.createOAuth2Request("id", false), new TestAuthentication("test2", false)); |
| 83 | + DefaultOAuth2AccessToken expectedOAuth2AccessToken = new DefaultOAuth2AccessToken(accessToken); |
| 84 | + expectedOAuth2AccessToken.setExpiration(new Date(System.currentTimeMillis() + 2000)); |
| 85 | + getTokenStore().storeAccessToken(expectedOAuth2AccessToken, expectedAuthentication); |
| 86 | + OAuth2AccessToken actualOAuth2AccessToken = getTokenStore().readAccessToken(accessToken); |
| 87 | + assertEquals(expectedOAuth2AccessToken, actualOAuth2AccessToken); |
| 88 | + assertEquals(expectedAuthentication, getTokenStore().readAuthentication(expectedOAuth2AccessToken)); |
| 89 | + // let the token expire |
| 90 | + Thread.sleep(5000); |
| 91 | + // now it should be gone |
| 92 | + assertNull(getTokenStore().readAccessToken(accessToken)); |
| 93 | + assertNull(getTokenStore().readAuthentication(expectedOAuth2AccessToken)); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void storeAccessTokenWithoutRefreshTokenRemoveAccessTokenVerifyTokenRemoved() { |
| 98 | + OAuth2Request request = RequestTokenFactory.createOAuth2Request("clientId", false); |
| 99 | + TestingAuthenticationToken authentication = new TestingAuthenticationToken("user", "password"); |
| 100 | + String accessToken = "accessToken-" + UUID.randomUUID(); |
| 101 | + OAuth2AccessToken oauth2AccessToken = new DefaultOAuth2AccessToken(accessToken); |
| 102 | + OAuth2Authentication oauth2Authentication = new OAuth2Authentication(request, authentication); |
| 103 | + getTokenStore().storeAccessToken(oauth2AccessToken, oauth2Authentication); |
| 104 | + getTokenStore().removeAccessToken(oauth2AccessToken); |
| 105 | + Collection<OAuth2AccessToken> oauth2AccessTokens = getTokenStore().findTokensByClientId(request.getClientId()); |
| 106 | + assertTrue(oauth2AccessTokens.isEmpty()); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void storeExpiringAccessTokenWithRefreshToken_RemoveExpiredAccessTokenUsingRefreshToken() throws InterruptedException { |
| 111 | + String accessToken = "accessToken-" + UUID.randomUUID(); |
| 112 | + OAuth2Authentication expectedAuthentication = new OAuth2Authentication(RequestTokenFactory.createOAuth2Request("id", false), new TestAuthentication("test2", false)); |
| 113 | + DefaultOAuth2AccessToken expectedOAuth2AccessToken = new DefaultOAuth2AccessToken(accessToken); |
| 114 | + expectedOAuth2AccessToken.setExpiration(new Date(System.currentTimeMillis() + 2000)); |
| 115 | + String refreshToken = "refreshToken-" + UUID.randomUUID(); |
| 116 | + DefaultOAuth2RefreshToken expectedRefreshToken = new DefaultOAuth2RefreshToken(refreshToken); |
| 117 | + expectedOAuth2AccessToken.setRefreshToken(expectedRefreshToken); |
| 118 | + getTokenStore().storeAccessToken(expectedOAuth2AccessToken, expectedAuthentication); |
| 119 | + // let the access token expire |
| 120 | + Thread.sleep(5000); |
| 121 | + // now it should be gone |
| 122 | + assertNull(getTokenStore().readAccessToken(accessToken)); |
| 123 | + // use refresh token to remove already expired access token, expect no issues since access token has already been removed. |
| 124 | + getTokenStore().removeAccessTokenUsingRefreshToken(expectedRefreshToken); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void storeAccessTokenWithRefreshToken_RemoveAccessTokenUsingRefreshToken() throws InterruptedException { |
| 129 | + String accessToken = "accessToken-" + UUID.randomUUID(); |
| 130 | + OAuth2Authentication expectedAuthentication = new OAuth2Authentication(RequestTokenFactory.createOAuth2Request("id", false), new TestAuthentication("test2", false)); |
| 131 | + DefaultOAuth2AccessToken expectedOAuth2AccessToken = new DefaultOAuth2AccessToken(accessToken); |
| 132 | + String refreshToken = "refreshToken-" + UUID.randomUUID(); |
| 133 | + DefaultOAuth2RefreshToken expectedRefreshToken = new DefaultOAuth2RefreshToken(refreshToken); |
| 134 | + expectedOAuth2AccessToken.setRefreshToken(expectedRefreshToken); |
| 135 | + getTokenStore().storeAccessToken(expectedOAuth2AccessToken, expectedAuthentication); |
| 136 | + // make sure access token is in the repository |
| 137 | + OAuth2AccessToken actualOAuth2AccessToken = getTokenStore().readAccessToken(accessToken); |
| 138 | + assertEquals(expectedOAuth2AccessToken, actualOAuth2AccessToken); |
| 139 | + // use refresh token to remove access token |
| 140 | + getTokenStore().removeAccessTokenUsingRefreshToken(expectedRefreshToken); |
| 141 | + // now it should be gone |
| 142 | + assertNull(getTokenStore().readAccessToken(accessToken)); |
| 143 | + } |
| 144 | + |
47 | 145 | } |
0 commit comments