Search in sources :

Example 6 with DeviceTokenCredentials

use of org.thingsboard.server.common.data.security.DeviceTokenCredentials in project thingsboard by thingsboard.

the class MqttTransportHandler method processAuthTokenConnect.

private void processAuthTokenConnect(ChannelHandlerContext ctx, MqttConnectMessage msg) {
    String userName = msg.payload().userName();
    if (StringUtils.isEmpty(userName)) {
        ctx.writeAndFlush(createMqttConnAckMsg(CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD));
        ctx.close();
    } else if (!deviceSessionCtx.login(new DeviceTokenCredentials(msg.payload().userName()))) {
        ctx.writeAndFlush(createMqttConnAckMsg(CONNECTION_REFUSED_NOT_AUTHORIZED));
        ctx.close();
    } else {
        ctx.writeAndFlush(createMqttConnAckMsg(CONNECTION_ACCEPTED));
        connected = true;
        checkGatewaySession();
    }
}
Also used : DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials)

Example 7 with DeviceTokenCredentials

use of org.thingsboard.server.common.data.security.DeviceTokenCredentials in project thingsboard by thingsboard.

the class DeviceApiController method getDeviceAttributes.

@RequestMapping(value = "/{deviceToken}/attributes", method = RequestMethod.GET, produces = "application/json")
public DeferredResult<ResponseEntity> getDeviceAttributes(@PathVariable("deviceToken") String deviceToken, @RequestParam(value = "clientKeys", required = false, defaultValue = "") String clientKeys, @RequestParam(value = "sharedKeys", required = false, defaultValue = "") String sharedKeys, HttpServletRequest httpRequest) {
    DeferredResult<ResponseEntity> responseWriter = new DeferredResult<ResponseEntity>();
    if (quotaExceeded(httpRequest, responseWriter)) {
        return responseWriter;
    }
    HttpSessionCtx ctx = getHttpSessionCtx(responseWriter);
    if (ctx.login(new DeviceTokenCredentials(deviceToken))) {
        GetAttributesRequest request;
        if (StringUtils.isEmpty(clientKeys) && StringUtils.isEmpty(sharedKeys)) {
            request = new BasicGetAttributesRequest(0);
        } else {
            Set<String> clientKeySet = !StringUtils.isEmpty(clientKeys) ? new HashSet<>(Arrays.asList(clientKeys.split(","))) : null;
            Set<String> sharedKeySet = !StringUtils.isEmpty(sharedKeys) ? new HashSet<>(Arrays.asList(sharedKeys.split(","))) : null;
            request = new BasicGetAttributesRequest(0, clientKeySet, sharedKeySet);
        }
        process(ctx, request);
    } else {
        responseWriter.setResult(new ResponseEntity<>(HttpStatus.UNAUTHORIZED));
    }
    return responseWriter;
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HttpSessionCtx(org.thingsboard.server.transport.http.session.HttpSessionCtx) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials) DeferredResult(org.springframework.web.context.request.async.DeferredResult)

Example 8 with DeviceTokenCredentials

use of org.thingsboard.server.common.data.security.DeviceTokenCredentials in project thingsboard by thingsboard.

the class DeviceApiController method postTelemetry.

@RequestMapping(value = "/{deviceToken}/telemetry", method = RequestMethod.POST)
public DeferredResult<ResponseEntity> postTelemetry(@PathVariable("deviceToken") String deviceToken, @RequestBody String json, HttpServletRequest request) {
    DeferredResult<ResponseEntity> responseWriter = new DeferredResult<ResponseEntity>();
    if (quotaExceeded(request, responseWriter)) {
        return responseWriter;
    }
    HttpSessionCtx ctx = getHttpSessionCtx(responseWriter);
    if (ctx.login(new DeviceTokenCredentials(deviceToken))) {
        try {
            process(ctx, JsonConverter.convertToTelemetry(new JsonParser().parse(json)));
        } catch (IllegalStateException | JsonSyntaxException ex) {
            responseWriter.setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
        }
    } else {
        responseWriter.setResult(new ResponseEntity<>(HttpStatus.UNAUTHORIZED));
    }
    return responseWriter;
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) JsonSyntaxException(com.google.gson.JsonSyntaxException) HttpSessionCtx(org.thingsboard.server.transport.http.session.HttpSessionCtx) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials) DeferredResult(org.springframework.web.context.request.async.DeferredResult) JsonParser(com.google.gson.JsonParser)

Example 9 with DeviceTokenCredentials

use of org.thingsboard.server.common.data.security.DeviceTokenCredentials in project thingsboard by thingsboard.

the class CoapTransportResource method decodeCredentials.

private Optional<DeviceCredentialsFilter> decodeCredentials(Request request) {
    List<String> uriPath = request.getOptions().getUriPath();
    DeviceCredentialsFilter credentials = null;
    if (uriPath.size() >= ACCESS_TOKEN_POSITION) {
        credentials = new DeviceTokenCredentials(uriPath.get(ACCESS_TOKEN_POSITION - 1));
    }
    return Optional.ofNullable(credentials);
}
Also used : DeviceCredentialsFilter(org.thingsboard.server.common.data.security.DeviceCredentialsFilter) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials)

Aggregations

DeviceTokenCredentials (org.thingsboard.server.common.data.security.DeviceTokenCredentials)9 ResponseEntity (org.springframework.http.ResponseEntity)6 DeferredResult (org.springframework.web.context.request.async.DeferredResult)6 HttpSessionCtx (org.thingsboard.server.transport.http.session.HttpSessionCtx)6 JsonSyntaxException (com.google.gson.JsonSyntaxException)5 JsonParser (com.google.gson.JsonParser)4 JsonObject (com.google.gson.JsonObject)2 DeviceCredentialsFilter (org.thingsboard.server.common.data.security.DeviceCredentialsFilter)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Test (org.junit.Test)1 Device (org.thingsboard.server.common.data.Device)1 BasicTsKvEntry (org.thingsboard.server.common.data.kv.BasicTsKvEntry)1 KvEntry (org.thingsboard.server.common.data.kv.KvEntry)1 StringDataEntry (org.thingsboard.server.common.data.kv.StringDataEntry)1 TsKvEntry (org.thingsboard.server.common.data.kv.TsKvEntry)1 ComponentDescriptor (org.thingsboard.server.common.data.plugin.ComponentDescriptor)1 BasicTelemetryUploadRequest (org.thingsboard.server.common.msg.core.BasicTelemetryUploadRequest)1 TelemetryStoragePlugin (org.thingsboard.server.extensions.core.plugin.telemetry.TelemetryStoragePlugin)1