Refactored
This commit is contained in:
parent
1628f79873
commit
32b9f58997
@ -54,14 +54,13 @@ public class OAuth2ServiceImpl implements OAuth2Service {
|
|||||||
|
|
||||||
private static final String OAUTH2_CLIENT_REGISTRATIONS_PARAMS = "oauth2ClientRegistrationsParams";
|
private static final String OAUTH2_CLIENT_REGISTRATIONS_PARAMS = "oauth2ClientRegistrationsParams";
|
||||||
private static final String OAUTH2_CLIENT_REGISTRATIONS_DOMAIN_NAME_PREFIX = "oauth2ClientRegistrationsDomainNamePrefix";
|
private static final String OAUTH2_CLIENT_REGISTRATIONS_DOMAIN_NAME_PREFIX = "oauth2ClientRegistrationsDomainNamePrefix";
|
||||||
|
|
||||||
private static final String ALLOW_OAUTH2_CONFIGURATION = "allowOAuth2Configuration";
|
private static final String ALLOW_OAUTH2_CONFIGURATION = "allowOAuth2Configuration";
|
||||||
|
|
||||||
|
|
||||||
private static final String SYSTEM_SETTINGS_OAUTH2_VALUE = "value";
|
private static final String SYSTEM_SETTINGS_OAUTH2_VALUE = "value";
|
||||||
|
|
||||||
private static final String OAUTH2_AUTHORIZATION_PATH_TEMPLATE = "/oauth2/authorization/%s";
|
private static final String OAUTH2_AUTHORIZATION_PATH_TEMPLATE = "/oauth2/authorization/%s";
|
||||||
|
|
||||||
|
private final ReentrantLock lock = new ReentrantLock();
|
||||||
|
private final Map<TenantId, OAuth2ClientsParams> clientsParams = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Environment environment;
|
private Environment environment;
|
||||||
|
|
||||||
@ -74,16 +73,11 @@ 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 boolean isInstall() {
|
private boolean isInstall() {
|
||||||
return environment.acceptsProfiles("install");
|
return environment.acceptsProfiles("install");
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO add field that invalidates cache in case write to cache fails after successful saving in DB
|
// TODO do I need to add a field that invalidates cache in case write to cache fails after successful saving in DB?
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
if (isInstall()) return;
|
if (isInstall()) return;
|
||||||
@ -95,7 +89,7 @@ public class OAuth2ServiceImpl implements OAuth2Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public OAuth2ClientRegistration getClientRegistration(String registrationId) {
|
public OAuth2ClientRegistration getClientRegistration(String registrationId) {
|
||||||
return clientsParams.values().stream()
|
return clientsParams.values().stream()
|
||||||
.flatMap(oAuth2ClientsParams -> oAuth2ClientsParams.getClientRegistrations().stream())
|
.flatMap(oAuth2ClientsParams -> oAuth2ClientsParams.getClientRegistrations().stream())
|
||||||
.filter(clientRegistration -> registrationId.equals(clientRegistration.getRegistrationId()))
|
.filter(clientRegistration -> registrationId.equals(clientRegistration.getRegistrationId()))
|
||||||
.findFirst()
|
.findFirst()
|
||||||
@ -113,14 +107,6 @@ public class OAuth2ServiceImpl implements OAuth2Service {
|
|||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
private OAuth2ClientInfo toClientInfo(OAuth2ClientRegistration clientRegistration) {
|
|
||||||
OAuth2ClientInfo client = new OAuth2ClientInfo();
|
|
||||||
client.setName(clientRegistration.getLoginButtonLabel());
|
|
||||||
client.setUrl(String.format(OAUTH2_AUTHORIZATION_PATH_TEMPLATE, clientRegistration.getRegistrationId()));
|
|
||||||
client.setIcon(clientRegistration.getLoginButtonIcon());
|
|
||||||
return client;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public OAuth2ClientsParams saveSystemOAuth2ClientsParams(OAuth2ClientsParams oAuth2ClientsParams) {
|
public OAuth2ClientsParams saveSystemOAuth2ClientsParams(OAuth2ClientsParams oAuth2ClientsParams) {
|
||||||
validate(oAuth2ClientsParams);
|
validate(oAuth2ClientsParams);
|
||||||
@ -286,11 +272,13 @@ public class OAuth2ServiceImpl implements OAuth2Service {
|
|||||||
try {
|
try {
|
||||||
return Futures.transform(jsonFuture,
|
return Futures.transform(jsonFuture,
|
||||||
clientsParamsByKvEntryKey -> {
|
clientsParamsByKvEntryKey -> {
|
||||||
Map<TenantId, OAuth2ClientsParams> tenantClientParams = clientsParamsByKvEntryKey.entrySet().stream()
|
Map<TenantId, OAuth2ClientsParams> tenantClientParams = clientsParamsByKvEntryKey != null ?
|
||||||
.collect(Collectors.toMap(
|
clientsParamsByKvEntryKey.entrySet().stream()
|
||||||
entry -> new TenantId(UUIDConverter.fromString(entry.getKey())),
|
.collect(Collectors.toMap(
|
||||||
entry -> constructOAuth2ClientsParams(entry.getValue())
|
entry -> new TenantId(UUIDConverter.fromString(entry.getKey())),
|
||||||
));
|
entry -> constructOAuth2ClientsParams(entry.getValue())
|
||||||
|
))
|
||||||
|
: new HashMap<>();
|
||||||
tenantClientParams.put(TenantId.SYS_TENANT_ID, systemOAuth2ClientsParams);
|
tenantClientParams.put(TenantId.SYS_TENANT_ID, systemOAuth2ClientsParams);
|
||||||
return tenantClientParams;
|
return tenantClientParams;
|
||||||
},
|
},
|
||||||
@ -341,6 +329,29 @@ public class OAuth2ServiceImpl implements OAuth2Service {
|
|||||||
}, MoreExecutors.directExecutor());
|
}, MoreExecutors.directExecutor());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private OAuth2ClientsParams getMergedOAuth2ClientsParams(String domainName) {
|
||||||
|
AdminSettings oauth2ClientsSettings = adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, constructAdminSettingsDomainKey(domainName));
|
||||||
|
OAuth2ClientsParams result;
|
||||||
|
if (oauth2ClientsSettings != null) {
|
||||||
|
String strEntityType = oauth2ClientsSettings.getJsonValue().get("entityType").asText();
|
||||||
|
String strEntityId = oauth2ClientsSettings.getJsonValue().get("entityId").asText();
|
||||||
|
EntityId entityId = EntityIdFactory.getByTypeAndId(strEntityType, strEntityId);
|
||||||
|
if (!entityId.getEntityType().equals(EntityType.TENANT)) {
|
||||||
|
log.error("Only tenant can configure OAuth2 for certain domain!");
|
||||||
|
throw new IllegalStateException("Only tenant can configure OAuth2 for certain domain!");
|
||||||
|
}
|
||||||
|
TenantId tenantId = (TenantId) entityId;
|
||||||
|
result = getTenantOAuth2ClientsParams(tenantId);
|
||||||
|
OAuth2ClientsParams systemOAuth2ClientsParams = getSystemOAuth2ClientsParams(TenantId.SYS_TENANT_ID);
|
||||||
|
if (systemOAuth2ClientsParams != null) {
|
||||||
|
result.getClientRegistrations().addAll(systemOAuth2ClientsParams.getClientRegistrations());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
result = getSystemOAuth2ClientsParams(TenantId.SYS_TENANT_ID);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
private String constructAdminSettingsDomainKey(String domainName) {
|
private String constructAdminSettingsDomainKey(String domainName) {
|
||||||
String clientRegistrationsKey;
|
String clientRegistrationsKey;
|
||||||
if (StringUtils.isEmpty(domainName)) {
|
if (StringUtils.isEmpty(domainName)) {
|
||||||
@ -367,27 +378,6 @@ public class OAuth2ServiceImpl implements OAuth2Service {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private OAuth2ClientsParams getMergedOAuth2ClientsParams(String domainName) {
|
|
||||||
AdminSettings oauth2ClientsSettings = adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, constructAdminSettingsDomainKey(domainName));
|
|
||||||
OAuth2ClientsParams result;
|
|
||||||
if (oauth2ClientsSettings != null) {
|
|
||||||
String strEntityType = oauth2ClientsSettings.getJsonValue().get("entityType").asText();
|
|
||||||
String strEntityId = oauth2ClientsSettings.getJsonValue().get("entityId").asText();
|
|
||||||
EntityId entityId = EntityIdFactory.getByTypeAndId(strEntityType, strEntityId);
|
|
||||||
if (!entityId.getEntityType().equals(EntityType.TENANT)) {
|
|
||||||
log.error("Only tenant can configure OAuth2 for certain domain!");
|
|
||||||
throw new IllegalStateException("Only tenant can configure OAuth2 for certain domain!");
|
|
||||||
}
|
|
||||||
TenantId tenantId = (TenantId) entityId;
|
|
||||||
result = getTenantOAuth2ClientsParams(tenantId);
|
|
||||||
OAuth2ClientsParams systemOAuth2ClientsParams = getSystemOAuth2ClientsParams(TenantId.SYS_TENANT_ID);
|
|
||||||
result.getClientRegistrations().addAll(systemOAuth2ClientsParams.getClientRegistrations());
|
|
||||||
} else {
|
|
||||||
result = getSystemOAuth2ClientsParams(TenantId.SYS_TENANT_ID);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String toJson(OAuth2ClientsParams oAuth2ClientsParams) {
|
private String toJson(OAuth2ClientsParams oAuth2ClientsParams) {
|
||||||
String json;
|
String json;
|
||||||
try {
|
try {
|
||||||
@ -399,6 +389,14 @@ public class OAuth2ServiceImpl implements OAuth2Service {
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private OAuth2ClientInfo toClientInfo(OAuth2ClientRegistration clientRegistration) {
|
||||||
|
OAuth2ClientInfo client = new OAuth2ClientInfo();
|
||||||
|
client.setName(clientRegistration.getLoginButtonLabel());
|
||||||
|
client.setUrl(String.format(OAUTH2_AUTHORIZATION_PATH_TEMPLATE, clientRegistration.getRegistrationId()));
|
||||||
|
client.setIcon(clientRegistration.getLoginButtonIcon());
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
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!");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user