Skip to content

Commit

Permalink
Initial implementation of internal token revocation events.
Browse files Browse the repository at this point in the history
Added the license header.

temp 1.

Temp2

Revert "Added the license header."

This reverts commit bec1c5e.

Revert "Initla implementation of the token persistance event."

This reverts commit 4112748.

temp3

Temp5

Temp

Added code review related changes.
  • Loading branch information
prasa7 authored and Lakith-Rambukkanage committed Oct 11, 2023
1 parent 3ce91ed commit 9e05abe
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 9 deletions.
4 changes: 4 additions & 0 deletions components/wso2is.key.manager.tokenpersistence/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,9 @@
<artifactId>org.wso2.carbon.idp.mgt</artifactId>
<version>${carbon.identity.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.km.ext.wso2is</groupId>
<artifactId>wso2is.notification.event.handlers</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ServiceReferenceHolder {
private TenantRegistryLoader tenantRegistryLoader;
private static ConfigurationContextService contextService;
private static InvalidTokenPersistenceService tokenPersistenceService;

private ServiceReferenceHolder() {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.wso2.carbon.identity.core.util.IdentityCoreInitializedEvent;
import org.wso2.carbon.identity.oauth.listener.*;
import org.wso2.carbon.identity.oauth.tokenprocessor.OAuth2RevocationProcessor;
import org.wso2.carbon.identity.oauth.tokenprocessor.RefreshTokenGrantProcessor;
import org.wso2.carbon.identity.oauth.tokenprocessor.TokenValidationProcessor;
Expand All @@ -36,6 +37,7 @@
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.ConfigurationContextService;
import org.wso2.is.key.manager.tokenpersistence.dao.ExtendedAccessTokenDAOImpl;
import org.wso2.is.key.manager.tokenpersistence.listner.*;
import org.wso2.is.key.manager.tokenpersistence.processor.InMemoryOAuth2RevocationProcessor;
import org.wso2.is.key.manager.tokenpersistence.processor.InMemoryRefreshTokenGrantProcessor;
import org.wso2.is.key.manager.tokenpersistence.processor.InMemoryTokenValidationProcessor;
Expand Down Expand Up @@ -66,6 +68,8 @@ protected void activate(ComponentContext cxt) {
new InMemoryRefreshTokenGrantProcessor(), null);
cxt.getBundleContext().registerService(TokenValidationProcessor.class,
new InMemoryTokenValidationProcessor(), null);
cxt.getBundleContext().registerService(OAuthApplicationMgtListener.class,
new APIMOAuthApplicationMgtListener(), null);
}

} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. 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
*
* 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
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.is.key.manager.tokenpersistence.listner;

import org.apache.commons.logging.*;
import org.wso2.carbon.identity.oauth.*;
import org.wso2.carbon.identity.oauth.dto.*;
import org.wso2.carbon.identity.oauth.listener.*;
import org.wso2.is.notification.*;
import org.wso2.is.notification.event.*;
import org.wso2.is.notification.internal.*;

import java.util.*;

public class APIMOAuthApplicationMgtListener implements OAuthApplicationMgtListener {

private static final Log log = LogFactory.getLog(APIMOAuthApplicationMgtListener.class);


@Override
public boolean isEnabled() {
return false;
}

@Override
public int getExecutionOrder() {
return 0;
}

@Override
public void doPreUpdateConsumerApplication(OAuthConsumerAppDTO oAuthConsumerAppDTO) throws IdentityOAuthAdminException {

}

@Override
public void doPreUpdateConsumerApplicationState(String s, String s1) throws IdentityOAuthAdminException {

}

@Override
public void doPreRemoveOAuthApplicationData(String s) throws IdentityOAuthAdminException {

}

@Override
public void doPostRevokeRegenerateOAuthSecret(String consumerKey, Properties properties)
throws IdentityOAuthAdminException {
InternalTokenRevocationEvent internalTokenRevocationEvent
= new InternalTokenRevocationEvent(consumerKey, properties);
ServiceReferenceHolder.getInstance().getEventSender().publishEvent(internalTokenRevocationEvent);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.wso2.carbon.identity.oauth2.token.handlers.grant.RefreshGrantHandler;
import org.wso2.carbon.identity.oauth2.util.OAuth2Util;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.is.notification.event.TokenRevocationEvent;
import org.wso2.is.notification.event.*;
import org.wso2.is.notification.internal.ServiceReferenceHolder;

import java.text.ParseException;
Expand All @@ -56,7 +56,6 @@ public class ApimOauthEventInterceptor extends AbstractOAuthEventInterceptor {
boolean enabled;
String username;
char[] password;
private EventSender eventSender;
private static final String JWT = "JWT";

public ApimOauthEventInterceptor() {
Expand All @@ -72,9 +71,10 @@ public ApimOauthEventInterceptor() {
if (StringUtils.isNotEmpty(usernameProperty) && StringUtils.isNotEmpty(passwordProperty)) {
username = NotificationUtil.replaceSystemProperty(usernameProperty);
password = NotificationUtil.replaceSystemProperty(passwordProperty).toCharArray();
eventSender = new EventSender(notificationEndpoint, username, String.valueOf(password), headerMap);
ServiceReferenceHolder.getInstance()
.setEventSender(new EventSender(notificationEndpoint, username, String.valueOf(password), headerMap));
} else {
eventSender = new EventSender(notificationEndpoint, headerMap);
ServiceReferenceHolder.getInstance().setEventSender(new EventSender(notificationEndpoint, headerMap));
}
}
}
Expand Down Expand Up @@ -234,7 +234,7 @@ private void publishEvent(TokenRevocationEvent tokenRevocationEvent) {

if (isEnabled()) {
if (StringUtils.isNotEmpty(notificationEndpoint)) {
eventSender.publishEvent(tokenRevocationEvent);
ServiceReferenceHolder.getInstance().getEventSender().publishEvent(tokenRevocationEvent);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
public class NotificationConstants {

public static final String TOKEN_REVOCATION_EVENT = "token_revocation";
public static final String INTERNAL_TOKEN_REVOCATION_EVENT = "internal_token_revocation";
public static final String NOTIFICATION_ENDPOINT = "notification_endpoint";
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

/*
* Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. 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
* 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
* specific language governing permissions and limitations
* under the License.
*
*/

package org.wso2.is.notification.event;

import org.wso2.is.notification.*;

import java.util.*;

/**
* Token Revocation Event Model to send Event.
*/
public class InternalTokenRevocationEvent extends Event {
private static final long serialVersionUID = 1L;

private String revocationTime;
private String consumerKey;
private String revocationType;

public InternalTokenRevocationEvent(String consumerKey, Properties properties) {

this.eventId = UUID.randomUUID().toString();
this.type = NotificationConstants.INTERNAL_TOKEN_REVOCATION_EVENT;
this.consumerKey = consumerKey;
this.revocationTime = String.valueOf(System.currentTimeMillis());
this.revocationType = (String) properties.getProperty("action");

}

public String getConsumerKey() {
return consumerKey;
}
public void setConsumerKey(String consumerKey) {

this.consumerKey = consumerKey;
}

public String getRevocationTime() {
return revocationTime;
}

public void setRevocationTime(String revocationTime) {
this.revocationTime = revocationTime;
}

public String getRevocationType() {
return revocationType;
}

public void setRevocationType(String revocationType) {
this.revocationType = revocationType;
}

@Override
public String toString() {

return "TokenRevocationEvent{" +
"eventId='" + eventId + '\'' +
", type='" + type + '\'' +
", consumerKey='" + consumerKey + '\'' +
", revocationTime=" + revocationTime +
", revocationType=" + revocationType +
", tenantId=" + tenantId +
", tenantDomain='" + tenantDomain + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import org.wso2.carbon.identity.oauth.event.OAuthEventInterceptor;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.ConfigurationContextService;
import org.wso2.is.notification.APIMTokenExchangeAuditLogger;
import org.wso2.is.notification.ApimOauthEventInterceptor;
import org.wso2.is.notification.*;

/**
* Activation class for notification
Expand All @@ -26,6 +25,7 @@ public class NotificationServiceComponent {

private static final Log log = LogFactory.getLog(NotificationServiceComponent.class);
ServiceRegistration<OAuthEventInterceptor> serviceRegistration;
ServiceRegistration<OAuthEventInterceptor> internalTokenRevocationService;
private ServiceRegistration<OAuthEventInterceptor> auditLoggerServiceRegistration;

@Activate
Expand Down Expand Up @@ -93,6 +93,9 @@ protected void deactivate(ComponentContext context) {
if (serviceRegistration != null) {
serviceRegistration.unregister();
}
if (internalTokenRevocationService != null) {
internalTokenRevocationService.unregister();
}
if (auditLoggerServiceRegistration != null) {
auditLoggerServiceRegistration.unregister();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.ConfigurationContextService;
import org.wso2.is.notification.*;

/**
* Holder class to hold service references used in notification.
Expand All @@ -11,6 +12,7 @@ public class ServiceReferenceHolder {
private static final ServiceReferenceHolder instance = new ServiceReferenceHolder();
private RealmService realmService;
private ConfigurationContextService contextService;
private EventSender eventSender;

public static ServiceReferenceHolder getInstance() {

Expand All @@ -37,4 +39,12 @@ public ConfigurationContextService getContextService() {
public void setContextService(ConfigurationContextService contextService) {
this.contextService = contextService;
}

public EventSender getEventSender() {
return eventSender;
}

public void setEventSender(EventSender eventSender) {
this.eventSender = eventSender;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@
TIME_CREATED TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
EXPIRY_TIMESTAMP BIGINT NOT NULL,
PRIMARY KEY (UUID)
)
);

CREATE TABLE IF NOT EXISTS AM_INTERNAL_TOKEN_REVOCATION (
ID VARCHAR(255) NOT NULL,
CONSUMER_KEY VARCHAR(255) NOT NULL,
TOKEN_TYPE VARCHAR(255) NOT NULL,
TIME_REVOKED VARCHAR(255) NOT NULL,
PRIMARY KEY (ID)
);

0 comments on commit 9e05abe

Please sign in to comment.