Skip to content

Commit

Permalink
Merge pull request #2187 from sadilchamishka/add-authorized-organizat…
Browse files Browse the repository at this point in the history
…ion-oauth2-token-table

Provide support for organization bound tokens for B2B applications
  • Loading branch information
sadilchamishka authored Oct 17, 2023
2 parents 83dae51 + 43bb4e5 commit 7799515
Show file tree
Hide file tree
Showing 19 changed files with 616 additions and 353 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*
* Copyright (c) 2013, WSO2 LLC. (https://www.wso2.com).
* Copyright (c) 2013-2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
Expand Down Expand Up @@ -574,6 +574,14 @@ public static class TokenBindings {
public static final String NONE = "NONE";
}

/**
* Define authorized organization default value.
*/
public static class AuthorizedOrganization {

public static final String NONE = "NONE";
}

/**
* Define logging constants.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,23 @@ public static void clearOAuthCache(String consumerKey, User authorizedUser, Stri
public static void clearOAuthCache(String consumerKey, AuthenticatedUser authorizedUser, String scope,
String tokenBindingReference) {

clearOAuthCache(consumerKey, authorizedUser, scope, tokenBindingReference,
OAuthConstants.AuthorizedOrganization.NONE);
}


/**
* Clear OAuth cache based on the application, authorized user, scope list and token binding reference.
*
* @param consumerKey Client id of the application the token issued to.
* @param authorizedUser Authorized user.
* @param scope Scope list.
* @param tokenBindingReference Token binding reference.
* @param authorizedOrganization Authorized organization.
*/
public static void clearOAuthCache(String consumerKey, AuthenticatedUser authorizedUser, String scope,
String tokenBindingReference, String authorizedOrganization) {

String authenticatedIDP = OAuth2Util.getAuthenticatedIDP(authorizedUser);

String userId;
Expand All @@ -309,8 +326,8 @@ public static void clearOAuthCache(String consumerKey, AuthenticatedUser authori
LOG.error("User id cannot be found for user: " + authorizedUser.getLoggableUserId());
return;
}
clearOAuthCacheByTenant(buildCacheKeyStringForToken(consumerKey, scope, userId,
authenticatedIDP, tokenBindingReference), tenantDomain);
clearOAuthCacheByTenant(OAuth2Util.buildCacheKeyStringForTokenWithUserIdOrgId(consumerKey, scope, userId,
authenticatedIDP, tokenBindingReference, authorizedOrganization), tenantDomain);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (c) 2013, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2013-2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 Inc. licenses this file to you under the Apache License,
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -11,7 +11,7 @@
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
Expand All @@ -31,6 +31,7 @@
import org.wso2.carbon.identity.oauth2.dao.TokenManagementDAO;
import org.wso2.carbon.identity.oauth2.token.handlers.response.AccessTokenResponseHandler;
import org.wso2.carbon.identity.oauth2.validators.scope.ScopeValidator;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.organization.management.service.OrganizationUserResidentResolverService;
import org.wso2.carbon.identity.role.mgt.core.RoleManagementService;
import org.wso2.carbon.user.core.service.RealmService;
Expand Down Expand Up @@ -59,6 +60,7 @@ public class OAuthComponentServiceHolder {
private Map<Integer, OAuthApplicationMgtListener> oAuthApplicationMgtListeners = new TreeMap<>();
private RoleManagementService roleManagementService;
private OrganizationUserResidentResolverService organizationUserResidentResolverService;
private OrganizationManager organizationManager;
private List<AccessTokenResponseHandler> accessTokenResponseHandlers = new ArrayList<>();
private AccessTokenDAO accessTokenDAOService;
private TokenManagementDAO tokenManagementDAOService;
Expand Down Expand Up @@ -237,6 +239,26 @@ public void setOrganizationUserResidentResolverService(
this.organizationUserResidentResolverService = organizationUserResidentResolverService;
}

/**
* Get OrganizationManager instance.
*
* @return OrganizationManager instance.
*/
public OrganizationManager getOrganizationManager() {

return organizationManager;
}

/**
* Set OrganizationManager instance.
*
* @param organizationManager OrganizationManager instance.
*/
public void setOrganizationManager(OrganizationManager organizationManager) {

this.organizationManager = organizationManager;
}

/**
* Registers access token response handlers for modifying token response attributes.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (c) 2013, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2013-2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 Inc. licenses this file to you under the Apache License,
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -11,7 +11,7 @@
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
Expand Down Expand Up @@ -43,6 +43,7 @@
import org.wso2.carbon.identity.oauth2.OAuth2Service;
import org.wso2.carbon.identity.oauth2.internal.OAuth2ServiceComponentHolder;
import org.wso2.carbon.identity.oauth2.token.handlers.response.AccessTokenResponseHandler;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.organization.management.service.OrganizationUserResidentResolverService;
import org.wso2.carbon.identity.role.mgt.core.RoleManagementService;
import org.wso2.carbon.user.core.listener.UserOperationEventListener;
Expand Down Expand Up @@ -356,4 +357,23 @@ protected void unsetOrganizationUserResidentResolverService(
}
OAuthComponentServiceHolder.getInstance().setOrganizationUserResidentResolverService(null);
}

@Reference(
name = "organization.service",
service = OrganizationManager.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetOrganizationManager"
)
protected void setOrganizationManager(OrganizationManager organizationManager) {

OAuthComponentServiceHolder.getInstance().setOrganizationManager(organizationManager);
log.debug("Set the organization management service.");
}

protected void unsetOrganizationManager(OrganizationManager organizationManager) {

OAuthComponentServiceHolder.getInstance().setOrganizationManager(null);
log.debug("Unset organization management service.");
}
}
Loading

0 comments on commit 7799515

Please sign in to comment.