Added saving SystemOAuth2 config in cache
This commit is contained in:
parent
8e1e1ec3eb
commit
2d42d4e3fd
@ -42,6 +42,7 @@ import java.io.IOException;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -75,6 +76,8 @@ public class OAuth2ServiceImpl implements OAuth2Service {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private TenantService tenantService;
|
private TenantService tenantService;
|
||||||
|
|
||||||
|
private final ReentrantLock lock = new ReentrantLock();
|
||||||
|
|
||||||
private final Map<TenantId, OAuth2ClientsParams> clientsParams = new ConcurrentHashMap<>();
|
private final Map<TenantId, OAuth2ClientsParams> clientsParams = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
|
||||||
@ -122,15 +125,43 @@ public class OAuth2ServiceImpl implements OAuth2Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public OAuth2ClientsParams saveSystemOAuth2ClientsParams(OAuth2ClientsParams oAuth2ClientsParams) {
|
public OAuth2ClientsParams saveSystemOAuth2ClientsParams(OAuth2ClientsParams oAuth2ClientsParams) {
|
||||||
// TODO check by registration ID in entities
|
|
||||||
validate(oAuth2ClientsParams);
|
validate(oAuth2ClientsParams);
|
||||||
|
|
||||||
|
validateUniqueRegistrationId(oAuth2ClientsParams, TenantId.SYS_TENANT_ID);
|
||||||
|
lock.lock();
|
||||||
|
try {
|
||||||
|
validateUniqueRegistrationId(oAuth2ClientsParams, TenantId.SYS_TENANT_ID);
|
||||||
|
AdminSettings clientRegistrationParamsSettings = createSystemOAuth2Settings(oAuth2ClientsParams);
|
||||||
|
adminSettingsService.saveAdminSettings(TenantId.SYS_TENANT_ID, clientRegistrationParamsSettings);
|
||||||
|
clientsParams.put(TenantId.SYS_TENANT_ID, oAuth2ClientsParams);
|
||||||
|
} finally {
|
||||||
|
lock.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
return getSystemOAuth2ClientsParams(TenantId.SYS_TENANT_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 AdminSettings createSystemOAuth2Settings(OAuth2ClientsParams oAuth2ClientsParams) {
|
||||||
AdminSettings clientRegistrationParamsSettings = new AdminSettings();
|
AdminSettings clientRegistrationParamsSettings = new AdminSettings();
|
||||||
clientRegistrationParamsSettings.setKey(OAUTH2_CLIENT_REGISTRATIONS_PARAMS);
|
clientRegistrationParamsSettings.setKey(OAUTH2_CLIENT_REGISTRATIONS_PARAMS);
|
||||||
ObjectNode clientRegistrationsNode = mapper.createObjectNode();
|
ObjectNode clientRegistrationsNode = mapper.createObjectNode();
|
||||||
|
|
||||||
oAuth2ClientsParams.setDomainName("");
|
|
||||||
String json;
|
String json;
|
||||||
try {
|
try {
|
||||||
json = mapper.writeValueAsString(oAuth2ClientsParams);
|
json = mapper.writeValueAsString(oAuth2ClientsParams);
|
||||||
@ -139,12 +170,9 @@ public class OAuth2ServiceImpl implements OAuth2Service {
|
|||||||
throw new IncorrectParameterException("Unable to convert OAuth2 Client Registration Params to JSON!");
|
throw new IncorrectParameterException("Unable to convert OAuth2 Client Registration Params to JSON!");
|
||||||
}
|
}
|
||||||
clientRegistrationsNode.put(SYSTEM_SETTINGS_OAUTH2_VALUE, json);
|
clientRegistrationsNode.put(SYSTEM_SETTINGS_OAUTH2_VALUE, json);
|
||||||
|
|
||||||
clientRegistrationParamsSettings.setJsonValue(clientRegistrationsNode);
|
clientRegistrationParamsSettings.setJsonValue(clientRegistrationsNode);
|
||||||
|
|
||||||
adminSettingsService.saveAdminSettings(TenantId.SYS_TENANT_ID, clientRegistrationParamsSettings);
|
return clientRegistrationParamsSettings;
|
||||||
|
|
||||||
return getSystemOAuth2ClientsParams(TenantId.SYS_TENANT_ID);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user