use of org.wso2.carbon.apimgt.api.model.AccessTokenRequest in project carbon-apimgt by wso2.
the class ApplicationUtilsTestCase method testCreateTokenRequestWhenKeyManagerNull.
@Test
public void testCreateTokenRequestWhenKeyManagerNull() throws APIManagementException {
PowerMockito.mockStatic(KeyManagerHolder.class);
AccessTokenRequest accessTokenRequest = new AccessTokenRequest();
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
AccessTokenRequest result = ApplicationUtils.createAccessTokenRequest(null, oAuthApplicationInfo, accessTokenRequest);
Assert.assertNull(result);
Mockito.verify(keyManager, Mockito.times(0)).buildAccessTokenRequestFromOAuthApp(Matchers.any(OAuthApplicationInfo.class), Matchers.any(AccessTokenRequest.class));
}
use of org.wso2.carbon.apimgt.api.model.AccessTokenRequest in project carbon-apimgt by wso2.
the class ApplicationUtilsTestCase method testPopulateTokenRequestWhenKeyManagerNull.
@Test
public void testPopulateTokenRequestWhenKeyManagerNull() throws APIManagementException {
PowerMockito.mockStatic(KeyManagerHolder.class);
AccessTokenRequest accessTokenRequest = new AccessTokenRequest();
ApplicationUtils.populateTokenRequest(null, "", accessTokenRequest);
Mockito.verify(keyManager, Mockito.times(0)).buildAccessTokenRequestFromJSON(Matchers.anyString(), Matchers.any(AccessTokenRequest.class));
}
use of org.wso2.carbon.apimgt.api.model.AccessTokenRequest in project carbon-apimgt by wso2.
the class ApplicationUtilsTestCase method testCreateTokenRequestWhenAccessTokenNotNull.
@Test
public void testCreateTokenRequestWhenAccessTokenNotNull() throws APIManagementException {
PowerMockito.mockStatic(KeyManagerHolder.class);
AccessTokenRequest accessTokenRequest = new AccessTokenRequest();
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
ApplicationUtils.createAccessTokenRequest(keyManager, oAuthApplicationInfo, accessTokenRequest);
Mockito.verify(keyManager, Mockito.times(1)).buildAccessTokenRequestFromOAuthApp(Matchers.any(OAuthApplicationInfo.class), Matchers.any(AccessTokenRequest.class));
}
use of org.wso2.carbon.apimgt.api.model.AccessTokenRequest in project carbon-apimgt by wso2.
the class AbstractApplicationRegistrationWorkflowExecutor method dogenerateKeysForApplication.
public static void dogenerateKeysForApplication(ApplicationRegistrationWorkflowDTO workflowDTO) throws APIManagementException {
log.debug("Registering Application and creating an Access Token... ");
Application application = workflowDTO.getApplication();
Subscriber subscriber = application.getSubscriber();
ApiMgtDAO dao = ApiMgtDAO.getInstance();
if (subscriber == null || workflowDTO.getAllowedDomains() == null) {
dao.populateAppRegistrationWorkflowDTO(workflowDTO);
}
try {
// get new key manager
// Here the default flow is set expecting an ID as the keymanager as this flow only involves new applications
String keyManagerId = workflowDTO.getKeyManager();
KeyManagerConfigurationDTO km = dao.getKeyManagerConfigurationByUUID(keyManagerId);
String tenantDomain = km.getOrganization();
String keyManagerName = km.getName();
KeyManager keyManager = KeyManagerHolder.getKeyManagerInstance(tenantDomain, keyManagerName);
if (keyManager == null) {
throw new APIManagementException("Key Manager " + keyManagerName + " not configured");
}
workflowDTO.getAppInfoDTO().getOAuthApplicationInfo().setClientName(application.getName());
// set applications attributes to the oAuthApplicationInfo
workflowDTO.getAppInfoDTO().getOAuthApplicationInfo().putAllAppAttributes(application.getApplicationAttributes());
// createApplication on oAuthorization server.
OAuthApplicationInfo oAuthApplication = keyManager.createApplication(workflowDTO.getAppInfoDTO());
// update associateApplication
ApplicationUtils.updateOAuthAppAssociation(application, workflowDTO.getKeyType(), oAuthApplication, keyManagerId);
// change create application status in to completed.
dao.updateApplicationRegistration(APIConstants.AppRegistrationStatus.REGISTRATION_COMPLETED, workflowDTO.getKeyType(), workflowDTO.getApplication().getId(), keyManagerId);
workflowDTO.setApplicationInfo(oAuthApplication);
AccessTokenInfo tokenInfo;
Object enableTokenGeneration = keyManager.getKeyManagerConfiguration().getParameter(APIConstants.KeyManager.ENABLE_TOKEN_GENERATION);
if (enableTokenGeneration != null && (Boolean) enableTokenGeneration && oAuthApplication.getJsonString().contains(APIConstants.GRANT_TYPE_CLIENT_CREDENTIALS)) {
AccessTokenRequest tokenRequest = ApplicationUtils.createAccessTokenRequest(keyManager, oAuthApplication, null);
tokenInfo = keyManager.getNewApplicationAccessToken(tokenRequest);
} else {
tokenInfo = new AccessTokenInfo();
tokenInfo.setAccessToken("");
tokenInfo.setValidityPeriod(0L);
String[] noScopes = new String[] { "N/A" };
tokenInfo.setScope(noScopes);
oAuthApplication.addParameter("tokenScope", Arrays.toString(noScopes));
}
workflowDTO.setAccessTokenInfo(tokenInfo);
} catch (Exception e) {
APIUtil.handleException("Error occurred while executing SubscriberKeyMgtClient.", e);
}
}
use of org.wso2.carbon.apimgt.api.model.AccessTokenRequest in project carbon-apimgt by wso2.
the class APIConsumerImpl method renewConsumerSecret.
/**
* Regenerate consumer secret.
*
* @param clientId For which consumer key we need to regenerate consumer secret.
* @param keyManagerName
* @return New consumer secret.
* @throws APIManagementException This is the custom exception class for API management.
*/
public String renewConsumerSecret(String clientId, String keyManagerName) throws APIManagementException {
// Create Token Request with parameters provided from UI.
AccessTokenRequest tokenRequest = new AccessTokenRequest();
tokenRequest.setClientId(clientId);
KeyManagerConfigurationDTO keyManagerConfigurationDTO = apiMgtDAO.getKeyManagerConfigurationByName(tenantDomain, keyManagerName);
if (keyManagerConfigurationDTO == null) {
keyManagerConfigurationDTO = apiMgtDAO.getKeyManagerConfigurationByUUID(keyManagerName);
if (keyManagerConfigurationDTO != null) {
keyManagerName = keyManagerConfigurationDTO.getName();
} else {
log.error("Key Manager: " + keyManagerName + " not found in database.");
throw new APIManagementException("Key Manager " + keyManagerName + " not found in database.", ExceptionCodes.KEY_MANAGER_NOT_FOUND);
}
}
KeyManager keyManager = KeyManagerHolder.getKeyManagerInstance(tenantDomain, keyManagerName);
return keyManager.getNewApplicationConsumerSecret(tokenRequest);
}
Aggregations