Skip to content

Commit e5d3ab2

Browse files
committed
Resolves #1
1 parent 934bd45 commit e5d3ab2

File tree

6 files changed

+157
-215
lines changed

6 files changed

+157
-215
lines changed

src/main/java/mertz/security/oauth2/provider/token/store/cassandra/CassandraTokenStore.java

Lines changed: 83 additions & 193 deletions
Large diffs are not rendered by default.

src/main/java/mertz/security/oauth2/provider/token/store/cassandra/cfg/OAuthUtil.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
package mertz.security.oauth2.provider.token.store.cassandra.cfg;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
35
import org.springframework.context.annotation.Bean;
46
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.security.oauth2.common.OAuth2AccessToken;
8+
import org.springframework.security.oauth2.provider.OAuth2Authentication;
59
import org.springframework.security.oauth2.provider.token.AuthenticationKeyGenerator;
610
import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator;
711

812
import com.fasterxml.jackson.databind.ObjectMapper;
13+
import com.fasterxml.jackson.databind.ObjectReader;
14+
import com.fasterxml.jackson.databind.ObjectWriter;
915

1016
@Configuration
1117
public class OAuthUtil {
1218

19+
private static final Logger logger = LoggerFactory.getLogger(OAuthUtil.class);
20+
21+
private static ObjectReader OAUTH2ACCESSTOKEN_OBJECT_READER = new ObjectMapper().readerFor(OAuth2AccessToken.class);
22+
23+
private static ObjectWriter OAUTH2ACCESSTOKEN_OBJECT_WRITER = new ObjectMapper().writerFor(OAuth2AccessToken.class);
24+
1325
@Bean
1426
public AuthenticationKeyGenerator getAuthenticationKeyGenerator() {
1527
return new DefaultAuthenticationKeyGenerator();
@@ -20,4 +32,31 @@ public ObjectMapper getObjectMapper() {
2032
return new ObjectMapper();
2133
}
2234

35+
public static OAuth2AccessToken deserializeOAuth2AccessToken(String jsonOAuth2AccessToken) {
36+
try {
37+
return OAUTH2ACCESSTOKEN_OBJECT_READER.readValue(jsonOAuth2AccessToken);
38+
} catch (Exception e) {
39+
logger.error("Error converting json string to OAuth2AccessToken. {}", jsonOAuth2AccessToken);
40+
throw new RuntimeException(e);
41+
}
42+
}
43+
44+
public static String serializeOAuth2AccessToken(OAuth2AccessToken oAuth2AccessToken) {
45+
try {
46+
return OAUTH2ACCESSTOKEN_OBJECT_WRITER.writeValueAsString(oAuth2AccessToken);
47+
} catch (Exception e) {
48+
logger.error("Error converting OAuth2AccessToken to json string. {}", oAuth2AccessToken);
49+
throw new RuntimeException(e);
50+
}
51+
}
52+
53+
public static String getApprovalKey(OAuth2Authentication authentication) {
54+
String userName = authentication.getUserAuthentication() == null ? "" : authentication.getUserAuthentication().getName();
55+
return getApprovalKey(authentication.getOAuth2Request().getClientId(), userName);
56+
}
57+
58+
public static String getApprovalKey(String clientId, String userName) {
59+
return clientId + (userName == null ? "" : ":" + userName);
60+
}
61+
2362
}
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
11
package mertz.security.oauth2.provider.token.store.cassandra.model;
22

3-
import java.util.Set;
4-
5-
import org.springframework.data.cassandra.mapping.PrimaryKey;
3+
import org.springframework.cassandra.core.PrimaryKeyType;
4+
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
65
import org.springframework.data.cassandra.mapping.Table;
76

87
@Table(value = ClientIdToAccessToken.TABLE)
98
public class ClientIdToAccessToken {
109

1110
public static final String TABLE = "client_id_to_access";
1211

13-
@PrimaryKey
14-
private String key;
12+
@PrimaryKeyColumn(name = "key", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
13+
private String key;
1514

16-
// Set of JSON
17-
private Set<String> oAuth2AccessTokenSet;
15+
@PrimaryKeyColumn(name = "oAuth2AccessToken", ordinal = 1, type = PrimaryKeyType.CLUSTERED)
16+
private String oAuth2AccessToken;
1817

19-
public ClientIdToAccessToken(String key, Set<String> oAuth2AccessTokenSet) {
18+
public ClientIdToAccessToken(String key, String oAuth2AccessToken) {
2019
super();
2120
this.key = key;
22-
this.oAuth2AccessTokenSet = oAuth2AccessTokenSet;
21+
this.oAuth2AccessToken = oAuth2AccessToken;
2322
}
2423

2524
public final String getKey() {
2625
return key;
2726
}
2827

29-
public final Set<String> getoAuth2AccessTokenSet() {
30-
return oAuth2AccessTokenSet;
28+
public final String getOAuth2AccessToken() {
29+
return oAuth2AccessToken;
3130
}
3231

3332
@Override
3433
public String toString() {
35-
return "ClientIdToAccessToken [key=" + key + ", oAuth2AccessTokenSet=" + oAuth2AccessTokenSet + "]";
34+
return "ClientIdToAccessToken [key=" + key + ", oAuth2AccessToken=" + oAuth2AccessToken + "]";
3635
}
3736

3837
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
package mertz.security.oauth2.provider.token.store.cassandra.model;
22

3-
import java.util.Set;
4-
5-
import org.springframework.data.cassandra.mapping.PrimaryKey;
3+
import org.springframework.cassandra.core.PrimaryKeyType;
4+
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
65
import org.springframework.data.cassandra.mapping.Table;
76

87
@Table(value = UsernameToAccessToken.TABLE)
98
public class UsernameToAccessToken {
109

1110
public static final String TABLE = "uname_to_access";
1211

13-
@PrimaryKey
12+
@PrimaryKeyColumn(name = "key", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
1413
private String key;
1514

1615
// Set of JSON
17-
private Set<String> oAuth2AccessTokenSet;
16+
@PrimaryKeyColumn(name = "oAuth2AccessToken", ordinal = 1, type = PrimaryKeyType.CLUSTERED)
17+
private String oAuth2AccessToken;
1818

19-
public UsernameToAccessToken(String key, Set<String> oAuth2AccessTokenSet) {
19+
public UsernameToAccessToken(String key, String oAuth2AccessToken) {
2020
super();
2121
this.key = key;
22-
this.oAuth2AccessTokenSet = oAuth2AccessTokenSet;
22+
this.oAuth2AccessToken = oAuth2AccessToken;
2323
}
2424

2525
public final String getKey() {
2626
return key;
2727
}
2828

29-
public final Set<String> getoAuth2AccessTokenSet() {
30-
return oAuth2AccessTokenSet;
29+
public final String getOAuth2AccessToken() {
30+
return oAuth2AccessToken;
3131
}
3232

3333
@Override
3434
public String toString() {
35-
return "UsernameToAccessToken [key=" + key + ", oAuth2AccessTokenSet=" + oAuth2AccessTokenSet + "]";
35+
return "UsernameToAccessToken [key=" + key + ", oAuth2AccessToken=" + oAuth2AccessToken + "]";
3636
}
3737

3838
}

src/main/java/mertz/security/oauth2/provider/token/store/cassandra/repo/ClientIdToAccessTokenRepository.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package mertz.security.oauth2.provider.token.store.cassandra.repo;
22

3+
import java.util.List;
4+
import java.util.Optional;
5+
36
import org.springframework.data.repository.CrudRepository;
47
import org.springframework.stereotype.Repository;
58

@@ -8,4 +11,8 @@
811
@Repository
912
public interface ClientIdToAccessTokenRepository extends CrudRepository<ClientIdToAccessToken, String> {
1013

14+
Optional<ClientIdToAccessToken> findByKeyAndOAuth2AccessToken(String key, String oAuth2AccessToken);
15+
16+
Optional<List<ClientIdToAccessToken>> findByKey(String key);
17+
1118
}

src/main/java/mertz/security/oauth2/provider/token/store/cassandra/repo/UsernameToAccessTokenRepository.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package mertz.security.oauth2.provider.token.store.cassandra.repo;
22

3+
import java.util.List;
4+
import java.util.Optional;
5+
36
import org.springframework.data.repository.CrudRepository;
47
import org.springframework.stereotype.Repository;
58

@@ -8,4 +11,8 @@
811
@Repository
912
public interface UsernameToAccessTokenRepository extends CrudRepository<UsernameToAccessToken, String> {
1013

14+
Optional<UsernameToAccessToken> findByKeyAndOAuth2AccessToken(String key, String oAuth2AccessToken);
15+
16+
Optional<List<UsernameToAccessToken>> findByKey(String key);
17+
1118
}

0 commit comments

Comments
 (0)