use of org.wso2.carbon.apimgt.impl.dto.TokenHandlingDto in project carbon-apimgt by wso2.
the class AbstractKeyManager method canHandleToken.
@Override
public boolean canHandleToken(String accessToken) throws APIManagementException {
boolean result = false;
boolean canHandle = false;
Object tokenHandlingScript = configuration.getParameter(APIConstants.KeyManager.TOKEN_FORMAT_STRING);
if (tokenHandlingScript != null && StringUtils.isNotEmpty((String) tokenHandlingScript)) {
TokenHandlingDto[] tokenHandlers = new Gson().fromJson((String) tokenHandlingScript, TokenHandlingDto[].class);
if (tokenHandlers.length == 0) {
return true;
}
for (TokenHandlingDto tokenHandler : tokenHandlers) {
if (tokenHandler.getEnable()) {
if (TokenHandlingDto.TypeEnum.REFERENCE.equals(tokenHandler.getType())) {
if (tokenHandler.getValue() != null && StringUtils.isNotEmpty(String.valueOf(tokenHandler.getValue()))) {
Pattern pattern = Pattern.compile((String) tokenHandler.getValue());
Matcher matcher = pattern.matcher(accessToken);
canHandle = matcher.find();
}
} else if (TokenHandlingDto.TypeEnum.JWT.equals(tokenHandler.getType()) && accessToken.contains(APIConstants.DOT)) {
Map<String, Map<String, String>> validationJson = (Map<String, Map<String, String>>) tokenHandler.getValue();
try {
SignedJWT signedJWT = SignedJWT.parse(accessToken);
JWTClaimsSet jwtClaimsSet = signedJWT.getJWTClaimsSet();
for (Map.Entry<String, Map<String, String>> entry : validationJson.entrySet()) {
if (APIConstants.KeyManager.VALIDATION_ENTRY_JWT_BODY.equals(entry.getKey())) {
boolean state = false;
for (Map.Entry<String, String> e : entry.getValue().entrySet()) {
String key = e.getKey();
String value = e.getValue();
Object claimValue = jwtClaimsSet.getClaim(key);
if (claimValue != null) {
Pattern pattern = Pattern.compile(value);
Matcher matcher = pattern.matcher((String) claimValue);
state = matcher.find();
} else {
state = false;
}
}
canHandle = state;
}
}
} catch (java.text.ParseException e) {
log.warn("Error while parsing Token", e);
}
}
if (canHandle) {
result = true;
break;
}
}
}
} else {
result = true;
}
return result;
}
use of org.wso2.carbon.apimgt.impl.dto.TokenHandlingDto in project carbon-apimgt by wso2.
the class KeyMgtRegistrationService method registerDefaultKeyManager.
public static void registerDefaultKeyManager(String organization) throws APIManagementException {
synchronized (KeyMgtRegistrationService.class.getName().concat(organization)) {
ApiMgtDAO instance = ApiMgtDAO.getInstance();
if (instance.getKeyManagerConfigurationByName(organization, APIConstants.KeyManager.DEFAULT_KEY_MANAGER) == null) {
APIManagerConfigurationService apiManagerConfigurationService = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService();
KeyManagerConfigurationDTO keyManagerConfigurationDTO = new KeyManagerConfigurationDTO();
keyManagerConfigurationDTO.setName(APIConstants.KeyManager.DEFAULT_KEY_MANAGER);
keyManagerConfigurationDTO.setEnabled(true);
keyManagerConfigurationDTO.setUuid(UUID.randomUUID().toString());
keyManagerConfigurationDTO.setOrganization(organization);
keyManagerConfigurationDTO.setDescription(APIConstants.KeyManager.DEFAULT_KEY_MANAGER_DESCRIPTION);
keyManagerConfigurationDTO.setTokenType(KeyManagerConfiguration.TokenType.DIRECT.toString());
if (apiManagerConfigurationService != null && apiManagerConfigurationService.getAPIManagerConfiguration() != null) {
String defaultKeyManagerType = apiManagerConfigurationService.getAPIManagerConfiguration().getFirstProperty(APIConstants.DEFAULT_KEY_MANAGER_TYPE);
if (StringUtils.isNotEmpty(defaultKeyManagerType)) {
keyManagerConfigurationDTO.setType(defaultKeyManagerType);
} else {
keyManagerConfigurationDTO.setType(APIConstants.KeyManager.DEFAULT_KEY_MANAGER_TYPE);
}
}
TokenHandlingDto tokenHandlingDto = new TokenHandlingDto();
tokenHandlingDto.setEnable(true);
tokenHandlingDto.setType(TokenHandlingDto.TypeEnum.REFERENCE);
tokenHandlingDto.setValue(APIConstants.KeyManager.UUID_REGEX);
keyManagerConfigurationDTO.addProperty(APIConstants.KeyManager.TOKEN_FORMAT_STRING, new Gson().toJson(Arrays.asList(tokenHandlingDto)));
instance.addKeyManagerConfiguration(keyManagerConfigurationDTO);
}
}
}
Aggregations