Search in sources :

Example 51 with OAuth2RefreshToken

use of org.springframework.security.oauth2.core.OAuth2RefreshToken in project entando-core by entando.

the class OAuth2TokenDAOTest method failReadAuthenticationForRefreshToken.

@Test(expected = RuntimeException.class)
public void failReadAuthenticationForRefreshToken() throws Exception {
    OAuth2RefreshToken refreshToken = new DefaultOAuth2RefreshToken("value_X2");
    try {
        when(this.stat.executeQuery()).thenReturn(res);
        Mockito.when(res.next()).thenReturn(true).thenReturn(false);
        Mockito.when(res.getString("localuser")).thenReturn("username");
        Mockito.when(res.getString("clientid")).thenThrow(SQLException.class);
        Mockito.when(res.getString("granttype")).thenReturn("password");
        OAuth2Authentication auth = this.tokenDAO.readAuthenticationForRefreshToken(refreshToken);
        Assert.fail();
    } catch (RuntimeException e) {
        Mockito.verify(stat, Mockito.times(1)).setString(Mockito.anyInt(), Mockito.anyString());
        Mockito.verify(res, Mockito.times(2)).getString(Mockito.anyString());
        Mockito.verify(res, Mockito.times(0)).getTimestamp(Mockito.anyString());
        Mockito.verify(stat, Mockito.times(1)).close();
        Mockito.verify(res, Mockito.times(1)).close();
        Mockito.verify(conn, Mockito.times(1)).close();
        throw e;
    }
}
Also used : OAuth2RefreshToken(org.springframework.security.oauth2.common.OAuth2RefreshToken) DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Test(org.junit.Test)

Example 52 with OAuth2RefreshToken

use of org.springframework.security.oauth2.core.OAuth2RefreshToken in project entando-core by entando.

the class OAuth2TokenDAOTest method readRefreshToken.

@Test
public void readRefreshToken() throws Exception {
    when(this.statForSearchId.executeQuery()).thenReturn(resForSearchId);
    Mockito.when(resForSearchId.next()).thenReturn(true).thenReturn(false);
    Mockito.when(resForSearchId.getString(Mockito.anyString())).thenReturn("refresh_token");
    OAuth2RefreshToken refreshToken = this.tokenDAO.readRefreshToken("refresh_token");
    Assert.assertNotNull(refreshToken);
    Assert.assertEquals("refresh_token", refreshToken.getValue());
    Mockito.verify(statForSearchId, Mockito.times(1)).setString(Mockito.anyInt(), Mockito.anyString());
    Mockito.verify(resForSearchId, Mockito.times(1)).getString(Mockito.anyString());
    this.executeFinalCheckForSearchId(true);
    Mockito.verify(conn, Mockito.times(1)).close();
}
Also used : OAuth2RefreshToken(org.springframework.security.oauth2.common.OAuth2RefreshToken) DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) Test(org.junit.Test)

Example 53 with OAuth2RefreshToken

use of org.springframework.security.oauth2.core.OAuth2RefreshToken in project entando-core by entando.

the class OAuth2TokenDAOTest method readRefreshToken_noteExists.

@Test
public void readRefreshToken_noteExists() throws Exception {
    when(this.statForSearchId.executeQuery()).thenReturn(resForSearchId);
    Mockito.when(resForSearchId.next()).thenReturn(false);
    OAuth2RefreshToken refreshToken = this.tokenDAO.readRefreshToken("refresh_token");
    Assert.assertNull(refreshToken);
    Mockito.verify(statForSearchId, Mockito.times(1)).setString(Mockito.anyInt(), Mockito.anyString());
    Mockito.verify(resForSearchId, Mockito.times(0)).getString(Mockito.anyString());
    this.executeFinalCheckForSearchId(true);
    Mockito.verify(conn, Mockito.times(1)).close();
}
Also used : OAuth2RefreshToken(org.springframework.security.oauth2.common.OAuth2RefreshToken) DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) Test(org.junit.Test)

Example 54 with OAuth2RefreshToken

use of org.springframework.security.oauth2.core.OAuth2RefreshToken in project entando-core by entando.

the class ApiOAuth2TokenManagerTest method removeAccessTokenUsingRefreshToken.

@Test
public void removeAccessTokenUsingRefreshToken() throws Exception {
    OAuth2RefreshToken refreshToken = new DefaultOAuth2RefreshToken("value_2");
    this.tokenManager.removeAccessTokenUsingRefreshToken(refreshToken);
    Mockito.verify(tokenDAO, Mockito.times(1)).removeAccessTokenUsingRefreshToken("value_2");
}
Also used : DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) OAuth2RefreshToken(org.springframework.security.oauth2.common.OAuth2RefreshToken) DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) Test(org.junit.Test)

Example 55 with OAuth2RefreshToken

use of org.springframework.security.oauth2.core.OAuth2RefreshToken in project jhipster-registry by jhipster.

the class AuthorizationHeaderUtil method refreshToken.

private String refreshToken(OAuth2AuthorizedClient client, OAuth2AuthenticationToken oauthToken) {
    OAuth2AccessTokenResponse atr = refreshTokenClient(client);
    if (atr == null || atr.getAccessToken() == null) {
        log.info("Failed to refresh token for user");
        return null;
    }
    OAuth2RefreshToken refreshToken = atr.getRefreshToken() != null ? atr.getRefreshToken() : client.getRefreshToken();
    OAuth2AuthorizedClient updatedClient = new OAuth2AuthorizedClient(client.getClientRegistration(), client.getPrincipalName(), atr.getAccessToken(), refreshToken);
    clientService.saveAuthorizedClient(updatedClient, oauthToken);
    return atr.getAccessToken().getTokenValue();
}
Also used : OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) OAuth2RefreshToken(org.springframework.security.oauth2.core.OAuth2RefreshToken) OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient)

Aggregations

OAuth2RefreshToken (org.springframework.security.oauth2.common.OAuth2RefreshToken)74 OAuth2RefreshToken (org.springframework.security.oauth2.core.OAuth2RefreshToken)57 Test (org.junit.jupiter.api.Test)41 Test (org.junit.Test)39 DefaultOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken)38 OAuth2AccessToken (org.springframework.security.oauth2.core.OAuth2AccessToken)33 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)31 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)25 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)25 OAuth2AuthorizedClient (org.springframework.security.oauth2.client.OAuth2AuthorizedClient)24 ExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)24 Authentication (org.springframework.security.core.Authentication)20 Instant (java.time.Instant)19 ClientRequest (org.springframework.web.reactive.function.client.ClientRequest)18 RegisteredClient (org.springframework.security.oauth2.server.authorization.client.RegisteredClient)17 DefaultExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken)16 HashMap (java.util.HashMap)15 OAuth2Authorization (org.springframework.security.oauth2.server.authorization.OAuth2Authorization)14 RedisConnection (org.springframework.data.redis.connection.RedisConnection)13 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)13