Search in sources :

Example 21 with OAuthApplicationInfo

use of org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo in project carbon-apimgt by wso2.

the class ApplicationMappingUtil method fromApplicationToDTO.

public static ApplicationDTO fromApplicationToDTO(Application application) {
    ApplicationDTO applicationDTO = new ApplicationDTO();
    applicationDTO.setApplicationId(application.getId());
    applicationDTO.setThrottlingTier(application.getPolicy().getPolicyName());
    applicationDTO.setDescription(application.getDescription());
    applicationDTO.setName(application.getName());
    applicationDTO.setLifeCycleStatus(application.getStatus());
    applicationDTO.setPermission(application.getPermissionString());
    applicationDTO.setSubscriber(application.getCreatedUser());
    List<ApplicationKeysDTO> applicationKeyDTOs = new ArrayList<>();
    for (OAuthApplicationInfo applicationKeys : application.getApplicationKeys()) {
        ApplicationKeysDTO applicationKeysDTO = ApplicationKeyMappingUtil.fromApplicationKeysToDTO(applicationKeys);
        applicationKeyDTOs.add(applicationKeysDTO);
    }
    applicationDTO.setToken(ApplicationKeyMappingUtil.fromApplicationTokenToDTO(application.getApplicationToken()));
    applicationDTO.setKeys(applicationKeyDTOs);
    return applicationDTO;
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationDTO) ApplicationKeysDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) ArrayList(java.util.ArrayList)

Example 22 with OAuthApplicationInfo

use of org.wso2.carbon.apimgt.core.models.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 23 with OAuthApplicationInfo

use of org.wso2.carbon.apimgt.core.models.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 24 with OAuthApplicationInfo

use of org.wso2.carbon.apimgt.core.models.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 25 with OAuthApplicationInfo

use of org.wso2.carbon.apimgt.core.models.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)

Aggregations

OAuthApplicationInfo (org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo)30 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)15 ArrayList (java.util.ArrayList)12 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)10 Test (org.junit.Test)9 ApplicationKeysDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO)9 KeyManagementException (org.wso2.carbon.apimgt.core.exception.KeyManagementException)8 HashMap (java.util.HashMap)7 DCRClientInfo (org.wso2.carbon.apimgt.core.auth.dto.DCRClientInfo)6 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)6 Response (feign.Response)5 Map (java.util.Map)5 Response (javax.ws.rs.core.Response)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)5 OAuth2IntrospectionResponse (org.wso2.carbon.apimgt.core.auth.dto.OAuth2IntrospectionResponse)5 ApplicationCreationResponse (org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse)5 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)5 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)5 Request (org.wso2.msf4j.Request)5