Search in sources :

Example 1 with TokenBinding

use of org.wso2.carbon.identity.oauth2.token.bindings.TokenBinding in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuthAdminServiceImpl method clearCacheByAccessTokenAndConsumerKey.

private void clearCacheByAccessTokenAndConsumerKey(AccessTokenDO accessTokenDO, String consumerKey) {
    String token = accessTokenDO.getAccessToken();
    AuthenticatedUser authenticatedUser = accessTokenDO.getAuthzUser();
    OAuthCacheKey cacheKeyToken = new OAuthCacheKey(token);
    String scope = buildScopeString(accessTokenDO.getScope());
    TokenBinding tokenBinding = accessTokenDO.getTokenBinding();
    String tokenBindingReference = (tokenBinding != null && StringUtils.isNotBlank(tokenBinding.getBindingReference())) ? tokenBinding.getBindingReference() : NONE;
    OAuthCache.getInstance().clearCacheEntry(cacheKeyToken);
    OAuthUtil.clearOAuthCache(consumerKey, authenticatedUser, scope, tokenBindingReference);
    OAuthUtil.clearOAuthCache(consumerKey, authenticatedUser, scope);
    OAuthUtil.clearOAuthCache(consumerKey, authenticatedUser);
    OAuthUtil.clearOAuthCache(accessTokenDO);
}
Also used : TokenBinding(org.wso2.carbon.identity.oauth2.token.bindings.TokenBinding) OAuthCacheKey(org.wso2.carbon.identity.oauth.cache.OAuthCacheKey) OAuth2Util.buildScopeString(org.wso2.carbon.identity.oauth2.util.OAuth2Util.buildScopeString) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)

Example 2 with TokenBinding

use of org.wso2.carbon.identity.oauth2.token.bindings.TokenBinding in project identity-inbound-auth-oauth by wso2-extensions.

the class TokenBindingMgtDAOImpl method getTokenBindingByBindingRef.

@Override
public Optional<TokenBinding> getTokenBindingByBindingRef(String tokenId, String bindingRef) throws IdentityOAuth2Exception {
    if (log.isDebugEnabled()) {
        log.debug("Getting token binding for the token id: " + tokenId + " and token binding ref: " + bindingRef);
    }
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false);
        PreparedStatement preparedStatement = connection.prepareStatement(RETRIEVE_TOKEN_BINDING_BY_TOKEN_ID_AND_BINDING_REF)) {
        preparedStatement.setString(1, tokenId);
        preparedStatement.setString(2, bindingRef);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            if (resultSet.next()) {
                TokenBinding tokenBinding = new TokenBinding(resultSet.getString("TOKEN_BINDING_TYPE"), bindingRef, resultSet.getString("TOKEN_BINDING_VALUE"));
                return Optional.of(tokenBinding);
            }
            return Optional.empty();
        }
    } catch (SQLException e) {
        throw new IdentityOAuth2Exception("Failed to get token binding for the token id: " + tokenId + " and " + "token binding ref: " + bindingRef, e);
    }
}
Also used : TokenBinding(org.wso2.carbon.identity.oauth2.token.bindings.TokenBinding) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 3 with TokenBinding

use of org.wso2.carbon.identity.oauth2.token.bindings.TokenBinding in project identity-inbound-auth-oauth by wso2-extensions.

the class TokenBindingMgtDAOImpl method storeTokenBinding.

@Override
public void storeTokenBinding(TokenBinding tokenBinding, int tenantId) throws IdentityOAuth2Exception {
    if (tokenBinding == null) {
        if (log.isDebugEnabled()) {
            log.debug("Token binding information is not available. " + "Returning without proceeding to store token binding information.");
        }
        return;
    }
    if (log.isDebugEnabled()) {
        log.debug("Storing token binding information" + " accessTokenId: " + tokenBinding.getTokenId() + " bindingType: " + tokenBinding.getBindingType() + " bindingRef: " + tokenBinding.getBindingReference());
    }
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false);
        PreparedStatement preparedStatement = connection.prepareStatement(STORE_TOKEN_BINDING)) {
        preparedStatement.setString(1, tokenBinding.getTokenId());
        preparedStatement.setString(2, tokenBinding.getBindingType());
        preparedStatement.setString(3, tokenBinding.getBindingReference());
        preparedStatement.setString(4, tokenBinding.getBindingValue());
        preparedStatement.setInt(5, tenantId);
        preparedStatement.execute();
    } catch (SQLException e) {
        String tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
        if (MultitenantConstants.SUPER_TENANT_ID != tenantId) {
            tenantDomain = IdentityTenantUtil.getTenantDomain(tenantId);
        }
        throw new IdentityOAuth2Exception("Failed to store token binding: " + tokenBinding.toString() + "in tenant: " + tenantDomain, e);
    }
}
Also used : IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 4 with TokenBinding

use of org.wso2.carbon.identity.oauth2.token.bindings.TokenBinding in project identity-inbound-auth-oauth by wso2-extensions.

the class AccessTokenIssuer method handleTokenBinding.

/**
 * Handle token binding for the grant type.
 *
 * @param tokenReqDTO  token request DTO.
 * @param grantType    grant type.
 * @param tokReqMsgCtx token request message context.
 * @param oAuthAppDO   oauth application.
 * @throws IdentityOAuth2Exception in case of failure.
 */
private void handleTokenBinding(OAuth2AccessTokenReqDTO tokenReqDTO, String grantType, OAuthTokenReqMessageContext tokReqMsgCtx, OAuthAppDO oAuthAppDO) throws IdentityOAuth2Exception {
    if (StringUtils.isBlank(oAuthAppDO.getTokenBindingType())) {
        tokReqMsgCtx.setTokenBinding(null);
        return;
    }
    Optional<TokenBinder> tokenBinderOptional = OAuth2ServiceComponentHolder.getInstance().getTokenBinder(oAuthAppDO.getTokenBindingType());
    if (!tokenBinderOptional.isPresent()) {
        throw new IdentityOAuth2Exception("Token binder for the binding type: " + oAuthAppDO.getTokenBindingType() + " is not registered.");
    }
    if (REFRESH_TOKEN.equals(grantType)) {
        // Token binding values are already set to the OAuthTokenReqMessageContext.
        return;
    }
    tokReqMsgCtx.setTokenBinding(null);
    TokenBinder tokenBinder = tokenBinderOptional.get();
    if (!tokenBinder.getSupportedGrantTypes().contains(grantType)) {
        return;
    }
    Optional<String> tokenBindingValueOptional = tokenBinder.getTokenBindingValue(tokenReqDTO);
    if (!tokenBindingValueOptional.isPresent()) {
        throw new IdentityOAuth2Exception("Token binding reference cannot be retrieved form the token binder: " + tokenBinder.getBindingType());
    }
    String tokenBindingValue = tokenBindingValueOptional.get();
    tokReqMsgCtx.setTokenBinding(new TokenBinding(tokenBinder.getBindingType(), OAuth2Util.getTokenBindingReference(tokenBindingValue), tokenBindingValue));
}
Also used : TokenBinding(org.wso2.carbon.identity.oauth2.token.bindings.TokenBinding) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) TokenBinder(org.wso2.carbon.identity.oauth2.token.bindings.TokenBinder)

Example 5 with TokenBinding

use of org.wso2.carbon.identity.oauth2.token.bindings.TokenBinding in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuth2ServiceTest method testIdentityExceptionForRevokeTokenByOAuthClient.

@Test
public void testIdentityExceptionForRevokeTokenByOAuthClient() throws Exception {
    setUpRevokeToken();
    mockStatic(IdentityTenantUtil.class);
    when(IdentityTenantUtil.getTenantId(anyString())).thenReturn(-1234);
    AccessTokenDO accessTokenDO = getAccessToken();
    TokenBinding tokenBinding = new TokenBinding();
    tokenBinding.setBindingReference("dummyReference");
    accessTokenDO.setTokenBinding(tokenBinding);
    when(OAuth2Util.findAccessToken(anyString(), anyBoolean())).thenThrow(IdentityException.class);
    OAuthRevocationRequestDTO revokeRequestDTO = getOAuthRevocationRequestDTO();
    OAuthRevocationResponseDTO oAuthRevocationResponseDTO = oAuth2Service.revokeTokenByOAuthClient(revokeRequestDTO);
    assertEquals(oAuthRevocationResponseDTO.getErrorMsg(), "Error occurred while revoking authorization grant for applications");
}
Also used : AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) TokenBinding(org.wso2.carbon.identity.oauth2.token.bindings.TokenBinding) OAuthRevocationRequestDTO(org.wso2.carbon.identity.oauth2.dto.OAuthRevocationRequestDTO) OAuthRevocationResponseDTO(org.wso2.carbon.identity.oauth2.dto.OAuthRevocationResponseDTO) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Aggregations

TokenBinding (org.wso2.carbon.identity.oauth2.token.bindings.TokenBinding)6 PreparedStatement (java.sql.PreparedStatement)3 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)3 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Test (org.testng.annotations.Test)2 OAuthRevocationRequestDTO (org.wso2.carbon.identity.oauth2.dto.OAuthRevocationRequestDTO)2 AccessTokenDO (org.wso2.carbon.identity.oauth2.model.AccessTokenDO)2 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)2 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)1 OAuthCacheKey (org.wso2.carbon.identity.oauth.cache.OAuthCacheKey)1 OAuthAppDO (org.wso2.carbon.identity.oauth.dao.OAuthAppDO)1 AccessTokenDAO (org.wso2.carbon.identity.oauth2.dao.AccessTokenDAO)1 OAuthTokenPersistenceFactory (org.wso2.carbon.identity.oauth2.dao.OAuthTokenPersistenceFactory)1 TokenManagementDAOImpl (org.wso2.carbon.identity.oauth2.dao.TokenManagementDAOImpl)1 OAuthRevocationResponseDTO (org.wso2.carbon.identity.oauth2.dto.OAuthRevocationResponseDTO)1 TokenBinder (org.wso2.carbon.identity.oauth2.token.bindings.TokenBinder)1 OAuth2Util.buildScopeString (org.wso2.carbon.identity.oauth2.util.OAuth2Util.buildScopeString)1