...
|
...
|
@@ -17,12 +17,23 @@ package org.thingsboard.server.controller; |
17
|
17
|
|
18
|
18
|
import lombok.extern.slf4j.Slf4j;
|
19
|
19
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
20
|
+import org.springframework.http.HttpStatus;
|
20
|
21
|
import org.springframework.security.access.prepost.PreAuthorize;
|
21
|
22
|
import org.springframework.web.bind.annotation.*;
|
22
|
23
|
import org.thingsboard.server.common.data.exception.ThingsboardException;
|
|
24
|
+import org.thingsboard.server.common.data.id.EntityId;
|
|
25
|
+import org.thingsboard.server.common.data.id.TenantId;
|
|
26
|
+import org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo;
|
23
|
27
|
import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistration;
|
|
28
|
+import org.thingsboard.server.common.data.oauth2.OAuth2ClientsParams;
|
|
29
|
+import org.thingsboard.server.common.data.security.Authority;
|
24
|
30
|
import org.thingsboard.server.dao.oauth2.OAuth2Service;
|
25
|
31
|
import org.thingsboard.server.queue.util.TbCoreComponent;
|
|
32
|
+import org.thingsboard.server.service.security.permission.Operation;
|
|
33
|
+import org.thingsboard.server.service.security.permission.Resource;
|
|
34
|
+
|
|
35
|
+import javax.servlet.http.HttpServletRequest;
|
|
36
|
+import java.util.List;
|
26
|
37
|
|
27
|
38
|
@RestController
|
28
|
39
|
@TbCoreComponent
|
...
|
...
|
@@ -34,14 +45,68 @@ public class OAuth2Controller extends BaseController { |
34
|
45
|
@Autowired
|
35
|
46
|
private OAuth2Service oauth2Service;
|
36
|
47
|
|
37
|
|
- @PreAuthorize("hasAnyAuthority('SYS_ADMIN')")
|
38
|
|
- @RequestMapping(value = "/oauth2/config/{" + REGISTRATION_ID + "}", method = RequestMethod.GET)
|
|
48
|
+ // TODO ask why POST
|
|
49
|
+ @RequestMapping(value = "/noauth/oauth2Clients", method = RequestMethod.POST)
|
|
50
|
+ @ResponseBody
|
|
51
|
+ public List<OAuth2ClientInfo> getOAuth2Clients(HttpServletRequest request) throws ThingsboardException {
|
|
52
|
+ try {
|
|
53
|
+ return oauth2Service.getOAuth2Clients(request.getServerName());
|
|
54
|
+ } catch (Exception e) {
|
|
55
|
+ throw handleException(e);
|
|
56
|
+ }
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
|
|
60
|
+ @RequestMapping(value = "/oauth2/currentOAuth2Configuration", method = RequestMethod.GET, produces = "application/json")
|
|
61
|
+ @ResponseBody
|
|
62
|
+ public OAuth2ClientsParams getCurrentOAuth2ClientsParams() throws ThingsboardException {
|
|
63
|
+ try {
|
|
64
|
+ Authority authority = getCurrentUser().getAuthority();
|
|
65
|
+ checkOAuth2ConfigPermissions(Operation.READ);
|
|
66
|
+ OAuth2ClientsParams oAuth2ClientsParams = null;
|
|
67
|
+ if (Authority.SYS_ADMIN.equals(authority)) {
|
|
68
|
+ oAuth2ClientsParams = oauth2Service.getSystemOAuth2ClientsParams(TenantId.SYS_TENANT_ID);
|
|
69
|
+ } else if (Authority.TENANT_ADMIN.equals(authority)) {
|
|
70
|
+ oAuth2ClientsParams = oauth2Service.getTenantOAuth2ClientsParams(getCurrentUser().getTenantId());
|
|
71
|
+ }
|
|
72
|
+ return oAuth2ClientsParams;
|
|
73
|
+ } catch (Exception e) {
|
|
74
|
+ throw handleException(e);
|
|
75
|
+ }
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
|
|
79
|
+ @RequestMapping(value = "/oauth2/oAuth2Configuration", method = RequestMethod.POST)
|
|
80
|
+ @ResponseStatus(value = HttpStatus.OK)
|
|
81
|
+ public OAuth2ClientsParams saveLoginWhiteLabelParams(@RequestBody OAuth2ClientsParams oAuth2ClientsParams) throws ThingsboardException {
|
|
82
|
+ try {
|
|
83
|
+ Authority authority = getCurrentUser().getAuthority();
|
|
84
|
+ checkOAuth2ConfigPermissions(Operation.WRITE);
|
|
85
|
+ OAuth2ClientsParams savedOAuth2ClientsParams = null;
|
|
86
|
+ if (Authority.SYS_ADMIN.equals(authority)) {
|
|
87
|
+ savedOAuth2ClientsParams = oauth2Service.saveSystemOAuth2ClientsParams(oAuth2ClientsParams);
|
|
88
|
+ } else if (Authority.TENANT_ADMIN.equals(authority)) {
|
|
89
|
+ savedOAuth2ClientsParams = oauth2Service.saveTenantOAuth2ClientsParams(getCurrentUser().getTenantId(), oAuth2ClientsParams);
|
|
90
|
+ }
|
|
91
|
+ return savedOAuth2ClientsParams;
|
|
92
|
+ } catch (Exception e) {
|
|
93
|
+ throw handleException(e);
|
|
94
|
+ }
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
|
|
98
|
+ @RequestMapping(value = "/oauth2/isOAuth2ConfigurationAllowed", method = RequestMethod.GET)
|
39
|
99
|
@ResponseBody
|
40
|
|
- public OAuth2ClientRegistration getClientRegistrationById(@PathVariable(REGISTRATION_ID) String registrationId) throws ThingsboardException {
|
|
100
|
+ public Boolean isOAuth2ConfigurationAllowed() throws ThingsboardException {
|
41
|
101
|
try {
|
42
|
|
- return oauth2Service.getClientRegistration(registrationId);
|
|
102
|
+ return oauth2Service.isOAuth2ClientRegistrationAllowed(getTenantId());
|
43
|
103
|
} catch (Exception e) {
|
44
|
104
|
throw handleException(e);
|
45
|
105
|
}
|
46
|
106
|
}
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+ private void checkOAuth2ConfigPermissions(Operation operation) throws ThingsboardException {
|
|
110
|
+ accessControlService.checkPermission(getCurrentUser(), Resource.OAUTH2_CONFIGURATION, operation);
|
|
111
|
+ }
|
47
|
112
|
} |
...
|
...
|
|