Commit 6a2bc5a9ffcd0a309b87d2bfbd6db48885b3846e

Authored by viktor
1 parent 5a00973b

Implemented 'getOAuth2Clients' method

... ... @@ -19,18 +19,17 @@ import com.fasterxml.jackson.core.JsonProcessingException;
19 19 import com.fasterxml.jackson.databind.JsonNode;
20 20 import com.fasterxml.jackson.databind.ObjectMapper;
21 21 import com.fasterxml.jackson.databind.node.ObjectNode;
  22 +import com.google.common.collect.Lists;
22 23 import com.google.common.util.concurrent.Futures;
23 24 import com.google.common.util.concurrent.ListenableFuture;
24 25 import com.google.common.util.concurrent.MoreExecutors;
25 26 import lombok.extern.slf4j.Slf4j;
  27 +import org.apache.commons.collections.ListUtils;
26 28 import org.springframework.beans.factory.annotation.Autowired;
27 29 import org.springframework.stereotype.Service;
28 30 import org.springframework.util.StringUtils;
29 31 import org.thingsboard.server.common.data.*;
30   -import org.thingsboard.server.common.data.id.AdminSettingsId;
31   -import org.thingsboard.server.common.data.id.CustomerId;
32   -import org.thingsboard.server.common.data.id.EntityId;
33   -import org.thingsboard.server.common.data.id.TenantId;
  32 +import org.thingsboard.server.common.data.id.*;
34 33 import org.thingsboard.server.common.data.kv.AttributeKvEntry;
35 34 import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry;
36 35 import org.thingsboard.server.common.data.kv.StringDataEntry;
... ... @@ -41,10 +40,13 @@ import org.thingsboard.server.dao.exception.IncorrectParameterException;
41 40 import org.thingsboard.server.dao.settings.AdminSettingsService;
42 41 import org.thingsboard.server.dao.tenant.TenantService;
43 42
  43 +import javax.annotation.PostConstruct;
44 44 import java.io.IOException;
45 45 import java.util.*;
  46 +import java.util.concurrent.ConcurrentHashMap;
46 47 import java.util.concurrent.ExecutionException;
47 48 import java.util.function.Consumer;
  49 +import java.util.stream.Collectors;
48 50
49 51 @Slf4j
50 52 @Service
... ... @@ -71,9 +73,30 @@ public class OAuth2ServiceImpl implements OAuth2Service {
71 73 @Autowired
72 74 private TenantService tenantService;
73 75
  76 + private final Map<String, OAuth2ClientRegistration> clientRegistrationsByRegistrationId = new ConcurrentHashMap<>();
  77 +
  78 + @PostConstruct
  79 + public void init(){
  80 +
  81 + }
  82 +
  83 + @Override
  84 + public OAuth2ClientRegistration getClientRegistration(String registrationId) {
  85 + return null;
  86 + }
  87 +
74 88 @Override
75 89 public List<OAuth2ClientInfo> getOAuth2Clients(String domainName) {
76   - return Collections.emptyList();
  90 + OAuth2ClientsParams oAuth2ClientsParams = getMergedOAuth2ClientsParams(domainName);
  91 + return oAuth2ClientsParams.getClientRegistrations().stream()
  92 + .map(clientRegistration -> {
  93 + OAuth2ClientInfo client = new OAuth2ClientInfo();
  94 + client.setName(clientRegistration.getLoginButtonLabel());
  95 + client.setUrl(String.format(OAUTH2_AUTHORIZATION_PATH_TEMPLATE, clientRegistration.getRegistrationId()));
  96 + client.setIcon(clientRegistration.getLoginButtonIcon());
  97 + return client;
  98 + })
  99 + .collect(Collectors.toList());
77 100 }
78 101
79 102 @Override
... ... @@ -209,11 +232,6 @@ public class OAuth2ServiceImpl implements OAuth2Service {
209 232 }
210 233 }
211 234
212   - @Override
213   - public OAuth2ClientRegistration getClientRegistration(String registrationId) {
214   - return null;
215   - }
216   -
217 235 private ListenableFuture<String> getOAuth2ClientsParamsAttribute(TenantId tenantId) {
218 236 ListenableFuture<List<AttributeKvEntry>> attributeKvEntriesFuture;
219 237 try {
... ... @@ -269,6 +287,27 @@ public class OAuth2ServiceImpl implements OAuth2Service {
269 287 return result;
270 288 }
271 289
  290 + private OAuth2ClientsParams getMergedOAuth2ClientsParams(String domainName) {
  291 + AdminSettings oauth2ClientsSettings = adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, constructClientRegistrationsKey(domainName));
  292 + OAuth2ClientsParams result;
  293 + if (oauth2ClientsSettings != null) {
  294 + String strEntityType = oauth2ClientsSettings.getJsonValue().get("entityType").asText();
  295 + String strEntityId = oauth2ClientsSettings.getJsonValue().get("entityId").asText();
  296 + EntityId entityId = EntityIdFactory.getByTypeAndId(strEntityType, strEntityId);
  297 + if (!entityId.getEntityType().equals(EntityType.TENANT)) {
  298 + log.error("Only tenant can configure OAuth2 for certain domain!");
  299 + throw new IllegalStateException("Only tenant can configure OAuth2 for certain domain!");
  300 + }
  301 + TenantId tenantId = (TenantId) entityId;
  302 + result = getTenantOAuth2ClientsParams(tenantId);
  303 + OAuth2ClientsParams systemOAuth2ClientsParams = getSystemOAuth2ClientsParams(TenantId.SYS_TENANT_ID);
  304 + result.getClientRegistrations().addAll(systemOAuth2ClientsParams.getClientRegistrations());
  305 + } else {
  306 + result = getSystemOAuth2ClientsParams(TenantId.SYS_TENANT_ID);
  307 + }
  308 + return result;
  309 + }
  310 +
272 311 private final Consumer<OAuth2ClientRegistration> validator = clientRegistration -> {
273 312 if (StringUtils.isEmpty(clientRegistration.getRegistrationId())) {
274 313 throw new DataValidationException("Registration ID should be specified!");
... ...