Search in sources :

Example 1 with CustomOAuth2Authentication

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

the class JdbcTokenStoreTests method testCustomToken.

@Test
public void testCustomToken() {
    OAuth2Authentication expectedAuthentication = new CustomOAuth2Authentication(RequestTokenFactory.createOAuth2Request("id", false), new TestAuthentication("test2", false));
    OAuth2AccessToken expectedOAuth2AccessToken = new CustomOAuth2AccessToken("customToken");
    getTokenStore().storeAccessToken(expectedOAuth2AccessToken, expectedAuthentication);
    Collection<OAuth2AccessToken> actualOAuth2AccessTokens = getTokenStore().findTokensByUserName("test2");
    assertFalse(actualOAuth2AccessTokens.isEmpty());
    for (OAuth2AccessToken token : actualOAuth2AccessTokens) {
        if (expectedOAuth2AccessToken.equals(token)) {
            return;
        }
    }
    fail("No token found!");
}
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) Test(org.junit.Test)

Example 2 with CustomOAuth2Authentication

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

the class JdbcTokenStoreTests method testAllowedCustomTokenWithCustomStrategy.

@Test
public void testAllowedCustomTokenWithCustomStrategy() {
    OAuth2Authentication expectedAuthentication = new CustomOAuth2Authentication(RequestTokenFactory.createOAuth2Request("id", false), new TestAuthentication("test3", false));
    OAuth2AccessToken expectedOAuth2AccessToken = new CustomOAuth2AccessToken("customToken");
    JdbcTokenStore tokenStore = getTokenStore();
    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");
    WhitelistedSerializationStrategy newStrategy = new WhitelistedSerializationStrategy(allowedClasses);
    SerializationStrategy oldStrategy = SerializationUtils.getSerializationStrategy();
    try {
        SerializationUtils.setSerializationStrategy(newStrategy);
        tokenStore.storeAccessToken(expectedOAuth2AccessToken, expectedAuthentication);
        Collection<OAuth2AccessToken> actualOAuth2AccessTokens = getTokenStore().findTokensByUserName("test3");
        assertEquals(1, actualOAuth2AccessTokens.size());
        OAuth2AccessToken actualToken = actualOAuth2AccessTokens.iterator().next();
        assertEquals(expectedOAuth2AccessToken, actualToken);
    } 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) ArrayList(java.util.ArrayList) DefaultSerializationStrategy(org.springframework.security.oauth2.common.util.DefaultSerializationStrategy) SerializationStrategy(org.springframework.security.oauth2.common.util.SerializationStrategy) WhitelistedSerializationStrategy(org.springframework.security.oauth2.common.util.WhitelistedSerializationStrategy) Test(org.junit.Test)

Example 3 with CustomOAuth2Authentication

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

the class RedisTokenStoreCustomTokenTests method testCustomTokenWithCustomSerializationStrategy.

@Test
public void testCustomTokenWithCustomSerializationStrategy() {
    OAuth2Request request = RequestTokenFactory.createOAuth2Request(CLIENT_ID, false);
    TestingAuthenticationToken authentication = new TestingAuthenticationToken("user", "password");
    OAuth2AccessToken oauth2AccessToken = new CustomOAuth2AccessToken("access-token-" + UUID.randomUUID());
    OAuth2Authentication oauth2Authentication = new CustomOAuth2Authentication(request, authentication);
    WhitelistedSerializationStrategy newStrategy = new WhitelistedSerializationStrategy(ALLOWED_CLASSES);
    SerializationStrategy oldStrategy = SerializationUtils.getSerializationStrategy();
    try {
        SerializationUtils.setSerializationStrategy(newStrategy);
        tokenStore.storeAccessToken(oauth2AccessToken, oauth2Authentication);
        OAuth2AccessToken token = tokenStore.getAccessToken(oauth2Authentication);
        assertNotNull(token);
        assertEquals(oauth2AccessToken, token);
    } finally {
        SerializationUtils.setSerializationStrategy(oldStrategy);
    }
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) CustomOAuth2Authentication(org.company.oauth2.CustomOAuth2Authentication) CustomOAuth2AccessToken(org.company.oauth2.CustomOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) CustomOAuth2AccessToken(org.company.oauth2.CustomOAuth2AccessToken) CustomOAuth2Authentication(org.company.oauth2.CustomOAuth2Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) WhitelistedSerializationStrategy(org.springframework.security.oauth2.common.util.WhitelistedSerializationStrategy) SerializationStrategy(org.springframework.security.oauth2.common.util.SerializationStrategy) WhitelistedSerializationStrategy(org.springframework.security.oauth2.common.util.WhitelistedSerializationStrategy) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.Test)

Example 4 with CustomOAuth2Authentication

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

the class JdbcAuthorizationCodeServicesTests method testCustomImplementation.

@Test
public void testCustomImplementation() {
    OAuth2Request storedOAuth2Request = RequestTokenFactory.createOAuth2Request("id", false);
    OAuth2Authentication expectedAuthentication = new CustomOAuth2Authentication(storedOAuth2Request, new CustomAuthentication("test2", false));
    String code = getAuthorizationCodeServices().createAuthorizationCode(expectedAuthentication);
    assertNotNull(code);
    OAuth2Authentication actualAuthentication = getAuthorizationCodeServices().consumeAuthorizationCode(code);
    assertEquals(expectedAuthentication, actualAuthentication);
}
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) CustomAuthentication(org.company.oauth2.CustomAuthentication) Test(org.junit.Test)

Example 5 with CustomOAuth2Authentication

use of org.company.oauth2.CustomOAuth2Authentication 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)

Aggregations

CustomOAuth2Authentication (org.company.oauth2.CustomOAuth2Authentication)8 Test (org.junit.Test)8 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)8 CustomAuthentication (org.company.oauth2.CustomAuthentication)5 CustomOAuth2AccessToken (org.company.oauth2.CustomOAuth2AccessToken)5 SerializationStrategy (org.springframework.security.oauth2.common.util.SerializationStrategy)5 WhitelistedSerializationStrategy (org.springframework.security.oauth2.common.util.WhitelistedSerializationStrategy)5 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)5 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)4 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)4 ArrayList (java.util.ArrayList)2 DefaultSerializationStrategy (org.springframework.security.oauth2.common.util.DefaultSerializationStrategy)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 DefaultExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken)1