Showing
26 changed files
with
812 additions
and
10 deletions
... | ... | @@ -48,4 +48,29 @@ CREATE TABLE IF NOT EXISTS oauth2_client_registration ( |
48 | 48 | custom_url varchar(255), |
49 | 49 | custom_username varchar(255), |
50 | 50 | custom_password varchar(255) |
51 | +); | |
52 | + | |
53 | +DROP TABLE IF EXISTS oauth2_client_registration_template; | |
54 | + | |
55 | +CREATE TABLE IF NOT EXISTS oauth2_client_registration_template ( | |
56 | + id uuid NOT NULL CONSTRAINT oauth2_client_registration_template_pkey PRIMARY KEY, | |
57 | + created_time bigint NOT NULL, | |
58 | + tenant_id uuid, | |
59 | + provider_id varchar(255), | |
60 | + authorization_uri varchar(255), | |
61 | + token_uri varchar(255), | |
62 | + scope varchar(255), | |
63 | + user_info_uri varchar(255), | |
64 | + user_name_attribute_name varchar(255), | |
65 | + jwk_set_uri varchar(255), | |
66 | + client_authentication_method varchar(255), | |
67 | + basic_email_attribute_key varchar(31), | |
68 | + basic_first_name_attribute_key varchar(31), | |
69 | + basic_last_name_attribute_key varchar(31), | |
70 | + basic_tenant_name_strategy varchar(31), | |
71 | + basic_tenant_name_pattern varchar(255), | |
72 | + basic_customer_name_pattern varchar(255), | |
73 | + basic_default_dashboard_name varchar(255), | |
74 | + basic_always_full_screen boolean, | |
75 | + CONSTRAINT oauth2_template_provider_id_unq_key UNIQUE (provider_id) | |
51 | 76 | ); |
\ No newline at end of file | ... | ... |
... | ... | @@ -51,6 +51,7 @@ import org.thingsboard.server.common.data.id.*; |
51 | 51 | import org.thingsboard.server.common.data.kv.AttributeKvEntry; |
52 | 52 | import org.thingsboard.server.common.data.kv.DataType; |
53 | 53 | import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistration; |
54 | +import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationTemplate; | |
54 | 55 | import org.thingsboard.server.common.data.page.PageLink; |
55 | 56 | import org.thingsboard.server.common.data.page.SortOrder; |
56 | 57 | import org.thingsboard.server.common.data.page.TimePageLink; |
... | ... | @@ -75,6 +76,7 @@ import org.thingsboard.server.dao.entityview.EntityViewService; |
75 | 76 | import org.thingsboard.server.dao.exception.DataValidationException; |
76 | 77 | import org.thingsboard.server.dao.exception.IncorrectParameterException; |
77 | 78 | import org.thingsboard.server.dao.model.ModelConstants; |
79 | +import org.thingsboard.server.dao.oauth2.OAuth2ConfigTemplateService; | |
78 | 80 | import org.thingsboard.server.dao.oauth2.OAuth2Service; |
79 | 81 | import org.thingsboard.server.dao.relation.RelationService; |
80 | 82 | import org.thingsboard.server.dao.rule.RuleChainService; |
... | ... | @@ -154,6 +156,9 @@ public abstract class BaseController { |
154 | 156 | protected OAuth2Service oAuth2Service; |
155 | 157 | |
156 | 158 | @Autowired |
159 | + protected OAuth2ConfigTemplateService oAuth2ConfigTemplateService; | |
160 | + | |
161 | + @Autowired | |
157 | 162 | protected ComponentDiscoveryService componentDescriptorService; |
158 | 163 | |
159 | 164 | @Autowired |
... | ... | @@ -385,6 +390,9 @@ public abstract class BaseController { |
385 | 390 | case OAUTH2_CLIENT_REGISTRATION: |
386 | 391 | checkOAuth2ClientRegistrationId(new OAuth2ClientRegistrationId(entityId.getId()), operation); |
387 | 392 | return; |
393 | + case OAUTH2_CLIENT_REGISTRATION_TEMPLATE: | |
394 | + checkOAuth2ClientRegistrationTemplateId(new OAuth2ClientRegistrationTemplateId(entityId.getId()), operation); | |
395 | + return; | |
388 | 396 | default: |
389 | 397 | throw new IllegalArgumentException("Unsupported entity type: " + entityId.getEntityType()); |
390 | 398 | } |
... | ... | @@ -549,6 +557,18 @@ public abstract class BaseController { |
549 | 557 | } |
550 | 558 | } |
551 | 559 | |
560 | + OAuth2ClientRegistrationTemplate checkOAuth2ClientRegistrationTemplateId(OAuth2ClientRegistrationTemplateId clientRegistrationTemplateId, Operation operation) throws ThingsboardException { | |
561 | + try { | |
562 | + validateId(clientRegistrationTemplateId, "Incorrect oAuth2ClientRegistrationTemplateId " + clientRegistrationTemplateId); | |
563 | + OAuth2ClientRegistrationTemplate clientRegistrationTemplate = oAuth2ConfigTemplateService.findClientRegistrationTemplateById(clientRegistrationTemplateId); | |
564 | + checkNotNull(clientRegistrationTemplate); | |
565 | + accessControlService.checkPermission(getCurrentUser(), Resource.OAUTH2_CONFIGURATION_TEMPLATE, operation, clientRegistrationTemplateId, clientRegistrationTemplate); | |
566 | + return clientRegistrationTemplate; | |
567 | + } catch (Exception e) { | |
568 | + throw handleException(e, false); | |
569 | + } | |
570 | + } | |
571 | + | |
552 | 572 | ComponentDescriptor checkComponentDescriptorByClazz(String clazz) throws ThingsboardException { |
553 | 573 | try { |
554 | 574 | log.debug("[{}] Lookup component descriptor", clazz); | ... | ... |
... | ... | @@ -26,9 +26,11 @@ import org.thingsboard.server.common.data.audit.ActionType; |
26 | 26 | import org.thingsboard.server.common.data.exception.ThingsboardException; |
27 | 27 | import org.thingsboard.server.common.data.id.DashboardId; |
28 | 28 | import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationId; |
29 | +import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationTemplateId; | |
29 | 30 | import org.thingsboard.server.common.data.id.TenantId; |
30 | 31 | import org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo; |
31 | 32 | import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistration; |
33 | +import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationTemplate; | |
32 | 34 | import org.thingsboard.server.common.data.oauth2.OAuth2ClientsParams; |
33 | 35 | import org.thingsboard.server.common.data.security.Authority; |
34 | 36 | import org.thingsboard.server.dao.oauth2.OAuth2Service; |
... | ... | @@ -44,8 +46,8 @@ import java.util.List; |
44 | 46 | @RequestMapping("/api") |
45 | 47 | @Slf4j |
46 | 48 | public class OAuth2Controller extends BaseController { |
47 | - public static final String CLIENT_REGISTRATION_ID = "clientRegistrationId"; | |
48 | - private static final String REGISTRATION_ID = "registrationId"; | |
49 | + private static final String CLIENT_REGISTRATION_ID = "clientRegistrationId"; | |
50 | + private static final String CLIENT_REGISTRATION_TEMPLATE_ID = "clientRegistrationTemplateId"; | |
49 | 51 | |
50 | 52 | @RequestMapping(value = "/noauth/oauth2Clients", method = RequestMethod.POST) |
51 | 53 | @ResponseBody |
... | ... | @@ -91,6 +93,19 @@ public class OAuth2Controller extends BaseController { |
91 | 93 | } |
92 | 94 | } |
93 | 95 | |
96 | + @PreAuthorize("hasAnyAuthority('SYS_ADMIN')") | |
97 | + @RequestMapping(value = "/oauth2/config/template", method = RequestMethod.POST) | |
98 | + @ResponseStatus(value = HttpStatus.OK) | |
99 | + public OAuth2ClientRegistrationTemplate saveClientRegistrationTemplate(@RequestBody OAuth2ClientRegistrationTemplate clientRegistrationTemplate) throws ThingsboardException { | |
100 | + try { | |
101 | + clientRegistrationTemplate.setTenantId(getCurrentUser().getTenantId()); | |
102 | + checkEntity(clientRegistrationTemplate.getId(), clientRegistrationTemplate, Resource.OAUTH2_CONFIGURATION_TEMPLATE); | |
103 | + return oAuth2ConfigTemplateService.saveClientRegistrationTemplate(clientRegistrationTemplate); | |
104 | + } catch (Exception e) { | |
105 | + throw handleException(e); | |
106 | + } | |
107 | + } | |
108 | + | |
94 | 109 | @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") |
95 | 110 | @RequestMapping(value = "/oauth2/config/{clientRegistrationId}", method = RequestMethod.DELETE) |
96 | 111 | @ResponseStatus(value = HttpStatus.OK) |
... | ... | @@ -116,6 +131,31 @@ public class OAuth2Controller extends BaseController { |
116 | 131 | } |
117 | 132 | } |
118 | 133 | |
134 | + @PreAuthorize("hasAnyAuthority('SYS_ADMIN')") | |
135 | + @RequestMapping(value = "/oauth2/config/template/{clientRegistrationTemplateId}", method = RequestMethod.DELETE) | |
136 | + @ResponseStatus(value = HttpStatus.OK) | |
137 | + public void deleteClientRegistrationTemplate(@PathVariable(CLIENT_REGISTRATION_TEMPLATE_ID) String strClientRegistrationTemplateId) throws ThingsboardException { | |
138 | + checkParameter(CLIENT_REGISTRATION_TEMPLATE_ID, strClientRegistrationTemplateId); | |
139 | + try { | |
140 | + OAuth2ClientRegistrationTemplateId clientRegistrationTemplateId = new OAuth2ClientRegistrationTemplateId(toUUID(strClientRegistrationTemplateId)); | |
141 | + OAuth2ClientRegistrationTemplate clientRegistrationTemplate = checkOAuth2ClientRegistrationTemplateId(clientRegistrationTemplateId, Operation.DELETE); | |
142 | + oAuth2ConfigTemplateService.deleteClientRegistrationTemplateById(clientRegistrationTemplateId); | |
143 | + | |
144 | + logEntityAction(clientRegistrationTemplateId, clientRegistrationTemplate, | |
145 | + null, | |
146 | + ActionType.DELETED, null, strClientRegistrationTemplateId); | |
147 | + | |
148 | + } catch (Exception e) { | |
149 | + | |
150 | + logEntityAction(emptyId(EntityType.OAUTH2_CLIENT_REGISTRATION_TEMPLATE), | |
151 | + null, | |
152 | + null, | |
153 | + ActionType.DELETED, e, strClientRegistrationTemplateId); | |
154 | + | |
155 | + throw handleException(e); | |
156 | + } | |
157 | + } | |
158 | + | |
119 | 159 | @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") |
120 | 160 | @RequestMapping(value = "/oauth2/config/isAllowed", method = RequestMethod.GET) |
121 | 161 | @ResponseBody |
... | ... | @@ -127,11 +167,25 @@ public class OAuth2Controller extends BaseController { |
127 | 167 | } |
128 | 168 | } |
129 | 169 | |
170 | + | |
171 | + | |
172 | + @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") | |
173 | + @RequestMapping(value = "/oauth2/config/template", method = RequestMethod.GET, produces = "application/json") | |
174 | + @ResponseBody | |
175 | + public List<OAuth2ClientRegistrationTemplate> getClientRegistrationTemplates() throws ThingsboardException { | |
176 | + try { | |
177 | + checkOAuth2ConfigTemplatePermissions(Operation.READ); | |
178 | + return oAuth2ConfigTemplateService.findAllClientRegistrationTemplates(); | |
179 | + } catch (Exception e) { | |
180 | + throw handleException(e); | |
181 | + } | |
182 | + } | |
183 | + | |
130 | 184 | private void checkOAuth2ConfigPermissions(Operation operation) throws ThingsboardException { |
131 | 185 | accessControlService.checkPermission(getCurrentUser(), Resource.OAUTH2_CONFIGURATION, operation); |
132 | 186 | } |
133 | 187 | |
134 | - private void checkOAuth2ConfigPermissions(Operation operation, OAuth2ClientRegistration clientRegistration) throws ThingsboardException { | |
135 | - accessControlService.checkPermission(getCurrentUser(), Resource.OAUTH2_CONFIGURATION, operation, clientRegistration.getId(), clientRegistration); | |
188 | + private void checkOAuth2ConfigTemplatePermissions(Operation operation) throws ThingsboardException { | |
189 | + accessControlService.checkPermission(getCurrentUser(), Resource.OAUTH2_CONFIGURATION_TEMPLATE, operation); | |
136 | 190 | } |
137 | 191 | } | ... | ... |
... | ... | @@ -33,6 +33,7 @@ public enum Resource { |
33 | 33 | WIDGETS_BUNDLE(EntityType.WIDGETS_BUNDLE), |
34 | 34 | WIDGET_TYPE(EntityType.WIDGET_TYPE), |
35 | 35 | OAUTH2_CONFIGURATION(EntityType.OAUTH2_CLIENT_REGISTRATION), |
36 | + OAUTH2_CONFIGURATION_TEMPLATE(EntityType.OAUTH2_CLIENT_REGISTRATION_TEMPLATE), | |
36 | 37 | ; |
37 | 38 | |
38 | 39 | private final EntityType entityType; | ... | ... |
... | ... | @@ -40,6 +40,7 @@ public class SysAdminPermissions extends AbstractPermissions { |
40 | 40 | put(Resource.WIDGETS_BUNDLE, systemEntityPermissionChecker); |
41 | 41 | put(Resource.WIDGET_TYPE, systemEntityPermissionChecker); |
42 | 42 | put(Resource.OAUTH2_CONFIGURATION, sysAdminOAuth2ConfigPermissionChecker); |
43 | + put(Resource.OAUTH2_CONFIGURATION_TEMPLATE, PermissionChecker.allowAllPermissionChecker); | |
43 | 44 | } |
44 | 45 | |
45 | 46 | private static final PermissionChecker systemEntityPermissionChecker = new PermissionChecker() { | ... | ... |
... | ... | @@ -48,6 +48,7 @@ public class TenantAdminPermissions extends AbstractPermissions { |
48 | 48 | put(Resource.WIDGETS_BUNDLE, widgetsPermissionChecker); |
49 | 49 | put(Resource.WIDGET_TYPE, widgetsPermissionChecker); |
50 | 50 | put(Resource.OAUTH2_CONFIGURATION, tenantOAuth2ConfigPermissionChecker); |
51 | + put(Resource.OAUTH2_CONFIGURATION_TEMPLATE, tenantOAuth2ConfigTemplatePermissionChecker); | |
51 | 52 | } |
52 | 53 | |
53 | 54 | public static final PermissionChecker tenantEntityPermissionChecker = new PermissionChecker() { |
... | ... | @@ -108,7 +109,7 @@ public class TenantAdminPermissions extends AbstractPermissions { |
108 | 109 | |
109 | 110 | }; |
110 | 111 | |
111 | - private final PermissionChecker tenantOAuth2ConfigPermissionChecker = new PermissionChecker() { | |
112 | + private final PermissionChecker tenantOAuth2ConfigPermissionChecker = new PermissionChecker() { | |
112 | 113 | @Override |
113 | 114 | public boolean hasPermission(SecurityUser user, Operation operation) { |
114 | 115 | return oAuth2Service.isOAuth2ClientRegistrationAllowed(user.getTenantId()); |
... | ... | @@ -122,4 +123,16 @@ public class TenantAdminPermissions extends AbstractPermissions { |
122 | 123 | return hasPermission(user, operation); |
123 | 124 | } |
124 | 125 | }; |
126 | + | |
127 | + private static final PermissionChecker tenantOAuth2ConfigTemplatePermissionChecker = new PermissionChecker() { | |
128 | + @Override | |
129 | + public boolean hasPermission(SecurityUser user, Operation operation) { | |
130 | + return operation == Operation.READ; | |
131 | + } | |
132 | + | |
133 | + @Override | |
134 | + public boolean hasPermission(SecurityUser user, Operation operation, EntityId entityId, HasTenantId entity) { | |
135 | + return operation == Operation.READ; | |
136 | + } | |
137 | + }; | |
125 | 138 | } | ... | ... |
common/dao-api/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2ConfigTemplateService.java
0 → 100644
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.dao.oauth2; | |
17 | + | |
18 | +import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationTemplateId; | |
19 | +import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationTemplate; | |
20 | + | |
21 | +import java.util.List; | |
22 | + | |
23 | +public interface OAuth2ConfigTemplateService { | |
24 | + OAuth2ClientRegistrationTemplate saveClientRegistrationTemplate(OAuth2ClientRegistrationTemplate clientRegistrationTemplate); | |
25 | + | |
26 | + OAuth2ClientRegistrationTemplate findClientRegistrationTemplateById(OAuth2ClientRegistrationTemplateId templateId); | |
27 | + | |
28 | + List<OAuth2ClientRegistrationTemplate> findAllClientRegistrationTemplates(); | |
29 | + | |
30 | + void deleteClientRegistrationTemplateById(OAuth2ClientRegistrationTemplateId templateId); | |
31 | +} | ... | ... |
... | ... | @@ -19,5 +19,5 @@ package org.thingsboard.server.common.data; |
19 | 19 | * @author Andrew Shvayka |
20 | 20 | */ |
21 | 21 | public enum EntityType { |
22 | - TENANT, CUSTOMER, USER, DASHBOARD, ASSET, DEVICE, ALARM, RULE_CHAIN, RULE_NODE, ENTITY_VIEW, WIDGETS_BUNDLE, WIDGET_TYPE, OAUTH2_CLIENT_REGISTRATION | |
22 | + TENANT, CUSTOMER, USER, DASHBOARD, ASSET, DEVICE, ALARM, RULE_CHAIN, RULE_NODE, ENTITY_VIEW, WIDGETS_BUNDLE, WIDGET_TYPE, OAUTH2_CLIENT_REGISTRATION, OAUTH2_CLIENT_REGISTRATION_TEMPLATE | |
23 | 23 | } | ... | ... |
... | ... | @@ -64,6 +64,8 @@ public class EntityIdFactory { |
64 | 64 | return new WidgetTypeId(uuid); |
65 | 65 | case OAUTH2_CLIENT_REGISTRATION: |
66 | 66 | return new OAuth2ClientRegistrationId(uuid); |
67 | + case OAUTH2_CLIENT_REGISTRATION_TEMPLATE: | |
68 | + return new OAuth2ClientRegistrationTemplateId(uuid); | |
67 | 69 | } |
68 | 70 | throw new IllegalArgumentException("EntityType " + type + " is not supported!"); |
69 | 71 | } | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.common.data.id; | |
17 | + | |
18 | +import com.fasterxml.jackson.annotation.JsonCreator; | |
19 | +import com.fasterxml.jackson.annotation.JsonProperty; | |
20 | +import org.thingsboard.server.common.data.EntityType; | |
21 | + | |
22 | +import java.util.UUID; | |
23 | + | |
24 | +public class OAuth2ClientRegistrationTemplateId extends UUIDBased implements EntityId { | |
25 | + | |
26 | + @JsonCreator | |
27 | + public OAuth2ClientRegistrationTemplateId(@JsonProperty("id") UUID id) { | |
28 | + super(id); | |
29 | + } | |
30 | + | |
31 | + public static OAuth2ClientRegistrationTemplateId fromString(String clientRegistrationTemplateId) { | |
32 | + return new OAuth2ClientRegistrationTemplateId(UUID.fromString(clientRegistrationTemplateId)); | |
33 | + } | |
34 | + | |
35 | + @Override | |
36 | + public EntityType getEntityType() { | |
37 | + return EntityType.OAUTH2_CLIENT_REGISTRATION_TEMPLATE; | |
38 | + } | |
39 | +} | ... | ... |
... | ... | @@ -49,9 +49,9 @@ public class OAuth2ClientRegistration extends BaseData<OAuth2ClientRegistrationI |
49 | 49 | |
50 | 50 | public OAuth2ClientRegistration(OAuth2ClientRegistration clientRegistration) { |
51 | 51 | super(clientRegistration); |
52 | - this.tenantId = clientRegistration.getTenantId(); | |
53 | - this.domainName = clientRegistration.getDomainName(); | |
54 | - this.redirectUriTemplate = clientRegistration.getRedirectUriTemplate(); | |
52 | + this.tenantId = clientRegistration.tenantId; | |
53 | + this.domainName = clientRegistration.domainName; | |
54 | + this.redirectUriTemplate = clientRegistration.redirectUriTemplate; | |
55 | 55 | this.mapperConfig = clientRegistration.mapperConfig; |
56 | 56 | this.clientId = clientRegistration.clientId; |
57 | 57 | this.clientSecret = clientRegistration.clientSecret; | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.common.data.oauth2; | |
17 | + | |
18 | +import lombok.Data; | |
19 | +import lombok.EqualsAndHashCode; | |
20 | +import lombok.NoArgsConstructor; | |
21 | +import lombok.ToString; | |
22 | +import org.thingsboard.server.common.data.BaseData; | |
23 | +import org.thingsboard.server.common.data.HasName; | |
24 | +import org.thingsboard.server.common.data.HasTenantId; | |
25 | +import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationTemplateId; | |
26 | +import org.thingsboard.server.common.data.id.TenantId; | |
27 | + | |
28 | +import java.util.List; | |
29 | + | |
30 | +@EqualsAndHashCode(callSuper = true) | |
31 | +@Data | |
32 | +@ToString | |
33 | +@NoArgsConstructor | |
34 | +public class OAuth2ClientRegistrationTemplate extends BaseData<OAuth2ClientRegistrationTemplateId> implements HasTenantId, HasName { | |
35 | + | |
36 | + private TenantId tenantId; | |
37 | + private String providerId; | |
38 | + private OAuth2BasicMapperConfig basic; | |
39 | + private String authorizationUri; | |
40 | + private String accessTokenUri; | |
41 | + private List<String> scope; | |
42 | + private String userInfoUri; | |
43 | + private String userNameAttributeName; | |
44 | + private String jwkSetUri; | |
45 | + private String clientAuthenticationMethod; | |
46 | + | |
47 | + public OAuth2ClientRegistrationTemplate(OAuth2ClientRegistrationTemplate clientRegistrationTemplate) { | |
48 | + super(clientRegistrationTemplate); | |
49 | + this.tenantId = clientRegistrationTemplate.tenantId; | |
50 | + this.providerId = clientRegistrationTemplate.providerId; | |
51 | + this.basic = clientRegistrationTemplate.basic; | |
52 | + this.authorizationUri = clientRegistrationTemplate.authorizationUri; | |
53 | + this.accessTokenUri = clientRegistrationTemplate.accessTokenUri; | |
54 | + this.scope = clientRegistrationTemplate.scope; | |
55 | + this.userInfoUri = clientRegistrationTemplate.userInfoUri; | |
56 | + this.userNameAttributeName = clientRegistrationTemplate.userNameAttributeName; | |
57 | + this.jwkSetUri = clientRegistrationTemplate.jwkSetUri; | |
58 | + this.clientAuthenticationMethod = clientRegistrationTemplate.clientAuthenticationMethod; | |
59 | + } | |
60 | + | |
61 | + @Override | |
62 | + public String getName() { | |
63 | + return providerId; | |
64 | + } | |
65 | +} | ... | ... |
... | ... | @@ -359,6 +359,8 @@ public class ModelConstants { |
359 | 359 | */ |
360 | 360 | public static final String OAUTH2_TENANT_ID_PROPERTY = TENANT_ID_PROPERTY; |
361 | 361 | public static final String OAUTH2_CLIENT_REGISTRATION_COLUMN_FAMILY_NAME = "oauth2_client_registration"; |
362 | + public static final String OAUTH2_CLIENT_REGISTRATION_TEMPLATE_COLUMN_FAMILY_NAME = "oauth2_client_registration_template"; | |
363 | + public static final String OAUTH2_TEMPLATE_PROVIDER_ID_PROPERTY = "provider_id"; | |
362 | 364 | public static final String OAUTH2_DOMAIN_NAME_PROPERTY = "domain_name"; |
363 | 365 | public static final String OAUTH2_CLIENT_ID_PROPERTY = "client_id"; |
364 | 366 | public static final String OAUTH2_CLIENT_SECRET_PROPERTY = "client_secret"; | ... | ... |
... | ... | @@ -151,8 +151,8 @@ public class OAuth2ClientRegistrationEntity extends BaseSqlEntity<OAuth2ClientRe |
151 | 151 | OAuth2ClientRegistration clientRegistration = new OAuth2ClientRegistration(); |
152 | 152 | clientRegistration.setId(new OAuth2ClientRegistrationId(id)); |
153 | 153 | clientRegistration.setTenantId(new TenantId(tenantId)); |
154 | - clientRegistration.setDomainName(domainName); | |
155 | 154 | clientRegistration.setCreatedTime(createdTime); |
155 | + clientRegistration.setDomainName(domainName); | |
156 | 156 | clientRegistration.setMapperConfig( |
157 | 157 | OAuth2MapperConfig.builder() |
158 | 158 | .allowUserCreation(allowUserCreation) | ... | ... |
dao/src/main/java/org/thingsboard/server/dao/model/sql/OAuth2ClientRegistrationTemplateEntity.java
0 → 100644
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.dao.model.sql; | |
17 | + | |
18 | +import lombok.Data; | |
19 | +import lombok.EqualsAndHashCode; | |
20 | +import org.hibernate.annotations.TypeDef; | |
21 | +import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationId; | |
22 | +import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationTemplateId; | |
23 | +import org.thingsboard.server.common.data.id.TenantId; | |
24 | +import org.thingsboard.server.common.data.oauth2.*; | |
25 | +import org.thingsboard.server.dao.model.BaseSqlEntity; | |
26 | +import org.thingsboard.server.dao.model.ModelConstants; | |
27 | +import org.thingsboard.server.dao.util.mapping.JsonStringType; | |
28 | +import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationTemplate; | |
29 | + | |
30 | +import javax.persistence.*; | |
31 | +import java.util.Arrays; | |
32 | +import java.util.UUID; | |
33 | + | |
34 | +@Data | |
35 | +@EqualsAndHashCode(callSuper = true) | |
36 | +@Entity | |
37 | +@TypeDef(name = "json", typeClass = JsonStringType.class) | |
38 | +@Table(name = ModelConstants.OAUTH2_CLIENT_REGISTRATION_TEMPLATE_COLUMN_FAMILY_NAME) | |
39 | +public class OAuth2ClientRegistrationTemplateEntity extends BaseSqlEntity<OAuth2ClientRegistrationTemplate> { | |
40 | + | |
41 | + @Column(name = ModelConstants.OAUTH2_TENANT_ID_PROPERTY, columnDefinition = "uuid") | |
42 | + private UUID tenantId; | |
43 | + | |
44 | + @Column(name = ModelConstants.OAUTH2_TEMPLATE_PROVIDER_ID_PROPERTY) | |
45 | + private String providerId; | |
46 | + @Column(name = ModelConstants.OAUTH2_AUTHORIZATION_URI_PROPERTY) | |
47 | + private String authorizationUri; | |
48 | + @Column(name = ModelConstants.OAUTH2_TOKEN_URI_PROPERTY) | |
49 | + private String tokenUri; | |
50 | + @Column(name = ModelConstants.OAUTH2_SCOPE_PROPERTY) | |
51 | + private String scope; | |
52 | + @Column(name = ModelConstants.OAUTH2_USER_INFO_URI_PROPERTY) | |
53 | + private String userInfoUri; | |
54 | + @Column(name = ModelConstants.OAUTH2_USER_NAME_ATTRIBUTE_NAME_PROPERTY) | |
55 | + private String userNameAttributeName; | |
56 | + @Column(name = ModelConstants.OAUTH2_JWK_SET_URI_PROPERTY) | |
57 | + private String jwkSetUri; | |
58 | + @Column(name = ModelConstants.OAUTH2_CLIENT_AUTHENTICATION_METHOD_PROPERTY) | |
59 | + private String clientAuthenticationMethod; | |
60 | + @Column(name = ModelConstants.OAUTH2_EMAIL_ATTRIBUTE_KEY_PROPERTY) | |
61 | + private String emailAttributeKey; | |
62 | + @Column(name = ModelConstants.OAUTH2_FIRST_NAME_ATTRIBUTE_KEY_PROPERTY) | |
63 | + private String firstNameAttributeKey; | |
64 | + @Column(name = ModelConstants.OAUTH2_LAST_NAME_ATTRIBUTE_KEY_PROPERTY) | |
65 | + private String lastNameAttributeKey; | |
66 | + @Enumerated(EnumType.STRING) | |
67 | + @Column(name = ModelConstants.OAUTH2_TENANT_NAME_STRATEGY_PROPERTY) | |
68 | + private TenantNameStrategyType tenantNameStrategy; | |
69 | + @Column(name = ModelConstants.OAUTH2_TENANT_NAME_PATTERN_PROPERTY) | |
70 | + private String tenantNamePattern; | |
71 | + @Column(name = ModelConstants.OAUTH2_CUSTOMER_NAME_PATTERN_PROPERTY) | |
72 | + private String customerNamePattern; | |
73 | + @Column(name = ModelConstants.OAUTH2_DEFAULT_DASHBOARD_NAME_PROPERTY) | |
74 | + private String defaultDashboardName; | |
75 | + @Column(name = ModelConstants.OAUTH2_ALWAYS_FULL_SCREEN_PROPERTY) | |
76 | + private Boolean alwaysFullScreen; | |
77 | + | |
78 | + public OAuth2ClientRegistrationTemplateEntity() { | |
79 | + } | |
80 | + | |
81 | + public OAuth2ClientRegistrationTemplateEntity(OAuth2ClientRegistrationTemplate clientRegistrationTemplate) { | |
82 | + if (clientRegistrationTemplate.getId() != null) { | |
83 | + this.setUuid(clientRegistrationTemplate.getId().getId()); | |
84 | + } | |
85 | + if (clientRegistrationTemplate.getTenantId() != null) { | |
86 | + this.tenantId = clientRegistrationTemplate.getTenantId().getId(); | |
87 | + } | |
88 | + this.createdTime = clientRegistrationTemplate.getCreatedTime(); | |
89 | + this.providerId = clientRegistrationTemplate.getProviderId(); | |
90 | + this.authorizationUri = clientRegistrationTemplate.getAuthorizationUri(); | |
91 | + this.tokenUri = clientRegistrationTemplate.getAccessTokenUri(); | |
92 | + this.scope = clientRegistrationTemplate.getScope().stream().reduce((result, element) -> result + "," + element).orElse(""); | |
93 | + this.userInfoUri = clientRegistrationTemplate.getUserInfoUri(); | |
94 | + this.userNameAttributeName = clientRegistrationTemplate.getUserNameAttributeName(); | |
95 | + this.jwkSetUri = clientRegistrationTemplate.getJwkSetUri(); | |
96 | + this.clientAuthenticationMethod = clientRegistrationTemplate.getClientAuthenticationMethod(); | |
97 | + OAuth2BasicMapperConfig basicConfig = clientRegistrationTemplate.getBasic(); | |
98 | + if (basicConfig != null) { | |
99 | + this.emailAttributeKey = basicConfig.getEmailAttributeKey(); | |
100 | + this.firstNameAttributeKey = basicConfig.getFirstNameAttributeKey(); | |
101 | + this.lastNameAttributeKey = basicConfig.getLastNameAttributeKey(); | |
102 | + this.tenantNameStrategy = basicConfig.getTenantNameStrategy(); | |
103 | + this.tenantNamePattern = basicConfig.getTenantNamePattern(); | |
104 | + this.customerNamePattern = basicConfig.getCustomerNamePattern(); | |
105 | + this.defaultDashboardName = basicConfig.getDefaultDashboardName(); | |
106 | + this.alwaysFullScreen = basicConfig.isAlwaysFullScreen(); | |
107 | + } | |
108 | + } | |
109 | + | |
110 | + @Override | |
111 | + public OAuth2ClientRegistrationTemplate toData() { | |
112 | + OAuth2ClientRegistrationTemplate clientRegistrationTemplate = new OAuth2ClientRegistrationTemplate(); | |
113 | + clientRegistrationTemplate.setId(new OAuth2ClientRegistrationTemplateId(id)); | |
114 | + clientRegistrationTemplate.setTenantId(new TenantId(tenantId)); | |
115 | + clientRegistrationTemplate.setCreatedTime(createdTime); | |
116 | + | |
117 | + clientRegistrationTemplate.setProviderId(providerId); | |
118 | + clientRegistrationTemplate.setBasic( | |
119 | + OAuth2BasicMapperConfig.builder() | |
120 | + .emailAttributeKey(emailAttributeKey) | |
121 | + .firstNameAttributeKey(firstNameAttributeKey) | |
122 | + .lastNameAttributeKey(lastNameAttributeKey) | |
123 | + .tenantNameStrategy(tenantNameStrategy) | |
124 | + .tenantNamePattern(tenantNamePattern) | |
125 | + .customerNamePattern(customerNamePattern) | |
126 | + .defaultDashboardName(defaultDashboardName) | |
127 | + .alwaysFullScreen(alwaysFullScreen) | |
128 | + .build() | |
129 | + ); | |
130 | + clientRegistrationTemplate.setAuthorizationUri(authorizationUri); | |
131 | + clientRegistrationTemplate.setAccessTokenUri(tokenUri); | |
132 | + clientRegistrationTemplate.setScope(Arrays.asList(scope.split(","))); | |
133 | + clientRegistrationTemplate.setUserInfoUri(userInfoUri); | |
134 | + clientRegistrationTemplate.setUserNameAttributeName(userNameAttributeName); | |
135 | + clientRegistrationTemplate.setJwkSetUri(jwkSetUri); | |
136 | + clientRegistrationTemplate.setClientAuthenticationMethod(clientAuthenticationMethod); | |
137 | + return clientRegistrationTemplate; | |
138 | + } | |
139 | +} | ... | ... |
dao/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2ClientRegistrationTemplateDao.java
0 → 100644
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.dao.oauth2; | |
17 | + | |
18 | +import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationTemplate; | |
19 | +import org.thingsboard.server.dao.Dao; | |
20 | + | |
21 | +import java.util.List; | |
22 | + | |
23 | +public interface OAuth2ClientRegistrationTemplateDao extends Dao<OAuth2ClientRegistrationTemplate> { | |
24 | + List<OAuth2ClientRegistrationTemplate> findAll(); | |
25 | +} | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.dao.oauth2; | |
17 | + | |
18 | +import lombok.extern.slf4j.Slf4j; | |
19 | +import org.hibernate.exception.ConstraintViolationException; | |
20 | +import org.springframework.beans.factory.annotation.Autowired; | |
21 | +import org.springframework.stereotype.Service; | |
22 | +import org.springframework.util.StringUtils; | |
23 | +import org.thingsboard.server.common.data.Tenant; | |
24 | +import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationTemplateId; | |
25 | +import org.thingsboard.server.common.data.id.TenantId; | |
26 | +import org.thingsboard.server.common.data.oauth2.*; | |
27 | +import org.thingsboard.server.dao.entity.AbstractEntityService; | |
28 | +import org.thingsboard.server.dao.exception.DataValidationException; | |
29 | +import org.thingsboard.server.dao.service.DataValidator; | |
30 | + | |
31 | +import java.util.List; | |
32 | + | |
33 | +import static org.thingsboard.server.dao.service.Validator.validateId; | |
34 | +import static org.thingsboard.server.dao.service.Validator.validateString; | |
35 | + | |
36 | +@Slf4j | |
37 | +@Service | |
38 | +public class OAuth2ConfigTemplateServiceImpl extends AbstractEntityService implements OAuth2ConfigTemplateService { | |
39 | + public static final String INCORRECT_CLIENT_REGISTRATION_TEMPLATE_ID = "Incorrect clientRegistrationTemplateId "; | |
40 | + | |
41 | + @Autowired | |
42 | + private OAuth2ClientRegistrationTemplateDao clientRegistrationTemplateDao; | |
43 | + | |
44 | + @Override | |
45 | + public OAuth2ClientRegistrationTemplate saveClientRegistrationTemplate(OAuth2ClientRegistrationTemplate clientRegistrationTemplate) { | |
46 | + log.trace("Executing saveClientRegistrationTemplate [{}]", clientRegistrationTemplate); | |
47 | + clientRegistrationTemplateValidator.validate(clientRegistrationTemplate, OAuth2ClientRegistrationTemplate::getTenantId); | |
48 | + OAuth2ClientRegistrationTemplate savedClientRegistrationTemplate; | |
49 | + try { | |
50 | + savedClientRegistrationTemplate = clientRegistrationTemplateDao.save(clientRegistrationTemplate.getTenantId(), clientRegistrationTemplate); | |
51 | + } catch (Exception t) { | |
52 | + ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); | |
53 | + if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("oauth2_template_provider_id_unq_key")) { | |
54 | + throw new DataValidationException("Client registration template with such providerId already exists!"); | |
55 | + } else { | |
56 | + throw t; | |
57 | + } | |
58 | + } | |
59 | + return savedClientRegistrationTemplate; | |
60 | + } | |
61 | + | |
62 | + @Override | |
63 | + public OAuth2ClientRegistrationTemplate findClientRegistrationTemplateById(OAuth2ClientRegistrationTemplateId templateId) { | |
64 | + log.trace("Executing findClientRegistrationTemplateById [{}]", templateId); | |
65 | + validateId(templateId, INCORRECT_CLIENT_REGISTRATION_TEMPLATE_ID + templateId); | |
66 | + return clientRegistrationTemplateDao.findById(TenantId.SYS_TENANT_ID, templateId.getId()); | |
67 | + } | |
68 | + | |
69 | + @Override | |
70 | + public List<OAuth2ClientRegistrationTemplate> findAllClientRegistrationTemplates() { | |
71 | + log.trace("Executing findAllClientRegistrationTemplates"); | |
72 | + return clientRegistrationTemplateDao.findAll(); | |
73 | + } | |
74 | + | |
75 | + @Override | |
76 | + public void deleteClientRegistrationTemplateById(OAuth2ClientRegistrationTemplateId templateId) { | |
77 | + log.trace("Executing deleteClientRegistrationTemplateById [{}]", templateId); | |
78 | + validateId(templateId, INCORRECT_CLIENT_REGISTRATION_TEMPLATE_ID + templateId); | |
79 | + clientRegistrationTemplateDao.removeById(TenantId.SYS_TENANT_ID, templateId.getId()); | |
80 | + } | |
81 | + | |
82 | + private DataValidator<OAuth2ClientRegistrationTemplate> clientRegistrationTemplateValidator = | |
83 | + new DataValidator<OAuth2ClientRegistrationTemplate>() { | |
84 | + | |
85 | + @Override | |
86 | + protected void validateCreate(TenantId tenantId, OAuth2ClientRegistrationTemplate clientRegistrationTemplate) { | |
87 | + } | |
88 | + | |
89 | + @Override | |
90 | + protected void validateUpdate(TenantId tenantId, OAuth2ClientRegistrationTemplate clientRegistrationTemplate) { | |
91 | + } | |
92 | + | |
93 | + @Override | |
94 | + protected void validateDataImpl(TenantId tenantId, OAuth2ClientRegistrationTemplate clientRegistrationTemplate) { | |
95 | + if (StringUtils.isEmpty(clientRegistrationTemplate.getProviderId())) { | |
96 | + throw new DataValidationException("Provider ID should be specified!"); | |
97 | + } | |
98 | + if (clientRegistrationTemplate.getBasic() == null) { | |
99 | + throw new DataValidationException("Basic mapper config should be specified!"); | |
100 | + } | |
101 | + if (clientRegistrationTemplate.getTenantId() == null | |
102 | + || !TenantId.SYS_TENANT_ID.equals(clientRegistrationTemplate.getTenantId())) { | |
103 | + throw new DataValidationException("Client registration template should be assigned to system admin!"); | |
104 | + } | |
105 | + } | |
106 | + }; | |
107 | +} | ... | ... |
dao/src/main/java/org/thingsboard/server/dao/sql/oauth2/JpaOAuth2ClientRegistrationTemplateDao.java
0 → 100644
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.dao.sql.oauth2; | |
17 | + | |
18 | +import lombok.RequiredArgsConstructor; | |
19 | +import org.springframework.data.repository.CrudRepository; | |
20 | +import org.springframework.stereotype.Component; | |
21 | +import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationTemplate; | |
22 | +import org.thingsboard.server.dao.DaoUtil; | |
23 | +import org.thingsboard.server.dao.model.sql.OAuth2ClientRegistrationTemplateEntity; | |
24 | +import org.thingsboard.server.dao.oauth2.OAuth2ClientRegistrationTemplateDao; | |
25 | +import org.thingsboard.server.dao.sql.JpaAbstractDao; | |
26 | + | |
27 | +import java.util.ArrayList; | |
28 | +import java.util.List; | |
29 | +import java.util.UUID; | |
30 | + | |
31 | +@Component | |
32 | +@RequiredArgsConstructor | |
33 | +public class JpaOAuth2ClientRegistrationTemplateDao extends JpaAbstractDao<OAuth2ClientRegistrationTemplateEntity, OAuth2ClientRegistrationTemplate> implements OAuth2ClientRegistrationTemplateDao { | |
34 | + private final OAuth2ClientRegistrationTemplateRepository repository; | |
35 | + | |
36 | + @Override | |
37 | + protected Class<OAuth2ClientRegistrationTemplateEntity> getEntityClass() { | |
38 | + return OAuth2ClientRegistrationTemplateEntity.class; | |
39 | + } | |
40 | + | |
41 | + @Override | |
42 | + protected CrudRepository<OAuth2ClientRegistrationTemplateEntity, UUID> getCrudRepository() { | |
43 | + return repository; | |
44 | + } | |
45 | + | |
46 | + @Override | |
47 | + public List<OAuth2ClientRegistrationTemplate> findAll() { | |
48 | + Iterable<OAuth2ClientRegistrationTemplateEntity> entities = repository.findAll(); | |
49 | + List<OAuth2ClientRegistrationTemplate> result = new ArrayList<>(); | |
50 | + entities.forEach(entity -> result.add(DaoUtil.getData(entity))); | |
51 | + return result; | |
52 | + } | |
53 | +} | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.dao.sql.oauth2; | |
17 | + | |
18 | +import org.springframework.data.repository.CrudRepository; | |
19 | +import org.thingsboard.server.dao.model.sql.OAuth2ClientRegistrationTemplateEntity; | |
20 | + | |
21 | +import java.util.UUID; | |
22 | + | |
23 | +public interface OAuth2ClientRegistrationTemplateRepository extends CrudRepository<OAuth2ClientRegistrationTemplateEntity, UUID> { | |
24 | +} | ... | ... |
... | ... | @@ -324,3 +324,26 @@ CREATE TABLE IF NOT EXISTS oauth2_client_registration ( |
324 | 324 | custom_username varchar(255), |
325 | 325 | custom_password varchar(255) |
326 | 326 | ); |
327 | + | |
328 | +CREATE TABLE IF NOT EXISTS oauth2_client_registration_template ( | |
329 | + id uuid NOT NULL CONSTRAINT oauth2_client_registration_template_pkey PRIMARY KEY, | |
330 | + created_time bigint NOT NULL, | |
331 | + tenant_id uuid, | |
332 | + provider_id varchar(255), | |
333 | + authorization_uri varchar(255), | |
334 | + token_uri varchar(255), | |
335 | + scope varchar(255), | |
336 | + user_info_uri varchar(255), | |
337 | + user_name_attribute_name varchar(255), | |
338 | + jwk_set_uri varchar(255), | |
339 | + client_authentication_method varchar(255), | |
340 | + basic_email_attribute_key varchar(31), | |
341 | + basic_first_name_attribute_key varchar(31), | |
342 | + basic_last_name_attribute_key varchar(31), | |
343 | + basic_tenant_name_strategy varchar(31), | |
344 | + basic_tenant_name_pattern varchar(255), | |
345 | + basic_customer_name_pattern varchar(255), | |
346 | + basic_default_dashboard_name varchar(255), | |
347 | + basic_always_full_screen boolean, | |
348 | + CONSTRAINT oauth2_template_provider_id_unq_key UNIQUE (provider_id) | |
349 | +); | ... | ... |
... | ... | @@ -340,6 +340,29 @@ CREATE TABLE IF NOT EXISTS oauth2_client_registration ( |
340 | 340 | custom_password varchar(255) |
341 | 341 | ); |
342 | 342 | |
343 | +CREATE TABLE IF NOT EXISTS oauth2_client_registration_template ( | |
344 | + id uuid NOT NULL CONSTRAINT oauth2_client_registration_template_pkey PRIMARY KEY, | |
345 | + created_time bigint NOT NULL, | |
346 | + tenant_id uuid, | |
347 | + provider_id varchar(255), | |
348 | + authorization_uri varchar(255), | |
349 | + token_uri varchar(255), | |
350 | + scope varchar(255), | |
351 | + user_info_uri varchar(255), | |
352 | + user_name_attribute_name varchar(255), | |
353 | + jwk_set_uri varchar(255), | |
354 | + client_authentication_method varchar(255), | |
355 | + basic_email_attribute_key varchar(31), | |
356 | + basic_first_name_attribute_key varchar(31), | |
357 | + basic_last_name_attribute_key varchar(31), | |
358 | + basic_tenant_name_strategy varchar(31), | |
359 | + basic_tenant_name_pattern varchar(255), | |
360 | + basic_customer_name_pattern varchar(255), | |
361 | + basic_default_dashboard_name varchar(255), | |
362 | + basic_always_full_screen boolean, | |
363 | + CONSTRAINT oauth2_template_provider_id_unq_key UNIQUE (provider_id) | |
364 | +); | |
365 | + | |
343 | 366 | CREATE OR REPLACE PROCEDURE cleanup_events_by_ttl(IN ttl bigint, IN debug_ttl bigint, INOUT deleted bigint) |
344 | 367 | LANGUAGE plpgsql AS |
345 | 368 | $$ | ... | ... |
dao/src/test/java/org/thingsboard/server/dao/service/BaseOAuth2ConfigTemplateServiceTest.java
0 → 100644
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.dao.service; | |
17 | + | |
18 | +import org.junit.After; | |
19 | +import org.junit.Assert; | |
20 | +import org.junit.Before; | |
21 | +import org.junit.Test; | |
22 | +import org.springframework.beans.factory.annotation.Autowired; | |
23 | +import org.thingsboard.server.common.data.id.TenantId; | |
24 | +import org.thingsboard.server.common.data.oauth2.OAuth2BasicMapperConfig; | |
25 | +import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationTemplate; | |
26 | +import org.thingsboard.server.dao.exception.DataValidationException; | |
27 | +import org.thingsboard.server.dao.oauth2.OAuth2ConfigTemplateService; | |
28 | + | |
29 | +import java.util.Arrays; | |
30 | +import java.util.UUID; | |
31 | + | |
32 | +public class BaseOAuth2ConfigTemplateServiceTest extends AbstractServiceTest { | |
33 | + | |
34 | + @Autowired | |
35 | + protected OAuth2ConfigTemplateService oAuth2ConfigTemplateService; | |
36 | + | |
37 | + private TenantId tenantId; | |
38 | + | |
39 | + @Before | |
40 | + public void beforeRun() throws Exception { | |
41 | + Assert.assertTrue(oAuth2ConfigTemplateService.findAllClientRegistrationTemplates().isEmpty()); | |
42 | + } | |
43 | + | |
44 | + @After | |
45 | + public void after() throws Exception { | |
46 | + oAuth2ConfigTemplateService.findAllClientRegistrationTemplates().forEach(clientRegistrationTemplate -> { | |
47 | + oAuth2ConfigTemplateService.deleteClientRegistrationTemplateById(clientRegistrationTemplate.getId()); | |
48 | + }); | |
49 | + | |
50 | + Assert.assertTrue(oAuth2ConfigTemplateService.findAllClientRegistrationTemplates().isEmpty()); | |
51 | + } | |
52 | + | |
53 | + | |
54 | + @Test(expected = DataValidationException.class) | |
55 | + public void testSaveDuplicateProviderId() { | |
56 | + OAuth2ClientRegistrationTemplate first = validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, "providerId"); | |
57 | + OAuth2ClientRegistrationTemplate second = validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, "providerId"); | |
58 | + oAuth2ConfigTemplateService.saveClientRegistrationTemplate(first); | |
59 | + oAuth2ConfigTemplateService.saveClientRegistrationTemplate(second); | |
60 | + } | |
61 | + | |
62 | + @Test | |
63 | + public void testCreateNewTemplate() { | |
64 | + OAuth2ClientRegistrationTemplate clientRegistrationTemplate = validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, UUID.randomUUID().toString()); | |
65 | + OAuth2ClientRegistrationTemplate savedClientRegistrationTemplate = oAuth2ConfigTemplateService.saveClientRegistrationTemplate(clientRegistrationTemplate); | |
66 | + | |
67 | + Assert.assertNotNull(savedClientRegistrationTemplate); | |
68 | + Assert.assertNotNull(savedClientRegistrationTemplate.getId()); | |
69 | + clientRegistrationTemplate.setId(savedClientRegistrationTemplate.getId()); | |
70 | + clientRegistrationTemplate.setCreatedTime(savedClientRegistrationTemplate.getCreatedTime()); | |
71 | + Assert.assertEquals(clientRegistrationTemplate, savedClientRegistrationTemplate); | |
72 | + } | |
73 | + | |
74 | + @Test | |
75 | + public void testFindTemplate() { | |
76 | + OAuth2ClientRegistrationTemplate clientRegistrationTemplate = validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, UUID.randomUUID().toString()); | |
77 | + OAuth2ClientRegistrationTemplate savedClientRegistrationTemplate = oAuth2ConfigTemplateService.saveClientRegistrationTemplate(clientRegistrationTemplate); | |
78 | + | |
79 | + OAuth2ClientRegistrationTemplate foundClientRegistrationTemplate = oAuth2ConfigTemplateService.findClientRegistrationTemplateById(savedClientRegistrationTemplate.getId()); | |
80 | + Assert.assertEquals(savedClientRegistrationTemplate, foundClientRegistrationTemplate); | |
81 | + } | |
82 | + | |
83 | + @Test | |
84 | + public void testFindAll() { | |
85 | + oAuth2ConfigTemplateService.saveClientRegistrationTemplate(validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, UUID.randomUUID().toString())); | |
86 | + oAuth2ConfigTemplateService.saveClientRegistrationTemplate(validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, UUID.randomUUID().toString())); | |
87 | + | |
88 | + Assert.assertEquals(2, oAuth2ConfigTemplateService.findAllClientRegistrationTemplates().size()); | |
89 | + } | |
90 | + | |
91 | + @Test | |
92 | + public void testDeleteTemplate() { | |
93 | + oAuth2ConfigTemplateService.saveClientRegistrationTemplate(validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, UUID.randomUUID().toString())); | |
94 | + oAuth2ConfigTemplateService.saveClientRegistrationTemplate(validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, UUID.randomUUID().toString())); | |
95 | + OAuth2ClientRegistrationTemplate saved = oAuth2ConfigTemplateService.saveClientRegistrationTemplate(validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, UUID.randomUUID().toString())); | |
96 | + | |
97 | + Assert.assertEquals(3, oAuth2ConfigTemplateService.findAllClientRegistrationTemplates().size()); | |
98 | + Assert.assertNotNull(oAuth2ConfigTemplateService.findClientRegistrationTemplateById(saved.getId())); | |
99 | + | |
100 | + oAuth2ConfigTemplateService.deleteClientRegistrationTemplateById(saved.getId()); | |
101 | + | |
102 | + Assert.assertEquals(2, oAuth2ConfigTemplateService.findAllClientRegistrationTemplates().size()); | |
103 | + Assert.assertNull(oAuth2ConfigTemplateService.findClientRegistrationTemplateById(saved.getId())); | |
104 | + } | |
105 | + | |
106 | + private OAuth2ClientRegistrationTemplate validClientRegistrationTemplate(TenantId tenantId, String providerId) { | |
107 | + OAuth2ClientRegistrationTemplate clientRegistrationTemplate = new OAuth2ClientRegistrationTemplate(); | |
108 | + clientRegistrationTemplate.setProviderId(providerId); | |
109 | + clientRegistrationTemplate.setTenantId(tenantId); | |
110 | + clientRegistrationTemplate.setBasic( | |
111 | + OAuth2BasicMapperConfig.builder() | |
112 | + .firstNameAttributeKey("firstName") | |
113 | + .lastNameAttributeKey("lastName") | |
114 | + .emailAttributeKey("email") | |
115 | + .tenantNamePattern("tenant") | |
116 | + .defaultDashboardName("Test") | |
117 | + .alwaysFullScreen(true) | |
118 | + .build() | |
119 | + ); | |
120 | + clientRegistrationTemplate.setAuthorizationUri("authorizationUri"); | |
121 | + clientRegistrationTemplate.setAccessTokenUri("tokenUri"); | |
122 | + clientRegistrationTemplate.setScope(Arrays.asList("scope1", "scope2")); | |
123 | + clientRegistrationTemplate.setUserInfoUri("userInfoUri"); | |
124 | + clientRegistrationTemplate.setUserNameAttributeName("userNameAttributeName"); | |
125 | + clientRegistrationTemplate.setJwkSetUri("jwkSetUri"); | |
126 | + clientRegistrationTemplate.setClientAuthenticationMethod("clientAuthenticationMethod"); | |
127 | + return clientRegistrationTemplate; | |
128 | + } | |
129 | +} | ... | ... |
dao/src/test/java/org/thingsboard/server/dao/service/sql/OAuth2ConfigTemplateServiceSqlTest.java
0 → 100644
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.dao.service.sql; | |
17 | + | |
18 | +import org.thingsboard.server.dao.service.BaseOAuth2ConfigTemplateServiceTest; | |
19 | +import org.thingsboard.server.dao.service.DaoSqlTest; | |
20 | + | |
21 | +@DaoSqlTest | |
22 | +public class OAuth2ConfigTemplateServiceSqlTest extends BaseOAuth2ConfigTemplateServiceTest { | |
23 | +} | ... | ... |
... | ... | @@ -21,4 +21,5 @@ DROP TABLE IF EXISTS rule_node; |
21 | 21 | DROP TABLE IF EXISTS rule_chain; |
22 | 22 | DROP TABLE IF EXISTS entity_view; |
23 | 23 | DROP TABLE IF EXISTS oauth2_client_registration; |
24 | +DROP TABLE IF EXISTS oauth2_client_registration_template; | |
24 | 25 | DROP FUNCTION IF EXISTS to_uuid; | ... | ... |
... | ... | @@ -22,4 +22,5 @@ DROP TABLE IF EXISTS rule_node; |
22 | 22 | DROP TABLE IF EXISTS rule_chain; |
23 | 23 | DROP TABLE IF EXISTS entity_view; |
24 | 24 | DROP TABLE IF EXISTS oauth2_client_registration; |
25 | +DROP TABLE IF EXISTS oauth2_client_registration_template; | |
25 | 26 | DROP TABLE IF EXISTS tb_schema_settings; |
\ No newline at end of file | ... | ... |
... | ... | @@ -22,4 +22,5 @@ DROP TABLE IF EXISTS rule_node; |
22 | 22 | DROP TABLE IF EXISTS rule_chain; |
23 | 23 | DROP TABLE IF EXISTS entity_view; |
24 | 24 | DROP TABLE IF EXISTS oauth2_client_registration; |
25 | +DROP TABLE IF EXISTS oauth2_client_registration_template; | |
25 | 26 | DROP TABLE IF EXISTS tb_schema_settings; |
\ No newline at end of file | ... | ... |