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);
}
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;
}
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();
}
}
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;
}
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());
}
Aggregations