|
|
@ -44,9 +44,7 @@ import java.util.concurrent.ConcurrentHashMap; |
|
|
import java.util.concurrent.ExecutionException; |
|
|
import java.util.concurrent.ExecutionException; |
|
|
import java.util.concurrent.locks.ReentrantLock; |
|
|
import java.util.concurrent.locks.ReentrantLock; |
|
|
import java.util.function.Consumer; |
|
|
import java.util.function.Consumer; |
|
|
import java.util.function.Function; |
|
|
|
|
|
import java.util.stream.Collectors; |
|
|
import java.util.stream.Collectors; |
|
|
import java.util.stream.Stream; |
|
|
|
|
|
|
|
|
|
|
|
@Slf4j |
|
|
@Slf4j |
|
|
@Service |
|
|
@Service |
|
|
@ -131,7 +129,7 @@ public class OAuth2ServiceImpl implements OAuth2Service { |
|
|
lock.lock(); |
|
|
lock.lock(); |
|
|
try { |
|
|
try { |
|
|
validateUniqueRegistrationId(oAuth2ClientsParams, TenantId.SYS_TENANT_ID); |
|
|
validateUniqueRegistrationId(oAuth2ClientsParams, TenantId.SYS_TENANT_ID); |
|
|
AdminSettings clientRegistrationParamsSettings = createSystemOAuth2Settings(oAuth2ClientsParams); |
|
|
AdminSettings clientRegistrationParamsSettings = createSystemAdminSettings(oAuth2ClientsParams); |
|
|
adminSettingsService.saveAdminSettings(TenantId.SYS_TENANT_ID, clientRegistrationParamsSettings); |
|
|
adminSettingsService.saveAdminSettings(TenantId.SYS_TENANT_ID, clientRegistrationParamsSettings); |
|
|
clientsParams.put(TenantId.SYS_TENANT_ID, oAuth2ClientsParams); |
|
|
clientsParams.put(TenantId.SYS_TENANT_ID, oAuth2ClientsParams); |
|
|
} finally { |
|
|
} finally { |
|
|
@ -141,59 +139,50 @@ public class OAuth2ServiceImpl implements OAuth2Service { |
|
|
return getSystemOAuth2ClientsParams(TenantId.SYS_TENANT_ID); |
|
|
return getSystemOAuth2ClientsParams(TenantId.SYS_TENANT_ID); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void validateUniqueRegistrationId(OAuth2ClientsParams inputOAuth2ClientsParams, TenantId tenantId) { |
|
|
@Override |
|
|
inputOAuth2ClientsParams.getClientRegistrations().stream() |
|
|
public OAuth2ClientsParams saveTenantOAuth2ClientsParams(TenantId tenantId, OAuth2ClientsParams oAuth2ClientsParams) { |
|
|
.map(OAuth2ClientRegistration::getRegistrationId) |
|
|
// TODO what if tenant saves config for several different domain names, do we need to check it
|
|
|
.forEach(registrationId -> { |
|
|
validate(oAuth2ClientsParams); |
|
|
clientsParams.forEach((paramsTenantId, oAuth2ClientsParams) -> { |
|
|
// TODO check by registration ID in system
|
|
|
boolean registrationExists = oAuth2ClientsParams.getClientRegistrations().stream() |
|
|
|
|
|
.map(OAuth2ClientRegistration::getRegistrationId) |
|
|
|
|
|
.anyMatch(registrationId::equals); |
|
|
|
|
|
if (registrationExists && !tenantId.equals(paramsTenantId)) { |
|
|
|
|
|
log.error("Current registrationId [{}] already registered in the system!", registrationId); |
|
|
|
|
|
throw new IncorrectParameterException("Current registrationId [" + registrationId + "] already registered in the system!"); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private AdminSettings createSystemOAuth2Settings(OAuth2ClientsParams oAuth2ClientsParams) { |
|
|
String adminSettingsId = processTenantAdminSettings(tenantId, oAuth2ClientsParams.getDomainName(), oAuth2ClientsParams.getAdminSettingsId()); |
|
|
AdminSettings clientRegistrationParamsSettings = new AdminSettings(); |
|
|
oAuth2ClientsParams.setAdminSettingsId(adminSettingsId); |
|
|
clientRegistrationParamsSettings.setKey(OAUTH2_CLIENT_REGISTRATIONS_PARAMS); |
|
|
|
|
|
ObjectNode clientRegistrationsNode = mapper.createObjectNode(); |
|
|
|
|
|
|
|
|
|
|
|
String json; |
|
|
List<AttributeKvEntry> attributes = createOAuth2ClientsParamsAttributes(oAuth2ClientsParams); |
|
|
try { |
|
|
try { |
|
|
json = mapper.writeValueAsString(oAuth2ClientsParams); |
|
|
// TODO ask if I need .get() here
|
|
|
} catch (JsonProcessingException e) { |
|
|
attributesService.save(tenantId, tenantId, DataConstants.SERVER_SCOPE, attributes).get(); |
|
|
log.error("Unable to convert OAuth2 Client Registration Params to JSON!", e); |
|
|
} catch (Exception e) { |
|
|
throw new IncorrectParameterException("Unable to convert OAuth2 Client Registration Params to JSON!"); |
|
|
log.error("Unable to save OAuth2 Client Registration Params to attributes!", e); |
|
|
|
|
|
throw new IncorrectParameterException("Unable to save OAuth2 Client Registration Params to attributes!"); |
|
|
} |
|
|
} |
|
|
clientRegistrationsNode.put(SYSTEM_SETTINGS_OAUTH2_VALUE, json); |
|
|
return getTenantOAuth2ClientsParams(tenantId); |
|
|
clientRegistrationParamsSettings.setJsonValue(clientRegistrationsNode); |
|
|
} |
|
|
|
|
|
|
|
|
return clientRegistrationParamsSettings; |
|
|
private List<AttributeKvEntry> createOAuth2ClientsParamsAttributes(OAuth2ClientsParams oAuth2ClientsParams) { |
|
|
|
|
|
String json = toJson(oAuth2ClientsParams); |
|
|
|
|
|
List<AttributeKvEntry> attributes = new ArrayList<>(); |
|
|
|
|
|
long ts = System.currentTimeMillis(); |
|
|
|
|
|
attributes.add(new BaseAttributeKvEntry(new StringDataEntry(OAUTH2_CLIENT_REGISTRATIONS_PARAMS, json), ts)); |
|
|
|
|
|
return attributes; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
private String processTenantAdminSettings(TenantId tenantId, String domainName, String prevAdminSettingsId) { |
|
|
public OAuth2ClientsParams saveTenantOAuth2ClientsParams(TenantId tenantId, OAuth2ClientsParams oAuth2ClientsParams) { |
|
|
String selectedDomainSettingsKey = constructAdminSettingsDomainKey(domainName); |
|
|
// TODO ask what if tenant saves config for several different domain names, do we need to check it
|
|
|
AdminSettings existentAdminSettingsByKey = adminSettingsService.findAdminSettingsByKey(tenantId, selectedDomainSettingsKey); |
|
|
// TODO check by registration ID in system
|
|
|
if (StringUtils.isEmpty(prevAdminSettingsId)) { |
|
|
validate(oAuth2ClientsParams); |
|
|
|
|
|
String clientRegistrationsKey = constructClientRegistrationsKey(oAuth2ClientsParams.getDomainName()); |
|
|
|
|
|
AdminSettings existentAdminSettingsByKey = adminSettingsService.findAdminSettingsByKey(tenantId, clientRegistrationsKey); |
|
|
|
|
|
if (StringUtils.isEmpty(oAuth2ClientsParams.getAdminSettingsId())) { |
|
|
|
|
|
if (existentAdminSettingsByKey == null) { |
|
|
if (existentAdminSettingsByKey == null) { |
|
|
existentAdminSettingsByKey = saveOAuth2ClientSettings(tenantId, clientRegistrationsKey); |
|
|
AdminSettings tenantAdminSettings = createTenantAdminSettings(tenantId, selectedDomainSettingsKey); |
|
|
oAuth2ClientsParams.setAdminSettingsId(existentAdminSettingsByKey.getId().getId().toString()); |
|
|
existentAdminSettingsByKey = adminSettingsService.saveAdminSettings(tenantId, tenantAdminSettings); |
|
|
|
|
|
return existentAdminSettingsByKey.getId().getId().toString(); |
|
|
} else { |
|
|
} else { |
|
|
log.error("Current domain name [{}] already registered in the system!", oAuth2ClientsParams.getDomainName()); |
|
|
log.error("Current domain name [{}] already registered in the system!", domainName); |
|
|
throw new IncorrectParameterException("Current domain name [" + oAuth2ClientsParams.getDomainName() + "] already registered in the system!"); |
|
|
throw new IncorrectParameterException("Current domain name [" + domainName + "] already registered in the system!"); |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
AdminSettings existentOAuth2ClientsSettingsById = adminSettingsService.findAdminSettingsById( |
|
|
AdminSettings existentOAuth2ClientsSettingsById = adminSettingsService.findAdminSettingsById( |
|
|
tenantId, |
|
|
tenantId, |
|
|
new AdminSettingsId(UUID.fromString(oAuth2ClientsParams.getAdminSettingsId())) |
|
|
new AdminSettingsId(UUID.fromString(prevAdminSettingsId)) |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
if (existentOAuth2ClientsSettingsById == null) { |
|
|
if (existentOAuth2ClientsSettingsById == null) { |
|
|
@ -201,36 +190,61 @@ public class OAuth2ServiceImpl implements OAuth2Service { |
|
|
throw new IllegalStateException("Admin setting ID is already set in login white labeling object, but doesn't exist in the database"); |
|
|
throw new IllegalStateException("Admin setting ID is already set in login white labeling object, but doesn't exist in the database"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (!existentOAuth2ClientsSettingsById.getKey().equals(clientRegistrationsKey)) { |
|
|
if (!existentOAuth2ClientsSettingsById.getKey().equals(selectedDomainSettingsKey)) { |
|
|
if (existentAdminSettingsByKey == null) { |
|
|
if (existentAdminSettingsByKey == null) { |
|
|
adminSettingsService.deleteAdminSettingsByKey(tenantId, existentOAuth2ClientsSettingsById.getKey()); |
|
|
AdminSettings newOAuth2ClientsSettings = replaceExistentAdminSettings(tenantId, selectedDomainSettingsKey, existentOAuth2ClientsSettingsById.getKey()); |
|
|
AdminSettings newOAuth2ClientsSettings = saveOAuth2ClientSettings(tenantId, clientRegistrationsKey); |
|
|
return newOAuth2ClientsSettings.getId().getId().toString(); |
|
|
oAuth2ClientsParams.setAdminSettingsId(newOAuth2ClientsSettings.getId().getId().toString()); |
|
|
|
|
|
} else { |
|
|
} else { |
|
|
log.error("Current domain name [{}] already registered in the system!", oAuth2ClientsParams.getDomainName()); |
|
|
log.error("Current domain name [{}] already registered in the system!", domainName); |
|
|
throw new IncorrectParameterException("Current domain name [" + oAuth2ClientsParams.getDomainName() + "] already registered in the system!"); |
|
|
throw new IncorrectParameterException("Current domain name [" + domainName + "] already registered in the system!"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return prevAdminSettingsId; |
|
|
} |
|
|
} |
|
|
// TODO refactor
|
|
|
} |
|
|
String json; |
|
|
|
|
|
try { |
|
|
private AdminSettings replaceExistentAdminSettings(TenantId tenantId, String newKey, String oldKey) { |
|
|
json = mapper.writeValueAsString(oAuth2ClientsParams); |
|
|
adminSettingsService.deleteAdminSettingsByKey(tenantId, oldKey); |
|
|
} catch (JsonProcessingException e) { |
|
|
AdminSettings tenantAdminSettings = createTenantAdminSettings(tenantId, newKey); |
|
|
log.error("Unable to convert OAuth2 Client Registration Params to JSON!", e); |
|
|
return adminSettingsService.saveAdminSettings(tenantId, tenantAdminSettings); |
|
|
throw new IncorrectParameterException("Unable to convert OAuth2 Client Registration Params to JSON!"); |
|
|
} |
|
|
} |
|
|
|
|
|
List<AttributeKvEntry> attributes = new ArrayList<>(); |
|
|
private AdminSettings createTenantAdminSettings(TenantId tenantId, String clientRegistrationsKey) { |
|
|
long ts = System.currentTimeMillis(); |
|
|
AdminSettings clientRegistrationParamsSettings = new AdminSettings(); |
|
|
attributes.add(new BaseAttributeKvEntry(new StringDataEntry(OAUTH2_CLIENT_REGISTRATIONS_PARAMS, json), ts)); |
|
|
clientRegistrationParamsSettings.setKey(clientRegistrationsKey); |
|
|
try { |
|
|
ObjectNode node = mapper.createObjectNode(); |
|
|
// TODO ask if I need here .get()
|
|
|
node.put("entityType", EntityType.TENANT.name()); |
|
|
attributesService.save(tenantId, tenantId, DataConstants.SERVER_SCOPE, attributes).get(); |
|
|
node.put("entityId", tenantId.toString()); |
|
|
} catch (Exception e) { |
|
|
clientRegistrationParamsSettings.setJsonValue(node); |
|
|
log.error("Unable to save OAuth2 Client Registration Params to attributes!", e); |
|
|
return clientRegistrationParamsSettings; |
|
|
throw new IncorrectParameterException("Unable to save OAuth2 Client Registration Params to attributes!"); |
|
|
} |
|
|
} |
|
|
|
|
|
return getTenantOAuth2ClientsParams(tenantId); |
|
|
private AdminSettings createSystemAdminSettings(OAuth2ClientsParams oAuth2ClientsParams) { |
|
|
|
|
|
AdminSettings clientRegistrationParamsSettings = new AdminSettings(); |
|
|
|
|
|
clientRegistrationParamsSettings.setKey(OAUTH2_CLIENT_REGISTRATIONS_PARAMS); |
|
|
|
|
|
ObjectNode clientRegistrationsNode = mapper.createObjectNode(); |
|
|
|
|
|
|
|
|
|
|
|
String json = toJson(oAuth2ClientsParams); |
|
|
|
|
|
clientRegistrationsNode.put(SYSTEM_SETTINGS_OAUTH2_VALUE, json); |
|
|
|
|
|
clientRegistrationParamsSettings.setJsonValue(clientRegistrationsNode); |
|
|
|
|
|
|
|
|
|
|
|
return clientRegistrationParamsSettings; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void validateUniqueRegistrationId(OAuth2ClientsParams inputOAuth2ClientsParams, TenantId tenantId) { |
|
|
|
|
|
inputOAuth2ClientsParams.getClientRegistrations().stream() |
|
|
|
|
|
.map(OAuth2ClientRegistration::getRegistrationId) |
|
|
|
|
|
.forEach(registrationId -> { |
|
|
|
|
|
clientsParams.forEach((paramsTenantId, oAuth2ClientsParams) -> { |
|
|
|
|
|
boolean registrationExists = oAuth2ClientsParams.getClientRegistrations().stream() |
|
|
|
|
|
.map(OAuth2ClientRegistration::getRegistrationId) |
|
|
|
|
|
.anyMatch(registrationId::equals); |
|
|
|
|
|
if (registrationExists && !tenantId.equals(paramsTenantId)) { |
|
|
|
|
|
log.error("Current registrationId [{}] already registered in the system!", registrationId); |
|
|
|
|
|
throw new IncorrectParameterException("Current registrationId [" + registrationId + "] already registered in the system!"); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void validate(OAuth2ClientsParams oAuth2ClientsParams) { |
|
|
private void validate(OAuth2ClientsParams oAuth2ClientsParams) { |
|
|
@ -276,8 +290,8 @@ public class OAuth2ServiceImpl implements OAuth2Service { |
|
|
OAuth2ClientsParams params = getTenantOAuth2ClientsParams(tenantId); |
|
|
OAuth2ClientsParams params = getTenantOAuth2ClientsParams(tenantId); |
|
|
if (!StringUtils.isEmpty(params.getDomainName())) { |
|
|
if (!StringUtils.isEmpty(params.getDomainName())) { |
|
|
// TODO don't we need to delete from attributes?
|
|
|
// TODO don't we need to delete from attributes?
|
|
|
String oauth2ClientsParamsKey = constructClientRegistrationsKey(params.getDomainName()); |
|
|
String settingsKey = constructAdminSettingsDomainKey(params.getDomainName()); |
|
|
adminSettingsService.deleteAdminSettingsByKey(tenantId, oauth2ClientsParamsKey); |
|
|
adminSettingsService.deleteAdminSettingsByKey(tenantId, settingsKey); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -310,17 +324,7 @@ public class OAuth2ServiceImpl implements OAuth2Service { |
|
|
}, MoreExecutors.directExecutor()); |
|
|
}, MoreExecutors.directExecutor()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private AdminSettings saveOAuth2ClientSettings(TenantId tenantId, String clientRegistrationsKey) { |
|
|
private String constructAdminSettingsDomainKey(String domainName) { |
|
|
AdminSettings oauth2ClientsSettings = new AdminSettings(); |
|
|
|
|
|
oauth2ClientsSettings.setKey(clientRegistrationsKey); |
|
|
|
|
|
ObjectNode node = mapper.createObjectNode(); |
|
|
|
|
|
node.put("entityType", EntityType.TENANT.name()); |
|
|
|
|
|
node.put("entityId", tenantId.toString()); |
|
|
|
|
|
oauth2ClientsSettings.setJsonValue(node); |
|
|
|
|
|
return adminSettingsService.saveAdminSettings(tenantId, oauth2ClientsSettings); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private String constructClientRegistrationsKey(String domainName) { |
|
|
|
|
|
String clientRegistrationsKey; |
|
|
String clientRegistrationsKey; |
|
|
if (StringUtils.isEmpty(domainName)) { |
|
|
if (StringUtils.isEmpty(domainName)) { |
|
|
clientRegistrationsKey = OAUTH2_CLIENT_REGISTRATIONS_PARAMS; |
|
|
clientRegistrationsKey = OAUTH2_CLIENT_REGISTRATIONS_PARAMS; |
|
|
@ -347,7 +351,7 @@ public class OAuth2ServiceImpl implements OAuth2Service { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private OAuth2ClientsParams getMergedOAuth2ClientsParams(String domainName) { |
|
|
private OAuth2ClientsParams getMergedOAuth2ClientsParams(String domainName) { |
|
|
AdminSettings oauth2ClientsSettings = adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, constructClientRegistrationsKey(domainName)); |
|
|
AdminSettings oauth2ClientsSettings = adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, constructAdminSettingsDomainKey(domainName)); |
|
|
OAuth2ClientsParams result; |
|
|
OAuth2ClientsParams result; |
|
|
if (oauth2ClientsSettings != null) { |
|
|
if (oauth2ClientsSettings != null) { |
|
|
String strEntityType = oauth2ClientsSettings.getJsonValue().get("entityType").asText(); |
|
|
String strEntityType = oauth2ClientsSettings.getJsonValue().get("entityType").asText(); |
|
|
@ -367,6 +371,17 @@ public class OAuth2ServiceImpl implements OAuth2Service { |
|
|
return result; |
|
|
return result; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private String toJson(OAuth2ClientsParams oAuth2ClientsParams) { |
|
|
|
|
|
String json; |
|
|
|
|
|
try { |
|
|
|
|
|
json = mapper.writeValueAsString(oAuth2ClientsParams); |
|
|
|
|
|
} catch (JsonProcessingException e) { |
|
|
|
|
|
log.error("Unable to convert OAuth2 Client Registration Params to JSON!", e); |
|
|
|
|
|
throw new IncorrectParameterException("Unable to convert OAuth2 Client Registration Params to JSON!"); |
|
|
|
|
|
} |
|
|
|
|
|
return json; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private final Consumer<OAuth2ClientRegistration> validator = clientRegistration -> { |
|
|
private final Consumer<OAuth2ClientRegistration> validator = clientRegistration -> { |
|
|
if (StringUtils.isEmpty(clientRegistration.getRegistrationId())) { |
|
|
if (StringUtils.isEmpty(clientRegistration.getRegistrationId())) { |
|
|
throw new DataValidationException("Registration ID should be specified!"); |
|
|
throw new DataValidationException("Registration ID should be specified!"); |
|
|
|