Search in sources :

Example 41 with OAuthApplicationInfo

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);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 42 with OAuthApplicationInfo

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;
}
Also used : AccessTokenRequest(org.wso2.carbon.apimgt.core.models.AccessTokenRequest) KeyManagementException(org.wso2.carbon.apimgt.core.exception.KeyManagementException)

Example 43 with OAuthApplicationInfo

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());
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) ApplicationCreationResponse(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse) Response(javax.ws.rs.core.Response) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) Request(org.wso2.msf4j.Request) ArrayList(java.util.ArrayList) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 44 with OAuthApplicationInfo

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());
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) ApplicationCreationResponse(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse) Response(javax.ws.rs.core.Response) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) Request(org.wso2.msf4j.Request) ArrayList(java.util.ArrayList) ApplicationKeyGenerateRequestDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeyGenerateRequestDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 45 with OAuthApplicationInfo

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);
}
Also used : ApplicationKeysDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

OAuthApplicationInfo (org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo)37 OAuthApplicationInfo (org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo)30 Test (org.junit.Test)22 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)21 HashMap (java.util.HashMap)19 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)18 OAuthAppRequest (org.wso2.carbon.apimgt.api.model.OAuthAppRequest)15 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)15 ArrayList (java.util.ArrayList)13 Map (java.util.Map)13 KeyManagerConfigurationDTO (org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO)11 Application (org.wso2.carbon.apimgt.api.model.Application)11 KeyManager (org.wso2.carbon.apimgt.api.model.KeyManager)10 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)10 JsonObject (com.google.gson.JsonObject)9 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)9 ApplicationKeysDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO)9 JSONObject (org.json.simple.JSONObject)8 AccessTokenRequest (org.wso2.carbon.apimgt.api.model.AccessTokenRequest)8 Gson (com.google.gson.Gson)7