use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerConfigurationDTO in project carbon-apimgt by wso2.
the class ApiMgtDAO method getKeyManagerConfigurationsByOrganization.
public List<KeyManagerConfigurationDTO> getKeyManagerConfigurationsByOrganization(String organization) throws APIManagementException {
List<KeyManagerConfigurationDTO> keyManagerConfigurationDTOS = new ArrayList<>();
final String query = "SELECT * FROM AM_KEY_MANAGER WHERE ORGANIZATION = ? ";
try (Connection conn = APIMgtDBUtil.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement(query)) {
preparedStatement.setString(1, organization);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
KeyManagerConfigurationDTO keyManagerConfigurationDTO = new KeyManagerConfigurationDTO();
String uuid = resultSet.getString("UUID");
keyManagerConfigurationDTO.setUuid(uuid);
keyManagerConfigurationDTO.setName(resultSet.getString("NAME"));
keyManagerConfigurationDTO.setDisplayName(resultSet.getString("DISPLAY_NAME"));
keyManagerConfigurationDTO.setDescription(resultSet.getString("DESCRIPTION"));
keyManagerConfigurationDTO.setType(resultSet.getString("TYPE"));
keyManagerConfigurationDTO.setEnabled(resultSet.getBoolean("ENABLED"));
keyManagerConfigurationDTO.setOrganization(organization);
keyManagerConfigurationDTO.setTokenType(resultSet.getString("TOKEN_TYPE"));
keyManagerConfigurationDTO.setExternalReferenceId(resultSet.getString("EXTERNAL_REFERENCE_ID"));
try (InputStream configuration = resultSet.getBinaryStream("CONFIGURATION")) {
String configurationContent = IOUtils.toString(configuration);
Map map = new Gson().fromJson(configurationContent, Map.class);
keyManagerConfigurationDTO.setAdditionalProperties(map);
} catch (IOException e) {
log.error("Error while converting configurations in " + uuid, e);
}
keyManagerConfigurationDTOS.add(keyManagerConfigurationDTO);
}
}
} catch (SQLException e) {
throw new APIManagementException("Error while retrieving key manager configurations for organization " + organization, e);
}
return keyManagerConfigurationDTOS;
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerConfigurationDTO in project carbon-apimgt by wso2.
the class ApiMgtDAO method updateKeyManagerConfiguration.
public void updateKeyManagerConfiguration(KeyManagerConfigurationDTO keyManagerConfigurationDTO) throws APIManagementException {
try (Connection conn = APIMgtDBUtil.getConnection()) {
conn.setAutoCommit(false);
try (PreparedStatement preparedStatement = conn.prepareStatement(SQLConstants.KeyManagerSqlConstants.UPDATE_KEY_MANAGER)) {
preparedStatement.setString(1, keyManagerConfigurationDTO.getName());
preparedStatement.setString(2, keyManagerConfigurationDTO.getDescription());
preparedStatement.setString(3, keyManagerConfigurationDTO.getType());
String configurationJson = new Gson().toJson(keyManagerConfigurationDTO.getAdditionalProperties());
preparedStatement.setBinaryStream(4, new ByteArrayInputStream(configurationJson.getBytes()));
preparedStatement.setString(5, keyManagerConfigurationDTO.getOrganization());
preparedStatement.setBoolean(6, keyManagerConfigurationDTO.isEnabled());
preparedStatement.setString(7, keyManagerConfigurationDTO.getDisplayName());
preparedStatement.setString(8, keyManagerConfigurationDTO.getTokenType());
preparedStatement.setString(9, keyManagerConfigurationDTO.getExternalReferenceId());
preparedStatement.setString(10, keyManagerConfigurationDTO.getUuid());
preparedStatement.executeUpdate();
conn.commit();
} catch (SQLException e) {
conn.rollback();
throw e;
}
} catch (SQLException e) {
throw new APIManagementException("Error while Updating key manager configuration with name " + keyManagerConfigurationDTO.getName() + " in tenant " + keyManagerConfigurationDTO.getOrganization(), e);
}
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerConfigurationDTO in project carbon-apimgt by wso2.
the class KeymanagersApiServiceImpl method toKeyManagerDTO.
public static KeyManagerDTO toKeyManagerDTO(String tenantDomain, KeyManagerConfigurationDTO keyManagerConfigurationDTO) {
KeyManagerDTO keyManagerDTO = new KeyManagerDTO();
keyManagerDTO.setEnabled(keyManagerConfigurationDTO.isEnabled());
keyManagerDTO.setName(keyManagerConfigurationDTO.getName());
keyManagerDTO.setTenantDomain(tenantDomain);
keyManagerDTO.setType(keyManagerConfigurationDTO.getType());
keyManagerDTO.setTokenType(KeyManagerDTO.TokenTypeEnum.fromValue(keyManagerConfigurationDTO.getTokenType()));
keyManagerDTO.setConfiguration(keyManagerConfigurationDTO.getAdditionalProperties());
return keyManagerDTO;
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerConfigurationDTO in project carbon-apimgt by wso2.
the class APIProviderImpl method validateKeyManagers.
private void validateKeyManagers(API api) throws APIManagementException {
List<KeyManagerConfigurationDTO> keyManagerConfigurationsByTenant = apiMgtDAO.getKeyManagerConfigurationsByOrganization(tenantDomain);
List<String> configuredMissingKeyManagers = new ArrayList<>();
for (String keyManager : api.getKeyManagers()) {
if (!APIConstants.KeyManager.API_LEVEL_ALL_KEY_MANAGERS.equals(keyManager)) {
KeyManagerConfigurationDTO selectedKeyManager = null;
for (KeyManagerConfigurationDTO keyManagerConfigurationDTO : keyManagerConfigurationsByTenant) {
if (keyManager.equals(keyManagerConfigurationDTO.getName())) {
selectedKeyManager = keyManagerConfigurationDTO;
break;
}
}
if (selectedKeyManager == null) {
configuredMissingKeyManagers.add(keyManager);
}
}
}
if (!configuredMissingKeyManagers.isEmpty()) {
throw new APIManagementException("Key Manager(s) Not found :" + String.join(" , ", configuredMissingKeyManagers), ExceptionCodes.KEY_MANAGER_NOT_REGISTERED);
}
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerConfigurationDTO in project carbon-apimgt by wso2.
the class AbstractAPIManager method getApplicationKeys.
/**
* Returns the key associated with given application id.
*
* @param applicationId Id of the Application.
* @return APIKey The key of the application.
* @throws APIManagementException
*/
protected Set<APIKey> getApplicationKeys(int applicationId, String xWso2Tenant) throws APIManagementException {
Set<APIKey> apiKeyList = apiMgtDAO.getKeyMappingsFromApplicationId(applicationId);
if (StringUtils.isNotEmpty(xWso2Tenant)) {
int tenantId = APIUtil.getInternalOrganizationId(xWso2Tenant);
// To handle choreo scenario. due to keymanagers are not per organization atm. using ST
if (tenantId == MultitenantConstants.SUPER_TENANT_ID) {
xWso2Tenant = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
}
}
Set<APIKey> resultantApiKeyList = new HashSet<>();
for (APIKey apiKey : apiKeyList) {
String keyManagerName = apiKey.getKeyManager();
String consumerKey = apiKey.getConsumerKey();
String tenantDomain = this.tenantDomain;
if (StringUtils.isNotEmpty(xWso2Tenant)) {
tenantDomain = xWso2Tenant;
}
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.");
continue;
}
}
if (tenantDomain != null && !tenantDomain.equalsIgnoreCase(keyManagerConfigurationDTO.getOrganization())) {
continue;
}
KeyManager keyManager = null;
if (keyManagerConfigurationDTO.isEnabled()) {
keyManager = KeyManagerHolder.getKeyManagerInstance(tenantDomain, keyManagerName);
} else {
continue;
}
apiKey.setKeyManager(keyManagerConfigurationDTO.getName());
if (StringUtils.isNotEmpty(consumerKey)) {
if (keyManager != null) {
if (APIConstants.OAuthAppMode.MAPPED.name().equalsIgnoreCase(apiKey.getCreateMode()) && !isOauthAppValidation()) {
resultantApiKeyList.add(apiKey);
} else {
OAuthApplicationInfo oAuthApplicationInfo = null;
try {
oAuthApplicationInfo = keyManager.retrieveApplication(consumerKey);
} catch (APIManagementException e) {
log.error("Error while retrieving Application Information", e);
continue;
}
if (StringUtils.isNotEmpty(apiKey.getAppMetaData())) {
OAuthApplicationInfo storedOAuthApplicationInfo = new Gson().fromJson(apiKey.getAppMetaData(), OAuthApplicationInfo.class);
if (oAuthApplicationInfo == null) {
oAuthApplicationInfo = storedOAuthApplicationInfo;
} else {
if (StringUtils.isEmpty(oAuthApplicationInfo.getCallBackURL())) {
oAuthApplicationInfo.setCallBackURL(storedOAuthApplicationInfo.getCallBackURL());
}
if ("null".equalsIgnoreCase(oAuthApplicationInfo.getCallBackURL())) {
oAuthApplicationInfo.setCallBackURL("");
}
if (oAuthApplicationInfo.getParameter(APIConstants.JSON_GRANT_TYPES) == null && storedOAuthApplicationInfo.getParameter(APIConstants.JSON_GRANT_TYPES) != null) {
if (storedOAuthApplicationInfo.getParameter(APIConstants.JSON_GRANT_TYPES) instanceof String) {
oAuthApplicationInfo.addParameter(APIConstants.JSON_GRANT_TYPES, ((String) storedOAuthApplicationInfo.getParameter(APIConstants.JSON_GRANT_TYPES)).replace(",", " "));
} else {
oAuthApplicationInfo.addParameter(APIConstants.JSON_GRANT_TYPES, storedOAuthApplicationInfo.getParameter(APIConstants.JSON_GRANT_TYPES));
}
}
if (StringUtils.isEmpty(oAuthApplicationInfo.getClientSecret()) && StringUtils.isNotEmpty(storedOAuthApplicationInfo.getClientSecret())) {
oAuthApplicationInfo.setClientSecret(storedOAuthApplicationInfo.getClientSecret());
}
}
}
AccessTokenInfo tokenInfo = keyManager.getAccessTokenByConsumerKey(consumerKey);
if (oAuthApplicationInfo != null) {
apiKey.setConsumerSecret(oAuthApplicationInfo.getClientSecret());
apiKey.setCallbackUrl(oAuthApplicationInfo.getCallBackURL());
apiKey.setGrantTypes((String) oAuthApplicationInfo.getParameter(APIConstants.JSON_GRANT_TYPES));
if (oAuthApplicationInfo.getParameter(APIConstants.JSON_ADDITIONAL_PROPERTIES) != null) {
apiKey.setAdditionalProperties(oAuthApplicationInfo.getParameter(APIConstants.JSON_ADDITIONAL_PROPERTIES));
}
}
if (tokenInfo != null) {
apiKey.setAccessToken(tokenInfo.getAccessToken());
apiKey.setValidityPeriod(tokenInfo.getValidityPeriod());
} else {
if (log.isDebugEnabled()) {
log.debug("Access token does not exist for Consumer Key: " + consumerKey);
}
}
resultantApiKeyList.add(apiKey);
}
} else {
log.error("Key Manager " + keyManagerName + " not initialized in tenant " + tenantDomain);
}
} else {
resultantApiKeyList.add(apiKey);
}
}
return resultantApiKeyList;
}
Aggregations