Search in sources :

Example 6 with CustomOAuth2AccessToken

use of org.company.oauth2.CustomOAuth2AccessToken in project spring-security-oauth by spring-projects.

the class JdbcClientTokenServicesTests method testSaveAndRetrieveCustomTokenWithCustomSerializationStrategy.

@Test
public void testSaveAndRetrieveCustomTokenWithCustomSerializationStrategy() {
    List<String> allowedClasses = new ArrayList<String>();
    allowedClasses.add("java.util.");
    allowedClasses.add("org.springframework.security.");
    allowedClasses.add("org.company.oauth2.CustomOAuth2AccessToken");
    WhitelistedSerializationStrategy newStrategy = new WhitelistedSerializationStrategy(allowedClasses);
    SerializationStrategy oldStrategy = SerializationUtils.getSerializationStrategy();
    try {
        SerializationUtils.setSerializationStrategy(newStrategy);
        OAuth2AccessToken accessToken = new CustomOAuth2AccessToken("FOO");
        Authentication authentication = new UsernamePasswordAuthenticationToken("marissa", "koala");
        AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
        resource.setClientId("client");
        resource.setScope(Arrays.asList("foo", "bar"));
        tokenStore.saveAccessToken(resource, authentication, accessToken);
        OAuth2AccessToken result = tokenStore.getAccessToken(resource, authentication);
        assertNotNull(result);
        assertEquals(accessToken, result);
    } finally {
        SerializationUtils.setSerializationStrategy(oldStrategy);
    }
}
Also used : CustomOAuth2AccessToken(org.company.oauth2.CustomOAuth2AccessToken) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) Authentication(org.springframework.security.core.Authentication) WhitelistedSerializationStrategy(org.springframework.security.oauth2.common.util.WhitelistedSerializationStrategy) CustomOAuth2AccessToken(org.company.oauth2.CustomOAuth2AccessToken) ArrayList(java.util.ArrayList) SerializationStrategy(org.springframework.security.oauth2.common.util.SerializationStrategy) WhitelistedSerializationStrategy(org.springframework.security.oauth2.common.util.WhitelistedSerializationStrategy) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) Test(org.junit.Test)

Example 7 with CustomOAuth2AccessToken

use of org.company.oauth2.CustomOAuth2AccessToken in project spring-security-oauth by spring-projects.

the class JdbcClientTokenServicesTests method testSaveAndRetrieveNotAllowedCustomToken.

@Test(expected = IllegalArgumentException.class)
public void testSaveAndRetrieveNotAllowedCustomToken() {
    OAuth2AccessToken accessToken = new CustomOAuth2AccessToken("FOO");
    Authentication authentication = new UsernamePasswordAuthenticationToken("marissa", "koala");
    AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
    resource.setClientId("client");
    resource.setScope(Arrays.asList("foo", "bar"));
    WhitelistedSerializationStrategy newStrategy = new WhitelistedSerializationStrategy();
    SerializationStrategy oldStrategy = SerializationUtils.getSerializationStrategy();
    try {
        SerializationUtils.setSerializationStrategy(newStrategy);
        tokenStore.saveAccessToken(resource, authentication, accessToken);
        tokenStore.getAccessToken(resource, authentication);
    } finally {
        SerializationUtils.setSerializationStrategy(oldStrategy);
    }
}
Also used : CustomOAuth2AccessToken(org.company.oauth2.CustomOAuth2AccessToken) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) Authentication(org.springframework.security.core.Authentication) CustomOAuth2AccessToken(org.company.oauth2.CustomOAuth2AccessToken) WhitelistedSerializationStrategy(org.springframework.security.oauth2.common.util.WhitelistedSerializationStrategy) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) SerializationStrategy(org.springframework.security.oauth2.common.util.SerializationStrategy) WhitelistedSerializationStrategy(org.springframework.security.oauth2.common.util.WhitelistedSerializationStrategy) Test(org.junit.Test)

Example 8 with CustomOAuth2AccessToken

use of org.company.oauth2.CustomOAuth2AccessToken in project spring-security-oauth by spring-projects.

the class JdbcClientTokenServicesTests method testSaveAndRetrieveCustomToken.

@Test
public void testSaveAndRetrieveCustomToken() {
    OAuth2AccessToken accessToken = new CustomOAuth2AccessToken("FOO");
    Authentication authentication = new UsernamePasswordAuthenticationToken("marissa", "koala");
    AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
    resource.setClientId("client");
    resource.setScope(Arrays.asList("foo", "bar"));
    tokenStore.saveAccessToken(resource, authentication, accessToken);
    OAuth2AccessToken result = tokenStore.getAccessToken(resource, authentication);
    assertNotNull(result);
    assertEquals(accessToken, result);
}
Also used : CustomOAuth2AccessToken(org.company.oauth2.CustomOAuth2AccessToken) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) Authentication(org.springframework.security.core.Authentication) CustomOAuth2AccessToken(org.company.oauth2.CustomOAuth2AccessToken) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) Test(org.junit.Test)

Example 9 with CustomOAuth2AccessToken

use of org.company.oauth2.CustomOAuth2AccessToken in project spring-security-oauth by spring-projects.

the class JdbcTokenStoreTests method testNotAllowedCustomTokenWithCustomStrategy.

@Test
public void testNotAllowedCustomTokenWithCustomStrategy() {
    OAuth2Authentication authentication = new CustomOAuth2Authentication(RequestTokenFactory.createOAuth2Request("id", false), new CustomAuthentication("test4", false));
    OAuth2AccessToken accessToken = new CustomOAuth2AccessToken("customToken");
    JdbcTokenStore tokenStore = getTokenStore();
    WhitelistedSerializationStrategy newStrategy = new WhitelistedSerializationStrategy();
    SerializationStrategy oldStrategy = SerializationUtils.getSerializationStrategy();
    try {
        SerializationUtils.setSerializationStrategy(newStrategy);
        tokenStore.storeAccessToken(accessToken, authentication);
        Collection<OAuth2AccessToken> tokens = tokenStore.findTokensByUserName("test4");
        assertTrue(tokens.isEmpty());
    } finally {
        SerializationUtils.setSerializationStrategy(oldStrategy);
    }
}
Also used : CustomOAuth2Authentication(org.company.oauth2.CustomOAuth2Authentication) CustomOAuth2AccessToken(org.company.oauth2.CustomOAuth2AccessToken) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) CustomOAuth2Authentication(org.company.oauth2.CustomOAuth2Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) CustomOAuth2AccessToken(org.company.oauth2.CustomOAuth2AccessToken) WhitelistedSerializationStrategy(org.springframework.security.oauth2.common.util.WhitelistedSerializationStrategy) DefaultSerializationStrategy(org.springframework.security.oauth2.common.util.DefaultSerializationStrategy) SerializationStrategy(org.springframework.security.oauth2.common.util.SerializationStrategy) WhitelistedSerializationStrategy(org.springframework.security.oauth2.common.util.WhitelistedSerializationStrategy) CustomAuthentication(org.company.oauth2.CustomAuthentication) Test(org.junit.Test)

Example 10 with CustomOAuth2AccessToken

use of org.company.oauth2.CustomOAuth2AccessToken in project spring-security-oauth by spring-projects.

the class JdbcAuthorizationCodeServicesTests method testCustomImplementationWithCustomStrategy.

@Test
public void testCustomImplementationWithCustomStrategy() {
    OAuth2Request storedOAuth2Request = RequestTokenFactory.createOAuth2Request("id", false);
    OAuth2Authentication expectedAuthentication = new CustomOAuth2Authentication(storedOAuth2Request, new CustomAuthentication("test3", false));
    AuthorizationCodeServices jdbcAuthorizationCodeServices = getAuthorizationCodeServices();
    List<String> allowedClasses = new ArrayList<String>();
    allowedClasses.add("java.util.");
    allowedClasses.add("org.springframework.security.");
    allowedClasses.add("org.company.oauth2.CustomOAuth2AccessToken");
    allowedClasses.add("org.company.oauth2.CustomOAuth2Authentication");
    allowedClasses.add("org.company.oauth2.CustomAuthentication");
    WhitelistedSerializationStrategy newStrategy = new WhitelistedSerializationStrategy(allowedClasses);
    SerializationStrategy oldStrategy = SerializationUtils.getSerializationStrategy();
    try {
        SerializationUtils.setSerializationStrategy(newStrategy);
        String code = jdbcAuthorizationCodeServices.createAuthorizationCode(expectedAuthentication);
        assertNotNull(code);
        OAuth2Authentication actualAuthentication = getAuthorizationCodeServices().consumeAuthorizationCode(code);
        assertEquals(expectedAuthentication, actualAuthentication);
    } finally {
        SerializationUtils.setSerializationStrategy(oldStrategy);
    }
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) CustomOAuth2Authentication(org.company.oauth2.CustomOAuth2Authentication) CustomOAuth2Authentication(org.company.oauth2.CustomOAuth2Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) WhitelistedSerializationStrategy(org.springframework.security.oauth2.common.util.WhitelistedSerializationStrategy) ArrayList(java.util.ArrayList) SerializationStrategy(org.springframework.security.oauth2.common.util.SerializationStrategy) WhitelistedSerializationStrategy(org.springframework.security.oauth2.common.util.WhitelistedSerializationStrategy) CustomAuthentication(org.company.oauth2.CustomAuthentication) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)13 CustomOAuth2AccessToken (org.company.oauth2.CustomOAuth2AccessToken)12 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)11 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)9 CustomOAuth2Authentication (org.company.oauth2.CustomOAuth2Authentication)8 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)8 SerializationStrategy (org.springframework.security.oauth2.common.util.SerializationStrategy)7 WhitelistedSerializationStrategy (org.springframework.security.oauth2.common.util.WhitelistedSerializationStrategy)7 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)5 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)4 ArrayList (java.util.ArrayList)3 CustomAuthentication (org.company.oauth2.CustomAuthentication)3 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)3 Authentication (org.springframework.security.core.Authentication)3 AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)3 DefaultSerializationStrategy (org.springframework.security.oauth2.common.util.DefaultSerializationStrategy)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 DefaultExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken)1