Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change default token type of portal apps to JWT. #12281

Merged
merged 8 commits into from
Feb 25, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,30 @@
revokedTokenMap.get(APIConstants.NotificationEvent.CONSUMER_KEY) != null &&
revokedTokenMap.containsKey(APIConstants.NotificationEvent.REVOCATION_TIME) &&
revokedTokenMap.get(APIConstants.NotificationEvent.REVOCATION_TIME) != null) {
RevokedJWTDataHolder.getInstance().addRevokedConsumerKeyToMap(
(String) revokedTokenMap.get(APIConstants.NotificationEvent.CONSUMER_KEY),
(long) revokedTokenMap.get(APIConstants.NotificationEvent.REVOCATION_TIME));
try {
RevokedJWTDataHolder.getInstance().addRevokedConsumerKeyToMap(
(String) revokedTokenMap.get(APIConstants.NotificationEvent.CONSUMER_KEY),
convertRevokedTime(revokedTokenMap));
} catch (NumberFormatException e) {
log.warn("Event dropped due to unsupported value type for "

Check warning on line 97 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/listeners/GatewayTokenRevocationMessageListener.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/listeners/GatewayTokenRevocationMessageListener.java#L93-L97

Added lines #L93 - L97 were not covered by tests
+ APIConstants.NotificationEvent.REVOCATION_TIME + " : "
+ revokedTokenMap.get(APIConstants.NotificationEvent.REVOCATION_TIME));
}

Check warning on line 100 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/listeners/GatewayTokenRevocationMessageListener.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/listeners/GatewayTokenRevocationMessageListener.java#L99-L100

Added lines #L99 - L100 were not covered by tests
}
} else if (APIConstants.NotificationEvent.SUBJECT_ENTITY_REVOCATION_EVENT.equals(tokenType)) {
HashMap<String, Object> revokedTokenMap = base64Decode(revokedToken);
if (revokedTokenMap.get(APIConstants.NotificationEvent.ENTITY_TYPE) != null &&
revokedTokenMap.get(APIConstants.NotificationEvent.REVOCATION_TIME) != null &&
revokedTokenMap.get(APIConstants.NotificationEvent.ENTITY_ID) != null) {
String entityType = (String) revokedTokenMap.get(APIConstants.NotificationEvent.ENTITY_TYPE);
long revocationTime = (long) revokedTokenMap.get(APIConstants.NotificationEvent.REVOCATION_TIME);
long revocationTime = 0;

Check warning on line 108 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/listeners/GatewayTokenRevocationMessageListener.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/listeners/GatewayTokenRevocationMessageListener.java#L108

Added line #L108 was not covered by tests
try {
revocationTime = convertRevokedTime(revokedTokenMap);
} catch (NumberFormatException e) {
log.warn("Event dropped due to unsupported value type for "

Check warning on line 112 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/listeners/GatewayTokenRevocationMessageListener.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/listeners/GatewayTokenRevocationMessageListener.java#L110-L112

Added lines #L110 - L112 were not covered by tests
+ APIConstants.NotificationEvent.REVOCATION_TIME + " : "
+ revokedTokenMap.get(APIConstants.NotificationEvent.REVOCATION_TIME));
}

Check warning on line 115 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/listeners/GatewayTokenRevocationMessageListener.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/listeners/GatewayTokenRevocationMessageListener.java#L114-L115

Added lines #L114 - L115 were not covered by tests
String entityId = (String) revokedTokenMap.get(APIConstants.NotificationEvent.ENTITY_ID);
if (APIConstants.NotificationEvent.ENTITY_TYPE_USER_ID.equals(entityType)) {
RevokedJWTDataHolder.getInstance().addRevokedSubjectEntityUserToMap(entityId, revocationTime);
Expand Down Expand Up @@ -138,4 +151,9 @@
}
return new HashMap<>();
}

private long convertRevokedTime(HashMap<String, Object> revokedTokenMap) throws NumberFormatException {

return Long.parseLong((String) revokedTokenMap.get(APIConstants.NotificationEvent.REVOCATION_TIME));

Check warning on line 157 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/listeners/GatewayTokenRevocationMessageListener.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/listeners/GatewayTokenRevocationMessageListener.java#L157

Added line #L157 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
tokenIssuerDto.setJwksConfigurationDTO(jwksConfigurationDTO);
} else {
X509Certificate x509Certificate =
APIUtil.retrieveCertificateFromContent((String) certificateValue);
APIUtil.retrieveCertificateFromURLEncodedContent((String) certificateValue);

Check warning on line 214 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/factory/KeyManagerHolder.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/factory/KeyManagerHolder.java#L214

Added line #L214 was not covered by tests
if (x509Certificate != null) {
tokenIssuerDto.setCertificate(x509Certificate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9229,7 +9229,14 @@
return content.trim();
}

public static X509Certificate retrieveCertificateFromContent(String base64EncodedCertificate)
/**
* Util method to convert Base64 URL encoded certificate content to X509Certificate instance.
*
* @param base64EncodedCertificate Base64 URL encoded cert string
* @return javax.security.cert.X509Certificate
* @throws APIManagementException if an error occurs while retrieving from IDP
*/
public static X509Certificate retrieveCertificateFromURLEncodedContent(String base64EncodedCertificate)
throws APIManagementException {

if (base64EncodedCertificate != null) {
Expand All @@ -9253,6 +9260,30 @@
return null;
}

/**
* Util method to convert non URL encoded but base64 encoded certificate content to X509Certificate instance.
*
* @param base64EncodedCertificate Base64 encoded cert string (not URL encoded)
* @return javax.security.cert.X509Certificate
* @throws APIManagementException if an error occurs while retrieving from IDP
*/
public static X509Certificate retrieveCertificateFromContent(String base64EncodedCertificate)
throws APIManagementException {

if (base64EncodedCertificate != null) {
base64EncodedCertificate = APIUtil.getX509certificateContent(base64EncodedCertificate);
byte[] bytes = Base64.decodeBase64(base64EncodedCertificate.getBytes());
try (InputStream inputStream = new ByteArrayInputStream(bytes)) {
return X509Certificate.getInstance(inputStream);
} catch (IOException | javax.security.cert.CertificateException e) {
String msg = "Error while converting into X509Certificate";
log.error(msg, e);
throw new APIManagementException(msg, e);

Check warning on line 9281 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L9274-L9281

Added lines #L9274 - L9281 were not covered by tests
}
}
return null;

Check warning on line 9284 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L9284

Added line #L9284 was not covered by tests
}

/**
* Replace new RESTAPI Role mappings to tenant-conf.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@
import org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.rest.api.common.RestApiCommonUtil;
import org.wso2.carbon.apimgt.rest.api.common.RestApiConstants;
import org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO;
import org.wso2.carbon.apimgt.rest.api.dcr.web.RegistrationService;
import org.wso2.carbon.apimgt.rest.api.dcr.web.dto.FaultResponse;
import org.wso2.carbon.apimgt.rest.api.dcr.web.dto.RegistrationProfile;
import org.wso2.carbon.apimgt.rest.api.common.RestApiConstants;
import org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO;
import org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
import org.wso2.carbon.identity.application.common.model.InboundAuthenticationConfig;
import org.wso2.carbon.identity.application.common.model.InboundAuthenticationRequestConfig;
import org.wso2.carbon.identity.application.common.model.LocalAndOutboundAuthenticationConfig;
import org.wso2.carbon.identity.application.common.model.Property;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.application.common.model.ServiceProviderProperty;
Expand All @@ -54,11 +55,6 @@
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
Expand All @@ -68,6 +64,11 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static org.wso2.carbon.apimgt.api.model.ApplicationConstants.OAUTH_CLIENT_GRANT;
import static org.wso2.carbon.apimgt.api.model.ApplicationConstants.OAUTH_CLIENT_NAME;
Expand Down Expand Up @@ -275,10 +276,10 @@
*
* @param appRequest OAuthAppRequest object with client's payload content
* @return created Application
* @throws APIKeyMgtException if failed to create the a new application
* @throws APIManagementException if failed to create the new application
*/
private OAuthApplicationInfo createApplication(String applicationName, OAuthAppRequest appRequest,
String grantType) throws APIManagementException {
String grantType) throws APIManagementException {
String userName;
OAuthApplicationInfo applicationInfo = appRequest.getOAuthApplicationInfo();
String appName = applicationInfo.getClientName();
Expand Down Expand Up @@ -325,6 +326,14 @@
logoutConsentProperty.setValue(APIConstants.APP_SKIP_LOGOUT_CONSENT_VALUE);
serviceProviderProperties.add(logoutConsentProperty);

if (APIConstants.JWT.equals(applicationInfo.getTokenType())) {
LocalAndOutboundAuthenticationConfig localAndOutboundConfig =

Check warning on line 330 in components/apimgt/org.wso2.carbon.apimgt.rest.api.dcr/src/main/java/org/wso2/carbon/apimgt/rest/api/dcr/web/impl/RegistrationServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.dcr/src/main/java/org/wso2/carbon/apimgt/rest/api/dcr/web/impl/RegistrationServiceImpl.java#L330

Added line #L330 was not covered by tests
new LocalAndOutboundAuthenticationConfig();
localAndOutboundConfig.setSkipConsent(true);
localAndOutboundConfig.setSkipLogoutConsent(true);
localAndOutboundConfig.setUseTenantDomainInLocalSubjectIdentifier(true);
serviceProvider.setLocalAndOutBoundAuthenticationConfig(localAndOutboundConfig);

Check warning on line 335 in components/apimgt/org.wso2.carbon.apimgt.rest.api.dcr/src/main/java/org/wso2/carbon/apimgt/rest/api/dcr/web/impl/RegistrationServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.dcr/src/main/java/org/wso2/carbon/apimgt/rest/api/dcr/web/impl/RegistrationServiceImpl.java#L332-L335

Added lines #L332 - L335 were not covered by tests
}
String orgId = null;
try {
orgId = RestApiUtil.getValidatedOrganization(securityContext);
Expand Down
Loading
Loading