Search in sources :

Example 21 with AccessTokenRequest

use of org.wso2.carbon.apimgt.api.model.AccessTokenRequest in project carbon-apimgt by wso2.

the class ApplicationUtilsTestCase method testCreateTokenRequestWhenKeyManagerNull.

@Test
public void testCreateTokenRequestWhenKeyManagerNull() throws APIManagementException {
    PowerMockito.mockStatic(KeyManagerHolder.class);
    AccessTokenRequest accessTokenRequest = new AccessTokenRequest();
    OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
    AccessTokenRequest result = ApplicationUtils.createAccessTokenRequest(null, oAuthApplicationInfo, accessTokenRequest);
    Assert.assertNull(result);
    Mockito.verify(keyManager, Mockito.times(0)).buildAccessTokenRequestFromOAuthApp(Matchers.any(OAuthApplicationInfo.class), Matchers.any(AccessTokenRequest.class));
}
Also used : OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) AccessTokenRequest(org.wso2.carbon.apimgt.api.model.AccessTokenRequest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 22 with AccessTokenRequest

use of org.wso2.carbon.apimgt.api.model.AccessTokenRequest in project carbon-apimgt by wso2.

the class ApplicationUtilsTestCase method testPopulateTokenRequestWhenKeyManagerNull.

@Test
public void testPopulateTokenRequestWhenKeyManagerNull() throws APIManagementException {
    PowerMockito.mockStatic(KeyManagerHolder.class);
    AccessTokenRequest accessTokenRequest = new AccessTokenRequest();
    ApplicationUtils.populateTokenRequest(null, "", accessTokenRequest);
    Mockito.verify(keyManager, Mockito.times(0)).buildAccessTokenRequestFromJSON(Matchers.anyString(), Matchers.any(AccessTokenRequest.class));
}
Also used : AccessTokenRequest(org.wso2.carbon.apimgt.api.model.AccessTokenRequest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 23 with AccessTokenRequest

use of org.wso2.carbon.apimgt.api.model.AccessTokenRequest in project carbon-apimgt by wso2.

the class ApplicationUtilsTestCase method testCreateTokenRequestWhenAccessTokenNotNull.

@Test
public void testCreateTokenRequestWhenAccessTokenNotNull() throws APIManagementException {
    PowerMockito.mockStatic(KeyManagerHolder.class);
    AccessTokenRequest accessTokenRequest = new AccessTokenRequest();
    OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
    ApplicationUtils.createAccessTokenRequest(keyManager, oAuthApplicationInfo, accessTokenRequest);
    Mockito.verify(keyManager, Mockito.times(1)).buildAccessTokenRequestFromOAuthApp(Matchers.any(OAuthApplicationInfo.class), Matchers.any(AccessTokenRequest.class));
}
Also used : OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) AccessTokenRequest(org.wso2.carbon.apimgt.api.model.AccessTokenRequest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 24 with AccessTokenRequest

use of org.wso2.carbon.apimgt.api.model.AccessTokenRequest in project carbon-apimgt by wso2.

the class AbstractApplicationRegistrationWorkflowExecutor method dogenerateKeysForApplication.

public static void dogenerateKeysForApplication(ApplicationRegistrationWorkflowDTO workflowDTO) throws APIManagementException {
    log.debug("Registering Application and creating an Access Token... ");
    Application application = workflowDTO.getApplication();
    Subscriber subscriber = application.getSubscriber();
    ApiMgtDAO dao = ApiMgtDAO.getInstance();
    if (subscriber == null || workflowDTO.getAllowedDomains() == null) {
        dao.populateAppRegistrationWorkflowDTO(workflowDTO);
    }
    try {
        // get new key manager
        // Here the default flow is set expecting an ID as the keymanager as this flow only involves new applications
        String keyManagerId = workflowDTO.getKeyManager();
        KeyManagerConfigurationDTO km = dao.getKeyManagerConfigurationByUUID(keyManagerId);
        String tenantDomain = km.getOrganization();
        String keyManagerName = km.getName();
        KeyManager keyManager = KeyManagerHolder.getKeyManagerInstance(tenantDomain, keyManagerName);
        if (keyManager == null) {
            throw new APIManagementException("Key Manager " + keyManagerName + " not configured");
        }
        workflowDTO.getAppInfoDTO().getOAuthApplicationInfo().setClientName(application.getName());
        // set applications attributes to the oAuthApplicationInfo
        workflowDTO.getAppInfoDTO().getOAuthApplicationInfo().putAllAppAttributes(application.getApplicationAttributes());
        // createApplication on oAuthorization server.
        OAuthApplicationInfo oAuthApplication = keyManager.createApplication(workflowDTO.getAppInfoDTO());
        // update associateApplication
        ApplicationUtils.updateOAuthAppAssociation(application, workflowDTO.getKeyType(), oAuthApplication, keyManagerId);
        // change create application status in to completed.
        dao.updateApplicationRegistration(APIConstants.AppRegistrationStatus.REGISTRATION_COMPLETED, workflowDTO.getKeyType(), workflowDTO.getApplication().getId(), keyManagerId);
        workflowDTO.setApplicationInfo(oAuthApplication);
        AccessTokenInfo tokenInfo;
        Object enableTokenGeneration = keyManager.getKeyManagerConfiguration().getParameter(APIConstants.KeyManager.ENABLE_TOKEN_GENERATION);
        if (enableTokenGeneration != null && (Boolean) enableTokenGeneration && oAuthApplication.getJsonString().contains(APIConstants.GRANT_TYPE_CLIENT_CREDENTIALS)) {
            AccessTokenRequest tokenRequest = ApplicationUtils.createAccessTokenRequest(keyManager, oAuthApplication, null);
            tokenInfo = keyManager.getNewApplicationAccessToken(tokenRequest);
        } else {
            tokenInfo = new AccessTokenInfo();
            tokenInfo.setAccessToken("");
            tokenInfo.setValidityPeriod(0L);
            String[] noScopes = new String[] { "N/A" };
            tokenInfo.setScope(noScopes);
            oAuthApplication.addParameter("tokenScope", Arrays.toString(noScopes));
        }
        workflowDTO.setAccessTokenInfo(tokenInfo);
    } catch (Exception e) {
        APIUtil.handleException("Error occurred while executing SubscriberKeyMgtClient.", e);
    }
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) AccessTokenInfo(org.wso2.carbon.apimgt.api.model.AccessTokenInfo) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) AccessTokenRequest(org.wso2.carbon.apimgt.api.model.AccessTokenRequest) Application(org.wso2.carbon.apimgt.api.model.Application) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager)

Example 25 with AccessTokenRequest

use of org.wso2.carbon.apimgt.api.model.AccessTokenRequest in project carbon-apimgt by wso2.

the class APIConsumerImpl method renewConsumerSecret.

/**
 * Regenerate consumer secret.
 *
 * @param clientId For which consumer key we need to regenerate consumer secret.
 * @param keyManagerName
 * @return New consumer secret.
 * @throws APIManagementException This is the custom exception class for API management.
 */
public String renewConsumerSecret(String clientId, String keyManagerName) throws APIManagementException {
    // Create Token Request with parameters provided from UI.
    AccessTokenRequest tokenRequest = new AccessTokenRequest();
    tokenRequest.setClientId(clientId);
    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.");
            throw new APIManagementException("Key Manager " + keyManagerName + " not found in database.", ExceptionCodes.KEY_MANAGER_NOT_FOUND);
        }
    }
    KeyManager keyManager = KeyManagerHolder.getKeyManagerInstance(tenantDomain, keyManagerName);
    return keyManager.getNewApplicationConsumerSecret(tokenRequest);
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) AccessTokenRequest(org.wso2.carbon.apimgt.api.model.AccessTokenRequest) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager)

Aggregations

AccessTokenRequest (org.wso2.carbon.apimgt.api.model.AccessTokenRequest)13 AccessTokenRequest (org.wso2.carbon.apimgt.core.models.AccessTokenRequest)11 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)9 KeyManagementException (org.wso2.carbon.apimgt.core.exception.KeyManagementException)9 AccessTokenInfo (org.wso2.carbon.apimgt.core.models.AccessTokenInfo)8 Response (feign.Response)7 Test (org.junit.Test)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 OAuth2IntrospectionResponse (org.wso2.carbon.apimgt.core.auth.dto.OAuth2IntrospectionResponse)7 OAuth2TokenInfo (org.wso2.carbon.apimgt.core.auth.dto.OAuth2TokenInfo)7 Test (org.testng.annotations.Test)6 OAuthApplicationInfo (org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo)6 DCRMServiceStub (org.wso2.carbon.apimgt.core.auth.DCRMServiceStub)6 OAuth2ServiceStubs (org.wso2.carbon.apimgt.core.auth.OAuth2ServiceStubs)6 ScopeRegistration (org.wso2.carbon.apimgt.core.auth.ScopeRegistration)6 Gson (com.google.gson.Gson)5 KeyManagerConfigurationDTO (org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO)5 AccessTokenInfo (org.wso2.carbon.apimgt.api.model.AccessTokenInfo)4 KeyManager (org.wso2.carbon.apimgt.api.model.KeyManager)4 JSONObject (org.json.simple.JSONObject)3