use of org.wso2.carbon.apimgt.api.model.OAuthAppRequest in project carbon-apimgt by wso2.
the class AuthenticatorService method createDCRApplication.
/**
* This method creates a DCR application.
*
* @param clientName Name of the application to be created
* @param callBackURL Call back URL of the application
* @param grantTypes List of grant types of the application
* @return OAUthApplicationInfo - An object with DCR Application information
* @throws APIManagementException When creating DCR application fails
*/
private OAuthApplicationInfo createDCRApplication(String clientName, String callBackURL, List<String> grantTypes) throws APIManagementException {
OAuthApplicationInfo oAuthApplicationInfo;
try {
// Here the keyType:"Application" will be passed as a default value
// for the oAuthAppRequest constructor argument.
// This value is not related to DCR application creation.
OAuthAppRequest oAuthAppRequest = new OAuthAppRequest(clientName, callBackURL, AuthenticatorConstants.APPLICATION_KEY_TYPE, grantTypes);
if (systemApplicationDao.isConsumerKeyExistForApplication(clientName)) {
String consumerKey = systemApplicationDao.getConsumerKeyForApplication(clientName);
oAuthApplicationInfo = getKeyManager().retrieveApplication(consumerKey);
} else {
oAuthApplicationInfo = getKeyManager().createApplication(oAuthAppRequest);
if (oAuthApplicationInfo != null) {
systemApplicationDao.addApplicationKey(clientName, oAuthApplicationInfo.getClientId());
}
}
} catch (KeyManagementException | APIMgtDAOException e) {
String errorMsg = "Error while creating the keys for OAuth application : " + clientName;
log.error(errorMsg, e, ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
throw new APIManagementException(errorMsg, e, ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
}
return oAuthApplicationInfo;
}
use of org.wso2.carbon.apimgt.api.model.OAuthAppRequest in project carbon-apimgt by wso2.
the class ApiMgtDAO method populateAppRegistrationWorkflowDTO.
public void populateAppRegistrationWorkflowDTO(ApplicationRegistrationWorkflowDTO workflowDTO) throws APIManagementException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Application application = null;
Subscriber subscriber = null;
String registrationEntry = SQLConstants.GET_APPLICATION_REGISTRATION_ENTRY_BY_SUBSCRIBER_SQL;
try {
conn = APIMgtDBUtil.getConnection();
ps = conn.prepareStatement(registrationEntry);
ps.setString(1, workflowDTO.getExternalWorkflowReference());
rs = ps.executeQuery();
while (rs.next()) {
subscriber = new Subscriber(rs.getString("USER_ID"));
subscriber.setId(rs.getInt("SUBSCRIBER_ID"));
application = new Application(rs.getString("NAME"), subscriber);
application.setId(rs.getInt("APPLICATION_ID"));
application.setUUID(rs.getString("UUID"));
application.setTokenType(rs.getString("APP_TYPE"));
application.setApplicationWorkFlowStatus(rs.getString("APPLICATION_STATUS"));
application.setCallbackUrl(rs.getString("CALLBACK_URL"));
application.setDescription(rs.getString("DESCRIPTION"));
application.setTier(rs.getString("APPLICATION_TIER"));
workflowDTO.setApplication(application);
workflowDTO.setKeyType(rs.getString("TOKEN_TYPE"));
workflowDTO.setUserName(subscriber.getName());
workflowDTO.setDomainList(rs.getString("ALLOWED_DOMAINS"));
workflowDTO.setValidityTime(rs.getLong("VALIDITY_PERIOD"));
String tenantDomain = MultitenantUtils.getTenantDomain(subscriber.getName());
String keyManagerUUID = rs.getString("KEY_MANAGER");
workflowDTO.setKeyManager(keyManagerUUID);
KeyManagerConfigurationDTO keyManagerConfigurationByUUID = getKeyManagerConfigurationByUUID(conn, keyManagerUUID);
if (keyManagerConfigurationByUUID != null) {
OAuthAppRequest request = ApplicationUtils.createOauthAppRequest(application.getName(), null, application.getCallbackUrl(), rs.getString("TOKEN_SCOPE"), rs.getString("INPUTS"), application.getTokenType(), keyManagerConfigurationByUUID.getOrganization(), keyManagerConfigurationByUUID.getName());
request.setMappingId(workflowDTO.getWorkflowReference());
request.getOAuthApplicationInfo().setApplicationUUID(application.getUUID());
workflowDTO.setAppInfoDTO(request);
} else {
throw new APIManagementException("Error occured while finding the KeyManager from uuid " + keyManagerUUID + ".", ExceptionCodes.KEY_MANAGER_NOT_REGISTERED);
}
}
} catch (SQLException | IOException e) {
handleException("Error occurred while retrieving an " + "Application Registration Entry for Workflow : " + workflowDTO.getExternalWorkflowReference(), e);
} finally {
APIMgtDBUtil.closeAllConnections(ps, conn, rs);
}
}
use of org.wso2.carbon.apimgt.api.model.OAuthAppRequest in project carbon-apimgt by wso2.
the class AMDefaultKeyManagerImplTest method testCreateApplicationAppNameWithSpecialChars.
@Test
public void testCreateApplicationAppNameWithSpecialChars() throws APIManagementException, KeyManagerClientException {
String applicationName = "ÅÄÖÅÄÖ";
System.setProperty("carbon.home", "jhkjn");
PowerMockito.mockStatic(PrivilegedCarbonContext.class);
OAuthAppRequest oauthRequest = new OAuthAppRequest();
OAuthApplicationInfo oauthApplication = new OAuthApplicationInfo();
oauthApplication.setAppOwner(APP_OWNER);
oauthApplication.setCallBackURL(StringUtils.join(REDIRECT_URIS, ","));
oauthApplication.setClientName(applicationName);
oauthApplication.addParameter(ApplicationConstants.OAUTH_CLIENT_USERNAME, APP_OWNER);
oauthApplication.addParameter(ApplicationConstants.APP_KEY_TYPE, KEY_TYPE);
oauthApplication.setJsonString(getJSONString());
oauthRequest.setMappingId("123");
oauthRequest.setOAuthApplicationInfo(oauthApplication);
PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
ClientInfo response = new ClientInfo();
response.setClientId(CLIENT_ID);
response.setClientName(APP_UUID);
response.setClientSecret(CLIENT_SECRET);
response.setRedirectUris(Arrays.asList(REDIRECT_URIS));
response.setGrantTypes(Arrays.asList(GRANT_TYPES));
Mockito.when(dcrClient.createApplication(Mockito.any(ClientInfo.class))).thenReturn(response);
PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
Mockito.when(privilegedCarbonContext.getTenantDomain()).thenReturn(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
Mockito.when(APIUtil.getApplicationUUID(Mockito.anyString(), Mockito.anyString())).thenReturn(APP_UUID);
OAuthApplicationInfo oauthApplicationResponse = keyManager.createApplication(oauthRequest);
Assert.assertEquals(StringUtils.join(REDIRECT_URIS, ","), oauthApplicationResponse.getCallBackURL());
Assert.assertEquals(APP_UUID, oauthApplicationResponse.getClientName());
}
use of org.wso2.carbon.apimgt.api.model.OAuthAppRequest in project carbon-apimgt by wso2.
the class ApplicationUtilsTestCase method testCrateOauthAppRequest.
@Test
public void testCrateOauthAppRequest() throws APIManagementException {
PowerMockito.mockStatic(KeyManagerHolder.class);
PowerMockito.when(KeyManagerHolder.getKeyManagerInstance("carbon.super", "default")).thenReturn(keyManager);
OAuthAppRequest oAuthAppRequest = ApplicationUtils.createOauthAppRequest("client1", "clientId", "http://foo.com", "subscribe", "details", "DEFAULT", "carbon.super", "default");
Assert.assertNotNull(oAuthAppRequest);
}
use of org.wso2.carbon.apimgt.api.model.OAuthAppRequest in project carbon-apimgt by wso2.
the class ApplicationRegistrationWSWorkflowExecutorTest method testFailureToCompleteApplicationRegistrationWSWFWhenKeyGenerationFailed.
@Test
public void testFailureToCompleteApplicationRegistrationWSWFWhenKeyGenerationFailed() throws Exception {
applicationRegistrationWSWorkflowExecutor.setUsername(adminUsername);
applicationRegistrationWSWorkflowExecutor.setPassword(adminPassword.toCharArray());
workflowDTO.setStatus(WorkflowStatus.APPROVED);
PowerMockito.doThrow(new APIManagementException("Error occurred when updating the status of the Application " + "Registration process")).when(keyManager).createApplication((OAuthAppRequest) Mockito.anyObject());
try {
applicationRegistrationWSWorkflowExecutor.complete(workflowDTO);
Assert.fail("Expected WorkflowException has not occurred while completing application registration " + "workflow");
} catch (WorkflowException e) {
Assert.assertEquals(e.getMessage(), "Error occurred when updating the status of the Application " + "Registration process");
}
}
Aggregations