use of org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration in project carbon-apimgt by wso2.
the class APIMgtDAOTest method setUp.
@Before
public void setUp() throws Exception {
String dbConfigPath = System.getProperty("APIManagerDBConfigurationPath");
APIManagerConfiguration config = new APIManagerConfiguration();
initializeDatabase(dbConfigPath);
config.load(dbConfigPath);
ServiceReferenceHolder.getInstance().setAPIManagerConfigurationService(new APIManagerConfigurationServiceImpl(config));
List<Notifier> notifierList = new ArrayList<>();
Notifier subscriptionsNotifier = Mockito.mock(Notifier.class);
Mockito.when(subscriptionsNotifier.getType()).thenReturn(APIConstants.NotifierType.SUBSCRIPTIONS.name());
notifierList.add(subscriptionsNotifier);
ServiceReferenceHolder.getInstance().getNotifiersMap().put(subscriptionsNotifier.getType(), notifierList);
PowerMockito.mockStatic(KeyManagerHolder.class);
keyManager = Mockito.mock(KeyManager.class);
APIMgtDBUtil.initialize();
apiMgtDAO = ApiMgtDAO.getInstance();
IdentityTenantUtil.setRealmService(new TestRealmService());
String identityConfigPath = System.getProperty("IdentityConfigurationPath");
IdentityConfigParser.getInstance(identityConfigPath);
OAuthServerConfiguration oAuthServerConfiguration = OAuthServerConfiguration.getInstance();
ServiceReferenceHolder.getInstance().setOauthServerConfiguration(oAuthServerConfiguration);
}
use of org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration in project carbon-apimgt by wso2.
the class JWTValidatorImplTest method testValidateToken.
@Test
@PrepareForTest({ CertificateMgtUtils.class, JWTUtil.class, APIManagerConfiguration.class, ServiceReferenceHolder.class, APIManagerConfigurationService.class, APIUtil.class, X509CertUtils.class })
public void testValidateToken() {
TokenIssuerDto tokenIssuerDto = new TokenIssuerDto("https://localhost:9444/services");
Mockito.when(signedJWT.getHeader()).thenReturn(jwsHeader);
PowerMockito.mockStatic(JWTUtil.class);
byte[] encodedCertificateUnmatched = "aaaaaaaaaaaaaaaa".getBytes();
try {
PowerMockito.when(JWTUtil.verifyTokenSignature(signedJWT, KeyId)).thenReturn(true);
} catch (APIManagementException e) {
log.info("Exception while signature verification. " + e);
Assert.fail();
}
// Create a mock APIManagerConfiguration Object for retrieving properties from the deployment.toml
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.mockStatic(APIManagerConfiguration.class);
PowerMockito.mockStatic(APIManagerConfigurationService.class);
PowerMockito.mockStatic(APIUtil.class);
PowerMockito.mockStatic(CertificateMgtUtils.class);
PowerMockito.mockStatic(X509CertUtils.class);
APIManagerConfiguration apiManagerConfiguration = PowerMockito.mock(APIManagerConfiguration.class);
ServiceReferenceHolder serviceReferenceHolder = PowerMockito.mock(ServiceReferenceHolder.class);
APIManagerConfigurationService apiManagerConfigurationService = PowerMockito.mock(APIManagerConfigurationService.class);
OAuthServerConfiguration oAuthServerConfiguration = Mockito.mock(OAuthServerConfiguration.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
Mockito.when(oAuthServerConfiguration.getTimeStampSkewInSeconds()).thenReturn(300L);
Mockito.when(serviceReferenceHolder.getOauthServerConfiguration()).thenReturn(oAuthServerConfiguration);
JWTValidatorImpl jwtValidator = new JWTValidatorImpl();
JWKSConfigurationDTO jwksConfigurationDTO = new JWKSConfigurationDTO();
tokenIssuerDto.setJwksConfigurationDTO(jwksConfigurationDTO);
jwksConfigurationDTO.setEnabled(false);
jwtValidator.loadTokenIssuerConfiguration(tokenIssuerDto);
try {
JWTValidationInfo validatedInfo = jwtValidator.validateToken(signedJWTInfo);
assertTrue(validatedInfo.isValid(), "JWT certificate bound access token validation failed even when the" + " configuration is not enabled.");
} catch (APIManagementException e) {
Assert.fail();
}
// test when certificate is found in the trust store but cnf thumbprint is not matching with the certificate
MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito.mock(org.apache.axis2.context.MessageContext.class);
X509Certificate x509Certificate = Mockito.mock(X509Certificate.class);
java.security.cert.X509Certificate x509CertificateJava = Mockito.mock(java.security.cert.X509Certificate.class);
PowerMockito.when(CertificateMgtUtils.convert(x509Certificate)).thenReturn(Optional.of(x509CertificateJava));
X509Certificate[] sslCertObject = new X509Certificate[] { x509Certificate };
Mockito.when(axis2MsgCntxt.getProperty(NhttpConstants.SSL_CLIENT_AUTH_CERT_X509)).thenReturn(sslCertObject);
Map<String, String> headers = new HashMap<>();
Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(headers);
Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
X509Certificate x509CertificateUnMatched = Mockito.mock(X509Certificate.class);
java.security.cert.X509Certificate x509CertificateUnMatchedJava = Mockito.mock(java.security.cert.X509Certificate.class);
PowerMockito.when(CertificateMgtUtils.convert(x509CertificateUnMatched)).thenReturn(Optional.of(x509CertificateUnMatchedJava));
PowerMockito.when(X509CertUtils.computeSHA256Thumbprint(x509CertificateJava)).thenReturn(new Base64URL(CERT_HASH));
PowerMockito.when(X509CertUtils.computeSHA256Thumbprint(x509CertificateUnMatchedJava)).thenReturn(new Base64URL(encodedCertificateUnmatched.toString()));
signedJWTInfo.setX509ClientCertificate(x509CertificateUnMatched);
// Mock the properties read from the deployment.toml
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.ENABLE_CERTIFICATE_BOUND_ACCESS_TOKEN)).thenReturn("true");
try {
JWTValidationInfo validatedInfo = jwtValidator.validateToken(signedJWTInfo);
assertFalse(validatedInfo.isValid(), "JWT certificate bound access token validation successful even if the certificate thumbprint" + " is incorrect.");
} catch (APIManagementException e) {
Assert.fail();
}
// validate with correct certificate thumbprint
signedJWTInfo.setX509ClientCertificate(x509Certificate);
try {
JWTValidationInfo validatedInfo = jwtValidator.validateToken(signedJWTInfo);
assertTrue(validatedInfo.isValid(), "JWT certificate bound access token validation failed with the correct certificate thumbprint.");
} catch (APIManagementException e) {
Assert.fail();
}
// Test when certificate bound access token validation is enabled and cnf thumbprint validation is successful
// when client certificate is added in the trust store
signedJWTInfo.setX509ClientCertificate(null);
headers.put(BASE64_ENCODED_CLIENT_CERTIFICATE_HEADER, BASE64_ENCODED_CERT);
}
Aggregations