Browse Source

Refactor saveApiKey inside Controller

pull/14074/head
Andrii Landiak 1 year ago
parent
commit
49d3022d47
  1. 6
      application/src/main/java/org/thingsboard/server/controller/ApiKeyController.java
  2. 11
      application/src/test/java/org/thingsboard/server/controller/ApiKeyControllerTest.java
  3. 2
      common/data/src/main/java/org/thingsboard/server/common/data/pat/ApiKey.java

6
application/src/main/java/org/thingsboard/server/controller/ApiKeyController.java

@ -67,13 +67,15 @@ public class ApiKeyController extends BaseController {
notes = "Creates an API key for the given user and returns the token ONCE as 'ApiKey <hash>'." + SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH) notes = "Creates an API key for the given user and returns the token ONCE as 'ApiKey <hash>'." + SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('SYS_ADMIN','TENANT_ADMIN')") @PreAuthorize("hasAnyAuthority('SYS_ADMIN','TENANT_ADMIN')")
@PostMapping(value = "/apiKey") @PostMapping(value = "/apiKey")
public String saveApiKey( public ApiKey saveApiKey(
@Parameter(description = "A JSON value representing the Api Key token.") @Parameter(description = "A JSON value representing the Api Key token.")
@RequestBody @Valid ApiKeyInfo apiKeyInfo) throws ThingsboardException { @RequestBody @Valid ApiKeyInfo apiKeyInfo) throws ThingsboardException {
SecurityUser securityUser = getCurrentUser(); SecurityUser securityUser = getCurrentUser();
apiKeyInfo.setTenantId(securityUser.getTenantId()); apiKeyInfo.setTenantId(securityUser.getTenantId());
checkEntity(apiKeyInfo.getId(), apiKeyInfo, Resource.API_KEY); checkEntity(apiKeyInfo.getId(), apiKeyInfo, Resource.API_KEY);
return toUserApiKey(checkNotNull(apiKeyService.saveApiKey(securityUser.getTenantId(), apiKeyInfo)).getHash()); ApiKey savedApiKey = checkNotNull(apiKeyService.saveApiKey(securityUser.getTenantId(), apiKeyInfo));
savedApiKey.setHash(toUserApiKey(savedApiKey.getHash()));
return savedApiKey;
} }
@ApiOperation(value = "Get User Api Keys (getUserApiKeys)", @ApiOperation(value = "Get User Api Keys (getUserApiKeys)",

11
application/src/test/java/org/thingsboard/server/controller/ApiKeyControllerTest.java

@ -21,6 +21,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.pat.ApiKey;
import org.thingsboard.server.common.data.pat.ApiKeyInfo; import org.thingsboard.server.common.data.pat.ApiKeyInfo;
import org.thingsboard.server.dao.service.DaoSqlTest; import org.thingsboard.server.dao.service.DaoSqlTest;
@ -41,7 +42,7 @@ public class ApiKeyControllerTest extends AbstractControllerTest {
public void testSaveApiKey() throws Exception { public void testSaveApiKey() throws Exception {
ApiKeyInfo apiKeyInfo = constructApiKeyInfo("New API key description", true); ApiKeyInfo apiKeyInfo = constructApiKeyInfo("New API key description", true);
String apiKeyStr = doPost("/api/apiKey", apiKeyInfo, String.class); String apiKeyStr = doPost("/api/apiKey", apiKeyInfo, ApiKey.class).getHash();
Assert.assertTrue(apiKeyStr.startsWith(API_KEY_HEADER_PREFIX)); Assert.assertTrue(apiKeyStr.startsWith(API_KEY_HEADER_PREFIX));
PageData<ApiKeyInfo> pageData = doGetTypedWithPageLink("/api/apiKeys/" + tenantAdminUserId + "?", new TypeReference<>() {}, new PageLink(10, 0)); PageData<ApiKeyInfo> pageData = doGetTypedWithPageLink("/api/apiKeys/" + tenantAdminUserId + "?", new TypeReference<>() {}, new PageLink(10, 0));
@ -65,7 +66,7 @@ public class ApiKeyControllerTest extends AbstractControllerTest {
ApiKeyInfo apiKeyInfo = constructApiKeyInfo("Test API key description", true); ApiKeyInfo apiKeyInfo = constructApiKeyInfo("Test API key description", true);
int expectedSize = 10; int expectedSize = 10;
for (int i = 0; i < expectedSize; i++) { for (int i = 0; i < expectedSize; i++) {
doPost("/api/apiKey", apiKeyInfo, String.class); doPost("/api/apiKey", apiKeyInfo, ApiKey.class);
} }
PageData<ApiKeyInfo> pageData2 = doGetTypedWithPageLink("/api/apiKeys/" + tenantAdminUserId + "?", new TypeReference<>() {}, new PageLink(10, 0)); PageData<ApiKeyInfo> pageData2 = doGetTypedWithPageLink("/api/apiKeys/" + tenantAdminUserId + "?", new TypeReference<>() {}, new PageLink(10, 0));
@ -83,7 +84,7 @@ public class ApiKeyControllerTest extends AbstractControllerTest {
@Test @Test
public void testUpdateApiKeyDescription() throws Exception { public void testUpdateApiKeyDescription() throws Exception {
ApiKeyInfo apiKeyInfo = constructApiKeyInfo("Test API key description", true); ApiKeyInfo apiKeyInfo = constructApiKeyInfo("Test API key description", true);
doPost("/api/apiKey", apiKeyInfo, String.class); doPost("/api/apiKey", apiKeyInfo, ApiKey.class);
PageData<ApiKeyInfo> pageData = doGetTypedWithPageLink("/api/apiKeys/" + tenantAdminUserId + "?", new TypeReference<>() {}, new PageLink(10, 0)); PageData<ApiKeyInfo> pageData = doGetTypedWithPageLink("/api/apiKeys/" + tenantAdminUserId + "?", new TypeReference<>() {}, new PageLink(10, 0));
Assert.assertEquals(1, pageData.getData().size()); Assert.assertEquals(1, pageData.getData().size());
@ -102,7 +103,7 @@ public class ApiKeyControllerTest extends AbstractControllerTest {
@Test @Test
public void testEnableApiKey() throws Exception { public void testEnableApiKey() throws Exception {
ApiKeyInfo apiKeyInfo = constructApiKeyInfo("Test API key description", true); ApiKeyInfo apiKeyInfo = constructApiKeyInfo("Test API key description", true);
doPost("/api/apiKey", apiKeyInfo, String.class); doPost("/api/apiKey", apiKeyInfo, ApiKey.class);
PageData<ApiKeyInfo> pageData = doGetTypedWithPageLink("/api/apiKeys/" + tenantAdminUserId + "?", new TypeReference<>() {}, new PageLink(10, 0)); PageData<ApiKeyInfo> pageData = doGetTypedWithPageLink("/api/apiKeys/" + tenantAdminUserId + "?", new TypeReference<>() {}, new PageLink(10, 0));
Assert.assertEquals(1, pageData.getData().size()); Assert.assertEquals(1, pageData.getData().size());
@ -125,7 +126,7 @@ public class ApiKeyControllerTest extends AbstractControllerTest {
doDelete("/api/apiKey/" + UUID.randomUUID()).andExpect(status().isNotFound()); doDelete("/api/apiKey/" + UUID.randomUUID()).andExpect(status().isNotFound());
ApiKeyInfo apiKeyInfo = constructApiKeyInfo("Test API key description", false); ApiKeyInfo apiKeyInfo = constructApiKeyInfo("Test API key description", false);
doPost("/api/apiKey", apiKeyInfo, String.class); doPost("/api/apiKey", apiKeyInfo, ApiKey.class);
PageData<ApiKeyInfo> pageData = doGetTypedWithPageLink("/api/apiKeys/" + tenantAdminUserId + "?", new TypeReference<>() {}, new PageLink(10, 0)); PageData<ApiKeyInfo> pageData = doGetTypedWithPageLink("/api/apiKeys/" + tenantAdminUserId + "?", new TypeReference<>() {}, new PageLink(10, 0));
Assert.assertEquals(1, pageData.getData().size()); Assert.assertEquals(1, pageData.getData().size());

2
common/data/src/main/java/org/thingsboard/server/common/data/pat/ApiKey.java

@ -15,7 +15,6 @@
*/ */
package org.thingsboard.server.common.data.pat; package org.thingsboard.server.common.data.pat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
@ -34,7 +33,6 @@ public class ApiKey extends ApiKeyInfo {
@NoXss @NoXss
@Schema(description = "Api key hash value", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "Api key hash value", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonIgnore
private String hash; private String hash;
public ApiKey() { public ApiKey() {

Loading…
Cancel
Save