-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
200 additions
and
2 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
.../com/wso2/openbanking/accelerator/identity/token/validators/MTLSCertificateValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). All Rights Reserved. | ||
* | ||
* This software is the property of WSO2 LLC. and its suppliers, if any. | ||
* Dissemination of any information or reproduction of any material contained | ||
* herein in any form is strictly forbidden, unless permitted by WSO2 expressly. | ||
* You may not alter or remove any copyright or other notice from copies of this content. | ||
*/ | ||
|
||
package com.wso2.openbanking.accelerator.identity.token.validators; | ||
|
||
import com.wso2.openbanking.accelerator.common.exception.OpenBankingException; | ||
import com.wso2.openbanking.accelerator.common.util.CertificateUtils; | ||
import com.wso2.openbanking.accelerator.identity.token.util.TokenFilterException; | ||
import com.wso2.openbanking.accelerator.identity.util.IdentityCommonConstants; | ||
import com.wso2.openbanking.accelerator.identity.util.IdentityCommonUtil; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import java.security.cert.X509Certificate; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* MTLS Certificate Validator. | ||
* Validates the expiry status of the certificate. | ||
*/ | ||
public class MTLSCertificateValidator implements OBIdentityFilterValidator { | ||
|
||
private static final Log log = LogFactory.getLog(MTLSCertificateValidator.class); | ||
|
||
@Override | ||
public void validate(ServletRequest request, String clientId) throws TokenFilterException, ServletException { | ||
|
||
HttpServletRequest servletRequest = (HttpServletRequest) request; | ||
String mtlsCertificate = servletRequest.getHeader(IdentityCommonUtil.getMTLSAuthHeader()); | ||
// MTLSEnforcementValidator validates the presence of the certificate. | ||
if (mtlsCertificate != null) { | ||
try { | ||
X509Certificate x509Certificate = CertificateUtils.parseCertificate(mtlsCertificate); | ||
|
||
if (CertificateUtils.isExpired(x509Certificate)) { | ||
log.error("Certificate with the serial number " + | ||
x509Certificate.getSerialNumber() + " issued by the CA " + | ||
x509Certificate.getIssuerDN().toString() + " is expired"); | ||
throw new TokenFilterException(HttpServletResponse.SC_UNAUTHORIZED, | ||
"Invalid mutual TLS request. Client certificate is expired", | ||
"Certificate with the serial number " + x509Certificate.getSerialNumber() + | ||
" issued by the CA " + x509Certificate.getIssuerDN().toString() + " is expired"); | ||
} | ||
log.debug("Client certificate expiry validation completed successfully"); | ||
} catch (OpenBankingException e) { | ||
log.error("Invalid mutual TLS request. Client certificate is invalid", e); | ||
throw new TokenFilterException(HttpServletResponse.SC_UNAUTHORIZED, IdentityCommonConstants | ||
.OAUTH2_INVALID_CLIENT_MESSAGE, e.getMessage()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,7 +139,7 @@ public void testHandleCustomClaims() throws OpenBankingException, IdentityOAuth2 | |
|
||
|
||
assertEquals("123", jwtClaimsSet.getClaim("consent_id")); | ||
assertEquals("{x5t#S256=GA370hkNKyI1C060VmxL4xZtKyjD6aQUjrGKYWoeZX8}", jwtClaimsSet.getClaim( | ||
assertEquals("{x5t#S256=k0p--ML7nfkE2pULKryszJRBx2ThBMaxHgJOePosits}", jwtClaimsSet.getClaim( | ||
"cnf").toString()); | ||
assertEquals("[email protected]", jwtClaimsSet.getClaim("sub")); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
.../wso2/openbanking/accelerator/identity/token/validators/MTLSCertificateValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). All Rights Reserved. | ||
* | ||
* This software is the property of WSO2 LLC. and its suppliers, if any. | ||
* Dissemination of any information or reproduction of any material contained | ||
* herein in any form is strictly forbidden, unless permitted by WSO2 expressly. | ||
* You may not alter or remove any copyright or other notice from copies of this content. | ||
*/ | ||
|
||
package com.wso2.openbanking.accelerator.identity.token.validators; | ||
|
||
import com.wso2.openbanking.accelerator.common.exception.OpenBankingException; | ||
import com.wso2.openbanking.accelerator.identity.internal.IdentityExtensionsDataHolder; | ||
import com.wso2.openbanking.accelerator.identity.token.DefaultTokenFilter; | ||
import com.wso2.openbanking.accelerator.identity.token.TokenFilter; | ||
import com.wso2.openbanking.accelerator.identity.token.util.TestConstants; | ||
import com.wso2.openbanking.accelerator.identity.token.util.TestUtil; | ||
import com.wso2.openbanking.accelerator.identity.util.IdentityCommonConstants; | ||
import com.wso2.openbanking.accelerator.identity.util.IdentityCommonUtil; | ||
import org.apache.http.HttpStatus; | ||
import org.mockito.Mockito; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PowerMockIgnore; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.testng.PowerMockTestCase; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
|
||
@PrepareForTest({IdentityCommonUtil.class}) | ||
@PowerMockIgnore({"jdk.internal.reflect.*"}) | ||
public class MTLSCertificateValidatorTest extends PowerMockTestCase { | ||
|
||
MockHttpServletResponse response; | ||
MockHttpServletRequest request; | ||
FilterChain filterChain; | ||
TokenFilter filter; | ||
|
||
@BeforeMethod | ||
public void beforeMethod() throws ReflectiveOperationException, IOException, OpenBankingException { | ||
|
||
request = new MockHttpServletRequest(); | ||
response = new MockHttpServletResponse(); | ||
filterChain = Mockito.spy(FilterChain.class); | ||
|
||
List<OBIdentityFilterValidator> validators = new ArrayList<>(); | ||
MTLSCertificateValidator mtlsCertificateValidator = Mockito.spy(MTLSCertificateValidator.class); | ||
validators.add(mtlsCertificateValidator); | ||
|
||
filter = Mockito.spy(TokenFilter.class); | ||
Mockito.doReturn(new DefaultTokenFilter()).when(filter).getDefaultTokenFilter(); | ||
Mockito.doReturn(validators).when(filter).getValidators(); | ||
PowerMockito.mockStatic(IdentityCommonUtil.class); | ||
PowerMockito.when(IdentityCommonUtil.getMTLSAuthHeader()).thenReturn(TestConstants.CERTIFICATE_HEADER); | ||
PowerMockito.when(IdentityCommonUtil.getRegulatoryFromSPMetaData("test")).thenReturn(true); | ||
Map<String, Object> configMap = new HashMap<>(); | ||
configMap.put(IdentityCommonConstants.ENABLE_TRANSPORT_CERT_AS_HEADER, true); | ||
configMap.put(IdentityCommonConstants.CLIENT_CERTIFICATE_ENCODE, false); | ||
IdentityExtensionsDataHolder.getInstance().setConfigurationMap(configMap); | ||
|
||
request.setParameter(IdentityCommonConstants.CLIENT_ID, "test"); | ||
request.setAttribute(IdentityCommonConstants.JAVAX_SERVLET_REQUEST_CERTIFICATE, null); | ||
|
||
} | ||
|
||
@Test(description = "Test whether the expired certificate fails") | ||
public void testMTLSCertValidationWithExpiredCertificate() throws IOException, ServletException { | ||
|
||
request.addHeader(TestConstants.CERTIFICATE_HEADER, TestConstants.EXPIRED_CERTIFICATE_CONTENT); | ||
|
||
filter.doFilter(request, response, filterChain); | ||
Map<String, String> responseMap = TestUtil.getResponse(response.getOutputStream()); | ||
assertEquals(response.getStatus(), HttpStatus.SC_UNAUTHORIZED); | ||
assertEquals(responseMap.get(IdentityCommonConstants.OAUTH_ERROR), "invalid_client"); | ||
assertEquals(responseMap.get(IdentityCommonConstants.OAUTH_ERROR_DESCRIPTION), | ||
"Invalid mutual TLS request. Client certificate is expired"); | ||
|
||
} | ||
|
||
@Test(description = "Test whether the expired certificate fails") | ||
public void testMTLSCertValidationWithValidCertificate() throws IOException, ServletException { | ||
|
||
request.addHeader(TestConstants.CERTIFICATE_HEADER, TestConstants.CERTIFICATE_CONTENT); | ||
|
||
filter.doFilter(request, response, filterChain); | ||
assertEquals(response.getStatus(), HttpStatus.SC_OK); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters