Search in sources :

Example 1 with BasicMqttCredentials

use of org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials in project thingsboard by thingsboard.

the class DeviceBulkImportService method setUpBasicMqttCredentials.

private void setUpBasicMqttCredentials(Map<BulkImportColumnType, String> fields, DeviceCredentials credentials) {
    BasicMqttCredentials basicMqttCredentials = new BasicMqttCredentials();
    basicMqttCredentials.setClientId(fields.get(BulkImportColumnType.MQTT_CLIENT_ID));
    basicMqttCredentials.setUserName(fields.get(BulkImportColumnType.MQTT_USER_NAME));
    basicMqttCredentials.setPassword(fields.get(BulkImportColumnType.MQTT_PASSWORD));
    credentials.setCredentialsValue(JacksonUtil.toString(basicMqttCredentials));
}
Also used : BasicMqttCredentials(org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials)

Example 2 with BasicMqttCredentials

use of org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials in project thingsboard by thingsboard.

the class BasicMqttCredentialsTest method before.

@Before
public void before() throws Exception {
    loginSysAdmin();
    Tenant tenant = new Tenant();
    tenant.setTitle("My tenant");
    savedTenant = doPost("/api/tenant", tenant, Tenant.class);
    Assert.assertNotNull(savedTenant);
    tenantAdmin = new User();
    tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
    tenantAdmin.setTenantId(savedTenant.getId());
    tenantAdmin.setEmail("tenant" + atomicInteger.getAndIncrement() + "@thingsboard.org");
    tenantAdmin.setFirstName("Joe");
    tenantAdmin.setLastName("Downs");
    tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1");
    BasicMqttCredentials credValue = new BasicMqttCredentials();
    credValue.setClientId(CLIENT_ID);
    clientIdDevice = createDevice("clientIdDevice", credValue);
    credValue = new BasicMqttCredentials();
    credValue.setClientId(CLIENT_ID);
    credValue.setUserName(USER_NAME1);
    clientIdAndUserNameDevice1 = createDevice("clientIdAndUserNameDevice", credValue);
    credValue = new BasicMqttCredentials();
    credValue.setClientId(CLIENT_ID);
    credValue.setUserName(USER_NAME2);
    credValue.setPassword(PASSWORD);
    clientIdAndUserNameAndPasswordDevice2 = createDevice("clientIdAndUserNameAndPasswordDevice", credValue);
    credValue = new BasicMqttCredentials();
    credValue.setClientId(CLIENT_ID);
    credValue.setUserName(USER_NAME3);
    credValue.setPassword(PASSWORD);
    clientIdAndUserNameAndPasswordDevice3 = createDevice("clientIdAndUserNameAndPasswordDevice2", credValue);
    accessTokenDevice = createDevice("accessTokenDevice", USER_NAME1);
    accessToken2Device = createDevice("accessToken2Device", USER_NAME2);
}
Also used : BasicMqttCredentials(org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials) Tenant(org.thingsboard.server.common.data.Tenant) User(org.thingsboard.server.common.data.User) Before(org.junit.Before)

Example 3 with BasicMqttCredentials

use of org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials in project thingsboard by thingsboard.

the class DeviceCredentialsServiceImpl method formatSimpleMqttCredentials.

private void formatSimpleMqttCredentials(DeviceCredentials deviceCredentials) {
    BasicMqttCredentials mqttCredentials;
    try {
        mqttCredentials = JacksonUtil.fromString(deviceCredentials.getCredentialsValue(), BasicMqttCredentials.class);
        if (mqttCredentials == null) {
            throw new IllegalArgumentException();
        }
    } catch (IllegalArgumentException e) {
        throw new DeviceCredentialsValidationException("Invalid credentials body for simple mqtt credentials!");
    }
    if (StringUtils.isEmpty(mqttCredentials.getClientId()) && StringUtils.isEmpty(mqttCredentials.getUserName())) {
        throw new DeviceCredentialsValidationException("Both mqtt client id and user name are empty!");
    }
    if (StringUtils.isNotEmpty(mqttCredentials.getClientId()) && StringUtils.isNotEmpty(mqttCredentials.getPassword()) && StringUtils.isEmpty(mqttCredentials.getUserName())) {
        throw new DeviceCredentialsValidationException("Password cannot be specified along with client id");
    }
    if (StringUtils.isEmpty(mqttCredentials.getClientId())) {
        deviceCredentials.setCredentialsId(mqttCredentials.getUserName());
    } else if (StringUtils.isEmpty(mqttCredentials.getUserName())) {
        deviceCredentials.setCredentialsId(EncryptionUtil.getSha3Hash(mqttCredentials.getClientId()));
    } else {
        deviceCredentials.setCredentialsId(EncryptionUtil.getSha3Hash("|", mqttCredentials.getClientId(), mqttCredentials.getUserName()));
    }
    if (StringUtils.isNotEmpty(mqttCredentials.getPassword())) {
        mqttCredentials.setPassword(mqttCredentials.getPassword());
    }
    deviceCredentials.setCredentialsValue(JacksonUtil.toString(mqttCredentials));
}
Also used : BasicMqttCredentials(org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials) DeviceCredentialsValidationException(org.thingsboard.server.dao.exception.DeviceCredentialsValidationException)

Example 4 with BasicMqttCredentials

use of org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials in project thingsboard by thingsboard.

the class AbstractMqttProvisionJsonDeviceTest method processTestProvisioningCreateNewDeviceWithMqttBasic.

protected void processTestProvisioningCreateNewDeviceWithMqttBasic() throws Exception {
    super.processBeforeTest("Test Provision device3", "Test Provision gateway", TransportPayloadType.JSON, null, null, null, null, null, null, "testProvisionKey", "testProvisionSecret", DeviceProfileProvisionType.ALLOW_CREATE_NEW_DEVICES, false, false);
    String requestCredentials = ",\"credentialsType\": \"MQTT_BASIC\",\"clientId\": \"test_clientId\",\"username\": \"test_username\",\"password\": \"test_password\"";
    byte[] result = createMqttClientAndPublish(requestCredentials).getPayloadBytes();
    JsonObject response = JsonUtils.parse(new String(result)).getAsJsonObject();
    Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device");
    Assert.assertNotNull(createdDevice);
    DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId());
    Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.get("credentialsType").getAsString());
    Assert.assertEquals(deviceCredentials.getCredentialsType().name(), "MQTT_BASIC");
    Assert.assertEquals(deviceCredentials.getCredentialsId(), EncryptionUtil.getSha3Hash("|", "test_clientId", "test_username"));
    BasicMqttCredentials mqttCredentials = new BasicMqttCredentials();
    mqttCredentials.setClientId("test_clientId");
    mqttCredentials.setUserName("test_username");
    mqttCredentials.setPassword("test_password");
    Assert.assertEquals(deviceCredentials.getCredentialsValue(), JacksonUtil.toString(mqttCredentials));
    Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.get("status").getAsString());
}
Also used : BasicMqttCredentials(org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials) Device(org.thingsboard.server.common.data.Device) JsonObject(com.google.gson.JsonObject) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials)

Example 5 with BasicMqttCredentials

use of org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials in project thingsboard by thingsboard.

the class AbstractMqttProvisionProtoDeviceTest method processTestProvisioningCreateNewDeviceWithMqttBasic.

protected void processTestProvisioningCreateNewDeviceWithMqttBasic() throws Exception {
    super.processBeforeTest("Test Provision device3", "Test Provision gateway", TransportPayloadType.PROTOBUF, null, null, null, null, null, null, "testProvisionKey", "testProvisionSecret", DeviceProfileProvisionType.ALLOW_CREATE_NEW_DEVICES, false, false);
    CredentialsDataProto requestCredentials = CredentialsDataProto.newBuilder().setValidateBasicMqttCredRequestMsg(ValidateBasicMqttCredRequestMsg.newBuilder().setClientId("test_clientId").setUserName("test_username").setPassword("test_password").build()).build();
    ProvisionDeviceResponseMsg response = ProvisionDeviceResponseMsg.parseFrom(createMqttClientAndPublish(createTestsProvisionMessage(CredentialsType.MQTT_BASIC, requestCredentials)).getPayloadBytes());
    Device createdDevice = deviceService.findDeviceByTenantIdAndName(savedTenant.getTenantId(), "Test Provision device");
    Assert.assertNotNull(createdDevice);
    DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedTenant.getTenantId(), createdDevice.getId());
    Assert.assertEquals(deviceCredentials.getCredentialsType().name(), response.getCredentialsType().toString());
    Assert.assertEquals(deviceCredentials.getCredentialsType(), DeviceCredentialsType.MQTT_BASIC);
    Assert.assertEquals(deviceCredentials.getCredentialsId(), EncryptionUtil.getSha3Hash("|", "test_clientId", "test_username"));
    BasicMqttCredentials mqttCredentials = new BasicMqttCredentials();
    mqttCredentials.setClientId("test_clientId");
    mqttCredentials.setUserName("test_username");
    mqttCredentials.setPassword("test_password");
    Assert.assertEquals(deviceCredentials.getCredentialsValue(), JacksonUtil.toString(mqttCredentials));
    Assert.assertEquals(ProvisionResponseStatus.SUCCESS.name(), response.getStatus().toString());
}
Also used : CredentialsDataProto(org.thingsboard.server.gen.transport.TransportProtos.CredentialsDataProto) BasicMqttCredentials(org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials) ProvisionDeviceResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.ProvisionDeviceResponseMsg) Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials)

Aggregations

BasicMqttCredentials (org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials)6 Device (org.thingsboard.server.common.data.Device)3 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)3 JsonObject (com.google.gson.JsonObject)1 ExecutionException (java.util.concurrent.ExecutionException)1 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)1 Before (org.junit.Before)1 CacheEvict (org.springframework.cache.annotation.CacheEvict)1 Transactional (org.springframework.transaction.annotation.Transactional)1 Tenant (org.thingsboard.server.common.data.Tenant)1 User (org.thingsboard.server.common.data.User)1 ProvisionFailedException (org.thingsboard.server.dao.device.provision.ProvisionFailedException)1 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)1 DeviceCredentialsValidationException (org.thingsboard.server.dao.exception.DeviceCredentialsValidationException)1 CredentialsDataProto (org.thingsboard.server.gen.transport.TransportProtos.CredentialsDataProto)1 ProvisionDeviceResponseMsg (org.thingsboard.server.gen.transport.TransportProtos.ProvisionDeviceResponseMsg)1