use of org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class ApplicationRegistrationWSWorkflowExecutorTest method init.
@Before
public void init() throws Exception {
ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
ConfigurationContextService configurationContextService = Mockito.mock(ConfigurationContextService.class);
configurationContext = Mockito.mock(ConfigurationContext.class);
PowerMockito.when(serviceReferenceHolder.getContextService()).thenReturn(configurationContextService);
PowerMockito.when(configurationContextService.getClientConfigContext()).thenReturn(configurationContext);
serviceClient = Mockito.mock(ServiceClient.class);
PowerMockito.whenNew(ServiceClient.class).withAnyArguments().thenReturn(serviceClient);
applicationRegistrationWSWorkflowExecutor = new ApplicationRegistrationWSWorkflowExecutor();
apiMgtDAO = TestUtils.getApiMgtDAO();
application = new Application("test", new Subscriber("testUser"));
application.setCallbackUrl(callBaclURL);
application.setTier("Unlimited");
PowerMockito.mockStatic(KeyManagerHolder.class);
keyManager = Mockito.mock(KeyManager.class);
KeyManagerConfiguration keyManagerConfiguration = new KeyManagerConfiguration();
Mockito.when(keyManager.getKeyManagerConfiguration()).thenReturn(keyManagerConfiguration);
PowerMockito.when(KeyManagerHolder.getKeyManagerInstance("carbon.super", "default")).thenReturn(keyManager);
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
PowerMockito.when(keyManager.createApplication((OAuthAppRequest) Mockito.anyObject())).thenReturn(oAuthApplicationInfo);
OAuthAppRequest oAuthAppRequest = new OAuthAppRequest();
oAuthAppRequest.setOAuthApplicationInfo(oAuthApplicationInfo);
workflowDTO = new ApplicationRegistrationWorkflowDTO();
workflowDTO.setWorkflowReference("1");
workflowDTO.setApplication(application);
workflowDTO.setCallbackUrl(callBaclURL);
workflowDTO.setTenantDomain("carbon.super");
workflowDTO.setUserName("testUser");
workflowDTO.setExternalWorkflowReference("testUser");
workflowDTO.setKeyType("PRODUCTION");
workflowDTO.setAppInfoDTO(oAuthAppRequest);
KeyManagerConfigurationDTO kmConfigDTO = new KeyManagerConfigurationDTO();
kmConfigDTO.setOrganization("carbon.super");
kmConfigDTO.setName("default");
PowerMockito.when(apiMgtDAO.getKeyManagerConfigurationByUUID("default")).thenReturn(kmConfigDTO);
}
use of org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class AMDefaultKeyManagerImplTest method testCreateApplicationWithException.
@Test(expected = APIManagementException.class)
public void testCreateApplicationWithException() throws APIManagementException {
Mockito.when(APIUtil.getApplicationUUID(Mockito.anyString(), Mockito.anyString())).thenReturn(APP_UUID);
OAuthAppRequest oauthRequest = new OAuthAppRequest();
OAuthApplicationInfo oauthApplication = new OAuthApplicationInfo();
oauthRequest.setOAuthApplicationInfo(oauthApplication);
keyManager.createApplication(oauthRequest);
}
use of org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class AMDefaultKeyManagerImplTest method testCreateApplication.
@Test
public void testCreateApplication() throws APIManagementException, KeyManagerClientException {
PowerMockito.mockStatic(APIUtil.class);
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(APP_NAME);
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);
PowerMockito.when(APIUtil.isCrossTenantSubscriptionsEnabled()).thenReturn(false);
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.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class ApiMgtDAO method getClientOfApplication.
private Map<String, OAuthApplicationInfo> getClientOfApplication(String tenntDomain, int applicationID, String keyType) throws APIManagementException {
String sqlQuery = SQLConstants.GET_CLIENT_OF_APPLICATION_SQL;
Map<String, OAuthApplicationInfo> keyTypeWiseOAuthApps = new HashMap<>();
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection = APIMgtDBUtil.getConnection();
ps = connection.prepareStatement(sqlQuery);
ps.setInt(1, applicationID);
ps.setString(2, keyType);
rs = ps.executeQuery();
while (rs.next()) {
String consumerKey = rs.getString("CONSUMER_KEY");
String keyManagerName = rs.getString("KEY_MANAGER");
if (consumerKey != null) {
KeyManager keyManager = KeyManagerHolder.getKeyManagerInstance(tenntDomain, keyManagerName);
if (keyManager != null) {
OAuthApplicationInfo oAuthApplication = keyManager.retrieveApplication(consumerKey);
keyTypeWiseOAuthApps.put(keyManagerName, oAuthApplication);
}
}
}
} catch (SQLException e) {
handleException("Failed to get client of application. SQL error", e);
} finally {
APIMgtDBUtil.closeAllConnections(ps, connection, rs);
}
return keyTypeWiseOAuthApps;
}
use of org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class ApiMgtDAO method getApplicationById.
public Application getApplicationById(int applicationId, String userId, String groupId) throws APIManagementException {
Connection connection = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
Application application = null;
try {
connection = APIMgtDBUtil.getConnection();
String query = SQLConstants.GET_APPLICATION_BY_ID_SQL;
String whereClause = " AND SUB.USER_ID =?";
String whereClauseCaseInSensitive = " AND LOWER(SUB.USER_ID) =LOWER(?)";
String whereClauseWithGroupId = " AND (APP.GROUP_ID = ? OR ((APP.GROUP_ID='' OR APP.GROUP_ID IS NULL)" + " AND SUB.USER_ID = ?))";
String whereClauseWithGroupIdCaseInSensitive = " AND (APP.GROUP_ID = ? OR ((APP.GROUP_ID='' OR APP" + ".GROUP_ID IS NULL)" + " AND LOWER(SUB.USER_ID) = LOWER(?)))";
String whereClauseWithMultiGroupId = " AND ((APP.APPLICATION_ID IN (SELECT APPLICATION_ID FROM " + "AM_APPLICATION_GROUP_MAPPING WHERE GROUP_ID IN ($params) AND TENANT = ?)) OR SUB.USER_ID = ? )";
String whereClauseWithMultiGroupIdCaseInSensitive = " AND ((APP.APPLICATION_ID IN (SELECT " + "APPLICATION_ID FROM " + "AM_APPLICATION_GROUP_MAPPING WHERE GROUP_ID IN ($params) AND TENANT = ?)) OR LOWER(SUB" + ".USER_ID) = LOWER(?) )";
if (groupId != null && !"null".equals(groupId) && !groupId.isEmpty()) {
if (multiGroupAppSharingEnabled) {
Subscriber subscriber = getSubscriber(userId);
String tenantDomain = MultitenantUtils.getTenantDomain(subscriber.getName());
if (forceCaseInsensitiveComparisons) {
query = query + whereClauseWithMultiGroupIdCaseInSensitive;
} else {
query = query + whereClauseWithMultiGroupId;
}
String[] groupIds = groupId.split(",");
// since index 1 is applicationId
int parameterIndex = groupIds.length + 1;
// query params will fil from 2
prepStmt = fillQueryParams(connection, query, groupIds, 2);
prepStmt.setString(++parameterIndex, tenantDomain);
prepStmt.setInt(1, applicationId);
prepStmt.setString(++parameterIndex, userId);
} else {
if (forceCaseInsensitiveComparisons) {
query = query + whereClauseWithGroupIdCaseInSensitive;
} else {
query = query + whereClauseWithGroupId;
}
prepStmt = connection.prepareStatement(query);
prepStmt.setInt(1, applicationId);
prepStmt.setString(2, groupId);
prepStmt.setString(3, userId);
}
} else {
if (forceCaseInsensitiveComparisons) {
query = query + whereClauseCaseInSensitive;
} else {
query = query + whereClause;
}
prepStmt = connection.prepareStatement(query);
prepStmt.setInt(1, applicationId);
prepStmt.setString(2, userId);
}
rs = prepStmt.executeQuery();
if (rs.next()) {
String applicationName = rs.getString("NAME");
String subscriberId = rs.getString("SUBSCRIBER_ID");
String subscriberName = rs.getString("USER_ID");
Subscriber subscriber = new Subscriber(subscriberName);
subscriber.setId(Integer.parseInt(subscriberId));
application = new Application(applicationName, subscriber);
application.setOwner(rs.getString("CREATED_BY"));
application.setDescription(rs.getString("DESCRIPTION"));
application.setStatus(rs.getString("APPLICATION_STATUS"));
application.setCallbackUrl(rs.getString("CALLBACK_URL"));
application.setId(rs.getInt("APPLICATION_ID"));
application.setGroupId(rs.getString("GROUP_ID"));
application.setUUID(rs.getString("UUID"));
application.setTier(rs.getString("APPLICATION_TIER"));
subscriber.setId(rs.getInt("SUBSCRIBER_ID"));
String tenantDomain = MultitenantUtils.getTenantDomain(subscriberName);
Map<String, Map<String, OAuthApplicationInfo>> keyMap = getOAuthApplications(tenantDomain, application.getId());
application.getKeyManagerWiseOAuthApp().putAll(keyMap);
if (multiGroupAppSharingEnabled) {
if (application.getGroupId() == null || application.getGroupId().isEmpty()) {
application.setGroupId(getGroupId(connection, applicationId));
}
}
}
if (application != null) {
Map<String, String> applicationAttributes = getApplicationAttributes(connection, applicationId);
application.setApplicationAttributes(applicationAttributes);
}
} catch (SQLException e) {
handleException("Error while obtaining details of the Application : " + applicationId, e);
} finally {
APIMgtDBUtil.closeAllConnections(prepStmt, connection, rs);
}
return application;
}
Aggregations