Search in sources :

Example 1 with OAuth2AccessTokenImpl

use of org.entando.entando.aps.system.services.oauth2.model.OAuth2AccessTokenImpl in project entando-core by entando.

the class OAuth2TokenDAOTest method createMockAccessToken.

private OAuth2AccessToken createMockAccessToken() {
    OAuth2AccessTokenImpl token = new OAuth2AccessTokenImpl("token");
    token.setValue("token");
    token.setClientId("client_id");
    token.setExpiration(new Date());
    token.setGrantType("password");
    token.setLocalUser("username");
    token.setRefreshToken(new DefaultOAuth2RefreshToken("refresh"));
    token.setTokenType("bearer");
    return token;
}
Also used : DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) OAuth2AccessTokenImpl(org.entando.entando.aps.system.services.oauth2.model.OAuth2AccessTokenImpl) Date(java.util.Date)

Example 2 with OAuth2AccessTokenImpl

use of org.entando.entando.aps.system.services.oauth2.model.OAuth2AccessTokenImpl in project entando-core by entando.

the class ApiOAuth2TokenManagerTest method createAccessTokenForLocalUser.

@Test
public void createAccessTokenForLocalUser() {
    OAuth2AccessToken token = this.tokenManager.createAccessTokenForLocalUser("username");
    Assert.assertNotNull(token);
    Mockito.verify(tokenDAO, Mockito.times(1)).storeAccessToken(Mockito.any(OAuth2AccessToken.class), Mockito.eq(null));
    Assert.assertTrue(token instanceof OAuth2AccessTokenImpl);
    Assert.assertEquals("LOCAL_USER", ((OAuth2AccessTokenImpl) token).getClientId());
}
Also used : OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2AccessTokenImpl(org.entando.entando.aps.system.services.oauth2.model.OAuth2AccessTokenImpl) Test(org.junit.Test)

Example 3 with OAuth2AccessTokenImpl

use of org.entando.entando.aps.system.services.oauth2.model.OAuth2AccessTokenImpl in project entando-core by entando.

the class ApiOAuth2TokenManagerTest method getAccessToken.

@Test
public void getAccessToken() throws Exception {
    OAuth2AccessToken token = tokenManager.getAccessToken(this.createMockAuthentication());
    Assert.assertNotNull(token);
    Assert.assertTrue(token instanceof OAuth2AccessTokenImpl);
    Assert.assertEquals("clientId", ((OAuth2AccessTokenImpl) token).getClientId());
    Assert.assertEquals("username", ((OAuth2AccessTokenImpl) token).getLocalUser());
}
Also used : OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2AccessTokenImpl(org.entando.entando.aps.system.services.oauth2.model.OAuth2AccessTokenImpl) Test(org.junit.Test)

Example 4 with OAuth2AccessTokenImpl

use of org.entando.entando.aps.system.services.oauth2.model.OAuth2AccessTokenImpl in project entando-core by entando.

the class EntandoOauth2Interceptor method extractOAuthParameters.

protected UserDetails extractOAuthParameters(HttpServletRequest request) {
    try {
        // Clear previous session
        request.getSession().setAttribute("user", null);
        String accessToken = new EntandoBearerTokenExtractor().extractToken(request);
        if (StringUtils.isBlank(accessToken)) {
            return null;
        }
        final OAuth2AccessToken token = this.getoAuth2TokenManager().readAccessToken(accessToken);
        this.validateToken(request, accessToken, token);
        String username;
        if (token instanceof OAuth2AccessTokenImpl) {
            username = ((OAuth2AccessTokenImpl) token).getLocalUser();
        } else {
            Authentication auth = new EntandoBearerTokenExtractor().extract(request);
            username = auth.getPrincipal().toString();
        }
        UserDetails user = this.getAuthenticationProviderManager().getUser(username);
        if (user == null) {
            logger.warn("User {} not found ", username);
            return null;
        }
        request.getSession().setAttribute("user", user);
        request.getSession().setAttribute(SystemConstants.SESSIONPARAM_CURRENT_USER, user);
        return user;
    } catch (ApsSystemException ex) {
        logger.error("System exception {}", ex.getMessage());
        throw new EntandoTokenException("error parsing OAuth parameters", request, "guest");
    }
}
Also used : EntandoTokenException(org.entando.entando.web.common.exceptions.EntandoTokenException) UserDetails(com.agiletec.aps.system.services.user.UserDetails) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) Authentication(org.springframework.security.core.Authentication) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) OAuth2AccessTokenImpl(org.entando.entando.aps.system.services.oauth2.model.OAuth2AccessTokenImpl)

Example 5 with OAuth2AccessTokenImpl

use of org.entando.entando.aps.system.services.oauth2.model.OAuth2AccessTokenImpl in project entando-core by entando.

the class OAuth2TestUtils method getOAuth2Token.

public static OAuth2AccessToken getOAuth2Token(String username, String accessToken) {
    OAuth2AccessTokenImpl oAuth2Token = new OAuth2AccessTokenImpl(accessToken);
    oAuth2Token.setRefreshToken(new DefaultOAuth2RefreshToken("refresh_token"));
    oAuth2Token.setLocalUser(username);
    // gets a calendar using the default time zone and locale.
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.SECOND, 3600);
    oAuth2Token.setExpiration(calendar.getTime());
    oAuth2Token.setGrantType("password");
    return oAuth2Token;
}
Also used : DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) Calendar(java.util.Calendar) OAuth2AccessTokenImpl(org.entando.entando.aps.system.services.oauth2.model.OAuth2AccessTokenImpl)

Aggregations

OAuth2AccessTokenImpl (org.entando.entando.aps.system.services.oauth2.model.OAuth2AccessTokenImpl)11 DefaultOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken)6 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)4 Test (org.junit.Test)3 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)2 UserDetails (com.agiletec.aps.system.services.user.UserDetails)2 Calendar (java.util.Calendar)2 Date (java.util.Date)2 IAuthorizationManager (com.agiletec.aps.system.services.authorization.IAuthorizationManager)1 Role (com.agiletec.aps.system.services.role.Role)1 IUserManager (com.agiletec.aps.system.services.user.IUserManager)1 IApiOAuth2TokenManager (org.entando.entando.aps.system.services.oauth2.IApiOAuth2TokenManager)1 EntandoTokenException (org.entando.entando.web.common.exceptions.EntandoTokenException)1 EntandoBearerTokenExtractor (org.entando.entando.web.common.interceptor.EntandoBearerTokenExtractor)1 Authentication (org.springframework.security.core.Authentication)1