use of org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class ApplicationsApiServiceImplTestCase method testApplicationsApplicationIdKeysKeyTypePut.
@Test
public void testApplicationsApplicationIdKeysKeyTypePut() throws APIManagementException, NotFoundException {
TestUtil.printTestMethodName();
String applicationId = UUID.randomUUID().toString();
String accessToken = UUID.randomUUID().toString();
String clientID = UUID.randomUUID().toString();
String clientSecret = UUID.randomUUID().toString();
String keyType = "PRODUCTION";
ApplicationsApiServiceImpl applicationsApiService = new ApplicationsApiServiceImpl();
APIStore apiStore = Mockito.mock(APIStoreImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
Request request = getRequest();
PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
ApplicationTokenDTO applicationTokenDTO = new ApplicationTokenDTO();
applicationTokenDTO.setAccessToken(accessToken);
applicationTokenDTO.setTokenScopes("SCOPE1");
applicationTokenDTO.setValidityTime((long) 100000);
List<String> grantTypes = new ArrayList<>();
grantTypes.add("password");
grantTypes.add("jwt");
ApplicationKeysDTO applicationKeysDTO = new ApplicationKeysDTO();
applicationKeysDTO.setConsumerKey(clientID);
applicationKeysDTO.setConsumerSecret(clientSecret);
applicationKeysDTO.setKeyType(ApplicationKeysDTO.KeyTypeEnum.PRODUCTION);
applicationKeysDTO.setCallbackUrl(null);
applicationKeysDTO.setSupportedGrantTypes(grantTypes);
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
oAuthApplicationInfo.setKeyType(keyType);
oAuthApplicationInfo.setClientId(UUID.randomUUID().toString());
oAuthApplicationInfo.setClientSecret(UUID.randomUUID().toString());
oAuthApplicationInfo.setGrantTypes(grantTypes);
Mockito.when(apiStore.updateGrantTypesAndCallbackURL(applicationId, keyType, grantTypes, null)).thenReturn(oAuthApplicationInfo);
Response response = applicationsApiService.applicationsApplicationIdKeysKeyTypePut(applicationId, keyType, applicationKeysDTO, request);
Assert.assertEquals(200, response.getStatus());
}
use of org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class ApplicationKeyMappingUtil method fromApplicationKeysToDTO.
public static ApplicationKeysDTO fromApplicationKeysToDTO(OAuthApplicationInfo applicationKeys) {
ApplicationKeysDTO applicationKeyDTO = new ApplicationKeysDTO();
applicationKeyDTO.setKeyType(ApplicationKeysDTO.KeyTypeEnum.fromValue(applicationKeys.getKeyType()));
applicationKeyDTO.setConsumerKey(applicationKeys.getClientId());
applicationKeyDTO.setConsumerSecret(applicationKeys.getClientSecret());
applicationKeyDTO.setSupportedGrantTypes(applicationKeys.getGrantTypes());
return applicationKeyDTO;
}
use of org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class DefaultKeyManagerImpl method getOAuthApplicationInfo.
private OAuthApplicationInfo getOAuthApplicationInfo(Response response) throws IOException {
OAuthApplicationInfo oAuthApplicationInfoResponse = new OAuthApplicationInfo();
DCRClientInfo dcrClientInfoResponse = (DCRClientInfo) new GsonDecoder().decode(response, DCRClientInfo.class);
oAuthApplicationInfoResponse.setClientName(dcrClientInfoResponse.getClientName());
oAuthApplicationInfoResponse.setClientId(dcrClientInfoResponse.getClientId());
oAuthApplicationInfoResponse.setClientSecret(dcrClientInfoResponse.getClientSecret());
oAuthApplicationInfoResponse.setGrantTypes(dcrClientInfoResponse.getGrantTypes());
oAuthApplicationInfoResponse.setCallBackURL(dcrClientInfoResponse.getRedirectURIs().get(0));
return oAuthApplicationInfoResponse;
}
use of org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class DefaultKeyManagerImpl method updateApplication.
@Override
public OAuthApplicationInfo updateApplication(OAuthApplicationInfo oAuthApplicationInfo) throws KeyManagementException {
if (log.isDebugEnabled()) {
log.debug("Updating OAuth2 application with : " + oAuthApplicationInfo.toString());
}
String applicationName = oAuthApplicationInfo.getClientName();
String keyType = (String) oAuthApplicationInfo.getParameter(KeyManagerConstants.APP_KEY_TYPE);
if (keyType != null) {
// Derive oauth2 app name based on key type and user input for app name
applicationName = applicationName + '_' + keyType;
}
DCRClientInfo dcrClientInfo = new DCRClientInfo();
dcrClientInfo.setClientName(applicationName);
dcrClientInfo.setClientId(oAuthApplicationInfo.getClientId());
dcrClientInfo.setClientSecret(oAuthApplicationInfo.getClientSecret());
dcrClientInfo.addCallbackUrl(oAuthApplicationInfo.getCallBackURL());
dcrClientInfo.setGrantTypes(oAuthApplicationInfo.getGrantTypes());
Response response = dcrmServiceStub.updateApplication(dcrClientInfo, dcrClientInfo.getClientId());
if (response == null) {
throw new KeyManagementException("Error occurred while updating DCR application. Response is null", ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
}
if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
// 200 - Success
try {
OAuthApplicationInfo oAuthApplicationInfoResponse = getOAuthApplicationInfo(response);
// setting original parameter list
oAuthApplicationInfoResponse.setParameters(oAuthApplicationInfo.getParameters());
if (log.isDebugEnabled()) {
log.debug("OAuth2 application updated: " + oAuthApplicationInfoResponse.toString());
}
return oAuthApplicationInfoResponse;
} catch (IOException e) {
throw new KeyManagementException("Error occurred while parsing the DCR application update response " + "message.", e, ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
}
} else if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_400_BAD_REQUEST) {
// 400 - Known Error
try {
DCRError error = (DCRError) new GsonDecoder().decode(response, DCRError.class);
throw new KeyManagementException("Error occurred while updating DCR application. Error: " + error.getError() + ". Error Description: " + error.getErrorDescription() + ". Status Code: " + response.status(), ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
} catch (IOException e) {
throw new KeyManagementException("Error occurred while parsing the DCR error message.", e, ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
}
} else {
// Unknown Error
throw new KeyManagementException("Error occurred while updating DCR application. Error: " + response.body().toString() + " Status Code: " + response.status(), ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
}
}
use of org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class APIStoreImpl method updateGrantTypesAndCallbackURL.
@Override
public OAuthApplicationInfo updateGrantTypesAndCallbackURL(String applicationId, String keyType, List<String> grantTypes, String callbackURL) throws APIManagementException {
if (log.isDebugEnabled()) {
log.debug("Updating " + keyType + " grant type/callback of App: " + applicationId);
}
if (StringUtils.isEmpty(applicationId) || StringUtils.isEmpty(keyType)) {
String msg = "One of input values is null or empty. Application Id: " + applicationId + " Key Type: " + keyType;
log.error(msg);
throw new APIManagementException(msg, ExceptionCodes.OAUTH2_APP_RETRIEVAL_FAILED);
}
if (grantTypes == null || grantTypes.isEmpty() || StringUtils.isEmpty(callbackURL)) {
String msg = "Both Grant Types list and Callback URL can't be null or empty at once.";
log.error(msg);
throw new APIManagementException(msg, ExceptionCodes.OAUTH2_APP_RETRIEVAL_FAILED);
}
try {
OAuthApplicationInfo appFromDB = getApplicationDAO().getApplicationKeys(applicationId, keyType);
OAuthApplicationInfo oAuthApp = getKeyManager().retrieveApplication(appFromDB.getClientId());
oAuthApp.setGrantTypes(grantTypes);
oAuthApp.setCallBackURL(callbackURL);
oAuthApp = getKeyManager().updateApplication(oAuthApp);
if (log.isDebugEnabled()) {
log.debug("Updated " + keyType + " grant type/callback of App: " + applicationId);
}
return oAuthApp;
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while updating " + keyType + " grant type/callback of application: " + applicationId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
}
Aggregations