Search in sources :

Example 1 with ApplicationToken

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

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

the class APIStoreImpl method generateApplicationToken.

@Override
public ApplicationToken generateApplicationToken(String clientId, String clientSecret, String scopes, long validityPeriod, String tokenToBeRevoked) throws APIManagementException {
    log.debug("Generating a new application access token");
    AccessTokenRequest accessTokenRequest = new AccessTokenRequest();
    accessTokenRequest.setClientId(clientId);
    accessTokenRequest.setClientSecret(clientSecret);
    accessTokenRequest.setGrantType(KeyManagerConstants.CLIENT_CREDENTIALS_GRANT_TYPE);
    if (StringUtils.isEmpty(scopes)) {
        scopes = KeyManagerConstants.OAUTH2_DEFAULT_SCOPE;
    }
    accessTokenRequest.setScopes(scopes);
    accessTokenRequest.setValidityPeriod(validityPeriod);
    accessTokenRequest.setTokenToRevoke(tokenToBeRevoked);
    AccessTokenInfo newToken = getKeyManager().getNewAccessToken(accessTokenRequest);
    ApplicationToken applicationToken = new ApplicationToken();
    applicationToken.setAccessToken(newToken.getAccessToken());
    applicationToken.setValidityPeriod(newToken.getValidityPeriod());
    applicationToken.setScopes(newToken.getScopes());
    log.debug("Successfully created a new application access token.");
    return applicationToken;
}
Also used : AccessTokenInfo(org.wso2.carbon.apimgt.core.models.AccessTokenInfo) AccessTokenRequest(org.wso2.carbon.apimgt.core.models.AccessTokenRequest) ApplicationToken(org.wso2.carbon.apimgt.core.models.ApplicationToken)

Example 3 with ApplicationToken

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

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

Example 5 with ApplicationToken

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

the class ApplicationsApiServiceImplTestCase method testApplicationsApplicationIdGenerateTokenPost.

@Test
public void testApplicationsApplicationIdGenerateTokenPost() throws APIManagementException, NotFoundException {
    TestUtil.printTestMethodName();
    String applicationId = UUID.randomUUID().toString();
    String accessToken = UUID.randomUUID().toString();
    String clientID = UUID.randomUUID().toString();
    String clientSecret = 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);
    ApplicationToken applicationToken = new ApplicationToken();
    applicationToken.setAccessToken(accessToken);
    applicationToken.setValidityPeriod(10000);
    applicationToken.setScopes("SCOPE1");
    Mockito.when(apiStore.generateApplicationToken(clientID, clientSecret, "SCOPE1", 1000, "revokeToken")).thenReturn(applicationToken);
    ApplicationTokenGenerateRequestDTO generateRequestDTO = new ApplicationTokenGenerateRequestDTO();
    generateRequestDTO.setConsumerKey(clientID);
    generateRequestDTO.setConsumerSecret(clientSecret);
    generateRequestDTO.setRevokeToken("revokeToken");
    generateRequestDTO.setScopes("SCOPE1");
    generateRequestDTO.setValidityPeriod(10000);
    Response response = applicationsApiService.applicationsApplicationIdGenerateTokenPost(applicationId, generateRequestDTO, null, null, 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) ApplicationTokenGenerateRequestDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationTokenGenerateRequestDTO) Request(org.wso2.msf4j.Request) ApplicationToken(org.wso2.carbon.apimgt.core.models.ApplicationToken) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ApplicationToken (org.wso2.carbon.apimgt.core.models.ApplicationToken)4 ApplicationTokenDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationTokenDTO)3 Test (org.junit.Test)2 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Response (javax.ws.rs.core.Response)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 AccessTokenInfo (org.wso2.carbon.apimgt.core.models.AccessTokenInfo)1 AccessTokenRequest (org.wso2.carbon.apimgt.core.models.AccessTokenRequest)1 ApplicationCreationResponse (org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse)1 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)1 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)1 ApplicationTokenGenerateRequestDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationTokenGenerateRequestDTO)1 Request (org.wso2.msf4j.Request)1