use of org.wso2.carbon.apimgt.core.exception.KeyManagementException in project carbon-apimgt by wso2.
the class DefaultScopeRegistrationImplTest method testUpdateScope.
@Test
public void testUpdateScope() throws KeyManagementException {
DefaultScopeRegistrationServiceStub defaultScopeRegistrationServiceStub = Mockito.mock(DefaultScopeRegistrationServiceStub.class);
DefaultScopeRegistrationImpl defaultScopeRegistration = new DefaultScopeRegistrationImpl(defaultScopeRegistrationServiceStub);
ScopeInfo scopeInfo = new ScopeInfo();
scopeInfo.setDescription("cde");
scopeInfo.setDisplayName("abc");
Scope scope = new Scope();
scope.setName("abc");
scope.setDescription("cde");
Mockito.when(defaultScopeRegistrationServiceStub.updateScope(Mockito.any(ScopeInfo.class), Mockito.anyString())).thenReturn(Response.builder().status(200).headers(new HashMap<>()).body(new Gson().toJson(scopeInfo), feign.Util.UTF_8).build());
boolean status = defaultScopeRegistration.updateScope(scope);
Assert.assertTrue(status);
}
use of org.wso2.carbon.apimgt.core.exception.KeyManagementException in project carbon-apimgt by wso2.
the class DefaultScopeRegistrationImplTest method testScopeRegistrationFailed.
@Test
public void testScopeRegistrationFailed() {
DefaultScopeRegistrationServiceStub defaultScopeRegistrationServiceStub = Mockito.mock(DefaultScopeRegistrationServiceStub.class);
DefaultScopeRegistrationImpl defaultScopeRegistration = new DefaultScopeRegistrationImpl(defaultScopeRegistrationServiceStub);
ScopeInfo scopeInfo = new ScopeInfo();
scopeInfo.setName("abc");
scopeInfo.setDescription("cde");
Mockito.when(defaultScopeRegistrationServiceStub.registerScope(scopeInfo)).thenReturn(Response.builder().status(400).headers(new HashMap<>()).body(new Gson().toJson(scopeInfo), feign.Util.UTF_8).build());
Scope scope = new Scope();
scope.setName("abc");
scope.setDescription("cde");
try {
defaultScopeRegistration.registerScope(scope);
Assert.fail();
} catch (KeyManagementException e) {
Assert.assertEquals(e.getMessage(), "Scope Registration Failed");
}
}
use of org.wso2.carbon.apimgt.core.exception.KeyManagementException in project carbon-apimgt by wso2.
the class AuthenticatorServiceTestCase method testSetAccessTokenData.
@Test
public void testSetAccessTokenData() throws Exception {
// Happy Path
APIMConfigurationService apimConfigurationService = Mockito.mock(APIMConfigurationService.class);
EnvironmentConfigurations environmentConfigurations = new EnvironmentConfigurations();
Mockito.when(apimConfigurationService.getEnvironmentConfigurations()).thenReturn(environmentConfigurations);
APIMAppConfigurationService apimAppConfigurationService = Mockito.mock(APIMAppConfigurationService.class);
APIMAppConfigurations apimAppConfigurations = new APIMAppConfigurations();
Mockito.when(apimAppConfigurationService.getApimAppConfigurations()).thenReturn(apimAppConfigurations);
// // AccessTokenInfo object
AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
accessTokenInfo.setIdToken("eyJ4NXQiOiJObUptT0dVeE16WmxZak0yWkRSaE5UWmxZVEExWXpkaFpUUmlPV0UwTldJMk0ySm1PVGMxWkEiLCJraWQiOiJkMGVjNTE0YTMyYjZmODhjMGFiZDEyYTI4NDA2OTliZGQzZGViYTlkIiwiYWxnIjoiUlMyNTYifQ.eyJhdF9oYXNoIjoiWGg3bFZpSDZDS2pZLXRIT09JaWN5QSIsInN1YiI6ImFkbWluIiwiYXVkIjpbInR6NlJGQnhzdV93Z0RCd3FyUThvVmo3d25FTWEiXSwiYXpwIjoidHo2UkZCeHN1X3dnREJ3cXJROG9Wajd3bkVNYSIsImF1dGhfdGltZSI6MTUwMTczMzQ1NiwiaXNzIjoiaHR0cHM6XC9cL2xvY2FsaG9zdDo5NDQzXC9vYXV0aDJcL3Rva2VuIiwiZXhwIjoxNTAxNzM3MDU3LCJpYXQiOjE1MDE3MzM0NTd9.XXX-XXX");
accessTokenInfo.setValidityPeriod(-2L);
accessTokenInfo.setScopes("apim:subscribe openid");
// // Expected AuthResponseBean object
AuthResponseBean expectedAuthResponseBean = new AuthResponseBean();
expectedAuthResponseBean.setTokenValid(true);
expectedAuthResponseBean.setAuthUser("admin");
expectedAuthResponseBean.setScopes(accessTokenInfo.getScopes());
expectedAuthResponseBean.setType(AuthenticatorConstants.BEARER_PREFIX);
expectedAuthResponseBean.setValidityPeriod(accessTokenInfo.getValidityPeriod());
expectedAuthResponseBean.setIdToken(accessTokenInfo.getIdToken());
KeyManager keyManager = Mockito.mock(KeyManager.class);
SystemApplicationDao systemApplicationDao = Mockito.mock(SystemApplicationDao.class);
Mockito.when(systemApplicationDao.isConsumerKeyExistForApplication("store")).thenReturn(false);
MultiEnvironmentOverview multiEnvironmentOverview = new MultiEnvironmentOverview();
environmentConfigurations.setMultiEnvironmentOverview(multiEnvironmentOverview);
AuthenticatorService authenticatorService = new AuthenticatorService(keyManager, systemApplicationDao, apimConfigurationService, apimAppConfigurationService);
// // Actual response
AuthResponseBean authResponseBean = authenticatorService.getResponseBeanFromTokenInfo(accessTokenInfo);
Assert.assertTrue(EqualsBuilder.reflectionEquals(expectedAuthResponseBean, authResponseBean));
// Happy Path - When id token is null
// // AccessTokenInfo object with null id token
AccessTokenInfo invalidTokenInfo = new AccessTokenInfo();
invalidTokenInfo.setValidityPeriod(-2L);
invalidTokenInfo.setScopes("apim:subscribe openid");
// // Expected AuthResponseBean object when id token is null
AuthResponseBean expectedResponseBean = new AuthResponseBean();
expectedResponseBean.setTokenValid(true);
expectedResponseBean.setScopes(invalidTokenInfo.getScopes());
expectedResponseBean.setType(AuthenticatorConstants.BEARER_PREFIX);
expectedResponseBean.setValidityPeriod(invalidTokenInfo.getValidityPeriod());
expectedResponseBean.setIdToken(invalidTokenInfo.getIdToken());
expectedResponseBean.setAuthUser("admin");
// // Actual response when id token is null
AuthResponseBean responseBean = authenticatorService.getResponseBeanFromTokenInfo(invalidTokenInfo);
Assert.assertTrue(EqualsBuilder.reflectionEquals(expectedResponseBean, responseBean));
// Error Path - When parsing JWT fails and throws KeyManagementException
// // AccessTokenInfo object with invalid ID token format
AccessTokenInfo invalidAccessTokenInfo = new AccessTokenInfo();
invalidAccessTokenInfo.setIdToken("xxx-invalid-id-token-xxx");
invalidAccessTokenInfo.setValidityPeriod(-2L);
invalidAccessTokenInfo.setScopes("apim:subscribe openid");
try {
AuthResponseBean errorResponseBean = authenticatorService.getResponseBeanFromTokenInfo(invalidAccessTokenInfo);
} catch (KeyManagementException e) {
Assert.assertEquals(900986, e.getErrorHandler().getErrorCode());
}
}
use of org.wso2.carbon.apimgt.core.exception.KeyManagementException in project carbon-apimgt by wso2.
the class AuthenticatorService method getResponseBeanFromTokenInfo.
/**
* This method sets access token data.
*
* @param accessTokenInfo Information of the access token
* @return AuthResponseBean - An object with access token data
* @throws KeyManagementException When parsing JWT fails
*/
public AuthResponseBean getResponseBeanFromTokenInfo(AccessTokenInfo accessTokenInfo) throws KeyManagementException {
String authUser = null;
if (accessTokenInfo.getIdToken() != null) {
authUser = getUsernameFromJWT(accessTokenInfo.getIdToken());
}
if (authUser == null) {
authUser = AuthenticatorConstants.ADMIN_USER;
}
AuthResponseBean responseBean = new AuthResponseBean();
responseBean.setTokenValid(true);
responseBean.setAuthUser(authUser);
responseBean.setScopes(accessTokenInfo.getScopes());
responseBean.setType(AuthenticatorConstants.BEARER_PREFIX);
responseBean.setValidityPeriod(accessTokenInfo.getValidityPeriod());
responseBean.setIdToken(accessTokenInfo.getIdToken());
return responseBean;
}
Aggregations