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);
}
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);
}
}
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);
}
}
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));
}
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");
}
Aggregations