Search in sources :

Example 1 with ApiKeyGenerator

use of org.wso2.carbon.apimgt.impl.token.ApiKeyGenerator in project carbon-apimgt by wso2.

the class APIProviderImpl method generateApiKey.

@Override
public String generateApiKey(String apiId) throws APIManagementException {
    APIInfo apiInfo = apiMgtDAO.getAPIInfoByUUID(apiId);
    if (apiInfo == null) {
        throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with ID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
    }
    SubscribedApiDTO subscribedApiInfo = new SubscribedApiDTO();
    subscribedApiInfo.setName(apiInfo.getName());
    subscribedApiInfo.setContext(apiInfo.getContext());
    subscribedApiInfo.setPublisher(apiInfo.getProvider());
    subscribedApiInfo.setVersion(apiInfo.getVersion());
    JwtTokenInfoDTO jwtTokenInfoDTO = new JwtTokenInfoDTO();
    jwtTokenInfoDTO.setEndUserName(username);
    jwtTokenInfoDTO.setKeyType(APIConstants.API_KEY_TYPE_PRODUCTION);
    jwtTokenInfoDTO.setSubscribedApiDTOList(Arrays.asList(subscribedApiInfo));
    jwtTokenInfoDTO.setExpirationTime(60 * 1000);
    ApiKeyGenerator apiKeyGenerator = new InternalAPIKeyGenerator();
    return apiKeyGenerator.generateToken(jwtTokenInfoDTO);
}
Also used : SubscribedApiDTO(org.wso2.carbon.apimgt.impl.dto.SubscribedApiDTO) InternalAPIKeyGenerator(org.wso2.carbon.apimgt.impl.token.InternalAPIKeyGenerator) JwtTokenInfoDTO(org.wso2.carbon.apimgt.impl.dto.JwtTokenInfoDTO) PublisherAPIInfo(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIInfo) APIInfo(org.wso2.carbon.apimgt.api.model.APIInfo) ApiKeyGenerator(org.wso2.carbon.apimgt.impl.token.ApiKeyGenerator) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 2 with ApiKeyGenerator

use of org.wso2.carbon.apimgt.impl.token.ApiKeyGenerator in project carbon-apimgt by wso2.

the class APIConsumerImpl method generateApiKey.

@Override
public String generateApiKey(Application application, String userName, long validityPeriod, String permittedIP, String permittedReferer) throws APIManagementException {
    JwtTokenInfoDTO jwtTokenInfoDTO = APIUtil.getJwtTokenInfoDTO(application, userName, MultitenantUtils.getTenantDomain(userName));
    ApplicationDTO applicationDTO = new ApplicationDTO();
    applicationDTO.setId(application.getId());
    applicationDTO.setName(application.getName());
    applicationDTO.setOwner(application.getOwner());
    applicationDTO.setTier(application.getTier());
    applicationDTO.setUuid(application.getUUID());
    jwtTokenInfoDTO.setApplication(applicationDTO);
    jwtTokenInfoDTO.setSubscriber(userName);
    jwtTokenInfoDTO.setExpirationTime(validityPeriod);
    jwtTokenInfoDTO.setKeyType(application.getKeyType());
    jwtTokenInfoDTO.setPermittedIP(permittedIP);
    jwtTokenInfoDTO.setPermittedReferer(permittedReferer);
    ApiKeyGenerator apiKeyGenerator = loadApiKeyGenerator();
    return apiKeyGenerator.generateToken(jwtTokenInfoDTO);
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.impl.dto.ApplicationDTO) JwtTokenInfoDTO(org.wso2.carbon.apimgt.impl.dto.JwtTokenInfoDTO) ApiKeyGenerator(org.wso2.carbon.apimgt.impl.token.ApiKeyGenerator)

Example 3 with ApiKeyGenerator

use of org.wso2.carbon.apimgt.impl.token.ApiKeyGenerator in project carbon-apimgt by wso2.

the class APIConsumerImpl method loadApiKeyGenerator.

private ApiKeyGenerator loadApiKeyGenerator() {
    ApiKeyGenerator apiKeyGenerator = null;
    String keyGeneratorClassName = APIUtil.getApiKeyGeneratorImpl();
    try {
        Class keyGeneratorClass = APIConsumerImpl.class.getClassLoader().loadClass(keyGeneratorClassName);
        Constructor constructor = keyGeneratorClass.getDeclaredConstructor();
        apiKeyGenerator = (ApiKeyGenerator) constructor.newInstance();
    } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
        log.error("Error while loading the api key generator class: " + keyGeneratorClassName, e);
    }
    return apiKeyGenerator;
}
Also used : Constructor(java.lang.reflect.Constructor) ApiKeyGenerator(org.wso2.carbon.apimgt.impl.token.ApiKeyGenerator) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

ApiKeyGenerator (org.wso2.carbon.apimgt.impl.token.ApiKeyGenerator)3 JwtTokenInfoDTO (org.wso2.carbon.apimgt.impl.dto.JwtTokenInfoDTO)2 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)1 APIInfo (org.wso2.carbon.apimgt.api.model.APIInfo)1 ApplicationDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationDTO)1 SubscribedApiDTO (org.wso2.carbon.apimgt.impl.dto.SubscribedApiDTO)1 InternalAPIKeyGenerator (org.wso2.carbon.apimgt.impl.token.InternalAPIKeyGenerator)1 PublisherAPIInfo (org.wso2.carbon.apimgt.persistence.dto.PublisherAPIInfo)1