Browse Source

Added mapperConfig to OAuth2Template

pull/3575/head
vzikratyi 6 years ago
committed by Vladyslav_Prykhodko
parent
commit
4952c310a4
  1. 13
      application/src/main/data/json/system/oauth2_config_templates/facebook_config.json
  2. 7
      application/src/main/data/json/system/oauth2_config_templates/github_config.json
  3. 13
      application/src/main/data/json/system/oauth2_config_templates/google_config.json
  4. 6
      common/data/src/main/java/org/thingsboard/server/common/data/oauth2/OAuth2ClientRegistrationTemplate.java
  5. 50
      dao/src/main/java/org/thingsboard/server/dao/model/sql/OAuth2ClientRegistrationTemplateEntity.java
  6. 8
      dao/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2ConfigTemplateServiceImpl.java
  7. 10
      dao/src/test/java/org/thingsboard/server/dao/service/BaseOAuth2ConfigTemplateServiceTest.java

13
application/src/main/data/json/system/oauth2_config_templates/facebook_config.json

@ -7,11 +7,14 @@
"userInfoUri": "https://graph.facebook.com/me?fields=id,name,first_name,last_name,email", "userInfoUri": "https://graph.facebook.com/me?fields=id,name,first_name,last_name,email",
"clientAuthenticationMethod": "BASIC", "clientAuthenticationMethod": "BASIC",
"userNameAttributeName": "email", "userNameAttributeName": "email",
"basic": { "mapperConfig": {
"emailAttributeKey": "email", "type": "BASIC",
"firstNameAttributeKey": "first_name", "basic": {
"lastNameAttributeKey": "last_name", "emailAttributeKey": "email",
"tenantNameStrategy": "DOMAIN" "firstNameAttributeKey": "first_name",
"lastNameAttributeKey": "last_name",
"tenantNameStrategy": "DOMAIN"
}
}, },
"comment": null, "comment": null,
"loginButtonIcon": "mdi:facebook", "loginButtonIcon": "mdi:facebook",

7
application/src/main/data/json/system/oauth2_config_templates/github_config.json

@ -7,8 +7,11 @@
"userInfoUri": "https://api.github.com/user", "userInfoUri": "https://api.github.com/user",
"clientAuthenticationMethod": "BASIC", "clientAuthenticationMethod": "BASIC",
"userNameAttributeName": "login", "userNameAttributeName": "login",
"basic": { "mapperConfig": {
"tenantNameStrategy": "DOMAIN" "type": "GITHUB",
"basic": {
"tenantNameStrategy": "DOMAIN"
}
}, },
"comment": "In order to log into ThingsBoard you need to have user's email. You may configure and use Custom OAuth2 Mapper to get email information. Please refer to <a href=\"https://docs.github.com/en/rest/reference/users#list-email-addresses-for-the-authenticated-user\">Github Documentation</a>", "comment": "In order to log into ThingsBoard you need to have user's email. You may configure and use Custom OAuth2 Mapper to get email information. Please refer to <a href=\"https://docs.github.com/en/rest/reference/users#list-email-addresses-for-the-authenticated-user\">Github Documentation</a>",
"loginButtonIcon": "mdi:github", "loginButtonIcon": "mdi:github",

13
application/src/main/data/json/system/oauth2_config_templates/google_config.json

@ -8,11 +8,14 @@
"userInfoUri": "https://openidconnect.googleapis.com/v1/userinfo", "userInfoUri": "https://openidconnect.googleapis.com/v1/userinfo",
"clientAuthenticationMethod": "BASIC", "clientAuthenticationMethod": "BASIC",
"userNameAttributeName": "email", "userNameAttributeName": "email",
"basic": { "mapperConfig": {
"emailAttributeKey": "email", "type": "BASIC",
"firstNameAttributeKey": "given_name", "basic": {
"lastNameAttributeKey": "family_name", "emailAttributeKey": "email",
"tenantNameStrategy": "DOMAIN" "firstNameAttributeKey": "given_name",
"lastNameAttributeKey": "family_name",
"tenantNameStrategy": "DOMAIN"
}
}, },
"comment": null, "comment": null,
"loginButtonIcon": "mdi:google", "loginButtonIcon": "mdi:google",

6
common/data/src/main/java/org/thingsboard/server/common/data/oauth2/OAuth2ClientRegistrationTemplate.java

@ -34,8 +34,7 @@ import java.util.List;
public class OAuth2ClientRegistrationTemplate extends SearchTextBasedWithAdditionalInfo<OAuth2ClientRegistrationTemplateId> implements HasName { public class OAuth2ClientRegistrationTemplate extends SearchTextBasedWithAdditionalInfo<OAuth2ClientRegistrationTemplateId> implements HasName {
private String providerId; private String providerId;
private MapperType mapperType; private OAuth2MapperConfig mapperConfig;
private OAuth2BasicMapperConfig basic;
private String authorizationUri; private String authorizationUri;
private String accessTokenUri; private String accessTokenUri;
private List<String> scope; private List<String> scope;
@ -51,8 +50,7 @@ public class OAuth2ClientRegistrationTemplate extends SearchTextBasedWithAdditio
public OAuth2ClientRegistrationTemplate(OAuth2ClientRegistrationTemplate clientRegistrationTemplate) { public OAuth2ClientRegistrationTemplate(OAuth2ClientRegistrationTemplate clientRegistrationTemplate) {
super(clientRegistrationTemplate); super(clientRegistrationTemplate);
this.providerId = clientRegistrationTemplate.providerId; this.providerId = clientRegistrationTemplate.providerId;
this.mapperType = clientRegistrationTemplate.mapperType; this.mapperConfig = clientRegistrationTemplate.mapperConfig;
this.basic = clientRegistrationTemplate.basic;
this.authorizationUri = clientRegistrationTemplate.authorizationUri; this.authorizationUri = clientRegistrationTemplate.authorizationUri;
this.accessTokenUri = clientRegistrationTemplate.accessTokenUri; this.accessTokenUri = clientRegistrationTemplate.accessTokenUri;
this.scope = clientRegistrationTemplate.scope; this.scope = clientRegistrationTemplate.scope;

50
dao/src/main/java/org/thingsboard/server/dao/model/sql/OAuth2ClientRegistrationTemplateEntity.java

@ -109,17 +109,20 @@ public class OAuth2ClientRegistrationTemplateEntity extends BaseSqlEntity<OAuth2
this.loginButtonLabel = clientRegistrationTemplate.getLoginButtonLabel(); this.loginButtonLabel = clientRegistrationTemplate.getLoginButtonLabel();
this.helpLink = clientRegistrationTemplate.getHelpLink(); this.helpLink = clientRegistrationTemplate.getHelpLink();
this.additionalInfo = clientRegistrationTemplate.getAdditionalInfo(); this.additionalInfo = clientRegistrationTemplate.getAdditionalInfo();
this.type = clientRegistrationTemplate.getMapperType(); OAuth2MapperConfig mapperConfig = clientRegistrationTemplate.getMapperConfig();
OAuth2BasicMapperConfig basicConfig = clientRegistrationTemplate.getBasic(); if (mapperConfig != null){
if (basicConfig != null) { this.type = mapperConfig.getType();
this.emailAttributeKey = basicConfig.getEmailAttributeKey(); OAuth2BasicMapperConfig basicConfig = mapperConfig.getBasic();
this.firstNameAttributeKey = basicConfig.getFirstNameAttributeKey(); if (basicConfig != null) {
this.lastNameAttributeKey = basicConfig.getLastNameAttributeKey(); this.emailAttributeKey = basicConfig.getEmailAttributeKey();
this.tenantNameStrategy = basicConfig.getTenantNameStrategy(); this.firstNameAttributeKey = basicConfig.getFirstNameAttributeKey();
this.tenantNamePattern = basicConfig.getTenantNamePattern(); this.lastNameAttributeKey = basicConfig.getLastNameAttributeKey();
this.customerNamePattern = basicConfig.getCustomerNamePattern(); this.tenantNameStrategy = basicConfig.getTenantNameStrategy();
this.defaultDashboardName = basicConfig.getDefaultDashboardName(); this.tenantNamePattern = basicConfig.getTenantNamePattern();
this.alwaysFullScreen = basicConfig.isAlwaysFullScreen(); this.customerNamePattern = basicConfig.getCustomerNamePattern();
this.defaultDashboardName = basicConfig.getDefaultDashboardName();
this.alwaysFullScreen = basicConfig.isAlwaysFullScreen();
}
} }
} }
@ -130,18 +133,21 @@ public class OAuth2ClientRegistrationTemplateEntity extends BaseSqlEntity<OAuth2
clientRegistrationTemplate.setCreatedTime(createdTime); clientRegistrationTemplate.setCreatedTime(createdTime);
clientRegistrationTemplate.setAdditionalInfo(additionalInfo); clientRegistrationTemplate.setAdditionalInfo(additionalInfo);
clientRegistrationTemplate.setMapperType(type);
clientRegistrationTemplate.setProviderId(providerId); clientRegistrationTemplate.setProviderId(providerId);
clientRegistrationTemplate.setBasic( clientRegistrationTemplate.setMapperConfig(
OAuth2BasicMapperConfig.builder() OAuth2MapperConfig.builder()
.emailAttributeKey(emailAttributeKey) .type(type)
.firstNameAttributeKey(firstNameAttributeKey) .basic(OAuth2BasicMapperConfig.builder()
.lastNameAttributeKey(lastNameAttributeKey) .emailAttributeKey(emailAttributeKey)
.tenantNameStrategy(tenantNameStrategy) .firstNameAttributeKey(firstNameAttributeKey)
.tenantNamePattern(tenantNamePattern) .lastNameAttributeKey(lastNameAttributeKey)
.customerNamePattern(customerNamePattern) .tenantNameStrategy(tenantNameStrategy)
.defaultDashboardName(defaultDashboardName) .tenantNamePattern(tenantNamePattern)
.alwaysFullScreen(alwaysFullScreen) .customerNamePattern(customerNamePattern)
.defaultDashboardName(defaultDashboardName)
.alwaysFullScreen(alwaysFullScreen)
.build()
)
.build() .build()
); );
clientRegistrationTemplate.setAuthorizationUri(authorizationUri); clientRegistrationTemplate.setAuthorizationUri(authorizationUri);

8
dao/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2ConfigTemplateServiceImpl.java

@ -95,7 +95,13 @@ public class OAuth2ConfigTemplateServiceImpl extends AbstractEntityService imple
if (StringUtils.isEmpty(clientRegistrationTemplate.getProviderId())) { if (StringUtils.isEmpty(clientRegistrationTemplate.getProviderId())) {
throw new DataValidationException("Provider ID should be specified!"); throw new DataValidationException("Provider ID should be specified!");
} }
if (clientRegistrationTemplate.getBasic() == null) { if (clientRegistrationTemplate.getMapperConfig() == null) {
throw new DataValidationException("Mapper config should be specified!");
}
if (clientRegistrationTemplate.getMapperConfig().getType() == null) {
throw new DataValidationException("Mapper type should be specified!");
}
if (clientRegistrationTemplate.getMapperConfig().getBasic() == null) {
throw new DataValidationException("Basic mapper config should be specified!"); throw new DataValidationException("Basic mapper config should be specified!");
} }
} }

10
dao/src/test/java/org/thingsboard/server/dao/service/BaseOAuth2ConfigTemplateServiceTest.java

@ -24,6 +24,7 @@ import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.oauth2.MapperType; import org.thingsboard.server.common.data.oauth2.MapperType;
import org.thingsboard.server.common.data.oauth2.OAuth2BasicMapperConfig; import org.thingsboard.server.common.data.oauth2.OAuth2BasicMapperConfig;
import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationTemplate; import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationTemplate;
import org.thingsboard.server.common.data.oauth2.OAuth2MapperConfig;
import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.oauth2.OAuth2ConfigTemplateService; import org.thingsboard.server.dao.oauth2.OAuth2ConfigTemplateService;
@ -106,9 +107,9 @@ public class BaseOAuth2ConfigTemplateServiceTest extends AbstractServiceTest {
OAuth2ClientRegistrationTemplate clientRegistrationTemplate = new OAuth2ClientRegistrationTemplate(); OAuth2ClientRegistrationTemplate clientRegistrationTemplate = new OAuth2ClientRegistrationTemplate();
clientRegistrationTemplate.setProviderId(providerId); clientRegistrationTemplate.setProviderId(providerId);
clientRegistrationTemplate.setAdditionalInfo(mapper.createObjectNode().put(UUID.randomUUID().toString(), UUID.randomUUID().toString())); clientRegistrationTemplate.setAdditionalInfo(mapper.createObjectNode().put(UUID.randomUUID().toString(), UUID.randomUUID().toString()));
clientRegistrationTemplate.setMapperType(MapperType.BASIC); clientRegistrationTemplate.setMapperConfig(OAuth2MapperConfig.builder()
clientRegistrationTemplate.setBasic( .type(MapperType.BASIC)
OAuth2BasicMapperConfig.builder() .basic(OAuth2BasicMapperConfig.builder()
.firstNameAttributeKey("firstName") .firstNameAttributeKey("firstName")
.lastNameAttributeKey("lastName") .lastNameAttributeKey("lastName")
.emailAttributeKey("email") .emailAttributeKey("email")
@ -116,7 +117,8 @@ public class BaseOAuth2ConfigTemplateServiceTest extends AbstractServiceTest {
.defaultDashboardName("Test") .defaultDashboardName("Test")
.alwaysFullScreen(true) .alwaysFullScreen(true)
.build() .build()
); )
.build());
clientRegistrationTemplate.setAuthorizationUri("authorizationUri"); clientRegistrationTemplate.setAuthorizationUri("authorizationUri");
clientRegistrationTemplate.setAccessTokenUri("tokenUri"); clientRegistrationTemplate.setAccessTokenUri("tokenUri");
clientRegistrationTemplate.setScope(Arrays.asList("scope1", "scope2")); clientRegistrationTemplate.setScope(Arrays.asList("scope1", "scope2"));

Loading…
Cancel
Save