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