Search in sources :

Example 1 with LwM2MClientCredential

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

the class DeviceBulkImportService method setUpLwm2mCredentials.

private void setUpLwm2mCredentials(Map<BulkImportColumnType, String> fields, DeviceCredentials credentials) throws com.fasterxml.jackson.core.JsonProcessingException {
    ObjectNode lwm2mCredentials = JacksonUtil.newObjectNode();
    Set.of(BulkImportColumnType.LWM2M_CLIENT_SECURITY_CONFIG_MODE, BulkImportColumnType.LWM2M_BOOTSTRAP_SERVER_SECURITY_MODE, BulkImportColumnType.LWM2M_SERVER_SECURITY_MODE).stream().map(fields::get).filter(Objects::nonNull).forEach(securityMode -> {
        try {
            LwM2MSecurityMode.valueOf(securityMode.toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new DeviceCredentialsValidationException("Unknown LwM2M security mode: " + securityMode + ", (the mode should be: NO_SEC, PSK, RPK, X509)!");
        }
    });
    ObjectNode client = JacksonUtil.newObjectNode();
    setValues(client, fields, Set.of(BulkImportColumnType.LWM2M_CLIENT_SECURITY_CONFIG_MODE, BulkImportColumnType.LWM2M_CLIENT_ENDPOINT, BulkImportColumnType.LWM2M_CLIENT_IDENTITY, BulkImportColumnType.LWM2M_CLIENT_KEY, BulkImportColumnType.LWM2M_CLIENT_CERT));
    LwM2MClientCredential lwM2MClientCredential = JacksonUtil.treeToValue(client, LwM2MClientCredential.class);
    // so that only fields needed for specific type of lwM2MClientCredentials were saved in json
    lwm2mCredentials.set("client", JacksonUtil.valueToTree(lwM2MClientCredential));
    ObjectNode bootstrapServer = JacksonUtil.newObjectNode();
    setValues(bootstrapServer, fields, Set.of(BulkImportColumnType.LWM2M_BOOTSTRAP_SERVER_SECURITY_MODE, BulkImportColumnType.LWM2M_BOOTSTRAP_SERVER_PUBLIC_KEY_OR_ID, BulkImportColumnType.LWM2M_BOOTSTRAP_SERVER_SECRET_KEY));
    ObjectNode lwm2mServer = JacksonUtil.newObjectNode();
    setValues(lwm2mServer, fields, Set.of(BulkImportColumnType.LWM2M_SERVER_SECURITY_MODE, BulkImportColumnType.LWM2M_SERVER_CLIENT_PUBLIC_KEY_OR_ID, BulkImportColumnType.LWM2M_SERVER_CLIENT_SECRET_KEY));
    ObjectNode bootstrap = JacksonUtil.newObjectNode();
    bootstrap.set("bootstrapServer", bootstrapServer);
    bootstrap.set("lwm2mServer", lwm2mServer);
    lwm2mCredentials.set("bootstrap", bootstrap);
    credentials.setCredentialsValue(lwm2mCredentials.toString());
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) LwM2MClientCredential(org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MClientCredential) DeviceCredentialsValidationException(org.thingsboard.server.dao.exception.DeviceCredentialsValidationException)

Example 2 with LwM2MClientCredential

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

the class DeviceCredentialsServiceImpl method formatAndValidateSimpleLwm2mCredentials.

private void formatAndValidateSimpleLwm2mCredentials(DeviceCredentials deviceCredentials) {
    LwM2MDeviceCredentials lwM2MCredentials;
    try {
        lwM2MCredentials = JacksonUtil.fromString(deviceCredentials.getCredentialsValue(), LwM2MDeviceCredentials.class);
        validateLwM2MDeviceCredentials(lwM2MCredentials);
    } catch (IllegalArgumentException e) {
        throw new DeviceCredentialsValidationException("Invalid credentials body for LwM2M credentials!");
    }
    String credentialsId = null;
    LwM2MClientCredential clientCredentials = lwM2MCredentials.getClient();
    switch(clientCredentials.getSecurityConfigClientMode()) {
        case NO_SEC:
        case RPK:
            deviceCredentials.setCredentialsValue(JacksonUtil.toString(lwM2MCredentials));
            credentialsId = clientCredentials.getEndpoint();
            break;
        case PSK:
            credentialsId = ((PSKClientCredential) clientCredentials).getIdentity();
            break;
        case X509:
            deviceCredentials.setCredentialsValue(JacksonUtil.toString(lwM2MCredentials));
            X509ClientCredential x509ClientConfig = (X509ClientCredential) clientCredentials;
            if ((StringUtils.isNotBlank(x509ClientConfig.getCert()))) {
                String sha3Hash = EncryptionUtil.getSha3Hash(x509ClientConfig.getCert());
                credentialsId = sha3Hash;
            } else {
                credentialsId = x509ClientConfig.getEndpoint();
            }
            break;
    }
    if (credentialsId == null) {
        throw new DeviceCredentialsValidationException("Invalid credentials body for LwM2M credentials!");
    }
    deviceCredentials.setCredentialsId(credentialsId);
}
Also used : LwM2MDeviceCredentials(org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MDeviceCredentials) LwM2MClientCredential(org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MClientCredential) Validator.validateString(org.thingsboard.server.dao.service.Validator.validateString) DeviceCredentialsValidationException(org.thingsboard.server.dao.exception.DeviceCredentialsValidationException) X509ClientCredential(org.thingsboard.server.common.data.device.credentials.lwm2m.X509ClientCredential)

Example 3 with LwM2MClientCredential

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

the class DeviceCredentialsServiceImpl method validateLwM2MDeviceCredentials.

private void validateLwM2MDeviceCredentials(LwM2MDeviceCredentials lwM2MCredentials) {
    if (lwM2MCredentials == null) {
        throw new DeviceCredentialsValidationException("LwM2M credentials must be specified!");
    }
    LwM2MClientCredential clientCredentials = lwM2MCredentials.getClient();
    if (clientCredentials == null) {
        throw new DeviceCredentialsValidationException("LwM2M client credentials must be specified!");
    }
    validateLwM2MClientCredentials(clientCredentials);
    LwM2MBootstrapClientCredentials bootstrapCredentials = lwM2MCredentials.getBootstrap();
    if (bootstrapCredentials == null) {
        throw new DeviceCredentialsValidationException("LwM2M bootstrap credentials must be specified!");
    }
    LwM2MBootstrapClientCredential bootstrapServerCredentials = bootstrapCredentials.getBootstrapServer();
    if (bootstrapServerCredentials == null) {
        throw new DeviceCredentialsValidationException("LwM2M bootstrap server credentials must be specified!");
    }
    validateServerCredentials(bootstrapServerCredentials, "Bootstrap server");
    LwM2MBootstrapClientCredential lwm2MBootstrapClientCredential = bootstrapCredentials.getLwm2mServer();
    if (lwm2MBootstrapClientCredential == null) {
        throw new DeviceCredentialsValidationException("LwM2M lwm2m server credentials must be specified!");
    }
    validateServerCredentials(lwm2MBootstrapClientCredential, "LwM2M server");
}
Also used : LwM2MClientCredential(org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MClientCredential) LwM2MBootstrapClientCredential(org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MBootstrapClientCredential) DeviceCredentialsValidationException(org.thingsboard.server.dao.exception.DeviceCredentialsValidationException) LwM2MBootstrapClientCredentials(org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MBootstrapClientCredentials)

Aggregations

LwM2MClientCredential (org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MClientCredential)3 DeviceCredentialsValidationException (org.thingsboard.server.dao.exception.DeviceCredentialsValidationException)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 LwM2MBootstrapClientCredential (org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MBootstrapClientCredential)1 LwM2MBootstrapClientCredentials (org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MBootstrapClientCredentials)1 LwM2MDeviceCredentials (org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MDeviceCredentials)1 X509ClientCredential (org.thingsboard.server.common.data.device.credentials.lwm2m.X509ClientCredential)1 Validator.validateString (org.thingsboard.server.dao.service.Validator.validateString)1