Search in sources :

Example 1 with CoapDeviceAuthCallback

use of org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback in project thingsboard by thingsboard.

the class CoapTransportResource method processAccessTokenRequest.

private void processAccessTokenRequest(CoapExchange exchange, SessionMsgType type, Request request) {
    Optional<DeviceTokenCredentials> credentials = decodeCredentials(request);
    if (credentials.isEmpty()) {
        exchange.respond(CoAP.ResponseCode.UNAUTHORIZED);
        return;
    }
    transportService.process(DeviceTransportType.COAP, TransportProtos.ValidateDeviceTokenRequestMsg.newBuilder().setToken(credentials.get().getCredentialsId()).build(), new CoapDeviceAuthCallback(exchange, (deviceCredentials, deviceProfile) -> processRequest(exchange, type, request, deviceCredentials, deviceProfile)));
}
Also used : JsonParseException(com.google.gson.JsonParseException) CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange) Response(org.eclipse.californium.core.coap.Response) FeatureType(org.thingsboard.server.common.msg.session.FeatureType) TbCoapDtlsSessionInfo(org.thingsboard.server.coapserver.TbCoapDtlsSessionInfo) Exchange(org.eclipse.californium.core.network.Exchange) TransportServiceCallback(org.thingsboard.server.common.transport.TransportServiceCallback) CoapDeviceAuthCallback(org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback) TransportPayloadType(org.thingsboard.server.common.data.TransportPayloadType) CoapNoOpCallback(org.thingsboard.server.transport.coap.callback.CoapNoOpCallback) Random(java.util.Random) ConcurrentMap(java.util.concurrent.ConcurrentMap) SessionMsgType(org.thingsboard.server.common.msg.session.SessionMsgType) ObserveRelation(org.eclipse.californium.core.observe.ObserveRelation) Resource(org.eclipse.californium.core.server.resources.Resource) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeviceTransportType(org.thingsboard.server.common.data.DeviceTransportType) JsonConverter(org.thingsboard.server.common.transport.adaptor.JsonConverter) GetAttributesSyncSessionCallback(org.thingsboard.server.transport.coap.callback.GetAttributesSyncSessionCallback) CoapServerService(org.thingsboard.server.coapserver.CoapServerService) ToServerRpcSyncSessionCallback(org.thingsboard.server.transport.coap.callback.ToServerRpcSyncSessionCallback) CoapClientContext(org.thingsboard.server.transport.coap.client.CoapClientContext) TbCoapClientState(org.thingsboard.server.transport.coap.client.TbCoapClientState) CoapOkCallback(org.thingsboard.server.transport.coap.callback.CoapOkCallback) DataConstants(org.thingsboard.server.common.data.DataConstants) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) ResourceObserver(org.eclipse.californium.core.server.resources.ResourceObserver) UUID(java.util.UUID) InetSocketAddress(java.net.InetSocketAddress) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) List(java.util.List) Request(org.eclipse.californium.core.coap.Request) CoAP(org.eclipse.californium.core.coap.CoAP) KEY_SESSION_ID(org.eclipse.californium.elements.DtlsEndpointContext.KEY_SESSION_ID) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) Optional(java.util.Optional) ValidateDeviceCredentialsResponse(org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials) CoapDeviceAuthCallback(org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback)

Example 2 with CoapDeviceAuthCallback

use of org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback in project thingsboard by thingsboard.

the class CoapEfentoTransportResource method processMeasurementsRequest.

private void processMeasurementsRequest(CoapExchange exchange, Request request) {
    byte[] bytes = request.getPayload();
    try {
        MeasurementsProtos.ProtoMeasurements protoMeasurements = MeasurementsProtos.ProtoMeasurements.parseFrom(bytes);
        log.trace("Successfully parsed Efento ProtoMeasurements: [{}]", protoMeasurements.getCloudToken());
        String token = protoMeasurements.getCloudToken();
        transportService.process(DeviceTransportType.COAP, TransportProtos.ValidateDeviceTokenRequestMsg.newBuilder().setToken(token).build(), new CoapDeviceAuthCallback(exchange, (msg, deviceProfile) -> {
            TransportProtos.SessionInfoProto sessionInfo = SessionInfoCreator.create(msg, transportContext, UUID.randomUUID());
            UUID sessionId = new UUID(sessionInfo.getSessionIdMSB(), sessionInfo.getSessionIdLSB());
            try {
                validateEfentoTransportConfiguration(deviceProfile);
                List<EfentoMeasurements> efentoMeasurements = getEfentoMeasurements(protoMeasurements, sessionId);
                transportService.process(sessionInfo, transportContext.getEfentoCoapAdaptor().convertToPostTelemetry(sessionId, efentoMeasurements), new CoapEfentoCallback(exchange, CoAP.ResponseCode.CREATED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
                reportSubscriptionInfo(sessionInfo, false, false);
            } catch (AdaptorException e) {
                log.error("[{}] Failed to decode Efento ProtoMeasurements: ", sessionId, e);
                exchange.respond(CoAP.ResponseCode.BAD_REQUEST);
            }
        }));
    } catch (Exception e) {
        log.error("Failed to decode Efento ProtoMeasurements: ", e);
        exchange.respond(CoAP.ResponseCode.INTERNAL_SERVER_ERROR);
    }
}
Also used : CONFIGURATION(org.thingsboard.server.transport.coap.CoapTransportService.CONFIGURATION) JsonObject(com.google.gson.JsonObject) CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange) Response(org.eclipse.californium.core.coap.Response) Exchange(org.eclipse.californium.core.network.Exchange) CoapDeviceAuthCallback(org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback) MeasurementTypeProtos(org.thingsboard.server.gen.transport.coap.MeasurementTypeProtos) EfentoCoapDeviceTypeConfiguration(org.thingsboard.server.common.data.device.profile.EfentoCoapDeviceTypeConfiguration) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) CoapEfentoUtils(org.thingsboard.server.transport.coap.efento.utils.CoapEfentoUtils) Resource(org.eclipse.californium.core.server.resources.Resource) CURRENT_TIMESTAMP(org.thingsboard.server.transport.coap.CoapTransportService.CURRENT_TIMESTAMP) DEVICE_INFO(org.thingsboard.server.transport.coap.CoapTransportService.DEVICE_INFO) AbstractCoapTransportResource(org.thingsboard.server.transport.coap.AbstractCoapTransportResource) Map(java.util.Map) MEASUREMENTS(org.thingsboard.server.transport.coap.CoapTransportService.MEASUREMENTS) DeviceTransportType(org.thingsboard.server.common.data.DeviceTransportType) MeasurementsProtos(org.thingsboard.server.gen.transport.coap.MeasurementsProtos) CoapOkCallback(org.thingsboard.server.transport.coap.callback.CoapOkCallback) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) CoapTransportContext(org.thingsboard.server.transport.coap.CoapTransportContext) UUID(java.util.UUID) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) List(java.util.List) Request(org.eclipse.californium.core.coap.Request) CoapEfentoCallback(org.thingsboard.server.transport.coap.callback.CoapEfentoCallback) TreeMap(java.util.TreeMap) CoAP(org.eclipse.californium.core.coap.CoAP) CoapDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.CoapDeviceProfileTransportConfiguration) CollectionUtils(org.springframework.util.CollectionUtils) Data(lombok.Data) DeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) AllArgsConstructor(lombok.AllArgsConstructor) SessionInfoCreator(org.thingsboard.server.common.transport.auth.SessionInfoCreator) AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) CoapEfentoCallback(org.thingsboard.server.transport.coap.callback.CoapEfentoCallback) MeasurementsProtos(org.thingsboard.server.gen.transport.coap.MeasurementsProtos) CoapDeviceAuthCallback(org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback) AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException)

Example 3 with CoapDeviceAuthCallback

use of org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback in project thingsboard by thingsboard.

the class OtaPackageTransportResource method processAccessTokenRequest.

private void processAccessTokenRequest(CoapExchange exchange, Request request) {
    Optional<DeviceTokenCredentials> credentials = decodeCredentials(request);
    if (credentials.isEmpty()) {
        exchange.respond(CoAP.ResponseCode.UNAUTHORIZED);
        return;
    }
    transportService.process(DeviceTransportType.COAP, TransportProtos.ValidateDeviceTokenRequestMsg.newBuilder().setToken(credentials.get().getCredentialsId()).build(), new CoapDeviceAuthCallback(exchange, (msg, deviceProfile) -> {
        getOtaPackageCallback(msg, exchange, otaPackageType);
    }));
}
Also used : CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange) Response(org.eclipse.californium.core.coap.Response) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Exchange(org.eclipse.californium.core.network.Exchange) TransportServiceCallback(org.thingsboard.server.common.transport.TransportServiceCallback) CoapDeviceAuthCallback(org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback) StringUtils(org.thingsboard.server.common.data.StringUtils) UUID(java.util.UUID) TenantId(org.thingsboard.server.common.data.id.TenantId) OtaPackageType(org.thingsboard.server.common.data.ota.OtaPackageType) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Request(org.eclipse.californium.core.coap.Request) Resource(org.eclipse.californium.core.server.resources.Resource) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials) CoAP(org.eclipse.californium.core.coap.CoAP) DeviceTransportType(org.thingsboard.server.common.data.DeviceTransportType) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) Optional(java.util.Optional) ValidateDeviceCredentialsResponse(org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse) SessionInfoCreator(org.thingsboard.server.common.transport.auth.SessionInfoCreator) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials) CoapDeviceAuthCallback(org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback)

Aggregations

List (java.util.List)3 UUID (java.util.UUID)3 Slf4j (lombok.extern.slf4j.Slf4j)3 CoAP (org.eclipse.californium.core.coap.CoAP)3 Request (org.eclipse.californium.core.coap.Request)3 Response (org.eclipse.californium.core.coap.Response)3 Exchange (org.eclipse.californium.core.network.Exchange)3 CoapExchange (org.eclipse.californium.core.server.resources.CoapExchange)3 Resource (org.eclipse.californium.core.server.resources.Resource)3 DeviceTransportType (org.thingsboard.server.common.data.DeviceTransportType)3 TransportProtos (org.thingsboard.server.gen.transport.TransportProtos)3 CoapDeviceAuthCallback (org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback)3 Optional (java.util.Optional)2 TimeUnit (java.util.concurrent.TimeUnit)2 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)2 DeviceTokenCredentials (org.thingsboard.server.common.data.security.DeviceTokenCredentials)2 TransportServiceCallback (org.thingsboard.server.common.transport.TransportServiceCallback)2 AdaptorException (org.thingsboard.server.common.transport.adaptor.AdaptorException)2 SessionInfoCreator (org.thingsboard.server.common.transport.auth.SessionInfoCreator)2 ValidateDeviceCredentialsResponse (org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse)2