Search in sources :

Example 6 with ApplicationTokenDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationTokenDTO in project carbon-apimgt by wso2.

the class ApplicationKeyMappingUtilTestCase method testFromApplicationTokenToDTO.

@Test
public void testFromApplicationTokenToDTO() {
    String accessToken = "123123123123123123132";
    ApplicationToken applicationToken = new ApplicationToken();
    applicationToken.setAccessToken(accessToken);
    applicationToken.setScopes("Scope1");
    applicationToken.setValidityPeriod(100000);
    ApplicationTokenDTO applicationTokenDTO = ApplicationKeyMappingUtil.fromApplicationTokenToDTO(applicationToken);
    Assert.assertEquals(applicationTokenDTO.getAccessToken(), accessToken);
}
Also used : ApplicationTokenDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationTokenDTO) ApplicationToken(org.wso2.carbon.apimgt.core.models.ApplicationToken) Test(org.junit.Test)

Example 7 with ApplicationTokenDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationTokenDTO in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsApplicationIdOauthKeysKeyMappingIdGenerateTokenPost.

@Override
public Response applicationsApplicationIdOauthKeysKeyMappingIdGenerateTokenPost(String applicationId, String keyMappingId, ApplicationTokenGenerateRequestDTO body, String ifMatch, MessageContext messageContext) throws APIManagementException {
    String username = RestApiCommonUtil.getLoggedInUsername();
    APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
    Application application = apiConsumer.getApplicationByUUID(applicationId);
    if (application != null) {
        if (RestAPIStoreUtils.isUserAccessAllowedForApplication(application)) {
            ApplicationKeyDTO appKey = getApplicationKeyByAppIDAndKeyMapping(applicationId, keyMappingId);
            if (appKey != null) {
                String jsonInput = null;
                String grantType;
                if (ApplicationTokenGenerateRequestDTO.GrantTypeEnum.TOKEN_EXCHANGE.equals(body.getGrantType())) {
                    grantType = APIConstants.OAuthConstants.TOKEN_EXCHANGE;
                } else {
                    grantType = APIConstants.GRANT_TYPE_CLIENT_CREDENTIALS;
                }
                try {
                    // verify that the provided jsonInput is a valid json
                    if (body.getAdditionalProperties() != null && !body.getAdditionalProperties().toString().isEmpty()) {
                        jsonInput = validateAdditionalParameters(grantType, body);
                    }
                } catch (JsonProcessingException | ParseException | ClassCastException e) {
                    RestApiUtil.handleBadRequest("Error while generating " + appKey.getKeyType() + " token for " + "application " + applicationId + ". Invalid jsonInput '" + body.getAdditionalProperties() + "' provided.", log);
                }
                if (StringUtils.isNotEmpty(body.getConsumerSecret())) {
                    appKey.setConsumerSecret(body.getConsumerSecret());
                }
                String[] scopes = body.getScopes().toArray(new String[0]);
                try {
                    AccessTokenInfo response = apiConsumer.renewAccessToken(body.getRevokeToken(), appKey.getConsumerKey(), appKey.getConsumerSecret(), body.getValidityPeriod().toString(), scopes, jsonInput, appKey.getKeyManager(), grantType);
                    ApplicationTokenDTO appToken = new ApplicationTokenDTO();
                    appToken.setAccessToken(response.getAccessToken());
                    if (response.getScopes() != null) {
                        appToken.setTokenScopes(Arrays.asList(response.getScopes()));
                    }
                    appToken.setValidityTime(response.getValidityPeriod());
                    return Response.ok().entity(appToken).build();
                } catch (APIManagementException e) {
                    Long errorCode = e.getErrorHandler() != null ? e.getErrorHandler().getErrorCode() : ExceptionCodes.INTERNAL_ERROR.getErrorCode();
                    RestApiUtil.handleBadRequest(e.getMessage(), errorCode, log);
                }
            } else {
                RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APP_CONSUMER_KEY, keyMappingId, log);
            }
        } else {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
        }
    } else {
        RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
    }
    return null;
}
Also used : ApplicationTokenDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationTokenDTO) AccessTokenInfo(org.wso2.carbon.apimgt.api.model.AccessTokenInfo) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApplicationKeyDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) ParseException(org.json.simple.parser.ParseException) ExportedApplication(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication) Application(org.wso2.carbon.apimgt.api.model.Application) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 8 with ApplicationTokenDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationTokenDTO in project carbon-apimgt by wso2.

the class ApplicationKeyMappingUtil method fromApplicationKeyToDTO.

/**
 * Insert the application related details to a DTO Object using api key
 *
 * @param apiKey Object that contains details needed during token request
 * @return DTO object with application related details
 */
public static ApplicationKeyDTO fromApplicationKeyToDTO(APIKey apiKey) {
    ApplicationKeyDTO applicationKeyDTO = new ApplicationKeyDTO();
    applicationKeyDTO.setKeyType(ApplicationKeyDTO.KeyTypeEnum.valueOf(apiKey.getType()));
    applicationKeyDTO.setConsumerKey(apiKey.getConsumerKey());
    applicationKeyDTO.setConsumerSecret(apiKey.getConsumerSecret());
    applicationKeyDTO.setKeyState(apiKey.getState());
    applicationKeyDTO.setMode(ApplicationKeyDTO.ModeEnum.valueOf(apiKey.getCreateMode()));
    applicationKeyDTO.setKeyManager(apiKey.getKeyManager());
    applicationKeyDTO.setKeyMappingId(apiKey.getMappingId());
    if (apiKey.getGrantTypes() != null) {
        applicationKeyDTO.setSupportedGrantTypes(Arrays.asList(apiKey.getGrantTypes().split(" ")));
    } else {
        applicationKeyDTO.setSupportedGrantTypes(null);
    }
    applicationKeyDTO.setCallbackUrl(apiKey.getCallbackUrl());
    ApplicationTokenDTO tokenDTO = new ApplicationTokenDTO();
    if (apiKey.getTokenScope() != null) {
        tokenDTO.setTokenScopes(Arrays.asList(apiKey.getTokenScope().split(" ")));
    }
    tokenDTO.setAccessToken(apiKey.getAccessToken());
    tokenDTO.setValidityTime(apiKey.getValidityPeriod());
    applicationKeyDTO.setToken(tokenDTO);
    applicationKeyDTO.setAdditionalProperties(apiKey.getAdditionalProperties());
    return applicationKeyDTO;
}
Also used : ApplicationTokenDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationTokenDTO) ApplicationKeyDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO)

Example 9 with ApplicationTokenDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationTokenDTO in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsApplicationIdGenerateTokenPost.

/**
 * Generate an application token
 *
 * @param applicationId   Application ID
 * @param body            Application information which are required to generate tokens
 * @param ifMatch         If-Match header value
 * @param ifUnmodifiedSince If-UnModified-Since header value
 * @param request         msf4j request object
 * @return Generated application key detials
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response applicationsApplicationIdGenerateTokenPost(String applicationId, ApplicationTokenGenerateRequestDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    try {
        String username = RestApiUtil.getLoggedInUsername(request);
        APIStore apiConsumer = RestApiUtil.getConsumer(username);
        ApplicationToken token = apiConsumer.generateApplicationToken(body.getConsumerKey(), body.getConsumerSecret(), body.getScopes(), body.getValidityPeriod(), body.getRevokeToken());
        ApplicationTokenDTO appToken = ApplicationKeyMappingUtil.fromApplicationTokenToDTO(token);
        return Response.ok().entity(appToken).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while generating application tokens for application: " + applicationId;
        Map<String, String> paramList = new HashMap<>();
        paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : ApplicationTokenDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationTokenDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) ApplicationToken(org.wso2.carbon.apimgt.core.models.ApplicationToken) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 10 with ApplicationTokenDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationTokenDTO in project carbon-apimgt by wso2.

the class ApplicationKeyMappingUtil method fromApplicationTokenToDTO.

public static ApplicationTokenDTO fromApplicationTokenToDTO(ApplicationToken applicationToken) {
    if (applicationToken == null) {
        return null;
    }
    ApplicationTokenDTO applicationTokenDTO = new ApplicationTokenDTO();
    applicationTokenDTO.setAccessToken(applicationToken.getAccessToken());
    applicationTokenDTO.setTokenScopes(applicationToken.getScopes());
    applicationTokenDTO.setValidityTime(applicationToken.getValidityPeriod());
    return applicationTokenDTO;
}
Also used : ApplicationTokenDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationTokenDTO)

Aggregations

ApplicationTokenDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationTokenDTO)10 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)7 ApplicationKeysDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO)7 Response (javax.ws.rs.core.Response)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)6 ApplicationCreationResponse (org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse)6 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)6 Request (org.wso2.msf4j.Request)6 ApplicationDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationDTO)5 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)4 ApplicationKeyDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO)4 ApplicationTokenDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationTokenDTO)4 ParseException (org.json.simple.parser.ParseException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)2