Skip to content

Commit

Permalink
Merge pull request #2 from Mert-Z/mertz_tokenrepositoryfix
Browse files Browse the repository at this point in the history
Resolves #1
  • Loading branch information
Mert-Z authored Oct 4, 2017
2 parents 934bd45 + e5d3ab2 commit c09dff1
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 215 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
package mertz.security.oauth2.provider.token.store.cassandra.cfg;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.AuthenticationKeyGenerator;
import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;

@Configuration
public class OAuthUtil {

private static final Logger logger = LoggerFactory.getLogger(OAuthUtil.class);

private static ObjectReader OAUTH2ACCESSTOKEN_OBJECT_READER = new ObjectMapper().readerFor(OAuth2AccessToken.class);

private static ObjectWriter OAUTH2ACCESSTOKEN_OBJECT_WRITER = new ObjectMapper().writerFor(OAuth2AccessToken.class);

@Bean
public AuthenticationKeyGenerator getAuthenticationKeyGenerator() {
return new DefaultAuthenticationKeyGenerator();
Expand All @@ -20,4 +32,31 @@ public ObjectMapper getObjectMapper() {
return new ObjectMapper();
}

public static OAuth2AccessToken deserializeOAuth2AccessToken(String jsonOAuth2AccessToken) {
try {
return OAUTH2ACCESSTOKEN_OBJECT_READER.readValue(jsonOAuth2AccessToken);
} catch (Exception e) {
logger.error("Error converting json string to OAuth2AccessToken. {}", jsonOAuth2AccessToken);
throw new RuntimeException(e);
}
}

public static String serializeOAuth2AccessToken(OAuth2AccessToken oAuth2AccessToken) {
try {
return OAUTH2ACCESSTOKEN_OBJECT_WRITER.writeValueAsString(oAuth2AccessToken);
} catch (Exception e) {
logger.error("Error converting OAuth2AccessToken to json string. {}", oAuth2AccessToken);
throw new RuntimeException(e);
}
}

public static String getApprovalKey(OAuth2Authentication authentication) {
String userName = authentication.getUserAuthentication() == null ? "" : authentication.getUserAuthentication().getName();
return getApprovalKey(authentication.getOAuth2Request().getClientId(), userName);
}

public static String getApprovalKey(String clientId, String userName) {
return clientId + (userName == null ? "" : ":" + userName);
}

}
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
package mertz.security.oauth2.provider.token.store.cassandra.model;

import java.util.Set;

import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.cassandra.core.PrimaryKeyType;
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.mapping.Table;

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

public static final String TABLE = "client_id_to_access";

@PrimaryKey
private String key;
@PrimaryKeyColumn(name = "key", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
private String key;

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

public ClientIdToAccessToken(String key, Set<String> oAuth2AccessTokenSet) {
public ClientIdToAccessToken(String key, String oAuth2AccessToken) {
super();
this.key = key;
this.oAuth2AccessTokenSet = oAuth2AccessTokenSet;
this.oAuth2AccessToken = oAuth2AccessToken;
}

public final String getKey() {
return key;
}

public final Set<String> getoAuth2AccessTokenSet() {
return oAuth2AccessTokenSet;
public final String getOAuth2AccessToken() {
return oAuth2AccessToken;
}

@Override
public String toString() {
return "ClientIdToAccessToken [key=" + key + ", oAuth2AccessTokenSet=" + oAuth2AccessTokenSet + "]";
return "ClientIdToAccessToken [key=" + key + ", oAuth2AccessToken=" + oAuth2AccessToken + "]";
}

}
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
package mertz.security.oauth2.provider.token.store.cassandra.model;

import java.util.Set;

import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.cassandra.core.PrimaryKeyType;
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.mapping.Table;

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

public static final String TABLE = "uname_to_access";

@PrimaryKey
@PrimaryKeyColumn(name = "key", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
private String key;

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

public UsernameToAccessToken(String key, Set<String> oAuth2AccessTokenSet) {
public UsernameToAccessToken(String key, String oAuth2AccessToken) {
super();
this.key = key;
this.oAuth2AccessTokenSet = oAuth2AccessTokenSet;
this.oAuth2AccessToken = oAuth2AccessToken;
}

public final String getKey() {
return key;
}

public final Set<String> getoAuth2AccessTokenSet() {
return oAuth2AccessTokenSet;
public final String getOAuth2AccessToken() {
return oAuth2AccessToken;
}

@Override
public String toString() {
return "UsernameToAccessToken [key=" + key + ", oAuth2AccessTokenSet=" + oAuth2AccessTokenSet + "]";
return "UsernameToAccessToken [key=" + key + ", oAuth2AccessToken=" + oAuth2AccessToken + "]";
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package mertz.security.oauth2.provider.token.store.cassandra.repo;

import java.util.List;
import java.util.Optional;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -8,4 +11,8 @@
@Repository
public interface ClientIdToAccessTokenRepository extends CrudRepository<ClientIdToAccessToken, String> {

Optional<ClientIdToAccessToken> findByKeyAndOAuth2AccessToken(String key, String oAuth2AccessToken);

Optional<List<ClientIdToAccessToken>> findByKey(String key);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package mertz.security.oauth2.provider.token.store.cassandra.repo;

import java.util.List;
import java.util.Optional;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -8,4 +11,8 @@
@Repository
public interface UsernameToAccessTokenRepository extends CrudRepository<UsernameToAccessToken, String> {

Optional<UsernameToAccessToken> findByKeyAndOAuth2AccessToken(String key, String oAuth2AccessToken);

Optional<List<UsernameToAccessToken>> findByKey(String key);

}

0 comments on commit c09dff1

Please sign in to comment.