Search in sources :

Example 11 with KeyManagerDto

use of org.wso2.carbon.apimgt.impl.dto.KeyManagerDto in project carbon-apimgt by wso2.

the class APIProviderImpl method deleteSharedScope.

/**
 * Delete shared scope.
 *
 * @param scopeName    Shared scope name
 * @param tenantDomain tenant domain
 * @throws APIManagementException If failed to delete the scope
 */
@Override
public void deleteSharedScope(String scopeName, String tenantDomain) throws APIManagementException {
    if (log.isDebugEnabled()) {
        log.debug("Deleting shared scope " + scopeName);
    }
    Map<String, KeyManagerDto> tenantKeyManagers = KeyManagerHolder.getTenantKeyManagers(tenantDomain);
    for (Map.Entry<String, KeyManagerDto> keyManagerEntry : tenantKeyManagers.entrySet()) {
        KeyManager keyManager = keyManagerEntry.getValue().getKeyManager();
        if (keyManager != null) {
            try {
                keyManager.deleteScope(scopeName);
            } catch (APIManagementException e) {
                log.error("Error while Deleting Shared Scope " + scopeName + " from Key Manager " + keyManagerEntry.getKey(), e);
            }
        }
    }
    apiMgtDAO.deleteSharedScope(scopeName, tenantDomain);
    deleteScope(scopeName, APIUtil.getTenantIdFromTenantDomain(tenantDomain));
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager)

Example 12 with KeyManagerDto

use of org.wso2.carbon.apimgt.impl.dto.KeyManagerDto in project carbon-apimgt by wso2.

the class APIProviderImpl method addURITemplates.

/**
 * Add URI templates for the API.
 *
 * @param apiId    API Id
 * @param api      API
 * @param tenantId Tenant Id
 * @throws APIManagementException if fails to add URI templates for the API
 */
private void addURITemplates(int apiId, API api, int tenantId) throws APIManagementException {
    String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId);
    apiMgtDAO.addURITemplates(apiId, api, tenantId);
    Map<String, KeyManagerDto> tenantKeyManagers = KeyManagerHolder.getTenantKeyManagers(tenantDomain);
    for (Map.Entry<String, KeyManagerDto> keyManagerDtoEntry : tenantKeyManagers.entrySet()) {
        KeyManager keyManager = keyManagerDtoEntry.getValue().getKeyManager();
        if (keyManager != null) {
            try {
                keyManager.attachResourceScopes(api, api.getUriTemplates());
            } catch (APIManagementException e) {
                log.error("Error while Attaching Resource to scope in Key Manager " + keyManagerDtoEntry.getKey(), e);
            }
        }
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager)

Example 13 with KeyManagerDto

use of org.wso2.carbon.apimgt.impl.dto.KeyManagerDto in project carbon-apimgt by wso2.

the class APIProviderImpl method addSharedScope.

/**
 * Add Shared Scope by registering it in the KM and adding the scope as a Shared Scope in AM DB.
 *
 * @param scope        Shared Scope
 * @param tenantDomain Tenant domain
 * @return UUId of the added Shared Scope object
 * @throws APIManagementException if failed to add a scope
 */
@Override
public String addSharedScope(Scope scope, String tenantDomain) throws APIManagementException {
    Set<Scope> scopeSet = new HashSet<>();
    scopeSet.add(scope);
    int tenantId = APIUtil.getTenantIdFromTenantDomain(tenantDomain);
    addScopes(scopeSet, tenantId);
    Map<String, KeyManagerDto> tenantKeyManagers = KeyManagerHolder.getTenantKeyManagers(tenantDomain);
    for (Map.Entry<String, KeyManagerDto> keyManagerDtoEntry : tenantKeyManagers.entrySet()) {
        KeyManager keyManager = keyManagerDtoEntry.getValue().getKeyManager();
        if (keyManager != null) {
            try {
                keyManager.registerScope(scope);
            } catch (APIManagementException e) {
                log.error("Error occurred while registering Scope in Key Manager " + keyManagerDtoEntry.getKey(), e);
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Adding shared scope mapping: " + scope.getKey() + " to  Key Manager : " + keyManagerDtoEntry.getKey());
        }
    }
    return ApiMgtDAO.getInstance().addSharedScope(scope, tenantDomain);
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 14 with KeyManagerDto

use of org.wso2.carbon.apimgt.impl.dto.KeyManagerDto in project carbon-apimgt by wso2.

the class JWTValidationServiceImpl method validateJWTToken.

@Override
public JWTValidationInfo validateJWTToken(SignedJWTInfo signedJWTInfo) throws APIManagementException {
    String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
    JWTValidationInfo jwtValidationInfo = new JWTValidationInfo();
    String issuer = signedJWTInfo.getJwtClaimsSet().getIssuer();
    if (StringUtils.isNotEmpty(issuer)) {
        KeyManagerDto keyManagerDto = KeyManagerHolder.getKeyManagerByIssuer(tenantDomain, issuer);
        if (keyManagerDto != null && keyManagerDto.getJwtValidator() != null) {
            JWTValidationInfo validationInfo = keyManagerDto.getJwtValidator().validateToken(signedJWTInfo);
            validationInfo.setKeyManager(keyManagerDto.getName());
            return validationInfo;
        }
    }
    jwtValidationInfo.setValid(false);
    jwtValidationInfo.setValidationCode(APIConstants.KeyValidationStatus.API_AUTH_GENERAL_ERROR);
    return jwtValidationInfo;
}
Also used : KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) JWTValidationInfo(org.wso2.carbon.apimgt.common.gateway.dto.JWTValidationInfo)

Example 15 with KeyManagerDto

use of org.wso2.carbon.apimgt.impl.dto.KeyManagerDto in project carbon-apimgt by wso2.

the class JWTValidationServiceImpl method getKeyManagerNameIfJwtValidatorExist.

@Override
public String getKeyManagerNameIfJwtValidatorExist(SignedJWTInfo signedJWTInfo) throws APIManagementException {
    String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
    String issuer = signedJWTInfo.getJwtClaimsSet().getIssuer();
    KeyManagerDto keyManagerDto = KeyManagerHolder.getKeyManagerByIssuer(tenantDomain, issuer);
    if (keyManagerDto != null && keyManagerDto.getJwtValidator() != null) {
        return keyManagerDto.getName();
    } else {
        return null;
    }
}
Also used : KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto)

Aggregations

KeyManagerDto (org.wso2.carbon.apimgt.impl.dto.KeyManagerDto)16 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)15 HashMap (java.util.HashMap)13 Map (java.util.Map)13 KeyManager (org.wso2.carbon.apimgt.api.model.KeyManager)13 TreeMap (java.util.TreeMap)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 KeyManagerConfigurationDTO (org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO)5 Gson (com.google.gson.Gson)4 APIAdmin (org.wso2.carbon.apimgt.api.APIAdmin)4 APIAdminImpl (org.wso2.carbon.apimgt.impl.APIAdminImpl)4 JsonObject (com.google.gson.JsonObject)3 ArrayList (java.util.ArrayList)3 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)3 Scope (org.wso2.carbon.apimgt.api.model.Scope)3 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)3 KeyManagerDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerDTO)3 JsonArray (com.google.gson.JsonArray)2 HashSet (java.util.HashSet)2 List (java.util.List)2