Skip to content
This repository has been archived by the owner on May 31, 2022. It is now read-only.

Commit

Permalink
SECOAUTH-140: Extracted TokenStore strategy from RandomValueOAuth2Pro…
Browse files Browse the repository at this point in the history
…viderTokenServices.
  • Loading branch information
tekul committed Oct 18, 2011
1 parent 93e7f19 commit 64ded15
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 177 deletions.
Expand Up @@ -25,13 +25,13 @@
</http>

<bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased" xmlns="http://www.springframework.org/schema/beans">
<property name="decisionVoters">
<constructor-arg>
<list>
<bean class="org.springframework.security.oauth2.provider.vote.ScopeVoter" />
<bean class="org.springframework.security.access.vote.RoleVoter" />
<bean class="org.springframework.security.access.vote.AuthenticatedVoter" />
</list>
</property>
</constructor-arg>
</bean>

<authentication-manager alias="authenticationManager" xmlns="http://www.springframework.org/schema/security">
Expand All @@ -43,7 +43,10 @@
</authentication-provider>
</authentication-manager>

<bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.InMemoryOAuth2ProviderTokenServices">
<bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.RandomValueOAuth2ProviderTokenServices">
<property name="tokenStore">
<bean class="org.springframework.security.oauth2.provider.token.InMemoryTokenStore" />
</property>
<property name="supportRefreshToken" value="true" />
</bean>

Expand Down
@@ -1,11 +1,11 @@
/*
* Copyright 2008-2009 Web Cohesion
*
*
* 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.
Expand All @@ -23,14 +23,14 @@
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.security.oauth2.provider.filter.CompositeFilter;
import org.springframework.security.oauth2.provider.filter.OAuth2ExceptionHandlerFilter;
import org.springframework.security.oauth2.provider.token.InMemoryOAuth2ProviderTokenServices;
import org.springframework.security.oauth2.provider.token.InMemoryTokenStore;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

/**
* Parser for the OAuth "provider" element.
*
*
* @author Ryan Heaton
* @author Dave Syer
*/
Expand All @@ -57,13 +57,13 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
if (!StringUtils.hasText(tokenServicesRef)) {
tokenServicesRef = "oauth2TokenServices";
BeanDefinitionBuilder tokenServices = BeanDefinitionBuilder
.rootBeanDefinition(InMemoryOAuth2ProviderTokenServices.class);
.rootBeanDefinition(InMemoryTokenStore.class);
parserContext.getRegistry().registerBeanDefinition(tokenServicesRef, tokenServices.getBeanDefinition());
}

BeanDefinitionBuilder filterChain = BeanDefinitionBuilder.rootBeanDefinition(CompositeFilter.class);
filterChain.addPropertyValue("filters", filters);

Element authorizationServerElement = DomUtils.getChildElementByTagName(element, "authorization-server");
if (authorizationServerElement!=null) {
AuthorizationServerBeanDefinitionParser parser = new AuthorizationServerBeanDefinitionParser(tokenServicesRef);
Expand Down
Expand Up @@ -11,62 +11,53 @@
*
* @author Ryan Heaton
*/
public class InMemoryOAuth2ProviderTokenServices extends RandomValueOAuth2ProviderTokenServices {
public class InMemoryTokenStore implements TokenStore {

protected final ConcurrentHashMap<String, OAuth2AccessToken> accessTokenStore = new ConcurrentHashMap<String, OAuth2AccessToken>();
protected final ConcurrentHashMap<String, ExpiringOAuth2RefreshToken> refreshTokenStore = new ConcurrentHashMap<String, ExpiringOAuth2RefreshToken>();
protected final ConcurrentHashMap<String, OAuth2Authentication> authenticationStore = new ConcurrentHashMap<String, OAuth2Authentication>();
protected final ConcurrentHashMap<String, String> refreshTokenAssociation = new ConcurrentHashMap<String, String>();

@Override
protected OAuth2Authentication readAuthentication(OAuth2AccessToken token) {
public OAuth2Authentication readAuthentication(OAuth2AccessToken token) {
return this.authenticationStore.get(token.getValue());
}

@Override
protected void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
this.accessTokenStore.put(token.getValue(), token);
this.authenticationStore.put(token.getValue(), authentication);
if (token.getRefreshToken() != null && token.getRefreshToken().getValue() != null) {
this.refreshTokenAssociation.put(token.getRefreshToken().getValue(), token.getValue());
}
}

@Override
protected OAuth2AccessToken readAccessToken(String tokenValue) {
public OAuth2AccessToken readAccessToken(String tokenValue) {
return this.accessTokenStore.get(tokenValue);
}

@Override
protected void removeAccessToken(String tokenValue) {
public void removeAccessToken(String tokenValue) {
this.accessTokenStore.remove(tokenValue);
this.authenticationStore.remove(tokenValue);
}

@Override
protected OAuth2Authentication readAuthentication(ExpiringOAuth2RefreshToken token) {
public OAuth2Authentication readAuthentication(ExpiringOAuth2RefreshToken token) {
return this.authenticationStore.get(token.getValue());
}

@Override
protected void storeRefreshToken(ExpiringOAuth2RefreshToken refreshToken, OAuth2Authentication authentication) {
public void storeRefreshToken(ExpiringOAuth2RefreshToken refreshToken, OAuth2Authentication authentication) {
this.refreshTokenStore.put(refreshToken.getValue(), refreshToken);
this.authenticationStore.put(refreshToken.getValue(), authentication);
}

@Override
protected ExpiringOAuth2RefreshToken readRefreshToken(String tokenValue) {
public ExpiringOAuth2RefreshToken readRefreshToken(String tokenValue) {
return this.refreshTokenStore.get(tokenValue);
}

@Override
protected void removeRefreshToken(String tokenValue) {
public void removeRefreshToken(String tokenValue) {
this.refreshTokenStore.remove(tokenValue);
this.authenticationStore.remove(tokenValue);
}

@Override
protected void removeAccessTokenUsingRefreshToken(String refreshToken) {
public void removeAccessTokenUsingRefreshToken(String refreshToken) {
String accessToken = this.refreshTokenAssociation.remove(refreshToken);
if (accessToken != null) {
this.accessTokenStore.remove(accessToken);
Expand Down
Expand Up @@ -22,10 +22,11 @@
* Implementation of token services that stores tokens in a database.
*
* @author Ken Dombeck
* @author Luke Taylor
*/
public class JdbcOAuth2ProviderTokenServices extends RandomValueOAuth2ProviderTokenServices {
public class JdbcTokenStore implements TokenStore {

private static final Log LOG = LogFactory.getLog(JdbcOAuth2ProviderTokenServices.class);
private static final Log LOG = LogFactory.getLog(JdbcTokenStore.class);

private static final String DEFAULT_ACCESS_TOKEN_INSERT_STATEMENT = "insert into oauth_access_token (token_id, token, authentication, refresh_token) values (?, ?, ?, ?)";
private static final String DEFAULT_ACCESS_TOKEN_SELECT_STATEMENT = "select token_id, token from oauth_access_token where token_id = ?";
Expand All @@ -52,13 +53,12 @@ public class JdbcOAuth2ProviderTokenServices extends RandomValueOAuth2ProviderTo

private final JdbcTemplate jdbcTemplate;

public JdbcOAuth2ProviderTokenServices(DataSource dataSource) {
public JdbcTokenStore(DataSource dataSource) {
Assert.notNull(dataSource, "DataSource required");
this.jdbcTemplate = new JdbcTemplate(dataSource);
}

@Override
protected void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
String refreshToken = null;
if (token.getRefreshToken() != null) {
refreshToken = token.getRefreshToken().getValue();
Expand All @@ -74,8 +74,7 @@ protected void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication au
new int[]{Types.VARCHAR, Types.BLOB, Types.BLOB, Types.VARCHAR});
}

@Override
protected OAuth2AccessToken readAccessToken(String tokenValue) {
public OAuth2AccessToken readAccessToken(String tokenValue) {
OAuth2AccessToken accessToken = null;

try {
Expand All @@ -95,13 +94,11 @@ public OAuth2AccessToken mapRow(ResultSet rs, int rowNum) throws SQLException {
return accessToken;
}

@Override
protected void removeAccessToken(String tokenValue) {
public void removeAccessToken(String tokenValue) {
jdbcTemplate.update(deleteAccessTokenSql, tokenValue);
}

@Override
protected OAuth2Authentication readAuthentication(OAuth2AccessToken token) {
public OAuth2Authentication readAuthentication(OAuth2AccessToken token) {
OAuth2Authentication authentication = null;

try {
Expand All @@ -121,17 +118,15 @@ public OAuth2Authentication mapRow(ResultSet rs, int rowNum) throws SQLException
return authentication;
}

@Override
protected void storeRefreshToken(ExpiringOAuth2RefreshToken refreshToken, OAuth2Authentication authentication) {
public void storeRefreshToken(ExpiringOAuth2RefreshToken refreshToken, OAuth2Authentication authentication) {
jdbcTemplate.update(insertRefreshTokenSql,
new Object[]{refreshToken.getValue(),
new SqlLobValue(SerializationUtils.serialize(refreshToken)),
new SqlLobValue(SerializationUtils.serialize(authentication))},
new int[]{Types.VARCHAR, Types.BLOB, Types.BLOB});
}

@Override
protected ExpiringOAuth2RefreshToken readRefreshToken(String token) {
public ExpiringOAuth2RefreshToken readRefreshToken(String token) {
ExpiringOAuth2RefreshToken refreshToken = null;

try {
Expand All @@ -151,13 +146,11 @@ public ExpiringOAuth2RefreshToken mapRow(ResultSet rs, int rowNum) throws SQLExc
return refreshToken;
}

@Override
protected void removeRefreshToken(String token) {
public void removeRefreshToken(String token) {
jdbcTemplate.update(deleteRefreshTokenSql, token);
}

@Override
protected OAuth2Authentication readAuthentication(ExpiringOAuth2RefreshToken token) {
public OAuth2Authentication readAuthentication(ExpiringOAuth2RefreshToken token) {
OAuth2Authentication authentication = null;

try {
Expand All @@ -177,8 +170,7 @@ public OAuth2Authentication mapRow(ResultSet rs, int rowNum) throws SQLException
return authentication;
}

@Override
protected void removeAccessTokenUsingRefreshToken(String refreshToken) {
public void removeAccessTokenUsingRefreshToken(String refreshToken) {
jdbcTemplate.update(deleteAccessTokenFromRefreshTokenSql,
new Object[]{refreshToken},
new int[]{Types.VARCHAR});
Expand Down

0 comments on commit 64ded15

Please sign in to comment.