use of org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class ApplicationDAOImpl method getApplicationKeys.
@Override
public OAuthApplicationInfo getApplicationKeys(String appId, String keyType) throws APIMgtDAOException {
final String getApplicationKeysQuery = "SELECT CLIENT_ID FROM AM_APP_KEY_MAPPING " + "WHERE APPLICATION_ID = ? AND KEY_TYPE = ?";
try (Connection conn = DAOUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(getApplicationKeysQuery)) {
ps.setString(1, appId);
ps.setString(2, keyType);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
OAuthApplicationInfo appKeys = new OAuthApplicationInfo();
appKeys.setClientId(rs.getString("CLIENT_ID"));
appKeys.setKeyType(keyType);
return appKeys;
} else {
throw new APIMgtDAOException("Application Key mapping not found", ExceptionCodes.APPLICATION_KEY_MAPPING_NOT_FOUND);
}
}
} catch (SQLException ex) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + String.format("getting application keys(appId: %s, keyType: %s)", appId, keyType), ex);
}
}
use of org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class ApplicationUtils method createAccessTokenRequest.
public static AccessTokenRequest createAccessTokenRequest(OAuthApplicationInfo oAuthApplication) throws APIManagementException {
AccessTokenRequest tokenRequest = new AccessTokenRequest();
if (oAuthApplication.getClientId() != null || oAuthApplication.getClientSecret() != null) {
tokenRequest.setClientId(oAuthApplication.getClientId());
tokenRequest.setClientSecret(oAuthApplication.getClientSecret());
} else {
throw new KeyManagementException("Consumer key or Consumer Secret is missing.");
}
if (oAuthApplication.getParameter(KeyManagerConstants.TOKEN_SCOPES) != null) {
String tokenScopes = (String) oAuthApplication.getParameter(KeyManagerConstants.TOKEN_SCOPES);
tokenRequest.setScopes(tokenScopes);
oAuthApplication.addParameter(KeyManagerConstants.OAUTH_CLIENT_TOKEN_SCOPE, tokenScopes);
}
tokenRequest.setGrantType(KeyManagerConstants.CLIENT_CREDENTIALS_GRANT_TYPE);
if (oAuthApplication.getParameter(KeyManagerConstants.VALIDITY_PERIOD) != null) {
tokenRequest.setValidityPeriod(Long.parseLong((String) oAuthApplication.getParameter(KeyManagerConstants.VALIDITY_PERIOD)));
} else {
throw new KeyManagementException("Validity period missing for generated oAuth keys");
}
return tokenRequest;
}
use of org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class ApplicationsApiServiceImplTestCase method testApplicationsApplicationIdKeysGet.
@Test
public void testApplicationsApplicationIdKeysGet() throws APIManagementException, NotFoundException {
TestUtil.printTestMethodName();
String applicationId = UUID.randomUUID().toString();
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);
List<OAuthApplicationInfo> oAuthApplicationInfoList = new ArrayList<>();
Mockito.when(apiStore.getApplicationKeys(applicationId)).thenReturn(oAuthApplicationInfoList);
Response response = applicationsApiService.applicationsApplicationIdKeysGet(applicationId, request);
Assert.assertEquals(200, response.getStatus());
}
use of org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class ApplicationsApiServiceImplTestCase method testApplicationsApplicationIdGenerateKeysPost.
@Test
public void testApplicationsApplicationIdGenerateKeysPost() throws APIManagementException, NotFoundException {
TestUtil.printTestMethodName();
String applicationId = UUID.randomUUID().toString();
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);
List<String> grantTypes = new ArrayList<>();
grantTypes.add("password");
grantTypes.add("jwt");
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
oAuthApplicationInfo.setKeyType("PRODUCTION");
oAuthApplicationInfo.setClientId(UUID.randomUUID().toString());
oAuthApplicationInfo.setClientSecret(UUID.randomUUID().toString());
oAuthApplicationInfo.setGrantTypes(grantTypes);
Mockito.when(apiStore.generateApplicationKeys(applicationId, "PRODUCTION", null, grantTypes)).thenReturn(oAuthApplicationInfo);
ApplicationKeyGenerateRequestDTO applicationKeyGenerateRequestDTO = new ApplicationKeyGenerateRequestDTO();
applicationKeyGenerateRequestDTO.setKeyType(ApplicationKeyGenerateRequestDTO.KeyTypeEnum.PRODUCTION);
applicationKeyGenerateRequestDTO.setCallbackUrl(null);
applicationKeyGenerateRequestDTO.setGrantTypesToBeSupported(grantTypes);
Response response = applicationsApiService.applicationsApplicationIdGenerateKeysPost(applicationId, applicationKeyGenerateRequestDTO, request);
Assert.assertEquals(200, response.getStatus());
}
use of org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class ApplicationKeyMappingUtilTestCase method testFromApplicationKeysToDTO.
@Test
public void testFromApplicationKeysToDTO() {
String keyType = "PRODUCTION";
List<String> grantTypes = new ArrayList<>();
grantTypes.add("password");
grantTypes.add("jwt");
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
oAuthApplicationInfo.setKeyType(keyType);
oAuthApplicationInfo.setClientId("clientID");
oAuthApplicationInfo.setClientSecret("clientSecret");
oAuthApplicationInfo.setGrantTypes(grantTypes);
ApplicationKeysDTO applicationKeysDTO = ApplicationKeyMappingUtil.fromApplicationKeysToDTO(oAuthApplicationInfo);
Assert.assertEquals(applicationKeysDTO.getKeyType().toString(), keyType);
}
Aggregations