Search in sources :

Example 21 with OAuthApplicationInfo

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

the class AMDefaultKeyManagerImpl method updateApplication.

@Override
public OAuthApplicationInfo updateApplication(OAuthAppRequest appInfoDTO) throws APIManagementException {
    OAuthApplicationInfo oAuthApplicationInfo = appInfoDTO.getOAuthApplicationInfo();
    String userId = (String) oAuthApplicationInfo.getParameter(ApplicationConstants.OAUTH_CLIENT_USERNAME);
    String applicationName = oAuthApplicationInfo.getClientName();
    String oauthClientName = oAuthApplicationInfo.getApplicationUUID();
    String keyType = (String) oAuthApplicationInfo.getParameter(ApplicationConstants.APP_KEY_TYPE);
    if (StringUtils.isNotEmpty(applicationName) && StringUtils.isNotEmpty(keyType)) {
        // Replace the domain name separator with an underscore for secondary user stores
        String domain = UserCoreUtil.extractDomainFromName(userId);
        if (domain != null && !domain.isEmpty() && !UserCoreConstants.PRIMARY_DEFAULT_DOMAIN_NAME.equals(domain)) {
            userId = userId.replace(UserCoreConstants.DOMAIN_SEPARATOR, "_");
        }
        // Construct the application name subsequent to replacing email domain separator
        oauthClientName = String.format("%s_%s_%s", APIUtil.replaceEmailDomain(MultitenantUtils.getTenantAwareUsername(userId)), oauthClientName, keyType);
    } else {
        throw new APIManagementException("Missing required information for OAuth application update.");
    }
    log.debug("Updating OAuth Client with ID : " + oAuthApplicationInfo.getClientId());
    if (log.isDebugEnabled() && oAuthApplicationInfo.getCallBackURL() != null) {
        log.debug("CallBackURL : " + oAuthApplicationInfo.getCallBackURL());
    }
    if (log.isDebugEnabled() && applicationName != null) {
        log.debug("Client Name : " + oauthClientName);
    }
    ClientInfo request = createClientInfo(oAuthApplicationInfo, oauthClientName, true);
    ClientInfo createdClient;
    try {
        createdClient = dcrClient.updateApplication(Base64.getUrlEncoder().encodeToString(oAuthApplicationInfo.getClientId().getBytes(StandardCharsets.UTF_8)), request);
        return buildDTOFromClientInfo(createdClient, new OAuthApplicationInfo());
    } catch (KeyManagerClientException e) {
        handleException("Error occurred while updating OAuth Client : ", e);
        return null;
    }
}
Also used : KeyManagerClientException(org.wso2.carbon.apimgt.impl.kmclient.KeyManagerClientException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) ClientInfo(org.wso2.carbon.apimgt.impl.kmclient.model.ClientInfo)

Example 22 with OAuthApplicationInfo

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

the class AMDefaultKeyManagerImpl method updateApplicationOwner.

@Override
public OAuthApplicationInfo updateApplicationOwner(OAuthAppRequest appInfoDTO, String owner) throws APIManagementException {
    OAuthApplicationInfo oAuthApplicationInfo = appInfoDTO.getOAuthApplicationInfo();
    log.debug("Updating Application Owner : " + oAuthApplicationInfo.getClientId());
    ClientInfo updatedClient;
    try {
        updatedClient = dcrClient.updateApplicationOwner(owner, Base64.getUrlEncoder().encodeToString(oAuthApplicationInfo.getClientId().getBytes(StandardCharsets.UTF_8)));
        return buildDTOFromClientInfo(updatedClient, new OAuthApplicationInfo());
    } catch (KeyManagerClientException e) {
        handleException("Error occurred while updating OAuth Client : ", e);
        return null;
    }
}
Also used : KeyManagerClientException(org.wso2.carbon.apimgt.impl.kmclient.KeyManagerClientException) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) ClientInfo(org.wso2.carbon.apimgt.impl.kmclient.model.ClientInfo)

Example 23 with OAuthApplicationInfo

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

the class AMDefaultKeyManagerImpl method createApplication.

@Override
public OAuthApplicationInfo createApplication(OAuthAppRequest oauthAppRequest) throws APIManagementException {
    // OAuthApplications are created by calling to APIKeyMgtSubscriber Service
    OAuthApplicationInfo oAuthApplicationInfo = oauthAppRequest.getOAuthApplicationInfo();
    // Subscriber's name should be passed as a parameter, since it's under the subscriber the OAuth App is created.
    String userId = (String) oAuthApplicationInfo.getParameter(ApplicationConstants.OAUTH_CLIENT_USERNAME);
    if (StringUtils.isEmpty(userId)) {
        throw new APIManagementException("Missing user ID for OAuth application creation.");
    }
    String applicationName = oAuthApplicationInfo.getClientName();
    String oauthClientName = oauthAppRequest.getOAuthApplicationInfo().getApplicationUUID();
    String keyType = (String) oAuthApplicationInfo.getParameter(ApplicationConstants.APP_KEY_TYPE);
    if (StringUtils.isNotEmpty(applicationName) && StringUtils.isNotEmpty(keyType)) {
        String domain = UserCoreUtil.extractDomainFromName(userId);
        if (domain != null && !domain.isEmpty() && !UserCoreConstants.PRIMARY_DEFAULT_DOMAIN_NAME.equals(domain)) {
            userId = userId.replace(UserCoreConstants.DOMAIN_SEPARATOR, "_");
        }
        oauthClientName = String.format("%s_%s_%s", APIUtil.replaceEmailDomain(MultitenantUtils.getTenantAwareUsername(userId)), oauthClientName, keyType);
    } else {
        throw new APIManagementException("Missing required information for OAuth application creation.");
    }
    if (log.isDebugEnabled()) {
        log.debug("Trying to create OAuth application : " + oauthClientName + " for application: " + applicationName + " and key type: " + keyType);
    }
    String tokenScope = (String) oAuthApplicationInfo.getParameter("tokenScope");
    String[] tokenScopes = new String[1];
    tokenScopes[0] = tokenScope;
    ClientInfo request = createClientInfo(oAuthApplicationInfo, oauthClientName, false);
    ClientInfo createdClient;
    try {
        createdClient = dcrClient.createApplication(request);
        buildDTOFromClientInfo(createdClient, oAuthApplicationInfo);
        oAuthApplicationInfo.addParameter("tokenScope", tokenScopes);
        oAuthApplicationInfo.setIsSaasApplication(false);
        return oAuthApplicationInfo;
    } catch (KeyManagerClientException e) {
        handleException("Can not create OAuth application  : " + oauthClientName + " for application: " + applicationName + " and key type: " + keyType, e);
        return null;
    }
}
Also used : KeyManagerClientException(org.wso2.carbon.apimgt.impl.kmclient.KeyManagerClientException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) ClientInfo(org.wso2.carbon.apimgt.impl.kmclient.model.ClientInfo)

Example 24 with OAuthApplicationInfo

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

the class AMDefaultKeyManagerImpl method mapOAuthApplication.

/**
 * This method will create a new record at CLIENT_INFO table by given OauthAppRequest.
 *
 * @param appInfoRequest oAuth application properties will contain in this object
 * @return OAuthApplicationInfo with created oAuth application details.
 * @throws org.wso2.carbon.apimgt.api.APIManagementException
 */
@Override
public OAuthApplicationInfo mapOAuthApplication(OAuthAppRequest appInfoRequest) throws APIManagementException {
    // initiate OAuthApplicationInfo
    OAuthApplicationInfo oAuthApplicationInfo = appInfoRequest.getOAuthApplicationInfo();
    String consumerKey = oAuthApplicationInfo.getClientId();
    String tokenScope = (String) oAuthApplicationInfo.getParameter("tokenScope");
    String[] tokenScopes = new String[1];
    tokenScopes[0] = tokenScope;
    String clientSecret = (String) oAuthApplicationInfo.getParameter("client_secret");
    // for the first time we set default time period.
    oAuthApplicationInfo.addParameter(ApplicationConstants.VALIDITY_PERIOD, getConfigurationParamValue(APIConstants.IDENTITY_OAUTH2_FIELD_VALIDITY_PERIOD));
    String userId = (String) oAuthApplicationInfo.getParameter(ApplicationConstants.OAUTH_CLIENT_USERNAME);
    // check whether given consumer key and secret match or not. If it does not match throw an exception.
    ClientInfo clientInfo;
    try {
        clientInfo = dcrClient.getApplication(Base64.getUrlEncoder().encodeToString(consumerKey.getBytes(StandardCharsets.UTF_8)));
        buildDTOFromClientInfo(clientInfo, oAuthApplicationInfo);
    } catch (KeyManagerClientException e) {
        handleException("Some thing went wrong while getting OAuth application for given consumer key " + oAuthApplicationInfo.getClientId(), e);
    }
    if (!clientSecret.equals(oAuthApplicationInfo.getClientSecret())) {
        throw new APIManagementException("The secret key is wrong for the given consumer key " + consumerKey);
    }
    oAuthApplicationInfo.addParameter("tokenScope", tokenScopes);
    oAuthApplicationInfo.setIsSaasApplication(false);
    if (log.isDebugEnabled()) {
        log.debug("Creating semi-manual application for consumer id  :  " + oAuthApplicationInfo.getClientId());
    }
    return oAuthApplicationInfo;
}
Also used : KeyManagerClientException(org.wso2.carbon.apimgt.impl.kmclient.KeyManagerClientException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) ClientInfo(org.wso2.carbon.apimgt.impl.kmclient.model.ClientInfo)

Example 25 with OAuthApplicationInfo

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

the class APIMgtDAOTest method testCreateApplicationRegistrationEntry.

@Test
public void testCreateApplicationRegistrationEntry() throws Exception {
    Subscriber subscriber = new Subscriber("testCreateApplicationRegistrationEntry");
    subscriber.setTenantId(-1234);
    subscriber.setEmail("abc@wso2.com");
    subscriber.setSubscribedDate(new Date(System.currentTimeMillis()));
    apiMgtDAO.addSubscriber(subscriber, null);
    Policy applicationPolicy = getApplicationPolicy("testCreateApplicationRegistrationEntry");
    applicationPolicy.setTenantId(-1234);
    apiMgtDAO.addApplicationPolicy((ApplicationPolicy) applicationPolicy);
    Application application = new Application("testCreateApplicationRegistrationEntry", subscriber);
    application.setTier("testCreateApplicationRegistrationEntry");
    application.setId(apiMgtDAO.addApplication(application, "testCreateApplicationRegistrationEntry", "testOrg"));
    ApplicationRegistrationWorkflowDTO applicationRegistrationWorkflowDTO = new ApplicationRegistrationWorkflowDTO();
    applicationRegistrationWorkflowDTO.setApplication(application);
    applicationRegistrationWorkflowDTO.setKeyType("PRODUCTION");
    applicationRegistrationWorkflowDTO.setDomainList("*");
    applicationRegistrationWorkflowDTO.setWorkflowReference(UUID.randomUUID().toString());
    applicationRegistrationWorkflowDTO.setValidityTime(100L);
    applicationRegistrationWorkflowDTO.setExternalWorkflowReference(UUID.randomUUID().toString());
    applicationRegistrationWorkflowDTO.setStatus(WorkflowStatus.CREATED);
    applicationRegistrationWorkflowDTO.setKeyManager("Default");
    apiMgtDAO.addWorkflowEntry(applicationRegistrationWorkflowDTO);
    OAuthAppRequest oAuthAppRequest = new OAuthAppRequest();
    OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
    oAuthApplicationInfo.setJsonString("");
    oAuthApplicationInfo.addParameter("tokenScope", "deafault");
    oAuthAppRequest.setOAuthApplicationInfo(oAuthApplicationInfo);
    applicationRegistrationWorkflowDTO.setAppInfoDTO(oAuthAppRequest);
    APIIdentifier apiId = new APIIdentifier("testCreateApplicationRegistrationEntry", "testCreateApplicationRegistrationEntry", "1.0.0");
    API api = new API(apiId);
    api.setContext("/testCreateApplicationRegistrationEntry");
    api.setContextTemplate("/testCreateApplicationRegistrationEntry/{version}");
    api.setUUID(UUID.randomUUID().toString());
    api.setVersionTimestamp(String.valueOf(System.currentTimeMillis()));
    int internalAPIID2 = apiMgtDAO.addAPI(api, -1234, "org1");
    api.getId().setId(internalAPIID2);
    api.setOrganization("org1");
    APIIdentifier apiId1 = new APIIdentifier("testCreateApplicationRegistrationEntry1", "testCreateApplicationRegistrationEntry1", "1.0.0");
    API api1 = new API(apiId1);
    api1.setContext("/testCreateApplicationRegistrationEntry1");
    api1.setContextTemplate("/testCreateApplicationRegistrationEntry1/{version}");
    api1.setUUID(UUID.randomUUID().toString());
    int apiInternalId = apiMgtDAO.addAPI(api1, -1234, "org2");
    api1.getId().setId(apiInternalId);
    api1.setOrganization("org2");
    apiMgtDAO.createApplicationRegistrationEntry(applicationRegistrationWorkflowDTO, false);
    ApplicationRegistrationWorkflowDTO retrievedApplicationRegistrationWorkflowDTO = new ApplicationRegistrationWorkflowDTO();
    retrievedApplicationRegistrationWorkflowDTO.setExternalWorkflowReference(applicationRegistrationWorkflowDTO.getExternalWorkflowReference());
    apiMgtDAO.populateAppRegistrationWorkflowDTO(retrievedApplicationRegistrationWorkflowDTO);
    ApiTypeWrapper apiTypeWrapper = new ApiTypeWrapper(api);
    ApiTypeWrapper apiTypeWrapper1 = new ApiTypeWrapper(api1);
    apiMgtDAO.addSubscription(apiTypeWrapper, application, APIConstants.SubscriptionStatus.ON_HOLD, subscriber.getName());
    int subsId = apiMgtDAO.addSubscription(apiTypeWrapper1, application, APIConstants.SubscriptionStatus.ON_HOLD, subscriber.getName());
    assertTrue(apiMgtDAO.isContextExist(api.getContext()));
    assertTrue(api.getContext().equals(apiMgtDAO.getAPIContext(api.getUuid())));
    apiMgtDAO.removeSubscription(apiId, application.getId());
    apiMgtDAO.removeSubscriptionById(subsId);
    apiMgtDAO.deleteAPI(api.getUuid());
    apiMgtDAO.deleteAPI(api1.getUuid());
    assertNotNull(apiMgtDAO.getWorkflowReference(application.getName(), subscriber.getName()));
    applicationRegistrationWorkflowDTO.setStatus(WorkflowStatus.APPROVED);
    apiMgtDAO.updateWorkflowStatus(applicationRegistrationWorkflowDTO);
    assertNotNull(apiMgtDAO.retrieveWorkflow(applicationRegistrationWorkflowDTO.getExternalWorkflowReference()));
    assertNotNull(apiMgtDAO.retrieveWorkflowFromInternalReference(applicationRegistrationWorkflowDTO.getWorkflowReference(), applicationRegistrationWorkflowDTO.getWorkflowType()));
    apiMgtDAO.removeWorkflowEntry(applicationRegistrationWorkflowDTO.getExternalWorkflowReference(), applicationRegistrationWorkflowDTO.getWorkflowType());
    apiMgtDAO.deleteApplicationKeyMappingByApplicationIdAndType(application.getId(), "PRODUCTION");
    apiMgtDAO.deleteApplicationRegistration(application.getId(), "PRODUCTION", APIConstants.KeyManager.DEFAULT_KEY_MANAGER);
    apiMgtDAO.deleteApplication(application);
    apiMgtDAO.removeThrottlePolicy(PolicyConstants.POLICY_LEVEL_APP, "testCreateApplicationRegistrationEntry", -1234);
    deleteSubscriber(subscriber.getId());
}
Also used : QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.api.model.policy.Policy) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) ApplicationRegistrationWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) OAuthAppRequest(org.wso2.carbon.apimgt.api.model.OAuthAppRequest) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) API(org.wso2.carbon.apimgt.api.model.API) Application(org.wso2.carbon.apimgt.api.model.Application) Date(java.util.Date) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) 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